mach: Add js-runtime as a dependency for wasm applications

This is a workaround for a limitation right now. The html-generator has
no way to dynamically add JS sources (and that can't be done without
using a preprocessor library) so we hardcode js-runtime in it.

In the future, I think the correct behavior would be to move tools/
inside js-runtime along with a wasm application building SDK and get rid
of any direct JS access we have today (which is just
src/platform/wasm.zig and src/platform/mach.js).
This commit is contained in:
iddev5 2022-07-06 17:50:12 +05:30 committed by Stephen Gutekanst
parent d10ab25c5d
commit 0ff8edebd9
2 changed files with 14 additions and 7 deletions

View file

@ -5,6 +5,7 @@ const gpu_dawn = @import("gpu-dawn/build.zig");
pub const glfw = @import("glfw/build.zig"); pub const glfw = @import("glfw/build.zig");
pub const ecs = @import("ecs/build.zig"); pub const ecs = @import("ecs/build.zig");
const freetype = @import("freetype/build.zig"); const freetype = @import("freetype/build.zig");
const js_runtime = @import("js-runtime/build.zig");
const Pkg = std.build.Pkg; const Pkg = std.build.Pkg;
pub fn build(b: *std.build.Builder) void { pub fn build(b: *std.build.Builder) void {
@ -162,6 +163,7 @@ pub const App = struct {
if (options.target.toTarget().cpu.arch == .wasm32) { if (options.target.toTarget().cpu.arch == .wasm32) {
const lib = b.addSharedLibrary(options.name, thisDir() ++ "/src/platform/wasm.zig", .unversioned); const lib = b.addSharedLibrary(options.name, thisDir() ++ "/src/platform/wasm.zig", .unversioned);
lib.addPackage(gpu.pkg); lib.addPackage(gpu.pkg);
lib.addPackage(js_runtime.pkg);
break :blk lib; break :blk lib;
} else { } else {
@ -194,12 +196,14 @@ pub const App = struct {
// Set install directory to '{prefix}/www' // Set install directory to '{prefix}/www'
app.getInstallStep().?.dest_dir = web_install_dir; app.getInstallStep().?.dest_dir = web_install_dir;
const install_mach_js = app.b.addInstallFileWithDir( inline for (.{ "/src/platform/mach.js", "/js-runtime/src/js-runtime.js" }) |js| {
.{ .path = thisDir() ++ "/src/platform/mach.js" }, const install_js = app.b.addInstallFileWithDir(
.{ .path = thisDir() ++ js },
web_install_dir, web_install_dir,
"mach.js", std.fs.path.basename(js),
); );
app.getInstallStep().?.step.dependOn(&install_mach_js.step); app.getInstallStep().?.step.dependOn(&install_js.step);
}
const html_generator = app.b.addExecutable("html-generator", thisDir() ++ "/tools/html-generator.zig"); const html_generator = app.b.addExecutable("html-generator", thisDir() ++ "/tools/html-generator.zig");
html_generator.main_pkg_path = thisDir(); html_generator.main_pkg_path = thisDir();
@ -209,6 +213,7 @@ pub const App = struct {
u8, u8,
&.{ app.name, ".html" }, &.{ app.name, ".html" },
) catch unreachable, app.name }); ) catch unreachable, app.name });
run_html_generator.cwd = app.b.getInstallPath(web_install_dir, ""); run_html_generator.cwd = app.b.getInstallPath(web_install_dir, "");
app.getInstallStep().?.step.dependOn(&run_html_generator.step); app.getInstallStep().?.step.dependOn(&run_html_generator.step);
} }

View file

@ -6,9 +6,10 @@
<body> <body>
<script type="module"> <script type="module">
import {{ mach }} from "./mach.js"; import {{ mach }} from "./mach.js";
import {{ zig }} from "./js-runtime.js";
let imports = {{ let imports = {{
env: mach, env: {{ ...mach, ...zig }},
}}; }};
fetch("{s}.wasm") fetch("{s}.wasm")
@ -16,6 +17,7 @@
.then(buffer => WebAssembly.instantiate(buffer, imports)) .then(buffer => WebAssembly.instantiate(buffer, imports))
.then(results => results.instance) .then(results => results.instance)
.then(instance => {{ .then(instance => {{
zig.init(instance);
mach.init(instance); mach.init(instance);
instance.exports.wasmInit(); instance.exports.wasmInit();