index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const { assert } = require("chai");
  2. const { parse } = require("@webassemblyjs/wast-parser");
  3. const { moduleContextFromModuleAST } = require("../lib");
  4. const contextFromWast = wast => moduleContextFromModuleAST(parse(wast).body[0]);
  5. describe("module context", () => {
  6. describe("start segment", () => {
  7. it("should return the start function offset", () => {
  8. const context = contextFromWast(`
  9. (module
  10. (func)
  11. (func)
  12. (start 1)
  13. )
  14. `);
  15. assert.isOk(context.getStart());
  16. assert.typeOf(context.getStart(), "number");
  17. assert.equal(context.getStart(), 1);
  18. });
  19. it("should return null if no start function", () => {
  20. const context = contextFromWast(`
  21. (module (func))
  22. `);
  23. assert.isNull(context.getStart());
  24. });
  25. it("should retrive the type of implemented functions", () => {
  26. const context = contextFromWast(`
  27. (module
  28. (func (param i32) (result i64))
  29. (func (param i64) (result i32))
  30. (func (result i64))
  31. (func)
  32. )
  33. `);
  34. assert.deepEqual(context.getFunction(0), {
  35. args: ["i32"],
  36. result: ["i64"]
  37. });
  38. assert.deepEqual(context.getFunction(1), {
  39. args: ["i64"],
  40. result: ["i32"]
  41. });
  42. assert.deepEqual(context.getFunction(2), { args: [], result: ["i64"] });
  43. assert.deepEqual(context.getFunction(3), { args: [], result: [] });
  44. });
  45. it("should retrive the type of imported functions", () => {
  46. const context = contextFromWast(`
  47. (module
  48. (import "a" "a" (func (param i32) (result i32)))
  49. (import "a" "b" (func (result i64)))
  50. (import "a" "c" (func))
  51. (func (result f32))
  52. )
  53. `);
  54. assert.deepEqual(context.getFunction(0), {
  55. args: ["i32"],
  56. result: ["i32"]
  57. });
  58. assert.deepEqual(context.getFunction(1), {
  59. args: [],
  60. result: ["i64"]
  61. });
  62. assert.deepEqual(context.getFunction(2), { args: [], result: [] });
  63. assert.deepEqual(context.getFunction(3), { args: [], result: ["f32"] });
  64. });
  65. it("should retrive the type of functions with type ref", () => {
  66. const context = contextFromWast(`
  67. (module
  68. (type (func (param i32) (result i32)))
  69. (type (func (result i64)))
  70. (type (func))
  71. (import "a" "a" (func (type 0)))
  72. (import "a" "b" (func (type 1)))
  73. (func (type 2))
  74. )
  75. `);
  76. assert.deepEqual(context.getFunction(0), {
  77. args: ["i32"],
  78. result: ["i32"]
  79. });
  80. assert.deepEqual(context.getFunction(1), {
  81. args: [],
  82. result: ["i64"]
  83. });
  84. assert.deepEqual(context.getFunction(2), { args: [], result: [] });
  85. });
  86. });
  87. });