You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/compiler.md
+67-3
Original file line number
Diff line number
Diff line change
@@ -53,9 +53,14 @@ Optimization levels can also be tweaked manually: `--optimizeLevel` \(0-3\) indi
53
53
Typical output formats are WebAssembly binary \(.wasm, `--outFile`\) and/or text format \(.wat, `--textFile`\). Often, both are used in tandem to run and also inspect generated code.
54
54
55
55
```
56
-
--outFile, -o Specifies the output file. File extension indicates format.
57
-
--textFile, -t Specifies the text output file (.wat).
58
-
--tsdFile, -d Specifies the TypeScript definition output file (.d.ts).
56
+
--outFile, -o Specifies the WebAssembly output file (.wasm).
57
+
--textFile, -t Specifies the WebAssembly text output file (.wat).
58
+
--bindings, -b Specifies the bindings to generate (.js + .d.ts).
59
+
60
+
esm JavaScript bindings & typings for ESM integration.
61
+
raw Like esm, but exports just the instantiate function.
62
+
Useful where modules are meant to be instantiated
63
+
multiple times or non-ESM imports must be provided.
Also note that exporting an entire `class` has no effect at the module boundary (yet), and it is instead recommended to expose only the needed functionality as shown in the example above. Supported elements at the boundary are globals, functions and enums.
322
327
328
+
### Using esm bindings
329
+
330
+
Bindings generated with `--bindings esm` perform all the steps from compilation over instantiation to exporting the final interface. To do so, a few assumptions had to be made:
331
+
332
+
* The WebAssembly binary is located next to the JavaScript bindings file using the same name but with a `.wasm` extension.
333
+
```
334
+
build/mymodule.js
335
+
build/mymodule.wasm
336
+
```
337
+
338
+
* JavaScript globals in `globalThis` can be accessed directly via the `env` module namespace. For example, `console.log` can be manually imported through:
339
+
```ts
340
+
@external("env", "console.log")
341
+
declarefunction consoleLog(s:string):void
342
+
```
343
+
Note that this is just an example and `console.log` is already provided by the standard library when called from an AssemblyScript file. Other global functions not already provided by the standard library may require an import as of this example, though.
344
+
345
+
*Importsfromothernamespacesthan`env`, i.e. `(import "module" "name")`, becomean`import { name } from "module"`withinthebinding. Importingacustomfunction from a JavaScript file next to the bindings file can be achieved through:
346
+
```ts
347
+
@external("./otherfile.js", "myFunction")
348
+
declarefunction myFunction(...): ...
349
+
```
350
+
Similarly, importing a custom function from, say, a Node.js dependency can be achieved through:
Thesignatureofthesingle`instantiate`function exported by `--bindings raw` is:
361
+
362
+
```ts
363
+
export async function instantiate(module: WebAssembly.Module, imports?: WebAssembly.Imports): AdaptedExports
364
+
```
365
+
366
+
Notethatthefunction does not make any assumptions on how the module is to be compiled, but instead expects a readily compiled `WebAssembly.Module` as in this example:
0 commit comments