summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/function-references/call_ref.js
blob: 205f5329a96517c1005c653972af20e7268ed561 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// |jit-test| skip-if: !wasmFunctionReferencesEnabled()

let { plusOne } = wasmEvalText(`(module
  (; forward declaration so that ref.func works ;)
  (elem declare $plusOneRef)
  (type $t (func (param i32) (result i32)))

  (func $plusOneRef (param i32) (result i32)
    (i32.add
      local.get 0
      i32.const 1)
  )

  (func (export "plusOne") (param i32) (result i32)
    local.get 0
    ref.func $plusOneRef
    call_ref $t
  )
)`).exports;

assertEq(plusOne(3), 4);

// pass non-funcref type
wasmFailValidateText(`(module
  (type $t (func (param i32)))
  (func (param $a i32)
    local.get $a
    call_ref $t
  )
)`, /type mismatch: expression has type i32 but expected \(ref null \d+\)/);

wasmFailValidateText(`(module
  (type $t (func (param externref)))
  (func (param $a (ref extern))
    local.get $a
    call_ref $t
  )
)`, /type mismatch: expression has type \(ref extern\) but expected \(ref null \d+\)/);

// pass (non-subtype of) funcref
wasmFailValidateText(`(module
  (type $t (func (param i32) (result i32)))
  (func (param funcref)
    local.get 0
    call_ref $t
  )
)`, /type mismatch: expression has type funcref but expected \(ref null \d+\)/);  
  
// signature mismatch
wasmFailValidateText(`(module
  (type $t (func (param i32) (result i32)))
  (elem declare $plusOneRef)
  (func $plusOneRef (param f32) (result f32)
    (f32.add
      local.get 0
      f32.const 1.0)
  )

  (func (export "plusOne") (param i32) (result i32)
    local.get 0
    ref.func $plusOneRef
    call_ref $t
  )
)`, /type mismatch: expression has type \(ref \d+\) but expected \(ref null \d+\)/);

// Cross-instance calls
let { loadInt } = wasmEvalText(`(module
  (memory 1 1)
  (data (i32.const 0) "\\04\\00\\00\\00")
  (func (export "loadInt") (result i32)
    i32.const 0
    i32.load offset=0
  )
)`).exports;

let { callLoadInt } = wasmEvalText(`(module
  (type $t (func (result i32)))
  (elem declare 0)
  (import "" "loadInt" (func (result i32)))
  (func (export "callLoadInt") (result i32)
    ref.func 0
    call_ref $t
  )
)`, {"": { loadInt, }}).exports;

assertEq(loadInt(), 4);
assertEq(callLoadInt(), 4);

// Null call.
assertErrorMessage(function() {
  let { nullCall } = wasmEvalText(`(module
    (type $t (func (param i32) (result i32)))
    (func (export "nullCall") (param i32) (result i32)
      local.get 0
      ref.null $t
      call_ref $t
    )
  )`).exports;
  nullCall(3);
}, WebAssembly.RuntimeError, /dereferencing null pointer/);