Skip to content

Commit 8d4b8d9

Browse files
authored
Merge pull request #64 from capy-ui/wasm-no-async
WASM support without async
2 parents b222a37 + 5025df4 commit 8d4b8d9

File tree

5 files changed

+399
-47
lines changed

5 files changed

+399
-47
lines changed

build_capy.zig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ const WebServerStep = struct {
8787
} else if (std.mem.eql(u8, path, "/capy.js")) {
8888
file_path = try std.fs.path.join(req_allocator, &.{ build_root, "src/backends/wasm/capy.js" });
8989
content_type = "application/javascript";
90+
} else if (std.mem.eql(u8, path, "/capy-worker.js")) {
91+
file_path = try std.fs.path.join(req_allocator, &.{ build_root, "src/backends/wasm/capy-worker.js" });
92+
content_type = "application/javascript";
9093
} else if (std.mem.eql(u8, path, "/zig-app.wasm")) {
9194
file_path = self.exe.getOutputSource().getPath2(build, &self.step);
9295
content_type = "application/wasm";
@@ -112,6 +115,8 @@ const WebServerStep = struct {
112115
// try res.headers.append("Connection", res.request.headers.getFirstValue("Connection") orelse "close");
113116
try res.headers.append("Connection", "close");
114117
try res.headers.append("Content-Type", content_type);
118+
try res.headers.append("Cross-Origin-Opener-Policy", "same-origin");
119+
try res.headers.append("Cross-Origin-Embedder-Policy", "require-corp");
115120

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

298304
const serve = WebServerStep.create(b, step);
299305
const install_step = b.addInstallArtifact(step, .{});

src/backends/wasm/backend.zig

Lines changed: 74 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,16 @@ pub const Container = struct {
488488
_ = self;
489489
}
490490

491+
pub fn remove(self: *const Container, peer: PeerType) void {
492+
_ = peer;
493+
_ = self;
494+
}
495+
496+
pub fn setTabOrder(self: *const Container, peers: []const PeerType) void {
497+
_ = peers;
498+
_ = self;
499+
}
500+
491501
pub fn move(self: *const Container, peer: PeerType, x: u32, y: u32) void {
492502
_ = self;
493503
js.setPos(peer.element, x, y);
@@ -521,31 +531,59 @@ pub const HttpResponse = struct {
521531
}
522532
};
523533

524-
// Execution
525-
526-
fn executeStart() void {
527-
const startFn = @import("root").start;
528-
const ReturnType = @typeInfo(@TypeOf(startFn)).Fn.return_type.?;
529-
if (ReturnType == void) {
530-
startFn();
531-
} else {
532-
startFn() catch |err| @panic(@errorName(err));
533-
}
534-
}
534+
var stopExecution = false;
535535

536-
fn executeStep() void {
537-
// Check for events
536+
// Temporary execution until async is added back in Zig
537+
pub fn runStep(step: shared.EventLoopStep) bool {
538+
_ = step;
539+
js.yield();
538540
while (js.hasEvent()) {
539541
const eventId = js.popEvent();
540-
if (globalWindow) |window| {
541-
if (window.child) |child| {
542-
child.processEventFn(child.object, eventId);
543-
}
542+
switch (js.getEventType(eventId)) {
543+
else => {
544+
if (globalWindow) |window| {
545+
if (window.child) |child| {
546+
child.processEventFn(child.object, eventId);
547+
}
548+
}
549+
},
544550
}
545551
}
546-
lib.eventStep.callListeners();
552+
return !stopExecution;
553+
}
554+
555+
fn executeMain() void {
556+
const mainFn = @import("root").main;
557+
const ReturnType = @typeInfo(@TypeOf(mainFn)).Fn.return_type.?;
558+
if (ReturnType == void) {
559+
mainFn();
560+
} else {
561+
mainFn() catch |err| @panic(@errorName(err));
562+
}
563+
js.stopExecution();
564+
stopExecution = true;
547565
}
548566

567+
// Execution
568+
// TODO: reuse the old system when async is finally reimplemented in the zig compiler
569+
570+
// fn executeMain() callconv(.Async) void {
571+
// const mainFn = @import("root").main;
572+
// const ReturnType = @typeInfo(@TypeOf(mainFn)).Fn.return_type.?;
573+
// if (ReturnType == void) {
574+
// mainFn();
575+
// } else {
576+
// mainFn() catch |err| @panic(@errorName(err));
577+
// }
578+
// js.stopExecution();
579+
// }
580+
581+
// var frame: @Frame(executeMain) = undefined;
582+
// var result: void = {};
583+
// var suspending: bool = false;
584+
585+
// var resumePtr: anyframe = undefined;
586+
549587
fn milliTimestamp() i64 {
550588
return @as(i64, @intFromFloat(js.now()));
551589
}
@@ -587,11 +625,7 @@ pub const backendExport = struct {
587625

588626
const start = milliTimestamp();
589627
while (milliTimestamp() < start + @as(i64, @intCast(duration))) {
590-
// TODO: when zig async is restored, use suspend here
591-
// suspending = true;
592-
// suspend {
593-
// resumePtr = @frame();
594-
// }
628+
// TODO: better way to sleep like calling a jS function for sleep
595629
}
596630
return 0;
597631
}
@@ -633,15 +667,24 @@ pub const backendExport = struct {
633667

634668
//@breakpoint();
635669
js.stopExecution();
670+
stopExecution = true;
671+
while (true) {}
636672
}
637673

638674
pub export fn _start() callconv(.C) void {
639-
executeStart();
675+
executeMain();
640676
}
641677

642-
pub export fn _capyStep() callconv(.C) void {
643-
executeStep();
644-
}
678+
// pub export fn _start() callconv(.C) void {
679+
// _ = @asyncCall(&frame, &result, executeMain, .{});
680+
// }
681+
682+
// pub export fn _capyStep() callconv(.C) void {
683+
// if (suspending) {
684+
// suspending = false;
685+
// resume resumePtr;
686+
// }
687+
// }
645688
};
646689

647690
// pub fn runStep(step: shared.EventLoopStep) callconv(.Async) bool {
@@ -658,5 +701,9 @@ pub const backendExport = struct {
658701
// },
659702
// }
660703
// }
704+
// suspending = true;
705+
// suspend {
706+
// resumePtr = @frame();
707+
// }
661708
// return true;
662709
// }

0 commit comments

Comments
 (0)