Skip to content

Commit 02e6ba2

Browse files
dcodeIOkripken
authored andcommitted
Compile Binaryen to WebAssembly (#2503)
This PR enables compiling Binaryen to WebAssembly when building binaryen.js. Since WebAssembly is best compiled and instantiated asynchronously in browsers, it also adds a new mechanism to tell if respectively when the module is ready by means of one of the following: // Using a promise const binaryen = require("binaryen"); binaryen.ready.then(() => { ... use normally ... }); // Using await const binaryen = require("binaryen"); (async () => { await binaryen.ready; ... use normally ... })(); // Where top-level await is available const binaryen = await require("binaryen").ready; ... use normally ... One can also tell if Binaryen is already ready (for example when assuming it in follow-up code) by: if (/* we already know that */ binaryen.isReady) { ... use normally ... } else { throw Error("Binaryen is supposed to be ready here but isn't"); } The JS test cases have been updated accordingly by wrapping everything in a test function and invoking it once ready. Documentation will have to be updated as well to cover this of course. New file size is about 2.5mb, even though the Wasm becomes inlined into the JS file which makes distribution across different environments a lot easier. Also makes building binaryen (to either js or wasm) emit binaryen.js, and not binaryen_js.js etc. Supersedes and thus fixes #1381 With .ready it also fixes #2452
1 parent 81c16df commit 02e6ba2

25 files changed

Lines changed: 1085 additions & 913 deletions

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
*.sh text eol=lf
2+
test/binaryen.js/*.txt text eol=lf

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ Current Trunk
3232
- Add the ability to create multivalue Types in the C and JS APIs.
3333
- Remove named function types. They are replaced by `params` and `results` types
3434
local to each function.
35+
- Binaryen.js can now be compiled to Wasm using the `binaryen_wasm` target.
36+
Unlike the JS variant, the Wasm variant requires asynchronously awaiting the
37+
Wasm blob's instantiation and initialization before being usable, using the
38+
`binaryen.ready` promise, e.g. `binaryen.ready.then(() => ...)`.
3539

3640
v88
3741
---

CMakeLists.txt

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -365,22 +365,40 @@ install(TARGETS wasm-reduce DESTINATION ${CMAKE_INSTALL_BINDIR})
365365
# binaryen.js
366366
#
367367
# Note that we can't emit binaryen.js directly, as there is libbinaryen already
368-
# declared earlier, so we create binaryen_js.js, which must then be copied.
368+
# declared earlier, so we create binaryen_wasm/js.js, which must then be copied.
369+
# Note that SHELL: is needed as otherwise cmake will coalesce -s link flags
370+
# in an incorrect way for emscripten.
369371
if(EMSCRIPTEN)
370-
set(binaryen_js_SOURCES
372+
set(binaryen_emscripten_SOURCES
371373
src/binaryen-c.cpp
372374
)
375+
376+
# binaryen.js WebAssembly variant
377+
add_executable(binaryen_wasm
378+
${binaryen_emscripten_SOURCES})
379+
target_link_libraries(binaryen_wasm wasm asmjs emscripten-optimizer passes ir cfg support wasm)
380+
target_link_libraries(binaryen_wasm "-s MODULARIZE_INSTANCE=1")
381+
target_link_libraries(binaryen_wasm "-s NO_FILESYSTEM=0")
382+
target_link_libraries(binaryen_wasm "-s NODERAWFS=0")
383+
target_link_libraries(binaryen_wasm "-s EXPORT_NAME=Binaryen")
384+
target_link_libraries(binaryen_wasm "--post-js ${CMAKE_CURRENT_SOURCE_DIR}/src/js/binaryen.js-post.js")
385+
target_link_libraries(binaryen_wasm optimized "--closure 1")
386+
target_link_libraries(binaryen_wasm optimized "--llvm-lto 1")
387+
target_link_libraries(binaryen_wasm debug "--profiling")
388+
set_property(TARGET binaryen_wasm PROPERTY CXX_STANDARD 14)
389+
set_property(TARGET binaryen_wasm PROPERTY CXX_STANDARD_REQUIRED ON)
390+
install(TARGETS binaryen_wasm DESTINATION ${CMAKE_INSTALL_BINDIR})
391+
392+
# binaryen.js JavaScript variant
373393
add_executable(binaryen_js
374-
${binaryen_js_SOURCES})
394+
${binaryen_emscripten_SOURCES})
375395
target_link_libraries(binaryen_js wasm asmjs emscripten-optimizer passes ir cfg support wasm)
376-
# note that SHELL: is needed as otherwise cmake will coalesce -s link flags
377-
# in an incorrect way for emscripten
378396
target_link_libraries(binaryen_js "-s WASM=0")
397+
target_link_libraries(binaryen_js "-s WASM_ASYNC_COMPILATION=0")
379398
if(${CMAKE_CXX_COMPILER_VERSION} STREQUAL "6.0.1")
380399
# only valid with fastcomp and WASM=0
381-
target_link_libraries(binaryen_js "-s ELIMINATE_DUPLICATE_FUNCTIONS=1")
400+
target_link_libraries(binaryen_js "-s ELIMINATE_DUPLICATE_FUNCTIONS=1")
382401
endif()
383-
target_link_libraries(binaryen_js "-s WASM_ASYNC_COMPILATION=0")
384402
target_link_libraries(binaryen_js "-s MODULARIZE_INSTANCE=1")
385403
target_link_libraries(binaryen_js "-s NO_FILESYSTEM=0")
386404
target_link_libraries(binaryen_js "-s NODERAWFS=0")
@@ -393,6 +411,9 @@ if(EMSCRIPTEN)
393411
set_property(TARGET binaryen_js PROPERTY CXX_STANDARD 14)
394412
set_property(TARGET binaryen_js PROPERTY CXX_STANDARD_REQUIRED ON)
395413
install(TARGETS binaryen_js DESTINATION ${CMAKE_INSTALL_BINDIR})
414+
415+
# always emit as 'binaryen.js'
416+
set_target_properties(binaryen_wasm binaryen_js PROPERTIES OUTPUT_NAME "binaryen")
396417
endif()
397418

398419
# Testing

scripts/test/shared.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,7 @@ def is_exe(fpath):
184184
WASM_METADCE = [os.path.join(options.binaryen_bin, 'wasm-metadce')]
185185
WASM_EMSCRIPTEN_FINALIZE = [os.path.join(options.binaryen_bin,
186186
'wasm-emscripten-finalize')]
187-
# Due to cmake limitations, we emit binaryen_js.js (see CMakeLists.txt
188-
# for why).
189-
BINARYEN_JS = os.path.join(options.binaryen_bin, 'binaryen_js.js')
187+
BINARYEN_JS = os.path.join(options.binaryen_bin, 'binaryen.js')
190188

191189

192190
def wrap_with_valgrind(cmd):

0 commit comments

Comments
 (0)