From a04ad83de7e0b26e6109af64ebd3f8e3ee7fb8e4 Mon Sep 17 00:00:00 2001 From: mbasso Date: Fri, 14 Sep 2018 20:42:49 +0200 Subject: [PATCH 1/2] support compileStreaming --- src/worker.js | 19 +++++++---- test/worker.spec.js | 80 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 90 insertions(+), 9 deletions(-) diff --git a/src/worker.js b/src/worker.js index 9b3142a..7505bd1 100644 --- a/src/worker.js +++ b/src/worker.js @@ -16,12 +16,19 @@ export default function worker(e) { if (action === ACTIONS.COMPILE_MODULE) { Promise.resolve() - .then(() => ( - typeof payload === 'string' - ? fetch(payload).then(response => response.arrayBuffer()) - : payload - )) - .then(buffer => WebAssembly.compile(buffer)) + .then(() => { + let res; + if (typeof payload === 'string') { + res = fetch(payload); + if (WebAssembly.compileStreaming !== undefined) { + return WebAssembly.compileStreaming(res); + } + res = res.then(response => response.arrayBuffer()); + } else { + res = Promise.resolve(payload); + } + return res.then(buffer => WebAssembly.compile(buffer)); + }) .then(module => WebAssembly.instantiate( module, diff --git a/test/worker.spec.js b/test/worker.spec.js index 2b0b972..459203d 100644 --- a/test/worker.spec.js +++ b/test/worker.spec.js @@ -4,18 +4,18 @@ import ACTIONZ from '../src/actions'; import bytes from './bytes'; describe('worker', () => { - beforeAll(() => { + beforeEach(() => { fetchMock.mock(/bytes.wasm/, new Response(bytes)); }); - afterAll(() => { + afterEach(() => { fetchMock.restore(); }); // 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', (done) => { + it('should instantiate a module from a url using compileStreaming', (done) => { // initialize scope, this is automatically injected by ../src/index.js /* eslint-disable */ @@ -33,10 +33,84 @@ 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)) + ); + + + // 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(res).toBeDefined(); + expect(res.id).toEqual(id); + expect(res.action).toEqual(action); + expect(res.result).toEqual(0); + expect(res.payload).toBeDefined(); + expect(res.payload.exports).toBeDefined(); + expect(res.payload.exports.length).toEqual(2); + expect(res.payload.exports.indexOf('add')).not.toEqual(); + expect(res.payload.exports.indexOf('div')).not.toEqual(); + done(); + }, + }; + + // eval function to string, as we do in the main file + const func = eval('(' + worker.toString() + ')'); + /* eslint-enable */ + + func({ + data: { + id, + action, + payload: 'bytes.wasm', + }, + }); + }); + + it('should instantiate a module from a url using compile as fallback', (done) => { + // initialize scope, this is automatically injected by ../src/index.js + + /* eslint-disable */ + const ACTIONS = ACTIONZ; + let moduleInstance = null; + const getImportObject = undefined; + const importObject = { + memoryBase: 0, + tableBase: 0, + memory: new WebAssembly.Memory({ initial: 256 }), + table: new WebAssembly.Table({ initial: 0, element: 'anyfunc' }), + }; + + // helper variables + 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 = undefined; + // assert on post message // eslint-disable-next-line const self = { postMessage: (res) => { + expect(WebAssembly.compile).toHaveBeenCalled(); + WebAssembly.compileStreaming = _compileStreaming; + WebAssembly.compile = _compile; expect(res).toBeDefined(); expect(res.id).toEqual(id); expect(res.action).toEqual(action); From b0f963f47cfacd5714d47018abc7cc73899f9596 Mon Sep 17 00:00:00 2001 From: mbasso Date: Fri, 14 Sep 2018 20:47:21 +0200 Subject: [PATCH 2/2] v0.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 823b24f..2ca0b84 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wasm-worker", - "version": "0.1.0", + "version": "0.2.0", "description": "Move a WebAssembly module into its own thread", "main": "lib/index.js", "jsnext:main": "es/index.js",