feat: add deno_rust_wasm_import_functions

This commit is contained in:
2022-07-08 00:36:47 +08:00
parent 09c20602e9
commit e2d6b9c54f
11 changed files with 332 additions and 0 deletions

View File

@@ -0,0 +1,148 @@
// @generated file from wasmbuild -- do not edit
// deno-lint-ignore-file
// deno-fmt-ignore-file
// source-hash: 509bffc30c487fb405bbe67e9580cb31f09f9858
let wasm;
let cachedInt32Memory0;
const cachedTextDecoder = new TextDecoder("utf-8", {
ignoreBOM: true,
fatal: true,
});
cachedTextDecoder.decode();
let cachedUint8Memory0;
function getUint8Memory0() {
if (cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
}
function getStringFromWasm0(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}
/**
* @param {number} a
* @param {number} b
*/
export function alert_sum(a, b) {
wasm.alert_sum(a, b);
}
const imports = {
__wbindgen_placeholder__: {
__wbg_alert_1d2369aa1e130bee: function (arg0, arg1) {
alert(getStringFromWasm0(arg0, arg1));
},
},
};
const wasm_url = new URL("rs_lib_bg.wasm", import.meta.url);
/**
* Decompression callback
*
* @callback decompressCallback
* @param {Uint8Array} compressed
* @return {Uint8Array} decompressed
*/
/** Instantiates an instance of the Wasm module returning its functions.
* @remarks It is safe to call this multiple times and once successfully
* loaded it will always return a reference to the same object.
* @param {decompressCallback=} transform
*/
export async function instantiate(transform) {
return (await instantiateWithInstance(transform)).exports;
}
let instanceWithExports;
let lastLoadPromise;
/** Instantiates an instance of the Wasm module along with its exports.
* @remarks It is safe to call this multiple times and once successfully
* loaded it will always return a reference to the same object.
* @param {decompressCallback=} transform
* @returns {Promise<{
* instance: WebAssembly.Instance;
* exports: { alert_sum: typeof alert_sum }
* }>}
*/
export function instantiateWithInstance(transform) {
if (instanceWithExports != null) {
return Promise.resolve(instanceWithExports);
}
if (lastLoadPromise == null) {
lastLoadPromise = (async () => {
try {
const instance = (await instantiateModule(transform)).instance;
wasm = instance.exports;
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
instanceWithExports = {
instance,
exports: getWasmInstanceExports(),
};
return instanceWithExports;
} finally {
lastLoadPromise = null;
}
})();
}
return lastLoadPromise;
}
function getWasmInstanceExports() {
return { alert_sum };
}
/** Gets if the Wasm module has been instantiated. */
export function isInstantiated() {
return instanceWithExports != null;
}
async function instantiateModule(transform) {
switch (wasm_url.protocol) {
case "file:": {
if (typeof Deno !== "object") {
throw new Error("file urls are not supported in this environment");
}
if ("permissions" in Deno) {
Deno.permissions.request({ name: "read", path: wasm_url });
}
const wasmCode = await Deno.readFile(wasm_url);
return WebAssembly.instantiate(
!transform ? wasmCode : transform(wasmCode),
imports,
);
}
case "https:":
case "http:": {
if (typeof Deno === "object" && "permissions" in Deno) {
Deno.permissions.request({ name: "net", host: wasm_url.host });
}
const wasmResponse = await fetch(wasm_url);
if (transform) {
const wasmCode = new Uint8Array(await wasmResponse.arrayBuffer());
return WebAssembly.instantiate(transform(wasmCode), imports);
}
if (
wasmResponse.headers.get("content-type")?.toLowerCase().startsWith(
"application/wasm",
)
) {
return WebAssembly.instantiateStreaming(wasmResponse, imports);
} else {
return WebAssembly.instantiate(
await wasmResponse.arrayBuffer(),
imports,
);
}
}
default:
throw new Error(`Unsupported protocol: ${wasm_url.protocol}`);
}
}