File: exec.tjs

package info (click to toggle)
node-webassemblyjs 1.11.0%2Bdfsg%2B~cs10.10.16-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,040 kB
  • sloc: javascript: 28,630; makefile: 84; sh: 42; ansic: 16
file content (59 lines) | stat: -rw-r--r-- 1,768 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const PAGE_SIZE = Math.pow(2, 16);
const I32_SIZE = 4;

it("should export the memory instance", () => {
  return WebAssembly.instantiate(wasmmodule).then(m => {
    assert.containsAllKeys(m.instance.exports.memory, ["buffer"]);
  });
});

it("should store at the given location", () => {
  return WebAssembly.instantiate(wasmmodule).then(m => {
    const i32 = new Uint32Array(m.instance.exports.memory.buffer);

    // store a positive
    m.instance.exports.store(12, 45);
    assert.equal(i32[12 / I32_SIZE], 45);

    // store a negative
    m.instance.exports.store(16, -100);
    // note - we have to re-interpret the Uint32Array as signed
    assert.equal(i32[16 / I32_SIZE] | 0, -100);
  });
});

it("should throw for memory out of bounds", () => {
  return WebAssembly.instantiate(wasmmodule).then(m => {
    const i32 = new Uint32Array(m.instance.exports.memory.buffer);

    // upper bound
    m.instance.exports.store(PAGE_SIZE - I32_SIZE, 22);
    assert.equal(i32[PAGE_SIZE / I32_SIZE - 1], 22);

    assert.throws(() => {
      // upper bound exceeded
      m.instance.exports.store(PAGE_SIZE - I32_SIZE + 1, 22);
    }, "memory access out of bounds");
  });
});

it("should support offsets", () => {
  return WebAssembly.instantiate(wasmmodule).then(m => {
    const i32 = new Uint32Array(m.instance.exports.memory.buffer);

    // upper bound
    const offset = 4;
    m.instance.exports.store_with_offset(4, 22);
    assert.equal(i32[(4 + offset) / I32_SIZE - 1], 22);
  });
});

it("should support lower bit depth store", () => {
  return WebAssembly.instantiate(wasmmodule).then(m => {
    const i8 = new Uint8Array(m.instance.exports.memory.buffer);

    // upper bound
    m.instance.exports.store8(5, 22);
    assert.equal(i8[5], 22);
  });
});