Skip to content

Commit

Permalink
feat: use Thenable dynamic import
Browse files Browse the repository at this point in the history
  • Loading branch information
xtuc committed Mar 29, 2018
1 parent b700bca commit b207fb8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
54 changes: 30 additions & 24 deletions example/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,30 @@ function logfoo() {
console.log("foo");
}

function instantiate() {
// (module
// (import "./env.js" "mem" (memory 1))
// (import "./env.js" "number" (global i32))
// (import "./env.js" "logfoo" (func))
//
// (func (export "test") (result i32)
// (i32.load8_s (i32.const 0))
// )
// (func (export "getNumber") (result i32)
// (get_global 0)
// )
// (func (export "logfoofromwasm") call 0)
// )

Promise.resolve().then(function () { return test; }).then(({test, getNumber, logfoofromwasm}) => {
const i32 = new Uint32Array(mem.buffer);
console.log("test()", test(), "expected", i32[0]);

console.log("getNumber()", getNumber());

logfoofromwasm();
});

function then(resolve) {
if (typeof WebAssembly === 'undefined') {
throw new Error('WebAssembly is not supported');
}
Expand All @@ -25,35 +48,18 @@ function instantiate() {

const req = window.fetch("/modulee01d.wasm");

return WebAssembly
WebAssembly
.instantiateStreaming(req, {"./env.js":{
"mem":mem,
"number":number,
"logfoo":logfoo,
},
})
.then(res => res.instance.exports);
.then(res => res.instance.exports)
.then(resolve)
.catch(resolve);
}

// (module
// (import "./env.js" "mem" (memory 1))
// (import "./env.js" "number" (global i32))
// (import "./env.js" "logfoo" (func))
//
// (func (export "test") (result i32)
// (i32.load8_s (i32.const 0))
// )
// (func (export "getNumber") (result i32)
// (get_global 0)
// )
// (func (export "logfoofromwasm") call 0)
// )

instantiate().then(({test, getNumber, logfoofromwasm}) => {
const i32 = new Uint32Array(mem.buffer);
console.log("test()", test(), "expected", i32[0]);

console.log("getNumber()", getNumber());

logfoofromwasm();
var test = /*#__PURE__*/Object.freeze({
then: then
});
4 changes: 1 addition & 3 deletions example/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import instantiate from "./test.wasm"

import {mem} from "./env.js"

// (module
Expand All @@ -16,7 +14,7 @@ import {mem} from "./env.js"
// (func (export "logfoofromwasm") call 0)
// )

instantiate().then(({test, getNumber, logfoofromwasm}) => {
import("./test.wasm").then(({test, getNumber, logfoofromwasm}) => {
const i32 = new Uint32Array(mem.buffer);
console.log("test()", test(), "expected", i32[0]);

Expand Down
12 changes: 9 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function generateImportObject(o) {
const buildLoader = ({URL, IMPORT_OBJECT, IMPORTS}) => (`
${IMPORTS}
export default function instantiate() {
export function then(resolve) {
if (typeof WebAssembly === 'undefined') {
throw new Error('WebAssembly is not supported');
}
Expand All @@ -54,9 +54,11 @@ const buildLoader = ({URL, IMPORT_OBJECT, IMPORTS}) => (`
const req = window.fetch("${URL}");
return WebAssembly
WebAssembly
.instantiateStreaming(req, ${IMPORT_OBJECT})
.then(res => res.instance.exports);
.then(res => res.instance.exports)
.then(resolve)
.catch(resolve);
}
`);

Expand Down Expand Up @@ -114,5 +116,9 @@ export default function (options = {}) {

transform,
ongenerate,

options(opts) {
opts.experimentalDynamicImport = true;
}
};
}

0 comments on commit b207fb8

Please sign in to comment.