Last active
April 3, 2021 00:00
-
-
Save creationix/a91f8d5ded4ba809f45d41d06e2780d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern fn slowAddJS(a: u32, b: u32, frame: *@Frame(slowAdd)) void; | |
var addResult: u32 = 0; | |
export fn onSlowAdd(frame: *@Frame(slowAdd), result: u32) void { | |
addResult = result; | |
resume frame; | |
} | |
fn slowAdd(a: u32, b: u32) u32 { | |
slowAddJS(a, b, @frame()); | |
suspend; | |
return addResult; | |
} | |
extern fn print(val: u32) void; | |
fn realMain(a: u32, b: u32) void { | |
print(a); | |
print(b); | |
print(0); | |
const result = slowAdd(a, b); | |
print(a); | |
print(b); | |
print(result); | |
} | |
var mainFrame1: @Frame(realMain) = undefined; | |
var mainFrame2: @Frame(realMain) = undefined; | |
export fn main() void { | |
mainFrame1 = async realMain(1, 2); | |
mainFrame2 = async realMain(10, 20); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let exports; | |
var importObject = { | |
env: { | |
slowAddJS(a, b, frame) { | |
console.log("slowAddJS", { a, b, frame }); | |
setTimeout(() => { | |
console.log("Calling zig callback...") | |
exports.onSlowAdd(frame, a + b); | |
}, 100); | |
}, | |
print(a) { | |
console.log("print", { a }); | |
} | |
} | |
}; | |
WebAssembly.instantiateStreaming(fetch('main.wasm'), importObject) | |
.then(obj => { | |
exports = obj.instance.exports; | |
console.log("Calling zig main via wasm...") | |
exports.main() | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
log: