Skip to content
This repository has been archived by the owner on Oct 17, 2021. It is now read-only.

Commit

Permalink
Promisify, better tests 🚀
Browse files Browse the repository at this point in the history
CHANGES:

* Changed to always return a promise.
* Addressed #4
* Added specs testing a real module

TODO:

* Change docs
* Publish to npm
  • Loading branch information
ballercat committed Jul 30, 2017
1 parent 51e17cc commit af70e9f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,3 +1,3 @@
language: node_js
node_js:
- "7.8"
- "8.0"
Binary file removed counter.wasm
Binary file not shown.
43 changes: 0 additions & 43 deletions counter.wast

This file was deleted.

Binary file added factorial.wasm
Binary file not shown.
16 changes: 16 additions & 0 deletions jasmine.js
@@ -1,6 +1,22 @@
var Jasmine = require('jasmine');
var jasmine = new Jasmine();

const nodeVersionMajor = () => {
if (process && process.versions && process.versions.node) {
const full = process.versions.node;
const major = full.substr(0, full.indexOf('.'));
return Number.parseInt(major);
}

return Number.MAX_SAFE_INTEGER;
}

const major = nodeVersionMajor();
if (major < 8) {
throw new Error(
`Native WebAssembly Node API used in specs. Major Node version required >= 8, current ${major}`);
}

jasmine.loadConfig({
spec_dir: 'spec',
spec_files: [
Expand Down
10 changes: 2 additions & 8 deletions output.js
Expand Up @@ -2,25 +2,19 @@

const {
Module,
Instance,
instantiate,
Memory,
Table
} = WebAssembly;

let wa;
const make = source => {
// buffer should already be set
return wa = new Module(buffer);
};

const WebAssemblyModule = function(deps = {
'global': {},
'env': {
'memory': new Memory({initial: 10, limit: 100}),
'table': new Table({initial: 0, element: 'anyfunc'})
}
}) {
return new Instance(wa || make(), deps);
return instantiate(buffer, deps);
}

module.exports = WebAssemblyModule;
26 changes: 0 additions & 26 deletions spec/out.js

This file was deleted.

39 changes: 35 additions & 4 deletions spec/wasm-loader-spec.js
@@ -1,8 +1,18 @@
const loader = require('./../index');
const Module = require('module');
const { readFileSync } = require('fs');
const out = readFileSync(__dirname + '/out.js', 'utf-8');
const wasm = readFileSync(__dirname + '/../counter.wasm');
const wasm = readFileSync(__dirname + '/../factorial.wasm');

const __NOTAREALMODULE__ = '__NOTAREALMODULE__';
const FACTORIALS = [1, 1, 2, 6, 24, 120];

/**
* NOTE: Some gnarly node module gymnastics are down here.
* eval() type nonsense wouldn't really work here as we
* are compiling ouput to be a valid node Module :)
*
* All-in-all it's a nice way to test if the loader is working.
*/
describe('wasm-loader', () => {
let loaderContext;

Expand All @@ -11,15 +21,36 @@ describe('wasm-loader', () => {
resourcePath: './counter.wasm',
options: { context: __dirname + '/../' }
};
// Mess with the node module cache to keep tests pure
Module._cache[__NOTAREALMODULE__] = null;
});

it('should combine .wasm files to javascript', (done) => {
it('should create a promise', (done) => {
loaderContext.callback = (unused, output) => {
expect(output).toBe(out);
const module = new Module(__NOTAREALMODULE__, null);
module._compile(output, __NOTAREALMODULE__);
const shouldBePromise = module.exports();
expect(typeof shouldBePromise.then).toBe('function');

done();
}

loader.call(loaderContext, wasm);
});

it('should create a promise', (done) => {
loaderContext.callback = (unused, output) => {
const module = new Module(__NOTAREALMODULE__);
module._compile(output, __NOTAREALMODULE__);
module.exports().then(({ instance: { exports: { _Z4facti: factorial } } }) => {
for(let i = 0; i < FACTORIALS.length; i++) {
expect(factorial(i)).toBe(FACTORIALS[i]);
}
done();
});
}

loader.call(loaderContext, wasm);
});
});

0 comments on commit af70e9f

Please sign in to comment.