UNPKG

@as-pect/assembly

Version:

Write AssemblyScript tests at blazing fast speeds

65 lines (64 loc) 2.49 kB
async function instantiate(module, imports = {}) { const adaptedImports = { env: Object.setPrototypeOf({ abort(message, fileName, lineNumber, columnNumber) { // ~lib/builtins/abort(~lib/string/String | null?, ~lib/string/String | null?, u32?, u32?) => void message = __liftString(message >>> 0); fileName = __liftString(fileName >>> 0); lineNumber = lineNumber >>> 0; columnNumber = columnNumber >>> 0; (() => { // @external.js throw Error(`${message} in ${fileName}:${lineNumber}:${columnNumber}`); })(); }, }, Object.assign(Object.create(globalThis), imports.env || {})), }; const { exports } = await WebAssembly.instantiate(module, adaptedImports); const memory = exports.memory || imports.env.memory; const adaptedExports = Object.setPrototypeOf({ __call(func) { // assembly/internal/call/__call(() => void) => void func = __lowerInternref(func) || __notnull(); exports.__call(func); }, __ignoreLogs(value) { // assembly/internal/log/__ignoreLogs(bool) => void value = value ? 1 : 0; exports.__ignoreLogs(value); }, }, exports); function __liftString(pointer) { if (!pointer) return null; const end = pointer + new Uint32Array(memory.buffer)[pointer - 4 >>> 2] >>> 1, memoryU16 = new Uint16Array(memory.buffer); let start = pointer >>> 1, string = ""; while (end - start > 1024) string += String.fromCharCode(...memoryU16.subarray(start, start += 1024)); return string + String.fromCharCode(...memoryU16.subarray(start, end)); } class Internref extends Number {} function __lowerInternref(value) { if (value == null) return 0; if (value instanceof Internref) return value.valueOf(); throw TypeError("internref expected"); } function __notnull() { throw TypeError("value must not be null"); } return adaptedExports; } export const { memory, __call, __ignoreLogs, } = await (async url => instantiate( await (async () => { const isNodeOrBun = typeof process != "undefined" && process.versions != null && (process.versions.node != null || process.versions.bun != null); if (isNodeOrBun) { return globalThis.WebAssembly.compile(await (await import("node:fs/promises")).readFile(url)); } else { return await globalThis.WebAssembly.compileStreaming(globalThis.fetch(url)); } })(), { } ))(new URL("release.wasm", import.meta.url));