diff --git a/integrations/deno/melody_wasm.d.ts b/integrations/deno/melody_wasm.d.ts index cf38885..a918a0c 100644 --- a/integrations/deno/melody_wasm.d.ts +++ b/integrations/deno/melody_wasm.d.ts @@ -1,53 +1,62 @@ /* tslint:disable */ /* eslint-disable */ /** -* -*Compiles Melody source code to a regular expression -* -*# Errors -* -*Throws an error if a compilation error is encountered -* -*# Example -* -*```js -*const source = ` -* ; -* -* option of "v"; -* -* capture major { -* some of ; -* } -* -* "."; -* -* capture minor { -* some of ; -* } -* -* "."; -* -* capture patch { -* some of ; -* } -* -* ; -*`; -* -*try { -* const output = compiler(source); -* new RegExp(output).test("v1.1.1"); // true -*} catch (error) { -* // handle compilation error -*} -*``` -* @param {string} source -* @returns {string} -*/ + * + *Compiles Melody source code to a regular expression + * + *# Errors + * + *Throws an error if a compilation error is encountered + * + *# Example + * + *```js + *import init, { compiler } from "https://deno.land/x/melody/melody_wasm.js"; + * + *await init(); + * + *const source = ` + * ; + * + * option of "v"; + * + * capture major { + * some of ; + * } + * + * "."; + * + * capture minor { + * some of ; + * } + * + * "."; + * + * capture patch { + * some of ; + * } + * + * ; + *`; + * + *try { + * const output = compiler(source); + * new RegExp(output).test("v1.1.1"); // true + *} catch (error) { + * // handle compilation error + *} + *``` + * @param {string} source + * @returns {string} + */ export function compiler(source: string): string; -export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; +export type InitInput = + | RequestInfo + | URL + | Response + | BufferSource + | WebAssembly.Module; export interface InitOutput { readonly memory: WebAssembly.Memory; @@ -59,11 +68,22 @@ export interface InitOutput { } /** -* If `module_or_path` is {RequestInfo} or {URL}, makes a request and -* for everything else, calls `WebAssembly.instantiate` directly. -* -* @param {InitInput | Promise} module_or_path -* -* @returns {Promise} -*/ -export default function init (module_or_path?: InitInput | Promise): Promise; + * Synchronously compiles the given `bytes` and instantiates the WebAssembly module. + * + * @param {BufferSource} bytes + * + * @returns {InitOutput} + */ +export function initSync(bytes: BufferSource): InitOutput; + +/** + * If `module_or_path` is {RequestInfo} or {URL}, makes a request and + * for everything else, calls `WebAssembly.instantiate` directly. + * + * @param {InitInput | Promise} module_or_path + * + * @returns {Promise} + */ +export default function init( + module_or_path?: InitInput | Promise +): Promise; diff --git a/integrations/deno/melody_wasm.js b/integrations/deno/melody_wasm.js index 8d71e35..7921d42 100644 --- a/integrations/deno/melody_wasm.js +++ b/integrations/deno/melody_wasm.js @@ -7,15 +7,12 @@ const cachedTextDecoder = new TextDecoder("utf-8", { cachedTextDecoder.decode(); -let cachegetUint8Memory0 = null; +let cachedUint8Memory0; function getUint8Memory0() { - if ( - cachegetUint8Memory0 === null || - cachegetUint8Memory0.buffer !== wasm.memory.buffer - ) { - cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer); + if (cachedUint8Memory0.byteLength === 0) { + cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); } - return cachegetUint8Memory0; + return cachedUint8Memory0; } function getStringFromWasm0(ptr, len) { @@ -94,15 +91,12 @@ function passStringToWasm0(arg, malloc, realloc) { return ptr; } -let cachegetInt32Memory0 = null; +let cachedInt32Memory0; function getInt32Memory0() { - if ( - cachegetInt32Memory0 === null || - cachegetInt32Memory0.buffer !== wasm.memory.buffer - ) { - cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer); + if (cachedInt32Memory0.byteLength === 0) { + cachedInt32Memory0 = new Int32Array(wasm.memory.buffer); } - return cachegetInt32Memory0; + return cachedInt32Memory0; } function getObject(idx) { @@ -131,10 +125,6 @@ function takeObject(idx) { *# Example * *```js - *import init, { compiler } from "https://deno.land/x/melody/melody_wasm.js"; - * - *await init(); - * *const source = ` * ; * @@ -227,10 +217,7 @@ async function load(module, imports) { } } -async function init(input) { - if (typeof input === "undefined") { - input = new URL("melody_wasm_bg.wasm", import.meta.url); - } +function getImports() { const imports = {}; imports.wbg = {}; imports.wbg.__wbindgen_error_new = function (arg0, arg1) { @@ -238,6 +225,37 @@ async function init(input) { return addHeapObject(ret); }; + return imports; +} + +function initMemory(imports, maybe_memory) {} + +function finalizeInit(instance, module) { + wasm = instance.exports; + init.__wbindgen_wasm_module = module; + cachedInt32Memory0 = new Int32Array(wasm.memory.buffer); + cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer); + + return wasm; +} + +function initSync(bytes) { + const imports = getImports(); + + initMemory(imports); + + const module = new WebAssembly.Module(bytes); + const instance = new WebAssembly.Instance(module, imports); + + return finalizeInit(instance, module); +} + +async function init(input) { + if (typeof input === "undefined") { + input = new URL("melody_wasm_bg.wasm", import.meta.url); + } + const imports = getImports(); + if ( typeof input === "string" || (typeof Request === "function" && input instanceof Request) || @@ -246,12 +264,12 @@ async function init(input) { input = fetch(input); } - const { instance, module } = await load(await input, imports); + initMemory(imports); - wasm = instance.exports; - init.__wbindgen_wasm_module = module; + const { instance, module } = await load(await input, imports); - return wasm; + return finalizeInit(instance, module); } +export { initSync }; export default init; diff --git a/integrations/deno/melody_wasm_bg.wasm b/integrations/deno/melody_wasm_bg.wasm index 7e4b965..82bcc0b 100644 Binary files a/integrations/deno/melody_wasm_bg.wasm and b/integrations/deno/melody_wasm_bg.wasm differ