Skip to content

Commit

Permalink
instantiateStreaming
Browse files Browse the repository at this point in the history
  • Loading branch information
mbasso committed Sep 17, 2018
1 parent e81cf2d commit 51176bf
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 52 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wasm-worker",
"version": "0.2.0",
"version": "0.3.0",
"description": "Move a WebAssembly module into its own thread",
"main": "lib/index.js",
"jsnext:main": "es/index.js",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default function wasmWorker(source, options = {}) {
const { getImportObject, ...otherOptions } = options;

const worker = new Worker(
`data:,ACTIONS=${JSON.stringify(ACTIONS)}getImportObject=${getImportObject};` +
`data:,ACTIONS=${JSON.stringify(ACTIONS)};getImportObject=${getImportObject};` +
`moduleInstance=null;onmessage=${workerOnMessage}`,
otherOptions,
);
Expand Down
53 changes: 27 additions & 26 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,41 @@ export default function worker(e) {
Promise.resolve()
.then(() => {
let res;
const importObject = getImportObject !== undefined
? getImportObject()
: {
memoryBase: 0,
tableBase: 0,
memory: new WebAssembly.Memory({ initial: 256 }),
table: new WebAssembly.Table({ initial: 0, element: 'anyfunc' }),
};

if (typeof payload === 'string') {
res = fetch(payload);
if (WebAssembly.compileStreaming !== undefined) {
return WebAssembly.compileStreaming(res);
if (WebAssembly.instantiateStreaming !== undefined) {
return WebAssembly.instantiateStreaming(res, importObject);
}
res = res.then(response => response.arrayBuffer());
} else {
res = Promise.resolve(payload);
}
return res.then(buffer => WebAssembly.compile(buffer));

return res
.then(buff => WebAssembly.compile(buff))
.then(module =>
WebAssembly.instantiate(module, importObject).then(instance => ({ module, instance })),
);
})
.then(({ module, instance }) => {
// eslint-disable-next-line
moduleInstance = instance;
onSuccess({
exports: WebAssembly.Module
.exports(module)
.filter(exp => exp.kind === 'function')
.map(exp => exp.name),
});
})
.then(module =>
WebAssembly.instantiate(
module,
getImportObject !== undefined
? getImportObject()
: {
memoryBase: 0,
tableBase: 0,
memory: new WebAssembly.Memory({ initial: 256 }),
table: new WebAssembly.Table({ initial: 0, element: 'anyfunc' }),
},
)
.then((inst) => {
// eslint-disable-next-line
moduleInstance = inst;
onSuccess({
exports: WebAssembly.Module
.exports(module)
.filter(exp => exp.kind === 'function')
.map(exp => exp.name),
});
}),
)
.catch(onError);
} else if (action === ACTIONS.CALL_FUNCTION_EXPORT) {
const { func, params } = payload;
Expand Down
53 changes: 29 additions & 24 deletions test/worker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import bytes from './bytes';

describe('worker', () => {
beforeEach(() => {
fetchMock.mock(/bytes.wasm/, new Response(bytes));
fetchMock.mock(/bytes.wasm/, new Response(bytes, {
status: 200,
statusText: 'OK',
headers: {
'Content-type': 'application/wasm',
},
}));
});

afterEach(() => {
Expand All @@ -15,7 +21,7 @@ describe('worker', () => {
// we need this test because we cannot mock fetch in integrations
// since we are using 2 diffent contexts.
// so, use only the worker function
it('should instantiate a module from a url using compileStreaming', (done) => {
it('should instantiate a module from a url using instantiateStreaming', (done) => {
// initialize scope, this is automatically injected by ../src/index.js

/* eslint-disable */
Expand All @@ -33,26 +39,25 @@ describe('worker', () => {
const id = 0;
const action = ACTIONZ.COMPILE_MODULE;

const _compile = WebAssembly.compile;
WebAssembly.compile = jasmine.createSpy('compile').and.callFake((...params) =>
_compile(...params)
);
const _compileStreaming = WebAssembly.compileStreaming;
WebAssembly.compileStreaming = jasmine.createSpy('compileStreaming').and.callFake(req =>
req
.then(res => res.arrayBuffer())
.then(res => _compile(res))
const _instantiate = WebAssembly.instantiate;
WebAssembly.instantiate = jasmine.createSpy('instantiate').and.callFake((...params) =>
_instantiate(...params)
);
const _instantiateStreaming = WebAssembly.instantiateStreaming;
WebAssembly.instantiateStreaming =
jasmine.createSpy('instantiateStreaming').and.callFake((...params) =>
_instantiateStreaming(...params)
);


// assert on post message
// eslint-disable-next-line
const self = {
postMessage: (res) => {
expect(WebAssembly.compileStreaming).toHaveBeenCalled();
expect(WebAssembly.compile).not.toHaveBeenCalled();
WebAssembly.compileStreaming = _compileStreaming;
WebAssembly.compile = _compile;
expect(WebAssembly.instantiateStreaming).toHaveBeenCalled();
expect(WebAssembly.instantiate).not.toHaveBeenCalled();
WebAssembly.instantiateStreaming = _instantiateStreaming;
WebAssembly.instantiate = _instantiate;
expect(res).toBeDefined();
expect(res.id).toEqual(id);
expect(res.action).toEqual(action);
Expand All @@ -79,7 +84,7 @@ describe('worker', () => {
});
});

it('should instantiate a module from a url using compile as fallback', (done) => {
it('should instantiate a module from a url using instantiate as fallback', (done) => {
// initialize scope, this is automatically injected by ../src/index.js

/* eslint-disable */
Expand All @@ -97,20 +102,20 @@ describe('worker', () => {
const id = 0;
const action = ACTIONZ.COMPILE_MODULE;

const _compile = WebAssembly.compile;
WebAssembly.compile = jasmine.createSpy('compile').and.callFake((...params) =>
_compile(...params)
const _instantiate = WebAssembly.instantiate;
WebAssembly.instantiate = jasmine.createSpy('instantiate').and.callFake((...params) =>
_instantiate(...params)
);
const _compileStreaming = WebAssembly.compileStreaming;
WebAssembly.compileStreaming = undefined;
const _instantiateStreaming = WebAssembly.instantiateStreaming;
WebAssembly.instantiateStreaming = undefined;

// assert on post message
// eslint-disable-next-line
const self = {
postMessage: (res) => {
expect(WebAssembly.compile).toHaveBeenCalled();
WebAssembly.compileStreaming = _compileStreaming;
WebAssembly.compile = _compile;
expect(WebAssembly.instantiate).toHaveBeenCalled();
WebAssembly.instantiateStreaming = _instantiateStreaming;
WebAssembly.instantiate = _instantiate;
expect(res).toBeDefined();
expect(res.id).toEqual(id);
expect(res.action).toEqual(action);
Expand Down

0 comments on commit 51176bf

Please sign in to comment.