From d39fc66a0fec0875f054265d0bf61641d01a119a Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Tue, 12 Nov 2024 12:10:42 -0800 Subject: [PATCH 1/9] Initial commit --- build.zig | 3 +- content/shell_minimal.html | 51 ++++++++++++++++ src/dummy.zig | 1 + src/zemscripten.zig | 122 +++++++++++++++++++++++++++++++++++-- 4 files changed, 172 insertions(+), 5 deletions(-) create mode 100644 content/shell_minimal.html create mode 100644 src/dummy.zig diff --git a/build.zig b/build.zig index a41c751..5fa3c2d 100644 --- a/build.zig +++ b/build.zig @@ -3,11 +3,12 @@ const std = @import("std"); pub const emsdk_ver_major = "3"; pub const emsdk_ver_minor = "1"; -pub const emsdk_ver_tiny = "52"; +pub const emsdk_ver_tiny = "70"; pub const emsdk_version = emsdk_ver_major ++ "." ++ emsdk_ver_minor ++ "." ++ emsdk_ver_tiny; pub fn build(b: *std.Build) void { _ = b.addModule("root", .{ .root_source_file = b.path("src/zemscripten.zig") }); + _ = b.addModule("dummy", .{ .root_source_file = b.path("src/dummy.zig") }); } pub fn emccPath(b: *std.Build) []const u8 { diff --git a/content/shell_minimal.html b/content/shell_minimal.html new file mode 100644 index 0000000..9e4ee64 --- /dev/null +++ b/content/shell_minimal.html @@ -0,0 +1,51 @@ + + + + + + + Resource Simulation + + + + + + + {{{ SCRIPT }}} + + diff --git a/src/dummy.zig b/src/dummy.zig new file mode 100644 index 0000000..e95cb4c --- /dev/null +++ b/src/dummy.zig @@ -0,0 +1 @@ +pub const is_emscripten = false; diff --git a/src/zemscripten.zig b/src/zemscripten.zig index faa60e4..7f6500d 100644 --- a/src/zemscripten.zig +++ b/src/zemscripten.zig @@ -6,10 +6,7 @@ comptime { _ = std.testing.refAllDeclsRecursive(@This()); } -extern fn emscripten_err([*c]const u8) void; -extern fn emscripten_console_error([*c]const u8) void; -extern fn emscripten_console_warn([*c]const u8) void; -extern fn emscripten_console_log([*c]const u8) void; +pub extern fn emscripten_sleep(ms: u32) void; pub const MainLoopCallback = *const fn () callconv(.C) void; extern fn emscripten_set_main_loop(MainLoopCallback, c_int, c_int) void; @@ -21,6 +18,123 @@ pub const AnimationFrameCallback = *const fn (f64, ?*anyopaque) callconv(.C) c_i extern fn emscripten_request_animation_frame_loop(AnimationFrameCallback, ?*anyopaque) void; pub const requestAnimationFrameLoop = emscripten_request_animation_frame_loop; +pub const EmscriptenResult = enum(i16) { + success = 0, + deferred = 1, + not_supported = -1, + failed_not_deferred = -2, + invalid_target = -3, + unknown_target = -4, + invalid_param = -5, + failed = -6, + no_data = -7, + timed_out = -8, +}; +pub const CanvasSizeChangedCallback = *const fn ( + i16, + *anyopaque, + ?*anyopaque, +) callconv(.C) c_int; +pub fn setResizeCallback( + cb: CanvasSizeChangedCallback, + use_capture: bool, + user_data: ?*anyopaque, +) EmscriptenResult { + const result = emscripten_set_resize_callback_on_thread( + "2", + user_data, + @intFromBool(use_capture), + cb, + 2, + ); + return @enumFromInt(result); +} +extern fn emscripten_set_resize_callback_on_thread( + [*:0]const u8, + ?*anyopaque, + c_int, + CanvasSizeChangedCallback, + c_int, +) c_int; + +pub fn getElementCssSize( + target_id: [:0]const u8, + width: *f64, + height: *f64, +) EmscriptenResult { + return @enumFromInt(emscripten_get_element_css_size( + target_id, + width, + height, + )); +} +extern fn emscripten_get_element_css_size([*:0]const u8, *f64, *f64) c_int; + +/// EmmalocAllocator allocator +/// use with linker flag -sMALLOC=emmalloc +/// for details see docs: https://github.com/emscripten-core/emscripten/blob/main/system/lib/emmalloc.c +extern fn emmalloc_memalign(u32, u32) ?*anyopaque; +extern fn emmalloc_realloc_try(?*anyopaque, u32) ?*anyopaque; +extern fn emmalloc_free(?*anyopaque) void; +pub const EmmalocAllocator = struct { + const Self = @This(); + dummy: u32 = undefined, + + pub fn allocator(self: *Self) std.mem.Allocator { + return .{ + .ptr = self, + .vtable = &.{ + .alloc = &alloc, + .resize = &resize, + .free = &free, + }, + }; + } + + fn alloc( + ctx: *anyopaque, + len: usize, + ptr_align_log2: u8, + return_address: usize, + ) ?[*]u8 { + _ = ctx; + _ = return_address; + const ptr_align: u32 = @as(u32, 1) << @as(u5, @intCast(ptr_align_log2)); + if (!std.math.isPowerOfTwo(ptr_align)) unreachable; + const ptr = emmalloc_memalign(ptr_align, len) orelse return null; + return @ptrCast(ptr); + } + + fn resize( + ctx: *anyopaque, + buf: []u8, + buf_align_log2: u8, + new_len: usize, + return_address: usize, + ) bool { + _ = ctx; + _ = return_address; + _ = buf_align_log2; + return emmalloc_realloc_try(buf.ptr, new_len) != null; + } + + fn free( + ctx: *anyopaque, + buf: []u8, + buf_align_log2: u8, + return_address: usize, + ) void { + _ = ctx; + _ = buf_align_log2; + _ = return_address; + return emmalloc_free(buf.ptr); + } +}; + +extern fn emscripten_err([*c]const u8) void; +extern fn emscripten_console_error([*c]const u8) void; +extern fn emscripten_console_warn([*c]const u8) void; +extern fn emscripten_console_log([*c]const u8) void; /// std.panic impl pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, ret_addr: ?usize) noreturn { _ = error_return_trace; From 5236a1046028a2d5b0e3547476d06529dec1c98f Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Sun, 17 Nov 2024 17:20:21 -0800 Subject: [PATCH 2/9] Expose content dir and set shell file --- build.zig | 2 +- build.zig.zon | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/build.zig b/build.zig index 5fa3c2d..1ed676c 100644 --- a/build.zig +++ b/build.zig @@ -132,7 +132,7 @@ pub fn emccStep( use_preload_plugins: bool = false, embed_paths: ?[]const EmccFilePath = null, preload_paths: ?[]const EmccFilePath = null, - shell_file_path: ?[]const u8 = null, + shell_file_path: ?[]const u8 = "content/shell_minimal.html", install_dir: std.Build.InstallDir, }, ) *std.Build.Step { diff --git a/build.zig.zon b/build.zig.zon index a870b48..b499450 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,10 +1,11 @@ .{ .name = "zemscripten", - .version = "0.2.0-dev", + .version = "0.2.1-dev", .paths = .{ "build.zig", "build.zig.zon", "src", + "content", "LICENSE", "README.md", }, From aa99455ace81e2a74f7995d1d3124826c878c247 Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Thu, 28 Nov 2024 16:25:06 -0700 Subject: [PATCH 3/9] Update emscripten --- build.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.zig b/build.zig index 1ed676c..6f16406 100644 --- a/build.zig +++ b/build.zig @@ -3,7 +3,7 @@ const std = @import("std"); pub const emsdk_ver_major = "3"; pub const emsdk_ver_minor = "1"; -pub const emsdk_ver_tiny = "70"; +pub const emsdk_ver_tiny = "73"; pub const emsdk_version = emsdk_ver_major ++ "." ++ emsdk_ver_minor ++ "." ++ emsdk_ver_tiny; pub fn build(b: *std.Build) void { @@ -132,7 +132,7 @@ pub fn emccStep( use_preload_plugins: bool = false, embed_paths: ?[]const EmccFilePath = null, preload_paths: ?[]const EmccFilePath = null, - shell_file_path: ?[]const u8 = "content/shell_minimal.html", + shell_file_path: ?[]const u8 = null, install_dir: std.Build.InstallDir, }, ) *std.Build.Step { From cbcddefcec61b413880cbad274cfe104378ace7c Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Sat, 14 Dec 2024 15:45:05 -0800 Subject: [PATCH 4/9] Add emscripten html file --- README.md | 1 + content/shell.html | 105 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 content/shell.html diff --git a/README.md b/README.md index 2a52def..e3ad347 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ Add zemscripten's "root" module to your wasm compile target., then create an `em .embed_paths = &.{}, .preload_paths = &.{}, .install_dir = .{ .custom = "web" }, + .shell_file_path = "content/shell.html", }, ); emcc_step.dependOn(activate_emsdk_step); diff --git a/content/shell.html b/content/shell.html new file mode 100644 index 0000000..df59a40 --- /dev/null +++ b/content/shell.html @@ -0,0 +1,105 @@ + + + + + + Emscripten-Generated Code + + + + {{{ SHELL_LOGO }}} + +
+
Downloading...
+ + + Resize canvas + Lock/hide mouse pointer     + + + + +
+ +
+ +
+ +
+ + + + {{{ SCRIPT }}} + + From 54de61c032f88f7031dd5548c3713062379de196 Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Sat, 14 Dec 2024 15:51:11 -0800 Subject: [PATCH 5/9] Give user a default html file from emsdk --- build.zig | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/build.zig b/build.zig index 6f16406..4cfc154 100644 --- a/build.zig +++ b/build.zig @@ -33,6 +33,13 @@ pub fn emrunPath(b: *std.Build) []const u8 { }) catch unreachable; } +pub fn htmlPath(b: *std.Build) []const u8 { + return std.fs.path.join(b.allocator, &.{ + b.dependency("emsdk", .{}).path("").getPath(b), + "upstream/emscripten/src/shell.html", + }) catch unreachable; +} + pub fn activateEmsdkStep(b: *std.Build) *std.Build.Step { const emsdk_script_path = std.fs.path.join(b.allocator, &.{ b.dependency("emsdk", .{}).path("").getPath(b), From bc2ce6f8ba2dd3be61f6eace2fa112355a7f038b Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Sat, 14 Dec 2024 15:57:25 -0800 Subject: [PATCH 6/9] Link to emscripten html file --- README.md | 2 +- content/shell.html | 105 ------------------------------------- content/shell_minimal.html | 51 ------------------ 3 files changed, 1 insertion(+), 157 deletions(-) delete mode 100644 content/shell.html delete mode 100644 content/shell_minimal.html diff --git a/README.md b/README.md index e3ad347..b1a3c01 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Add zemscripten's "root" module to your wasm compile target., then create an `em .embed_paths = &.{}, .preload_paths = &.{}, .install_dir = .{ .custom = "web" }, - .shell_file_path = "content/shell.html", + .shell_file_path = @import("zemscripten").htmlPath(b), }, ); emcc_step.dependOn(activate_emsdk_step); diff --git a/content/shell.html b/content/shell.html deleted file mode 100644 index df59a40..0000000 --- a/content/shell.html +++ /dev/null @@ -1,105 +0,0 @@ - - - - - - Emscripten-Generated Code - - - - {{{ SHELL_LOGO }}} - -
-
Downloading...
- - - Resize canvas - Lock/hide mouse pointer     - - - - -
- -
- -
- -
- - - - {{{ SCRIPT }}} - - diff --git a/content/shell_minimal.html b/content/shell_minimal.html deleted file mode 100644 index 9e4ee64..0000000 --- a/content/shell_minimal.html +++ /dev/null @@ -1,51 +0,0 @@ - - - - - - - Resource Simulation - - - - - - - {{{ SCRIPT }}} - - From ab50362b82cca3fb8ce6dc895895fc24b3402b5c Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Fri, 31 Jan 2025 15:52:55 -0800 Subject: [PATCH 7/9] Helpful Nitpicks --- README.md | 20 +++++++++++++++++++- build.zig.zon | 2 +- src/dummy.zig | 1 - src/zemscripten.zig | 6 +++--- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index b1a3c01..c3d9276 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,6 @@ Add zemscripten's "root" module to your wasm compile target., then create an `em .embed_paths = &.{}, .preload_paths = &.{}, .install_dir = .{ .custom = "web" }, - .shell_file_path = @import("zemscripten").htmlPath(b), }, ); emcc_step.dependOn(activate_emsdk_step); @@ -53,6 +52,24 @@ Add zemscripten's "root" module to your wasm compile target., then create an `em b.getInstallStep().dependOn(emcc_step); ``` +To use a custom html file emccStep() accepts a shell_file_path option: +```zig + const emcc_step = @import("zemscripten").emccStep( + b, + wasm, + .{ + .optimize = optimize, + .flags = emcc_flags, + .settings = emcc_settings, + .use_preload_plugins = true, + .embed_paths = &.{}, + .preload_paths = &.{}, + .install_dir = .{ .custom = "web" }, + .shell_file_path = "path/to/file" + }, + ); +``` + Now you can use the provided Zig panic and log overrides in your wasm's root module and define the entry point that invoked by the js output of `emcc` (by default it looks for a symbol named `main`). For example: ```zig const std = @import("std"); @@ -86,3 +103,4 @@ You can also define a run step that invokes `emrun`. This will serve the html lo b.step("emrun", "Build and open the web app locally using emrun").dependOn(emrun_step); ``` See the [emrun documentation](https://emscripten.org/docs/compiling/Running-html-files-with-emrun.html) for the difference args that can be used. + diff --git a/build.zig.zon b/build.zig.zon index b499450..877954b 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,6 +1,6 @@ .{ .name = "zemscripten", - .version = "0.2.1-dev", + .version = "0.2.0-dev", .paths = .{ "build.zig", "build.zig.zon", diff --git a/src/dummy.zig b/src/dummy.zig index e95cb4c..e69de29 100644 --- a/src/dummy.zig +++ b/src/dummy.zig @@ -1 +0,0 @@ -pub const is_emscripten = false; diff --git a/src/zemscripten.zig b/src/zemscripten.zig index 7f6500d..b864f34 100644 --- a/src/zemscripten.zig +++ b/src/zemscripten.zig @@ -70,9 +70,9 @@ pub fn getElementCssSize( } extern fn emscripten_get_element_css_size([*:0]const u8, *f64, *f64) c_int; -/// EmmalocAllocator allocator -/// use with linker flag -sMALLOC=emmalloc -/// for details see docs: https://github.com/emscripten-core/emscripten/blob/main/system/lib/emmalloc.c +// EmmalocAllocator allocator +// use with linker flag -sMALLOC=emmalloc +// for details see docs: https://github.com/emscripten-core/emscripten/blob/main/system/lib/emmalloc.c extern fn emmalloc_memalign(u32, u32) ?*anyopaque; extern fn emmalloc_realloc_try(?*anyopaque, u32) ?*anyopaque; extern fn emmalloc_free(?*anyopaque) void; From 58c0078e71b467add5cc00208c8ee3327e52c58a Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Fri, 31 Jan 2025 19:22:45 -0800 Subject: [PATCH 8/9] Add optimizations flag and remove content from zon --- build.zig | 11 ++++++++--- build.zig.zon | 1 - 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/build.zig b/build.zig index 4cfc154..d314831 100644 --- a/build.zig +++ b/build.zig @@ -85,9 +85,14 @@ pub const EmccFlags = std.StringHashMap(void); pub fn emccDefaultFlags(allocator: std.mem.Allocator, optimize: std.builtin.OptimizeMode) EmccFlags { var args = EmccFlags.init(allocator); - if (optimize == .Debug) { - args.put("-Og", {}) catch unreachable; - args.put("-gsource-map", {}) catch unreachable; + switch (optimize) { + .Debug => { + args.put("-gsource-map", {}) catch unreachable; + }, + .ReleaseSmall, .ReleaseFast => { + args.put("-O3", {}) catch unreachable; + }, + else => {}, } return args; } diff --git a/build.zig.zon b/build.zig.zon index 877954b..a870b48 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -5,7 +5,6 @@ "build.zig", "build.zig.zon", "src", - "content", "LICENSE", "README.md", }, From d05e572dd1578bf818645809f4152ec6f9b9e6be Mon Sep 17 00:00:00 2001 From: Connor Rowland Date: Sun, 2 Feb 2025 17:20:29 -0800 Subject: [PATCH 9/9] Remove dummy file --- build.zig | 1 - src/dummy.zig | 0 2 files changed, 1 deletion(-) delete mode 100644 src/dummy.zig diff --git a/build.zig b/build.zig index d314831..526aa86 100644 --- a/build.zig +++ b/build.zig @@ -8,7 +8,6 @@ pub const emsdk_version = emsdk_ver_major ++ "." ++ emsdk_ver_minor ++ "." ++ em pub fn build(b: *std.Build) void { _ = b.addModule("root", .{ .root_source_file = b.path("src/zemscripten.zig") }); - _ = b.addModule("dummy", .{ .root_source_file = b.path("src/dummy.zig") }); } pub fn emccPath(b: *std.Build) []const u8 { diff --git a/src/dummy.zig b/src/dummy.zig deleted file mode 100644 index e69de29..0000000