@@ -2,13 +2,16 @@ import { TwelfStatus, TwelfError, TwelfExecResponse, TwelfSideEffectData, TwelfE
2
2
import { WasiSnapshotPreview1 , args_get , args_sizes_get , clock_time_get , environ_sizes_get , fd_write } from "./wasi" ;
3
3
4
4
type TwelfExports = {
5
- memory : WebAssembly . Memory ;
6
5
twelf_open ( argc : number , argv : number ) : void ;
7
6
allocate ( size : number ) : number ;
8
7
execute ( ) : TwelfStatus ;
9
8
unsafe ( u : boolean ) : void ;
10
9
} ;
11
10
11
+ type EnvImports = {
12
+ memory : WebAssembly . Memory ,
13
+ }
14
+
12
15
function debug ( _x : string ) : void {
13
16
// console.log(x);
14
17
}
@@ -20,14 +23,28 @@ async function getWasm(url: string): Promise<ArrayBuffer> {
20
23
export async function mkTwelfService ( wasmLoc : string , outputCallback : ( fd : number , str : string ) => void ) : Promise < TwelfService > {
21
24
const twelfWasm = getWasm ( wasmLoc ) ;
22
25
23
- let mem : WebAssembly . Memory | undefined ;
26
+ // We choose to build our own Memory object here instead of using
27
+ // exports.memory, so that we don't need to rebuild the .wasm file
28
+ // in the event we want to change, for example, `maximum`.
29
+ const mem : WebAssembly . Memory = new WebAssembly . Memory ( {
30
+ // these are both in units of 64k pages
31
+ initial : 1 << 10 ,
32
+ maximum : 1 << 14 ,
33
+ } ) ;
34
+
24
35
const argv : string [ ] = [ 'twelf' ] ;
25
- const imports : { wasi_snapshot_preview1 : WebAssembly . ModuleImports & WasiSnapshotPreview1 } = {
36
+ const imports : {
37
+ env : EnvImports ,
38
+ wasi_snapshot_preview1 : WebAssembly . ModuleImports & WasiSnapshotPreview1
39
+ } = {
40
+ env : {
41
+ memory : mem ,
42
+ } ,
26
43
wasi_snapshot_preview1 : {
27
- args_get : ( ...args ) => args_get ( mem ! , argv , ...args ) ,
28
- args_sizes_get : ( ...args ) => args_sizes_get ( mem ! , argv , ...args ) ,
29
- clock_time_get : ( ...args ) => clock_time_get ( mem ! , ...args ) ,
30
- environ_sizes_get : ( ...args ) => environ_sizes_get ( mem ! , ...args ) ,
44
+ args_get : ( ...args ) => args_get ( mem , argv , ...args ) ,
45
+ args_sizes_get : ( ...args ) => args_sizes_get ( mem , argv , ...args ) ,
46
+ clock_time_get : ( ...args ) => clock_time_get ( mem , ...args ) ,
47
+ environ_sizes_get : ( ...args ) => environ_sizes_get ( mem , ...args ) ,
31
48
environ_get : ( ) => { debug ( 'environ_get' ) ; } ,
32
49
proc_exit : ( ) => { debug ( 'proc_exit' ) ; throw new Error ( "proc_exit called, probably shouldn't happen" ) ; } ,
33
50
fd_close : ( ) => { debug ( 'fd_close' ) ; } ,
@@ -39,7 +56,7 @@ export async function mkTwelfService(wasmLoc: string, outputCallback: (fd: numbe
39
56
fd_prestat_get : ( ) => { debug ( 'fd_prestat_get' ) ; } ,
40
57
fd_read : ( ) => { debug ( 'fd_read' ) ; } ,
41
58
fd_seek : ( ) => { debug ( 'fd_seek' ) ; } ,
42
- fd_write : ( ...args ) => { debug ( 'fd_write' ) ; return fd_write ( mem ! , outputCallback , ...args ) ; } ,
59
+ fd_write : ( ...args ) => { debug ( 'fd_write' ) ; return fd_write ( mem , outputCallback , ...args ) ; } ,
43
60
44
61
// Paths
45
62
path_filestat_get : ( ) => { debug ( 'path_filestat_get' ) ; } ,
@@ -49,16 +66,14 @@ export async function mkTwelfService(wasmLoc: string, outputCallback: (fd: numbe
49
66
50
67
const source = await WebAssembly . instantiate ( await twelfWasm , imports ) ;
51
68
const exports = ( source . instance . exports as TwelfExports ) ;
52
- // give import implementations the ability to refer to memory
53
- mem = exports . memory ;
54
69
exports . twelf_open ( 0 , 0 ) ;
55
70
56
- return new TwelfService ( source . instance ) ;
71
+ return new TwelfService ( source . instance , mem ) ;
57
72
}
58
73
59
74
export class TwelfService {
60
75
61
- constructor ( public instance : WebAssembly . Instance ) { }
76
+ constructor ( public instance : WebAssembly . Instance , public memory : WebAssembly . Memory ) { }
62
77
63
78
unsafe ( u : boolean ) : void {
64
79
const exports = this . instance . exports as TwelfExports ;
@@ -67,7 +82,7 @@ export class TwelfService {
67
82
68
83
async exec ( input : string ) : Promise < TwelfStatus > {
69
84
const exports = this . instance . exports as TwelfExports ;
70
- const mem = exports . memory ;
85
+ const mem = this . memory ;
71
86
72
87
let status = ( ( ) => {
73
88
try {
0 commit comments