@@ -96,6 +96,10 @@ class WasmMemoryInterface {
96
96
} ;
97
97
loadPtr ( addr ) { return this . loadU32 ( addr ) ; }
98
98
99
+ loadB32 ( addr ) {
100
+ return this . loadU32 ( addr ) != 0 ;
101
+ }
102
+
99
103
loadBytes ( ptr , len ) {
100
104
return new Uint8Array ( this . memory . buffer , ptr , Number ( len ) ) ;
101
105
}
@@ -104,6 +108,16 @@ class WasmMemoryInterface {
104
108
const bytes = this . loadBytes ( ptr , Number ( len ) ) ;
105
109
return new TextDecoder ( ) . decode ( bytes ) ;
106
110
}
111
+
112
+ loadCstring ( ptr ) {
113
+ const start = this . loadPtr ( ptr ) ;
114
+ if ( start == 0 ) {
115
+ return null ;
116
+ }
117
+ let len = 0 ;
118
+ for ( ; this . mem . getUint8 ( start + len ) != 0 ; len += 1 ) { }
119
+ return this . loadString ( start , len ) ;
120
+ }
107
121
108
122
storeU8 ( addr , value ) { this . mem . setUint8 ( addr , value ) ; }
109
123
storeI8 ( addr , value ) { this . mem . setInt8 ( addr , value ) ; }
@@ -1245,7 +1259,7 @@ class WebGLInterface {
1245
1259
} ;
1246
1260
1247
1261
1248
- function odinSetupDefaultImports ( wasmMemoryInterface , consoleElement ) {
1262
+ function odinSetupDefaultImports ( wasmMemoryInterface , consoleElement , memory ) {
1249
1263
const MAX_INFO_CONSOLE_LINES = 512 ;
1250
1264
let infoConsoleLines = new Array ( ) ;
1251
1265
let currentLine = { } ;
@@ -1366,8 +1380,15 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) {
1366
1380
let event_temp_data = { } ;
1367
1381
1368
1382
let webglContext = new WebGLInterface ( wasmMemoryInterface ) ;
1383
+
1384
+ const env = { } ;
1385
+
1386
+ if ( memory ) {
1387
+ env . memory = memory ;
1388
+ }
1389
+
1369
1390
return {
1370
- " env" : { } ,
1391
+ env,
1371
1392
"odin_env" : {
1372
1393
write : ( fd , ptr , len ) => {
1373
1394
const str = wasmMemoryInterface . loadString ( ptr , len ) ;
@@ -1720,13 +1741,16 @@ function odinSetupDefaultImports(wasmMemoryInterface, consoleElement) {
1720
1741
* @param {string } wasmPath - Path to the WASM module to run
1721
1742
* @param {?HTMLPreElement } consoleElement - Optional console/pre element to append output to, in addition to the console
1722
1743
* @param {any } extraForeignImports - Imports, in addition to the default runtime to provide the module
1744
+ * @param {?WasmMemoryInterface } wasmMemoryInterface - Optional memory to use instead of the defaults
1723
1745
* @param {?int } intSize - Size (in bytes) of the integer type, should be 4 on `js_wasm32` and 8 on `js_wasm64p32`
1724
1746
*/
1725
- async function runWasm ( wasmPath , consoleElement , extraForeignImports , intSize = 4 ) {
1726
- const wasmMemoryInterface = new WasmMemoryInterface ( ) ;
1747
+ async function runWasm ( wasmPath , consoleElement , extraForeignImports , wasmMemoryInterface , intSize = 4 ) {
1748
+ if ( ! wasmMemoryInterface ) {
1749
+ wasmMemoryInterface = new WasmMemoryInterface ( ) ;
1750
+ }
1727
1751
wasmMemoryInterface . setIntSize ( intSize ) ;
1728
1752
1729
- let imports = odinSetupDefaultImports ( wasmMemoryInterface , consoleElement ) ;
1753
+ let imports = odinSetupDefaultImports ( wasmMemoryInterface , consoleElement , wasmMemoryInterface . memory ) ;
1730
1754
let exports = { } ;
1731
1755
1732
1756
if ( extraForeignImports !== undefined ) {
@@ -1741,11 +1765,17 @@ async function runWasm(wasmPath, consoleElement, extraForeignImports, intSize =
1741
1765
const wasm = await WebAssembly . instantiate ( file , imports ) ;
1742
1766
exports = wasm . instance . exports ;
1743
1767
wasmMemoryInterface . setExports ( exports ) ;
1744
- wasmMemoryInterface . setMemory ( exports . memory ) ;
1768
+
1769
+ if ( exports . memory ) {
1770
+ if ( wasmMemoryInterface . memory ) {
1771
+ console . warn ( "WASM module exports memory, but `runWasm` was given an interface with existing memory too" ) ;
1772
+ }
1773
+ wasmMemoryInterface . setMemory ( exports . memory ) ;
1774
+ }
1745
1775
1746
1776
exports . _start ( ) ;
1747
1777
1748
- // Define a `@export step :: proc(dt: f32) -> (continue : bool) {`
1778
+ // Define a `@export step :: proc(dt: f32) -> (keep_going : bool) {`
1749
1779
// in your app and it will get called every frame.
1750
1780
// return `false` to stop the execution of the module.
1751
1781
if ( exports . step ) {
0 commit comments