Skip to content

Commit 4cb9b03

Browse files
committed
Import rather than export memory
This change lands a `twelf.wasm` produced by a yet-unlanded branch of the twelf repository, namely standardml/twelf#20
1 parent 3eda272 commit 4cb9b03

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

public/assets/twelf.wasm

-36 Bytes
Binary file not shown.

src/twelf-service.ts

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ import { TwelfStatus, TwelfError, TwelfExecResponse, TwelfSideEffectData, TwelfE
22
import { WasiSnapshotPreview1, args_get, args_sizes_get, clock_time_get, environ_sizes_get, fd_write } from "./wasi";
33

44
type TwelfExports = {
5-
memory: WebAssembly.Memory;
65
twelf_open(argc: number, argv: number): void;
76
allocate(size: number): number;
87
execute(): TwelfStatus;
98
unsafe(u: boolean): void;
109
};
1110

11+
type EnvImports = {
12+
memory: WebAssembly.Memory,
13+
}
14+
1215
function debug(_x: string): void {
1316
// console.log(x);
1417
}
@@ -20,14 +23,28 @@ async function getWasm(url: string): Promise<ArrayBuffer> {
2023
export async function mkTwelfService(wasmLoc: string, outputCallback: (fd: number, str: string) => void): Promise<TwelfService> {
2124
const twelfWasm = getWasm(wasmLoc);
2225

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+
2435
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+
},
2643
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),
3148
environ_get: () => { debug('environ_get'); },
3249
proc_exit: () => { debug('proc_exit'); throw new Error("proc_exit called, probably shouldn't happen"); },
3350
fd_close: () => { debug('fd_close'); },
@@ -39,7 +56,7 @@ export async function mkTwelfService(wasmLoc: string, outputCallback: (fd: numbe
3956
fd_prestat_get: () => { debug('fd_prestat_get'); },
4057
fd_read: () => { debug('fd_read'); },
4158
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); },
4360

4461
// Paths
4562
path_filestat_get: () => { debug('path_filestat_get'); },
@@ -49,16 +66,14 @@ export async function mkTwelfService(wasmLoc: string, outputCallback: (fd: numbe
4966

5067
const source = await WebAssembly.instantiate(await twelfWasm, imports);
5168
const exports = (source.instance.exports as TwelfExports);
52-
// give import implementations the ability to refer to memory
53-
mem = exports.memory;
5469
exports.twelf_open(0, 0);
5570

56-
return new TwelfService(source.instance);
71+
return new TwelfService(source.instance, mem);
5772
}
5873

5974
export class TwelfService {
6075

61-
constructor(public instance: WebAssembly.Instance) { }
76+
constructor(public instance: WebAssembly.Instance, public memory: WebAssembly.Memory) { }
6277

6378
unsafe(u: boolean): void {
6479
const exports = this.instance.exports as TwelfExports;
@@ -67,7 +82,7 @@ export class TwelfService {
6782

6883
async exec(input: string): Promise<TwelfStatus> {
6984
const exports = this.instance.exports as TwelfExports;
70-
const mem = exports.memory;
85+
const mem = this.memory;
7186

7287
let status = (() => {
7388
try {

0 commit comments

Comments
 (0)