github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/integration_test/spectest/v2/testdata/ref_func.wast (about)

     1  (module
     2    (func (export "f") (param $x i32) (result i32) (local.get $x))
     3  )
     4  (register "M")
     5  
     6  (module
     7    (func $f (import "M" "f") (param i32) (result i32))
     8    (func $g (param $x i32) (result i32)
     9      (i32.add (local.get $x) (i32.const 1))
    10    )
    11  
    12    (global funcref (ref.func $f))
    13    (global funcref (ref.func $g))
    14    (global $v (mut funcref) (ref.func $f))
    15  
    16    (global funcref (ref.func $gf1))
    17    (global funcref (ref.func $gf2))
    18    (func (drop (ref.func $ff1)) (drop (ref.func $ff2)))
    19    (elem declare func $gf1 $ff1)
    20    (elem declare funcref (ref.func $gf2) (ref.func $ff2))
    21    (func $gf1)
    22    (func $gf2)
    23    (func $ff1)
    24    (func $ff2)
    25  
    26    (func (export "is_null-f") (result i32)
    27      (ref.is_null (ref.func $f))
    28    )
    29    (func (export "is_null-g") (result i32)
    30      (ref.is_null (ref.func $g))
    31    )
    32    (func (export "is_null-v") (result i32)
    33      (ref.is_null (global.get $v))
    34    )
    35  
    36    (func (export "set-f") (global.set $v (ref.func $f)))
    37    (func (export "set-g") (global.set $v (ref.func $g)))
    38  
    39    (table $t 1 funcref)
    40    (elem declare func $f $g)
    41  
    42    (func (export "call-f") (param $x i32) (result i32)
    43      (table.set $t (i32.const 0) (ref.func $f))
    44      (call_indirect $t (param i32) (result i32) (local.get $x) (i32.const 0))
    45    )
    46    (func (export "call-g") (param $x i32) (result i32)
    47      (table.set $t (i32.const 0) (ref.func $g))
    48      (call_indirect $t (param i32) (result i32) (local.get $x) (i32.const 0))
    49    )
    50    (func (export "call-v") (param $x i32) (result i32)
    51      (table.set $t (i32.const 0) (global.get $v))
    52      (call_indirect $t (param i32) (result i32) (local.get $x) (i32.const 0))
    53    )
    54  )
    55  
    56  (assert_return (invoke "is_null-f") (i32.const 0))
    57  (assert_return (invoke "is_null-g") (i32.const 0))
    58  (assert_return (invoke "is_null-v") (i32.const 0))
    59  
    60  (assert_return (invoke "call-f" (i32.const 4)) (i32.const 4))
    61  (assert_return (invoke "call-g" (i32.const 4)) (i32.const 5))
    62  (assert_return (invoke "call-v" (i32.const 4)) (i32.const 4))
    63  (invoke "set-g")
    64  (assert_return (invoke "call-v" (i32.const 4)) (i32.const 5))
    65  (invoke "set-f")
    66  (assert_return (invoke "call-v" (i32.const 4)) (i32.const 4))
    67  
    68  (assert_invalid
    69    (module
    70      (func $f (import "M" "f") (param i32) (result i32))
    71      (func $g (import "M" "g") (param i32) (result i32))
    72      (global funcref (ref.func 7))
    73    )
    74    "unknown function 7"
    75  )
    76  
    77  
    78  ;; Reference declaration
    79  
    80  (module
    81    (func $f1)
    82    (func $f2)
    83    (func $f3)
    84    (func $f4)
    85    (func $f5)
    86    (func $f6)
    87  
    88    (table $t 1 funcref)
    89  
    90    (global funcref (ref.func $f1))
    91    (export "f" (func $f2))
    92    (elem (table $t) (i32.const 0) func $f3)
    93    (elem (table $t) (i32.const 0) funcref (ref.func $f4))
    94    (elem func $f5)
    95    (elem funcref (ref.func $f6))
    96  
    97    (func
    98      (ref.func $f1)
    99      (ref.func $f2)
   100      (ref.func $f3)
   101      (ref.func $f4)
   102      (ref.func $f5)
   103      (ref.func $f6)
   104      (return)
   105    )
   106  )
   107  
   108  (assert_invalid
   109    (module (func $f (drop (ref.func $f))))
   110    "undeclared function reference"
   111  )
   112  (assert_invalid
   113    (module (start $f) (func $f (drop (ref.func $f))))
   114    "undeclared function reference"
   115  )