Skip to content

Commit

Permalink
Update emscripten to latest version, update wasm postprocessing
Browse files Browse the repository at this point in the history
  • Loading branch information
mainnet-pat authored Sep 16, 2024
1 parent 061ca18 commit 9d4efdd
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 16 deletions.
8 changes: 5 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ project(monero-ts-wasm)
# build with exception whitelist from file
file(STRINGS wasm_exception_whitelist.txt WASM_EXCEPTION_WHITELIST)
string(REPLACE ";" "," WASM_EXCEPTION_WHITELIST "${WASM_EXCEPTION_WHITELIST}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Oz -s EXCEPTION_CATCHING_ALLOWED='[${WASM_EXCEPTION_WHITELIST}]'")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Oz -s EXCEPTION_CATCHING_ALLOWED='[${WASM_EXCEPTION_WHITELIST}]' -D_REENTRANT")
add_definitions(-DAUTO_INITIALIZE_EASYLOGGINGPP -DNO_AES)

##############
Expand Down Expand Up @@ -277,7 +277,7 @@ EMCC_LINKER_FLAGS_BASE
# unsure if the -I...boost..include is necessary here due to include above
# TODO? does EXPORT_NAME need to be the same for both targets? (or should it be set per-target with …_WASM, …_ASMJS?)

"-Wall -Werror -Wno-js-compiler -Wl,--allow-undefined -std=c++14 -Oz \
"-Wall -Werror -Wno-js-compiler -Wl,--allow-undefined -std=c++14 -Oz -D_REENTRANT \
--bind \
-s MODULARIZE=1 \
-s 'EXPORT_NAME=\"monero_ts\"' \
Expand All @@ -286,14 +286,16 @@ EMCC_LINKER_FLAGS_BASE
-s EXIT_RUNTIME=0 \
-s PRECISE_F32=1 \
-s EXCEPTION_DEBUG=0 \
-s DEMANGLE_SUPPORT=0 \
-s NO_DYNAMIC_EXECUTION=1 \
-s NODEJS_CATCH_EXIT=0 \
-s RESERVED_FUNCTION_POINTERS=5 \
-s EXPORTED_RUNTIME_METHODS='[\"UTF8ToString\",\"stringToUTF8\",\"lengthBytesUTF8\",\"intArrayToString\",\"getTempRet0\",\"addFunction\"]' \
-s SINGLE_FILE=1 \
-s ALLOW_MEMORY_GROWTH=1 \
-s WASM_BIGINT=1 \
-s EXPORTED_FUNCTIONS='[_free, _malloc]' \
-s STACK_SIZE=5MB \
-s DEFAULT_PTHREAD_STACK_SIZE=2MB \
"
# • Disabling exception catching does not introduce silent failures
# • Probably don't need PRECISE_F32 but wouldn't want to not use it
Expand Down
13 changes: 4 additions & 9 deletions bin/postprocess_wasm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Buffer } from "buffer";
const fileNames = ["./build/monero.js"];

const postprocess = async (fileName) => {

// read input file
const source = await fs.promises.readFile(fileName, "utf8");

Expand All @@ -15,16 +14,16 @@ const postprocess = async (fileName) => {
}

// find wasmBinaryFile base64 string
const match = source.match(/wasmBinaryFile=\"data:application\/octet-stream;base64,(.+?(?=\"))/gm);
const match = source.match(/f=\"data:application\/octet-stream;base64,(.+?(?=\"))/gm);
if (!match?.length) {
console.log(`Skipping ${fileName}. wasmBinaryFile not base64 encoded`);
return;
}
const b64 = match[0].split('wasmBinaryFile="data:application/octet-stream;base64,')[1];
const b64 = match[0].split('f="data:application/octet-stream;base64,')[1];
const buf = Buffer.from(b64, 'base64');

// compress wasmBinaryFile
const compression = new CompressionStream("deflate");
const compression = new CompressionStream("gzip");
const compressedStream = new ReadableStream({
start(controller) {
controller.enqueue(new Uint8Array(buf));
Expand All @@ -41,11 +40,7 @@ const postprocess = async (fileName) => {
.replace(b64, cb64)
.replace(
"return intArrayFromBase64(filename.slice(dataURIPrefix.length))",
'const data=filename.slice(dataURIPrefix.length);if(data.startsWith("AGFzbQ"))return intArrayFromBase64(data);else{const bin=intArrayFromBase64(data);const ds = new DecompressionStream("deflate");const decompressedStream = new ReadableStream({start(controller){controller.enqueue(bin);controller.close();}}).pipeThrough(ds);return (new Response(decompressedStream)).arrayBuffer();}'
)
.replace(
"!wasmBinary&&(ENVIRONMENT_IS_WEB||ENVIRONMENT_IS_WORKER)",
"!wasmBinary&&false"
'const data=filename.slice(dataURIPrefix.length);if(data.startsWith("AGFzbQ"))return intArrayFromBase64(data);else{const bin=intArrayFromBase64(data);const ds = new DecompressionStream("gzip");const decompressedStream = new ReadableStream({start(controller){controller.enqueue(bin);controller.close();}}).pipeThrough(ds);return (new Response(decompressedStream)).arrayBuffer();}'
);

// write output file
Expand Down
2 changes: 1 addition & 1 deletion configs/emscripten.jam
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local EMSCRIPTEN = [ os.environ EMSCRIPTEN ] ;

using clang : emscripten
:
emcc -v -s USE_ZLIB=1 -s USE_PTHREADS=0
emcc -v -s USE_ZLIB=1 -s USE_PTHREADS=0 -D_REENTRANT
:
<root>$(EMSCRIPTEN)
<archiver>$(EMSCRIPTEN)/emar
Expand Down
3 changes: 0 additions & 3 deletions src/main/ts/common/LibraryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,7 @@ export default class LibraryUtils {
if (LibraryUtils.WASM_MODULE && LibraryUtils.FULL_LOADED) return LibraryUtils.WASM_MODULE;

// load module
const fetch_ = globalThis.fetch;
globalThis.fetch = undefined; // prevent fetch in worker
let module = await require("../../../../dist/monero")();
globalThis.fetch = fetch_;
LibraryUtils.WASM_MODULE = module;
delete LibraryUtils.WASM_MODULE.then;
LibraryUtils.FULL_LOADED = true;
Expand Down

0 comments on commit 9d4efdd

Please sign in to comment.