Projekt

Obecné

Profil

Stáhnout (2.75 KB) Statistiky
| Větev: | Revize:
1
const { assert } = require("chai");
2
const { parse } = require("@webassemblyjs/wast-parser");
3

    
4
const { moduleContextFromModuleAST } = require("../lib");
5

    
6
const contextFromWast = wast => moduleContextFromModuleAST(parse(wast).body[0]);
7

    
8
describe("module context", () => {
9
  describe("start segment", () => {
10
    it("should return the start function offset", () => {
11
      const context = contextFromWast(`
12
        (module
13
          (func)
14
          (func)
15
          (start 1)
16
        )
17
      `);
18

    
19
      assert.isOk(context.getStart());
20
      assert.typeOf(context.getStart(), "number");
21
      assert.equal(context.getStart(), 1);
22
    });
23

    
24
    it("should return null if no start function", () => {
25
      const context = contextFromWast(`
26
        (module (func))
27
      `);
28

    
29
      assert.isNull(context.getStart());
30
    });
31

    
32
    it("should retrive the type of implemented functions", () => {
33
      const context = contextFromWast(`
34
        (module
35
          (func (param i32) (result i64))
36
          (func (param i64) (result i32))
37
          (func (result i64))
38
          (func)
39
        )
40
      `);
41

    
42
      assert.deepEqual(context.getFunction(0), {
43
        args: ["i32"],
44
        result: ["i64"]
45
      });
46
      assert.deepEqual(context.getFunction(1), {
47
        args: ["i64"],
48
        result: ["i32"]
49
      });
50
      assert.deepEqual(context.getFunction(2), { args: [], result: ["i64"] });
51
      assert.deepEqual(context.getFunction(3), { args: [], result: [] });
52
    });
53

    
54
    it("should retrive the type of imported functions", () => {
55
      const context = contextFromWast(`
56
        (module
57
          (import "a" "a" (func (param i32) (result i32)))
58
          (import "a" "b" (func (result i64)))
59
          (import "a" "c" (func))
60
          (func (result f32))
61
        )
62
      `);
63

    
64
      assert.deepEqual(context.getFunction(0), {
65
        args: ["i32"],
66
        result: ["i32"]
67
      });
68
      assert.deepEqual(context.getFunction(1), {
69
        args: [],
70
        result: ["i64"]
71
      });
72
      assert.deepEqual(context.getFunction(2), { args: [], result: [] });
73
      assert.deepEqual(context.getFunction(3), { args: [], result: ["f32"] });
74
    });
75

    
76
    it("should retrive the type of functions with type ref", () => {
77
      const context = contextFromWast(`
78
        (module
79
          (type (func (param i32) (result i32)))
80
          (type (func (result i64)))
81
          (type (func))
82

    
83
          (import "a" "a" (func (type 0)))
84
          (import "a" "b" (func (type 1)))
85
          (func (type 2))
86
        )
87
      `);
88

    
89
      assert.deepEqual(context.getFunction(0), {
90
        args: ["i32"],
91
        result: ["i32"]
92
      });
93
      assert.deepEqual(context.getFunction(1), {
94
        args: [],
95
        result: ["i64"]
96
      });
97
      assert.deepEqual(context.getFunction(2), { args: [], result: [] });
98
    });
99
  });
100
});
    (1-1/1)