Skip to content

Commit f724af6

Browse files
authored
Add --js-library support (#16)
* Add `--js-library` support * Dedupe code
2 parents 00da03b + 9b0d735 commit f724af6

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

build.zig

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ pub const EmsdkAllocator = enum {
151151
pub const EmccDefaultSettingsOverrides = struct {
152152
optimize: std.builtin.OptimizeMode,
153153
emsdk_allocator: EmsdkAllocator = .emmalloc,
154-
shell_file: ?[]const u8 = null,
155154
};
156155

157156
pub fn emccDefaultSettings(allocator: std.mem.Allocator, options: EmccDefaultSettingsOverrides) EmccSettings {
@@ -182,6 +181,7 @@ pub const StepOptions = struct {
182181
embed_paths: ?[]const EmccFilePath = null,
183182
preload_paths: ?[]const EmccFilePath = null,
184183
shell_file_path: ?std.Build.LazyPath = null,
184+
js_library_path: ?std.Build.LazyPath = null,
185185
out_file_name: ?[]const u8 = null,
186186
install_dir: std.Build.InstallDir,
187187
};
@@ -227,13 +227,9 @@ pub fn emccStep(
227227
}
228228

229229
emcc.addArg("-o");
230-
const out_file = out_file: {
231-
if (options.out_file_name) |out_file_name| {
232-
break :out_file emcc.addOutputFileArg(out_file_name);
233-
} else {
234-
break :out_file emcc.addOutputFileArg(b.fmt("{s}.html", .{wasm.name}));
235-
}
236-
};
230+
const out_file = emcc.addOutputFileArg(
231+
options.out_file_name orelse b.fmt("{s}.html", .{wasm.name}),
232+
);
237233

238234
if (options.use_preload_plugins) {
239235
emcc.addArg("--use-preload-plugins");
@@ -273,6 +269,12 @@ pub fn emccStep(
273269
emcc.addFileInput(shell_file_path);
274270
}
275271

272+
if (options.js_library_path) |js_library_path| {
273+
emcc.addArg("--js-library");
274+
emcc.addFileArg(js_library_path);
275+
emcc.addFileInput(js_library_path);
276+
}
277+
276278
const install_step = b.addInstallDirectory(.{
277279
.source_dir = out_file.dirname(),
278280
.install_dir = options.install_dir,

src/zemscripten.zig

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub extern fn emscripten_sleep(ms: u32) void;
1111
pub const MainLoopCallback = *const fn () callconv(.c) void;
1212
extern fn emscripten_set_main_loop(MainLoopCallback, c_int, c_int) void;
1313
pub fn setMainLoop(cb: MainLoopCallback, maybe_fps: ?i16, simulate_infinite_loop: bool) void {
14-
emscripten_set_main_loop(cb, if (maybe_fps) |fps| fps else -1, @intFromBool(simulate_infinite_loop));
14+
emscripten_set_main_loop(cb, maybe_fps orelse -1, @intFromBool(simulate_infinite_loop));
1515
}
1616

1717
extern fn emscripten_cancel_main_loop() void;
@@ -22,7 +22,7 @@ pub fn cancelMainLoop() void {
2222
pub const MainLoopArgCallback = *const fn (arg: *anyopaque) callconv(.c) void;
2323
extern fn emscripten_set_main_loop_arg(MainLoopArgCallback, arg: *anyopaque, c_int, c_int) void;
2424
pub fn setMainLoopArg(cb: MainLoopArgCallback, arg: *anyopaque, maybe_fps: ?i16, simulate_infinite_loop: bool) void {
25-
emscripten_set_main_loop_arg(cb, arg, if (maybe_fps) |fps| fps else -1, @intFromBool(simulate_infinite_loop));
25+
emscripten_set_main_loop_arg(cb, arg, maybe_fps orelse -1, @intFromBool(simulate_infinite_loop));
2626
}
2727

2828
pub const AnimationFrameCallback = *const fn (f64, ?*anyopaque) callconv(.c) c_int;
@@ -111,9 +111,8 @@ pub const EmmallocAllocator = struct {
111111
) ?[*]u8 {
112112
_ = ctx;
113113
_ = return_address;
114-
const ptr_align: u32 = @as(u32, 1) << @as(u5, @intFromEnum(ptr_align_log2));
115-
if (!std.math.isPowerOfTwo(ptr_align)) unreachable;
116-
const ptr = emmalloc_memalign(ptr_align, len) orelse return null;
114+
const ptr_align = ptr_align_log2.toByteUnits();
115+
const ptr = emmalloc_memalign(@intCast(ptr_align), @intCast(len)) orelse return null;
117116
return @ptrCast(ptr);
118117
}
119118

@@ -127,7 +126,7 @@ pub const EmmallocAllocator = struct {
127126
_ = ctx;
128127
_ = return_address;
129128
_ = buf_align_log2;
130-
return emmalloc_realloc_try(buf.ptr, new_len) != null;
129+
return emmalloc_realloc_try(buf.ptr, @intCast(new_len)) != null;
131130
}
132131

133132
fn remap(

0 commit comments

Comments
 (0)