Skip to content

Commit

Permalink
add other files (2AM commit haha)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 4, 2024
1 parent 423d6ce commit 53cd883
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 15 deletions.
58 changes: 54 additions & 4 deletions src/plugin/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readPackageJSON } from "pkg-types";
import {
UMWASM_HELPERS_ID,
UNWASM_EXTERNAL_PREFIX,
Expand All @@ -8,19 +9,33 @@ import {
// https://marketplace.visualstudio.com/items?itemName=Tobermory.es6-string-html
const js = String.raw;

export function getWasmBinding(asset: WasmAsset, opts: UnwasmPluginOptions) {
export async function getWasmBinding(
asset: WasmAsset,
opts: UnwasmPluginOptions,
) {
// -- Auto load imports --
const autoImports = await getWasmImports(asset, opts);

// --- Environment dependent code to initialize the wasm module using inlined base 64 or dynamic import ---
const envCode: string = opts.esmImport
? js`
async function _instantiate(imports) {
${autoImports};
async function _instantiate(imports = _imports) {
const _mod = await import("${UNWASM_EXTERNAL_PREFIX}${asset.name}").then(r => r.default || r);
return WebAssembly.instantiate(_mod, imports)
try {
return await WebAssembly.instantiate(_mod, imports)
} catch (error) {
console.error('[wasm] [error]', error);
throw error;
}
}
`
: js`
import { base64ToUint8Array } from "${UMWASM_HELPERS_ID}";
${autoImports};
function _instantiate(imports) {
function _instantiate(imports = _imports) {
const _data = base64ToUint8Array("${asset.source.toString("base64")}")
return WebAssembly.instantiate(_data, imports)
}
Expand Down Expand Up @@ -136,3 +151,38 @@ export function createLazyWasmModule(_instantiator) {
}
`;
}

export async function getWasmImports(
asset: WasmAsset,
opts: UnwasmPluginOptions,

Check warning on line 157 in src/plugin/runtime.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest)

'opts' is defined but never used

Check warning on line 157 in src/plugin/runtime.ts

View workflow job for this annotation

GitHub Actions / autofix

'opts' is defined but never used
) {
const importNames = Object.keys(asset.imports || {});
if (importNames.length === 0) {
return "const _imports = { /* no imports */ }";
}
// Try to resolve from nearest package.json
const pkgJSON = await readPackageJSON(asset.id);

let code = "const _imports = {";

for (const moduleName of importNames) {
const importNames = asset.imports[moduleName];
const pkgImport =
pkgJSON.imports?.[moduleName] || pkgJSON.imports?.[`#${moduleName}`];

if (pkgImport) {
code = `import * as _imports_${moduleName} from "${pkgImport}";\n${code}`;
}
code += `\n ${moduleName}: {`;
for (const name of importNames) {
code += pkgImport
? `\n ${name}: _imports_${moduleName}.${name},\n`
: `\n ${name}: () => { throw new Error("\`${moduleName}.${name}\` is not provided!")},\n`;
}
code += " },\n";
}

code += "};\n";

return code;
}
6 changes: 0 additions & 6 deletions test/fixture/_shared.mjs

This file was deleted.

4 changes: 1 addition & 3 deletions test/fixture/dynamic-import.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const { sum } = await import("@fixture/wasm/sum.wasm");

const { imports } = await import("./_shared.mjs");
const { rand } = await import("@fixture/wasm/rand.wasm").then((r) =>
r.default(imports),
r.default(),
);

export function test() {
Expand Down
3 changes: 1 addition & 2 deletions test/fixture/static-import.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { imports } from "./_shared.mjs";
import { sum } from "@fixture/wasm/sum.wasm";
import initRand, { rand } from "@fixture/wasm/rand.wasm";

await initRand(imports);
await initRand();

export function test() {
if (sum(1, 2) !== 3) {
Expand Down
1 change: 1 addition & 0 deletions test/node_modules/@fixture/wasm/env.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/node_modules/@fixture/wasm/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 53cd883

Please sign in to comment.