From d3b5e1fcb3558cb826da06c5dfe3cccf16f6483a Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Sat, 3 Oct 2020 21:52:34 +0800 Subject: [PATCH] docs(wasi): fix usage example (denoland/deno#7808) The usage example is a bit out of date and not compatible with the stricter definition of the WebAssembly namespace. This fixes that and makes it a bit cleaner. --- wasi/README.md | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/wasi/README.md b/wasi/README.md index 580e7ddd0144..1a2fc4c72e29 100644 --- a/wasi/README.md +++ b/wasi/README.md @@ -59,23 +59,31 @@ import Context from "https://deno.land/std/wasi/snapshot_preview1.ts"; const context = new Context({ args: Deno.args, - env: Deno.env, + env: Deno.env.toObject(), }); const binary = await Deno.readFile("path/to/your/module.wasm"); const module = await WebAssembly.compile(binary); const instance = await WebAssembly.instantiate(module, { - wasi_snapshot_preview1: context.exports, + "wasi_snapshot_preview1": context.exports, }); -context.memory = context.exports.memory; +const { + _start: start, + _initialize: initialize, + memory, +} = instance.exports; -if (module.exports._start) { - instance.exports._start(); -} else if (module.exports._initialize) { - instance.exports._initialize(); +context.memory = memory as WebAssembly.Memory; + +if (start instanceof Function) { + start(); +} else if (initialize instanceof Function) { + initialize(); } else { - throw new Error("No entry point found"); + throw new Error( + "No '_start' or '_initialize' entry point found in WebAssembly module, make sure to compile with wasm32-wasi as the target.", + ); } ```