Skip to content

Commit

Permalink
Handle passing in custom memory (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
taybenlor committed Oct 12, 2023
1 parent 6751758 commit 78a1f1b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
27 changes: 15 additions & 12 deletions packages/wasi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,22 @@ const wasi = new WASI({
},
});

const myMemory = new WebAssembly.Memory({ initial: 32, maximum: 10000 });

// Then instantiate your binary with the imports provided by the wasi object
const wasm = await WebAssembly.instantiateStreaming(
fetch("/binary.wasm"),
{
...wasi.getImportObject(),

// Your own custom imports (e.g. memory)
{env: {memory: new WebAssembly.Memory({ initial: 32, maximum: 10000 })}}
}
);

// Finally start the WASI binary
const result = wasi.start(wasm);
const wasm = await WebAssembly.instantiateStreaming(fetch("/binary.wasm"), {
...wasi.getImportObject(),

// Your own custom imports (e.g. custom memory)
env: {
memory: myMemory,
},
});

// Finally start the WASI binary (with the custom memory)
const result = wasi.start(wasm, {
memory: myMemory,
});
```

## Using the WASIWorker
Expand Down
15 changes: 8 additions & 7 deletions packages/wasi/lib/wasi/wasi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,16 @@ export class WASI implements SnapshotPreview1 {
};
}

// Internal initialization
private init(wasm: WebAssembly.WebAssemblyInstantiatedSource) {
start(
wasm: WebAssembly.WebAssemblyInstantiatedSource,
options: {
memory?: WebAssembly.Memory;
} = {}
): WASIExecutionResult {
this.instance = wasm.instance;
this.module = wasm.module;
this.memory = this.instance.exports.memory as WebAssembly.Memory;
}

start(wasm: WebAssembly.WebAssemblyInstantiatedSource): WASIExecutionResult {
this.init(wasm);
this.memory =
options.memory ?? (this.instance.exports.memory as WebAssembly.Memory);

const entrypoint = this.instance.exports._start as () => void;
try {
Expand Down

0 comments on commit 78a1f1b

Please sign in to comment.