Skip to content

Commit

Permalink
Merge pull request #2 from mbasso/compileStreaming
Browse files Browse the repository at this point in the history
Support compileStreaming
  • Loading branch information
mbasso authored Sep 14, 2018
2 parents 77a2ae0 + b0f963f commit ba05e65
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 10 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.1.0",
"version": "0.2.0",
"description": "Move a WebAssembly module into its own thread",
"main": "lib/index.js",
"jsnext:main": "es/index.js",
Expand Down
19 changes: 13 additions & 6 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
80 changes: 77 additions & 3 deletions test/worker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
Expand Down

0 comments on commit ba05e65

Please sign in to comment.