Only rely on wasm.js (no asm.js duplication) (#25)

* Only rely on wasm.js (no asm.js duplication)

* Add browser entry

* Make asm.js tests pass

* Remove (now-unsued) mock dep
This commit is contained in:
Jaco Greeff
2019-07-19 11:06:31 +02:00
committed by GitHub
parent 309ba91122
commit 8c6d2aea12
8 changed files with 55 additions and 68 deletions
+1 -54
View File
@@ -16,7 +16,6 @@ mv pkg build
# shortcuts for files
echo "*** Adjusting output"
BGJ=build/wasm_bg.js
SRC_ASM=build/asm.js
SRC_WASM=build/wasm.js
DEF=build/wasm.d.ts
WSM=build/wasm_bg.wasm
@@ -34,9 +33,6 @@ echo "*** Optimising WASM output"
echo "*** Building asm.js version"
../../binaryen/bin/wasm2js --no-validation --output $TMP $OPT
# cleanup asm
sed -i -e 's/\.\/wasm/\.\/asm/g' $TMP
# get babel to take care of making it sane
NODE_OPTIONS=--max_old_space_size=8192 yarn babel $TMP --presets @babel/preset-env --out-file=$ASM
rm -rf $TMP
@@ -66,13 +62,11 @@ sed -i -e 's/const TextDecoder = require('\''util'\'')\.TextDecoder;/const { u8a
sed -i -e 's/let cachedTextDecoder = new /\/\/ let cachedTextDecoder = new /g' $SRC_WASM
sed -i -e 's/cachedTextDecoder\.decode/u8aToString/g' $SRC_WASM
cp -f $SRC_WASM $SRC_ASM
# construct our promise and add ready helpers (WASM)
echo "
module.exports.abort = function () { throw new Error('abort'); };
const createPromise = require('./wasm_bg');
const createPromise = require('./wasm_promise');
const wasmPromise = createPromise().catch(() => null);
module.exports.isReady = function () { return !!wasm; }
@@ -81,55 +75,8 @@ module.exports.waitReady = function () { return wasmPromise.then(() => !!wasm);
wasmPromise.then((_wasm) => { wasm = _wasm });
" >> $SRC_WASM
# construct our promise and add ready helpers (ASM)
echo "
module.exports.abort = function () { throw new Error('abort'); };
const asmjs = require('./wasm_asm');
const wasmPromise = Promise.resolve(asmjs);
module.exports.isReady = function () { return !!wasm; }
module.exports.waitReady = function () { return wasmPromise.then(() => !!wasm); }
wasmPromise.then((_wasm) => { wasm = _wasm });
" >> $SRC_ASM
# add extra methods to type definitions
echo "
export function isReady(): boolean;
export function waitReady(): Promise<boolean>;
" >> $DEF
# create the init promise handler
echo "
const pkg = require('./package.json');
const bytes = require('./wasm_wasm');
const js = require('./wasm');
module.exports = async function createExportPromise () {
const imports = {
'./wasm': js
};
if (!WebAssembly) {
if (process.env.CRYPTO_ASM) {
return require('./wasm_asm');
}
console.error(\`ERROR: Unable to initialize \${pkg.name}, WebAssembly is not available in this environment. If you requires an asm.js fallback, specify it with the 'CRYPTO_ASM' environment flag.\`);
return null;
}
try {
const { instance } = await WebAssembly.instantiate(bytes, imports);
return instance.exports;
} catch (error) {
console.error(\`ERROR: Unable to initialize \${pkg.name}:: \${error.message}\`);
// TODO: Return asm version here as a fallback
return null;
}
}
" > $BGJ
+1 -1
View File
@@ -11,7 +11,7 @@ echo "*** Testing WASM"
node ./test/wasm.js
echo "*** Testing ASM.js"
CRYPTO_ASM=1 node ./test/asm.js
node ./test/asm.js
echo "*** Testing via Jest"
yarn jest ./test/jest.spec.js