[6a3a178] | 1 | # @webassemblyjs/wasm-edit
|
---|
| 2 |
|
---|
| 3 | > Rewrite a WASM binary
|
---|
| 4 |
|
---|
| 5 | Replace in-place an AST node in the binary.
|
---|
| 6 |
|
---|
| 7 | ## Installation
|
---|
| 8 |
|
---|
| 9 | ```sh
|
---|
| 10 | yarn add @webassemblyjs/wasm-edit
|
---|
| 11 | ```
|
---|
| 12 |
|
---|
| 13 | ## Usage
|
---|
| 14 |
|
---|
| 15 | Update:
|
---|
| 16 |
|
---|
| 17 | ```js
|
---|
| 18 | import { edit } from "@webassemblyjs/wasm-edit";
|
---|
| 19 |
|
---|
| 20 | const binary = [/*...*/];
|
---|
| 21 |
|
---|
| 22 | const visitors = {
|
---|
| 23 | ModuleImport({ node }) {
|
---|
| 24 | node.module = "foo";
|
---|
| 25 | node.name = "bar";
|
---|
| 26 | }
|
---|
| 27 | };
|
---|
| 28 |
|
---|
| 29 | const newBinary = edit(binary, visitors);
|
---|
| 30 | ```
|
---|
| 31 |
|
---|
| 32 | Replace:
|
---|
| 33 |
|
---|
| 34 | ```js
|
---|
| 35 | import { edit } from "@webassemblyjs/wasm-edit";
|
---|
| 36 |
|
---|
| 37 | const binary = [/*...*/];
|
---|
| 38 |
|
---|
| 39 | const visitors = {
|
---|
| 40 | Instr(path) {
|
---|
| 41 | const newNode = t.callInstruction(t.indexLiteral(0));
|
---|
| 42 | path.replaceWith(newNode);
|
---|
| 43 | }
|
---|
| 44 | };
|
---|
| 45 |
|
---|
| 46 | const newBinary = edit(binary, visitors);
|
---|
| 47 | ```
|
---|
| 48 |
|
---|
| 49 | Remove:
|
---|
| 50 |
|
---|
| 51 | ```js
|
---|
| 52 | import { edit } from "@webassemblyjs/wasm-edit";
|
---|
| 53 |
|
---|
| 54 | const binary = [/*...*/];
|
---|
| 55 |
|
---|
| 56 | const visitors = {
|
---|
| 57 | ModuleExport({ node }) {
|
---|
| 58 | path.remove()
|
---|
| 59 | }
|
---|
| 60 | };
|
---|
| 61 |
|
---|
| 62 | const newBinary = edit(binary, visitors);
|
---|
| 63 | ```
|
---|
| 64 |
|
---|
| 65 | Insert:
|
---|
| 66 |
|
---|
| 67 | ```js
|
---|
| 68 | import { add } from "@webassemblyjs/wasm-edit";
|
---|
| 69 |
|
---|
| 70 | const binary = [/*...*/];
|
---|
| 71 |
|
---|
| 72 | const newBinary = add(actualBinary, [
|
---|
| 73 | t.moduleImport("env", "mem", t.memory(t.limit(1)))
|
---|
| 74 | ]);
|
---|
| 75 | ```
|
---|
| 76 |
|
---|
| 77 | ## Providing the AST
|
---|
| 78 |
|
---|
| 79 | Providing an AST allows you to handle the decoding yourself, here is the API:
|
---|
| 80 |
|
---|
| 81 | ```js
|
---|
| 82 | addWithAST(Program, ArrayBuffer, Array<Node>): ArrayBuffer;
|
---|
| 83 | editWithAST(Program, ArrayBuffer, visitors): ArrayBuffer;
|
---|
| 84 | ```
|
---|
| 85 |
|
---|
| 86 | Note that the AST will be updated in-place.
|
---|