Skip to content

Commit

Permalink
intiial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
konsumer committed Jul 8, 2024
0 parents commit d3f99eb
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
build
22 changes: 22 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.18)

project(wasm_shared
DESCRIPTION "null0 engine experiment: shared memory"
HOMEPAGE_URL "https://github.com/notnullgames/wasm-shared"
VERSION 0.0.1
LANGUAGES C
)

if(NOT EMSCRIPTEN)
message(FATAL_ERROR "You must compile with emscripten.")
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)

# 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)

20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
This is an experiment with sharing memory between 2 wasms, to work out ideas for null0.

This should allow passing pointers back & forth between host & cart.

## build

```
emcmake cmake -B build -GNinja
cmake --build build
```

## host

This is a web-based host, compiled with emscripten. It has it's memory exported.


## cart

This is user-code, and must import it's memory (which comes from host.)

6 changes: 6 additions & 0 deletions cart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// this will be imported from host
void logtest(char* message);

void test() {
logtest("I am inside the cart!");
}
1 change: 1 addition & 0 deletions cart.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_test
9 changes: 9 additions & 0 deletions host.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "stdio.h"

void logtest(char* message) {
printf("%s\n", message);
}

int main(int argc, char* argv[]) {
return 0;
}
2 changes: 2 additions & 0 deletions host.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_logtest
_main
28 changes: 28 additions & 0 deletions test.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<head>
<title>Shared Mem Wasm Test</title>
</head>

<body>
Check console.
</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
}
})).instance.exports

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

cart.test()

</script>

0 comments on commit d3f99eb

Please sign in to comment.