Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## Features/Changes
* Lib: fix the type of some DOM properties and methods (#1747)
* Test: use dune test stanzas (#1631)
* Wasm: dispatch `wasmoocaml:loaded` and `wasmoocaml:error` `CustomEvent`s on
`globalThis` so that surrounding JavaScript can wait for the asynchronous
Wasm instantiation to complete (#11)

# 5.9.1 (02-12-2024) - Lille

Expand Down
69 changes: 42 additions & 27 deletions runtime/wasm/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,35 +531,50 @@
}
return { instance: { exports: Object.assign(imports.env, imports.OCaml) } };
}
const wasmModule = await instantiateFromDir();
function dispatchLifecycleEvent(name, detail) {
if (
typeof globalThis.dispatchEvent === "function" &&
typeof CustomEvent === "function"
) {
globalThis.dispatchEvent(new CustomEvent(name, { detail }));
}
}

try {
const wasmModule = await instantiateFromDir();

var {
caml_callback,
caml_alloc_tm,
caml_start_fiber,
caml_handle_uncaught_exception,
caml_buffer,
caml_extract_string,
string_get,
string_set,
_initialize,
} = wasmModule.instance.exports;
var {
caml_callback,
caml_alloc_tm,
caml_start_fiber,
caml_handle_uncaught_exception,
caml_buffer,
caml_extract_string,
string_get,
string_set,
_initialize,
} = wasmModule.instance.exports;

var buffer = caml_buffer?.buffer;
var out_buffer = buffer && new Uint8Array(buffer, 0, buffer.length);
var buffer = caml_buffer?.buffer;
var out_buffer = buffer && new Uint8Array(buffer, 0, buffer.length);

start_fiber = make_promising(caml_start_fiber);
var _initialize = make_promising(_initialize);
var process = globalThis.process;
if (process && process.on) {
process.on("uncaughtException", (err, origin) =>
caml_handle_uncaught_exception(err),
);
} else if (globalThis.addEventListener) {
globalThis.addEventListener(
"error",
(event) => event.error && caml_handle_uncaught_exception(event.error),
);
start_fiber = make_promising(caml_start_fiber);
var _initialize = make_promising(_initialize);
var process = globalThis.process;
if (process && process.on) {
process.on("uncaughtException", (err, origin) =>
caml_handle_uncaught_exception(err),
);
} else if (globalThis.addEventListener) {
globalThis.addEventListener(
"error",
(event) => event.error && caml_handle_uncaught_exception(event.error),
);
}
await _initialize();
dispatchLifecycleEvent("wasmoocaml:loaded", { src });
} catch (error) {
dispatchLifecycleEvent("wasmoocaml:error", { src, error });
throw error;
}
await _initialize();
};
Loading