You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
1.3 KiB

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