Add asm.js generation with CRYPTO_ASM env var (#23)
* Add asm.js generation, import via index-asm.js * Less sed-y, more babel-y * Throw error when some failed * CRYPTO_ASM env flag * CHANGELOG
This commit is contained in:
+49
-13
@@ -16,11 +16,13 @@ mv pkg build
|
||||
# shortcuts for files
|
||||
echo "*** Adjusting output"
|
||||
BGJ=build/wasm_bg.js
|
||||
SRC=build/wasm.js
|
||||
SRC_ASM=build/asm.js
|
||||
SRC_WASM=build/wasm.js
|
||||
DEF=build/wasm.d.ts
|
||||
WSM=build/wasm_bg.wasm
|
||||
OPT=build/wasm_opt.wasm
|
||||
ASM=build/wasm_asm.js
|
||||
TMP=build/wasm_tmp.js
|
||||
|
||||
# optimise
|
||||
echo "*** Optimising WASM output"
|
||||
@@ -29,7 +31,15 @@ echo "*** Optimising WASM output"
|
||||
|
||||
# build asmjs version from the input (optimised) WASM
|
||||
# echo "*** Building asm.js version"
|
||||
# ../../binaryen/bin/wasm2js --no-validation --output $ASM $OPT
|
||||
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
|
||||
|
||||
# convert wasm to base64 structure
|
||||
echo "*** Packing WASM into base64"
|
||||
@@ -39,16 +49,26 @@ node ../../scripts/pack-wasm-base64.js
|
||||
echo "*** Copying package sources"
|
||||
cp src/js/* build/
|
||||
|
||||
# cleanup asm
|
||||
# sed -i -e 's/import {/\/\/ import {/g' $ASM
|
||||
# sed -i -e 's/function asmFunc/var schnorrkel = require('\''\.\/wasm'\''); function asmFunc/g' $ASM
|
||||
# sed -i -e 's/export const /module\.exports\./g' $ASM
|
||||
# sed -i -e 's/{abort.*},memasmFunc/schnorrkel, memasmFunc/g' $ASM
|
||||
|
||||
# we are swapping to a async interface for webpack support (wasm limits)
|
||||
sed -i -e 's/wasm = require/\/\/ wasm = require/g' $SRC
|
||||
sed -i -e 's/wasm = require/\/\/ wasm = require/g' $SRC_WASM
|
||||
|
||||
# construct our promise and add ready helpers
|
||||
# We don't want inline requires
|
||||
sed -i -e 's/var wasm;/const crypto = require('\''crypto'\''); let wasm; const requires = { crypto };/g' $SRC_WASM
|
||||
sed -i -e 's/return addHeapObject(require(varg0));/return addHeapObject(requires[varg0]);/g' $SRC_WASM
|
||||
|
||||
# this creates issues in both the browser and RN (@polkadot/util has a polyfill)
|
||||
sed -i -e 's/const TextEncoder = require('\''util'\'')\.TextEncoder;/const { stringToU8a } = require('\''@polkadot\/util'\'');/g' $SRC_WASM
|
||||
sed -i -e 's/let cachedTextEncoder = new /\/\/ let cachedTextEncoder = new /g' $SRC_WASM
|
||||
sed -i -e 's/cachedTextEncoder\.encode/stringToU8a/g' $SRC_WASM
|
||||
|
||||
# this creates issues in both the browser and RN (@polkadot/util has a polyfill)
|
||||
sed -i -e 's/const TextDecoder = require('\''util'\'')\.TextDecoder;/const { u8aToString } = require('\''@polkadot\/util'\'');/g' $SRC_WASM
|
||||
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'); };
|
||||
|
||||
@@ -59,7 +79,20 @@ module.exports.isReady = function () { return !!wasm; }
|
||||
module.exports.waitReady = function () { return wasmPromise.then(() => !!wasm); }
|
||||
|
||||
wasmPromise.then((_wasm) => { wasm = _wasm });
|
||||
" >> $SRC
|
||||
" >> $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 "
|
||||
@@ -79,9 +112,12 @@ module.exports = async function createExportPromise () {
|
||||
};
|
||||
|
||||
if (!WebAssembly) {
|
||||
console.error(\`ERROR: Unable to initialize \${pkg.name}, WebAssembly is not available in this environment\`);
|
||||
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.\`);
|
||||
|
||||
// TODO: Return asm version when not detected
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,6 @@ for PKG in "${PACKAGES[@]}"; do
|
||||
cd $PKG
|
||||
|
||||
../../scripts/build-package.sh
|
||||
./adjust.sh
|
||||
../../scripts/test-package.sh
|
||||
|
||||
rm -rf build/*-e build/package.json build/README.md
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
set -e
|
||||
|
||||
# wasm2js for wasm -> asm.js
|
||||
BINARYEN=( "wasm-opt" )
|
||||
BINARYEN=( "wasm-opt" "wasm2js" )
|
||||
|
||||
# install wasm-pack as required
|
||||
if ! [ -x "$(command -v wasm-pack)" ]; then
|
||||
|
||||
@@ -10,5 +10,8 @@ RUST_BACKTRACE=full rustup run nightly cargo test --release -- --nocapture
|
||||
echo "*** Testing WASM"
|
||||
node ./test/wasm.js
|
||||
|
||||
echo "*** Testing ASM.js"
|
||||
CRYPTO_ASM=1 node ./test/asm.js
|
||||
|
||||
echo "*** Testing via Jest"
|
||||
yarn jest ./test/jest.spec.js
|
||||
|
||||
Reference in New Issue
Block a user