Skip to content

Commit

Permalink
Merge pull request #64 from capy-ui/wasm-no-async
Browse files Browse the repository at this point in the history
WASM support without async
  • Loading branch information
zenith391 authored Oct 26, 2023
2 parents b222a37 + 5025df4 commit 8d4b8d9
Show file tree
Hide file tree
Showing 5 changed files with 399 additions and 47 deletions.
8 changes: 7 additions & 1 deletion build_capy.zig
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ const WebServerStep = struct {
} else if (std.mem.eql(u8, path, "/capy.js")) {
file_path = try std.fs.path.join(req_allocator, &.{ build_root, "src/backends/wasm/capy.js" });
content_type = "application/javascript";
} else if (std.mem.eql(u8, path, "/capy-worker.js")) {
file_path = try std.fs.path.join(req_allocator, &.{ build_root, "src/backends/wasm/capy-worker.js" });
content_type = "application/javascript";
} else if (std.mem.eql(u8, path, "/zig-app.wasm")) {
file_path = self.exe.getOutputSource().getPath2(build, &self.step);
content_type = "application/wasm";
Expand All @@ -112,6 +115,8 @@ const WebServerStep = struct {
// try res.headers.append("Connection", res.request.headers.getFirstValue("Connection") orelse "close");
try res.headers.append("Connection", "close");
try res.headers.append("Content-Type", content_type);
try res.headers.append("Cross-Origin-Opener-Policy", "same-origin");
try res.headers.append("Cross-Origin-Embedder-Policy", "require-corp");

try res.do();
try res.writer().writeAll(content);
Expand Down Expand Up @@ -290,10 +295,11 @@ pub fn install(step: *std.Build.CompileStep, options: CapyBuildOptions) !*std.Bu
// Things like the image reader require more stack than given by default
// TODO: remove once ziglang/zig#12589 is merged
step.stack_size = @max(step.stack_size orelse 0, 256 * 1024);
step.export_symbol_names = &.{ "_start", "_capyStep" };
if (step.optimize == .ReleaseSmall) {
step.strip = true;
}
step.export_symbol_names = &.{"_start"};
step.import_memory = true;

const serve = WebServerStep.create(b, step);
const install_step = b.addInstallArtifact(step, .{});
Expand Down
101 changes: 74 additions & 27 deletions src/backends/wasm/backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,16 @@ pub const Container = struct {
_ = self;
}

pub fn remove(self: *const Container, peer: PeerType) void {
_ = peer;
_ = self;
}

pub fn setTabOrder(self: *const Container, peers: []const PeerType) void {
_ = peers;
_ = self;
}

pub fn move(self: *const Container, peer: PeerType, x: u32, y: u32) void {
_ = self;
js.setPos(peer.element, x, y);
Expand Down Expand Up @@ -521,31 +531,59 @@ pub const HttpResponse = struct {
}
};

// Execution

fn executeStart() void {
const startFn = @import("root").start;
const ReturnType = @typeInfo(@TypeOf(startFn)).Fn.return_type.?;
if (ReturnType == void) {
startFn();
} else {
startFn() catch |err| @panic(@errorName(err));
}
}
var stopExecution = false;

fn executeStep() void {
// Check for events
// Temporary execution until async is added back in Zig
pub fn runStep(step: shared.EventLoopStep) bool {
_ = step;
js.yield();
while (js.hasEvent()) {
const eventId = js.popEvent();
if (globalWindow) |window| {
if (window.child) |child| {
child.processEventFn(child.object, eventId);
}
switch (js.getEventType(eventId)) {
else => {
if (globalWindow) |window| {
if (window.child) |child| {
child.processEventFn(child.object, eventId);
}
}
},
}
}
lib.eventStep.callListeners();
return !stopExecution;
}

fn executeMain() void {
const mainFn = @import("root").main;
const ReturnType = @typeInfo(@TypeOf(mainFn)).Fn.return_type.?;
if (ReturnType == void) {
mainFn();
} else {
mainFn() catch |err| @panic(@errorName(err));
}
js.stopExecution();
stopExecution = true;
}

// Execution
// TODO: reuse the old system when async is finally reimplemented in the zig compiler

// fn executeMain() callconv(.Async) void {
// const mainFn = @import("root").main;
// const ReturnType = @typeInfo(@TypeOf(mainFn)).Fn.return_type.?;
// if (ReturnType == void) {
// mainFn();
// } else {
// mainFn() catch |err| @panic(@errorName(err));
// }
// js.stopExecution();
// }

// var frame: @Frame(executeMain) = undefined;
// var result: void = {};
// var suspending: bool = false;

// var resumePtr: anyframe = undefined;

fn milliTimestamp() i64 {
return @as(i64, @intFromFloat(js.now()));
}
Expand Down Expand Up @@ -587,11 +625,7 @@ pub const backendExport = struct {

const start = milliTimestamp();
while (milliTimestamp() < start + @as(i64, @intCast(duration))) {
// TODO: when zig async is restored, use suspend here
// suspending = true;
// suspend {
// resumePtr = @frame();
// }
// TODO: better way to sleep like calling a jS function for sleep
}
return 0;
}
Expand Down Expand Up @@ -633,15 +667,24 @@ pub const backendExport = struct {

//@breakpoint();
js.stopExecution();
stopExecution = true;
while (true) {}
}

pub export fn _start() callconv(.C) void {
executeStart();
executeMain();
}

pub export fn _capyStep() callconv(.C) void {
executeStep();
}
// pub export fn _start() callconv(.C) void {
// _ = @asyncCall(&frame, &result, executeMain, .{});
// }

// pub export fn _capyStep() callconv(.C) void {
// if (suspending) {
// suspending = false;
// resume resumePtr;
// }
// }
};

// pub fn runStep(step: shared.EventLoopStep) callconv(.Async) bool {
Expand All @@ -658,5 +701,9 @@ pub const backendExport = struct {
// },
// }
// }
// suspending = true;
// suspend {
// resumePtr = @frame();
// }
// return true;
// }
Loading

0 comments on commit 8d4b8d9

Please sign in to comment.