diff options
Diffstat (limited to 'std/wasi/snapshot_preview1.ts')
-rw-r--r-- | std/wasi/snapshot_preview1.ts | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/std/wasi/snapshot_preview1.ts b/std/wasi/snapshot_preview1.ts index 1d9549f15..eb296c47d 100644 --- a/std/wasi/snapshot_preview1.ts +++ b/std/wasi/snapshot_preview1.ts @@ -270,7 +270,6 @@ export interface ContextOptions { args?: string[]; env?: { [key: string]: string | undefined }; preopens?: { [key: string]: string }; - memory?: WebAssembly.Memory; } export default class Context { @@ -285,7 +284,7 @@ export default class Context { constructor(options: ContextOptions) { this.args = options.args ? options.args : []; this.env = options.env ? options.env : {}; - this.memory = options.memory!; + this.memory = null!; this.fds = [ { @@ -1554,4 +1553,39 @@ export default class Context { }), }; } + + /** + * Attempt to begin execution of instance as a command by invoking its + * _start() export. + * + * If the instance does not contain a _start() export, or if the instance + * contains an _initialize export an error will be thrown. + * + * The instance must also have a WebAssembly.Memory export named "memory" + * which will be used as the address space, if it does not an error will be + * thrown. + */ + start(instance: WebAssembly.Instance) { + const { _start, _initialize, memory } = instance.exports; + + if (!(memory instanceof WebAssembly.Memory)) { + throw new TypeError("WebAsembly.instance must provide a memory export"); + } + + this.memory = memory; + + if (typeof _initialize == "function") { + throw new TypeError( + "WebAsembly.instance export _initialize must not be a function", + ); + } + + if (typeof _start != "function") { + throw new TypeError( + "WebAssembly.Instance export _start must be a function", + ); + } + + _start(); + } } |