Skip to content

Instantly share code, notes, and snippets.

@mizchi
Created April 30, 2024 12:29
Show Gist options
  • Save mizchi/f8ec626023c3e6d823708c2f204de15c to your computer and use it in GitHub Desktop.
Save mizchi/f8ec626023c3e6d823708c2f204de15c to your computer and use it in GitHub Desktop.
// gc impl
import { flush, js_string, setMemory, spectest } from "./js_string.ts";
const { instance: { exports } } = await WebAssembly.instantiateStreaming(
fetch(new URL("../target/wasm-gc/release/build/main/main.wasm", import.meta.url)),
{
js_string,
spectest,
js: {
setTimeout, clearTimeout,
setInterval, clearInterval,
run(fn: any) {
return fn();
}
},
"moonbit:ffi": {
make_closure(funcref: any, context: any) {
// console.log("make_closure", funcref, context);
return funcref.bind(null, context)
}
},
}
);
const {
run,
_start,
take_js_and_run,
["moonbit.memory"]: memory,
} = exports as any;
_start();
setMemory(memory);
run();
take_js_and_run(() => {
console.log("call me");
});
flush();
fn set_timeout(cb : () -> Unit, timeout : Int) -> Int = "js" "setTimeout"
fn clear_timeout(tid : Int) -> Unit = "js" "clearTimeout"
type JsFunc
fn run_js(js_fn : JsFunc) -> Unit = "js" "run"
pub fn run() -> Unit {
let tid = set_timeout(fn() { println("never called") }, 100)
clear_timeout(tid)
let _ = set_timeout(fn() { println("timeout called") }, 100)
()
}
pub fn take_js_and_run(func : JsFunc) -> Unit {
run_js(func)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment