Skip to content

Commit

Permalink
automate host-binding
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumer committed Jul 8, 2024
1 parent d3f99eb commit eff41b4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ endif()
set(CMAKE_EXECUTABLE_SUFFIX ".mjs")

add_executable(${PROJECT_NAME}-host host.c)
target_link_options(${PROJECT_NAME}-host PRIVATE -sINITIAL_MEMORY=512MB -sEXPORTED_FUNCTIONS=@${CMAKE_CURRENT_SOURCE_DIR}/host.txt -sEXPORTED_RUNTIME_METHODS=wasmMemory)
target_link_options(${PROJECT_NAME}-host PRIVATE -sINITIAL_MEMORY=128MB -sEXPORTED_FUNCTIONS=@${CMAKE_CURRENT_SOURCE_DIR}/host.txt -sEXPORTED_RUNTIME_METHODS=wasmMemory)

# normally I would do this without emscripten, but this makes it a bit easier
add_executable(${PROJECT_NAME}-cart cart.c)
target_link_options(${PROJECT_NAME}-cart PRIVATE -sWARN_ON_UNDEFINED_SYMBOLS=0 -sTOTAL_MEMORY=512MB --no-entry -sMODULARIZE -sEXPORTED_FUNCTIONS=@${CMAKE_CURRENT_SOURCE_DIR}/cart.txt -Wl,--import-memory -sSTANDALONE_WASM)
target_link_options(${PROJECT_NAME}-cart PRIVATE -sWARN_ON_UNDEFINED_SYMBOLS=0 -sTOTAL_MEMORY=128MB --no-entry -sMODULARIZE -sEXPORTED_FUNCTIONS=@${CMAKE_CURRENT_SOURCE_DIR}/cart.txt -Wl,--import-memory -sSTANDALONE_WASM)

30 changes: 19 additions & 11 deletions test.htm
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,30 @@
</body>

<script type="module">
// here I use the js for teh host, and the bare wasm for cart

// load emscripten host
import loadHost from './build/wasm_shared-host.mjs'
const host = await loadHost()

// cart is a "plain wasm", so set it up with regular browser stuff
const cart = (await WebAssembly.instantiateStreaming(fetch("./build/wasm_shared-cart.wasm"), {
env: {
memory: host.wasmMemory,
logtest: host._logtest

// this will take an emscripten host, and setup imports for plain-wasm cart
function getImports(host) {
const out = {
env: {
memory: host.wasmMemory
}
}
for (let n of Object.keys(host)) {
const m = /^_(.+)/g.exec(n)
if (m){
out.env[m[1]] = host[n]
}
}
})).instance.exports
return out
}

// call cart's test, which will call host's logtest

const importObj = getImports(await loadHost())
const cart = (await WebAssembly.instantiateStreaming(fetch("./build/wasm_shared-cart.wasm"), importObj)).instance.exports

// call cart's test, which will call host's logtest
cart.test()

</script>

0 comments on commit eff41b4

Please sign in to comment.