-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d3f99eb
Showing
8 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
_test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
_logtest | ||
_main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |