wa-lang.org/wazero@v1.0.2/internal/integration_test/spectest/v2/testdata/func.wast (about)

     1  ;; Test `func` declarations, i.e. functions
     2  
     3  (module
     4    ;; Auxiliary definition
     5    (type $sig (func))
     6    (func $dummy)
     7  
     8    ;; Syntax
     9  
    10    (func)
    11    (func (export "f"))
    12    (func $f)
    13    (func $h (export "g"))
    14  
    15    (func (local))
    16    (func (local) (local))
    17    (func (local i32))
    18    (func (local $x i32))
    19    (func (local i32 f64 i64))
    20    (func (local i32) (local f64))
    21    (func (local i32 f32) (local $x i64) (local) (local i32 f64))
    22  
    23    (func (param))
    24    (func (param) (param))
    25    (func (param i32))
    26    (func (param $x i32))
    27    (func (param i32 f64 i64))
    28    (func (param i32) (param f64))
    29    (func (param i32 f32) (param $x i64) (param) (param i32 f64))
    30  
    31    (func (result))
    32    (func (result) (result))
    33    (func (result i32) (unreachable))
    34    (func (result i32 f64 f32) (unreachable))
    35    (func (result i32) (result f64) (unreachable))
    36    (func (result i32 f32) (result i64) (result) (result i32 f64) (unreachable))
    37  
    38    (type $sig-1 (func))
    39    (type $sig-2 (func (result i32)))
    40    (type $sig-3 (func (param $x i32)))
    41    (type $sig-4 (func (param i32 f64 i32) (result i32)))
    42  
    43    (func (export "type-use-1") (type $sig-1))
    44    (func (export "type-use-2") (type $sig-2) (i32.const 0))
    45    (func (export "type-use-3") (type $sig-3))
    46    (func (export "type-use-4") (type $sig-4) (i32.const 0))
    47    (func (export "type-use-5") (type $sig-2) (result i32) (i32.const 0))
    48    (func (export "type-use-6") (type $sig-3) (param i32))
    49    (func (export "type-use-7")
    50      (type $sig-4) (param i32) (param f64 i32) (result i32) (i32.const 0)
    51    )
    52  
    53    (func (type $sig))
    54    (func (type $forward))  ;; forward reference
    55  
    56    (func $complex
    57      (param i32 f32) (param $x i64) (param) (param i32)
    58      (result) (result i32) (result) (result i64 i32)
    59      (local f32) (local $y i32) (local i64 i32) (local) (local f64 i32)
    60      (unreachable) (unreachable)
    61    )
    62    (func $complex-sig
    63      (type $sig)
    64      (local f32) (local $y i32) (local i64 i32) (local) (local f64 i32)
    65      (unreachable) (unreachable)
    66    )
    67  
    68    (type $forward (func))
    69  
    70    ;; Typing of locals
    71  
    72    (func (export "local-first-i32") (result i32) (local i32 i32) (local.get 0))
    73    (func (export "local-first-i64") (result i64) (local i64 i64) (local.get 0))
    74    (func (export "local-first-f32") (result f32) (local f32 f32) (local.get 0))
    75    (func (export "local-first-f64") (result f64) (local f64 f64) (local.get 0))
    76    (func (export "local-second-i32") (result i32) (local i32 i32) (local.get 1))
    77    (func (export "local-second-i64") (result i64) (local i64 i64) (local.get 1))
    78    (func (export "local-second-f32") (result f32) (local f32 f32) (local.get 1))
    79    (func (export "local-second-f64") (result f64) (local f64 f64) (local.get 1))
    80    (func (export "local-mixed") (result f64)
    81      (local f32) (local $x i32) (local i64 i32) (local) (local f64 i32)
    82      (drop (f32.neg (local.get 0)))
    83      (drop (i32.eqz (local.get 1)))
    84      (drop (i64.eqz (local.get 2)))
    85      (drop (i32.eqz (local.get 3)))
    86      (drop (f64.neg (local.get 4)))
    87      (drop (i32.eqz (local.get 5)))
    88      (local.get 4)
    89    )
    90  
    91    ;; Typing of parameters
    92  
    93    (func (export "param-first-i32") (param i32 i32) (result i32) (local.get 0))
    94    (func (export "param-first-i64") (param i64 i64) (result i64) (local.get 0))
    95    (func (export "param-first-f32") (param f32 f32) (result f32) (local.get 0))
    96    (func (export "param-first-f64") (param f64 f64) (result f64) (local.get 0))
    97    (func (export "param-second-i32") (param i32 i32) (result i32) (local.get 1))
    98    (func (export "param-second-i64") (param i64 i64) (result i64) (local.get 1))
    99    (func (export "param-second-f32") (param f32 f32) (result f32) (local.get 1))
   100    (func (export "param-second-f64") (param f64 f64) (result f64) (local.get 1))
   101    (func (export "param-mixed") (param f32 i32) (param) (param $x i64) (param i32 f64 i32)
   102      (result f64)
   103      (drop (f32.neg (local.get 0)))
   104      (drop (i32.eqz (local.get 1)))
   105      (drop (i64.eqz (local.get 2)))
   106      (drop (i32.eqz (local.get 3)))
   107      (drop (f64.neg (local.get 4)))
   108      (drop (i32.eqz (local.get 5)))
   109      (local.get 4)
   110    )
   111  
   112    ;; Typing of results
   113  
   114    (func (export "empty"))
   115    (func (export "value-void") (call $dummy))
   116    (func (export "value-i32") (result i32) (i32.const 77))
   117    (func (export "value-i64") (result i64) (i64.const 7777))
   118    (func (export "value-f32") (result f32) (f32.const 77.7))
   119    (func (export "value-f64") (result f64) (f64.const 77.77))
   120    (func (export "value-i32-f64") (result i32 f64) (i32.const 77) (f64.const 7))
   121    (func (export "value-i32-i32-i32") (result i32 i32 i32)
   122      (i32.const 1) (i32.const 2) (i32.const 3)
   123    )
   124    (func (export "value-block-void") (block (call $dummy) (call $dummy)))
   125    (func (export "value-block-i32") (result i32)
   126      (block (result i32) (call $dummy) (i32.const 77))
   127    )
   128    (func (export "value-block-i32-i64") (result i32 i64)
   129      (block (result i32 i64) (call $dummy) (i32.const 1) (i64.const 2))
   130    )
   131  
   132    (func (export "return-empty") (return))
   133    (func (export "return-i32") (result i32) (return (i32.const 78)))
   134    (func (export "return-i64") (result i64) (return (i64.const 7878)))
   135    (func (export "return-f32") (result f32) (return (f32.const 78.7)))
   136    (func (export "return-f64") (result f64) (return (f64.const 78.78)))
   137    (func (export "return-i32-f64") (result i32 f64)
   138      (return (i32.const 78) (f64.const 78.78))
   139    )
   140    (func (export "return-i32-i32-i32") (result i32 i32 i32)
   141      (return (i32.const 1) (i32.const 2) (i32.const 3))
   142    )
   143    (func (export "return-block-i32") (result i32)
   144      (return (block (result i32) (call $dummy) (i32.const 77)))
   145    )
   146    (func (export "return-block-i32-i64") (result i32 i64)
   147      (return (block (result i32 i64) (call $dummy) (i32.const 1) (i64.const 2)))
   148    )
   149  
   150    (func (export "break-empty") (br 0))
   151    (func (export "break-i32") (result i32) (br 0 (i32.const 79)))
   152    (func (export "break-i64") (result i64) (br 0 (i64.const 7979)))
   153    (func (export "break-f32") (result f32) (br 0 (f32.const 79.9)))
   154    (func (export "break-f64") (result f64) (br 0 (f64.const 79.79)))
   155    (func (export "break-i32-f64") (result i32 f64)
   156      (br 0 (i32.const 79) (f64.const 79.79))
   157    )
   158    (func (export "break-i32-i32-i32") (result i32 i32 i32)
   159      (br 0 (i32.const 1) (i32.const 2) (i32.const 3))
   160    )
   161    (func (export "break-block-i32") (result i32)
   162      (br 0 (block (result i32) (call $dummy) (i32.const 77)))
   163    )
   164    (func (export "break-block-i32-i64") (result i32 i64)
   165      (br 0 (block (result i32 i64) (call $dummy) (i32.const 1) (i64.const 2)))
   166    )
   167  
   168    (func (export "break-br_if-empty") (param i32)
   169      (br_if 0 (local.get 0))
   170    )
   171    (func (export "break-br_if-num") (param i32) (result i32)
   172      (drop (br_if 0 (i32.const 50) (local.get 0))) (i32.const 51)
   173    )
   174    (func (export "break-br_if-num-num") (param i32) (result i32 i64)
   175      (drop (drop (br_if 0 (i32.const 50) (i64.const 51) (local.get 0))))
   176      (i32.const 51) (i64.const 52)
   177    )
   178  
   179    (func (export "break-br_table-empty") (param i32)
   180      (br_table 0 0 0 (local.get 0))
   181    )
   182    (func (export "break-br_table-num") (param i32) (result i32)
   183      (br_table 0 0 (i32.const 50) (local.get 0)) (i32.const 51)
   184    )
   185    (func (export "break-br_table-num-num") (param i32) (result i32 i64)
   186      (br_table 0 0 (i32.const 50) (i64.const 51) (local.get 0))
   187      (i32.const 51) (i64.const 52)
   188    )
   189    (func (export "break-br_table-nested-empty") (param i32)
   190      (block (br_table 0 1 0 (local.get 0)))
   191    )
   192    (func (export "break-br_table-nested-num") (param i32) (result i32)
   193      (i32.add
   194        (block (result i32)
   195          (br_table 0 1 0 (i32.const 50) (local.get 0)) (i32.const 51)
   196        )
   197        (i32.const 2)
   198      )
   199    )
   200    (func (export "break-br_table-nested-num-num") (param i32) (result i32 i32)
   201      (i32.add
   202        (block (result i32 i32)
   203          (br_table 0 1 0 (i32.const 50) (i32.const 51) (local.get 0))
   204          (i32.const 51) (i32.const -3)
   205        )
   206      )
   207      (i32.const 52)
   208    )
   209  
   210    ;; Large signatures
   211  
   212    (func (export "large-sig")
   213      (param i32 i64 f32 f32 i32 f64 f32 i32 i32 i32 f32 f64 f64 f64 i32 i32 f32)
   214      (result f64 f32 i32 i32 i32 i64 f32 i32 i32 f32 f64 f64 i32 f32 i32 f64)
   215      (local.get 5)
   216      (local.get 2)
   217      (local.get 0)
   218      (local.get 8)
   219      (local.get 7)
   220      (local.get 1)
   221      (local.get 3)
   222      (local.get 9)
   223      (local.get 4)
   224      (local.get 6)
   225      (local.get 13)
   226      (local.get 11)
   227      (local.get 15)
   228      (local.get 16)
   229      (local.get 14)
   230      (local.get 12)
   231    )
   232  
   233    ;; Default initialization of locals
   234  
   235    (func (export "init-local-i32") (result i32) (local i32) (local.get 0))
   236    (func (export "init-local-i64") (result i64) (local i64) (local.get 0))
   237    (func (export "init-local-f32") (result f32) (local f32) (local.get 0))
   238    (func (export "init-local-f64") (result f64) (local f64) (local.get 0))
   239  )
   240  
   241  (assert_return (invoke "type-use-1"))
   242  (assert_return (invoke "type-use-2") (i32.const 0))
   243  (assert_return (invoke "type-use-3" (i32.const 1)))
   244  (assert_return
   245    (invoke "type-use-4" (i32.const 1) (f64.const 1) (i32.const 1))
   246    (i32.const 0)
   247  )
   248  (assert_return (invoke "type-use-5") (i32.const 0))
   249  (assert_return (invoke "type-use-6" (i32.const 1)))
   250  (assert_return
   251    (invoke "type-use-7" (i32.const 1) (f64.const 1) (i32.const 1))
   252    (i32.const 0)
   253  )
   254  
   255  (assert_return (invoke "local-first-i32") (i32.const 0))
   256  (assert_return (invoke "local-first-i64") (i64.const 0))
   257  (assert_return (invoke "local-first-f32") (f32.const 0))
   258  (assert_return (invoke "local-first-f64") (f64.const 0))
   259  (assert_return (invoke "local-second-i32") (i32.const 0))
   260  (assert_return (invoke "local-second-i64") (i64.const 0))
   261  (assert_return (invoke "local-second-f32") (f32.const 0))
   262  (assert_return (invoke "local-second-f64") (f64.const 0))
   263  (assert_return (invoke "local-mixed") (f64.const 0))
   264  
   265  (assert_return
   266    (invoke "param-first-i32" (i32.const 2) (i32.const 3)) (i32.const 2)
   267  )
   268  (assert_return
   269    (invoke "param-first-i64" (i64.const 2) (i64.const 3)) (i64.const 2)
   270  )
   271  (assert_return
   272    (invoke "param-first-f32" (f32.const 2) (f32.const 3)) (f32.const 2)
   273  )
   274  (assert_return
   275    (invoke "param-first-f64" (f64.const 2) (f64.const 3)) (f64.const 2)
   276  )
   277  (assert_return
   278    (invoke "param-second-i32" (i32.const 2) (i32.const 3)) (i32.const 3)
   279  )
   280  (assert_return
   281    (invoke "param-second-i64" (i64.const 2) (i64.const 3)) (i64.const 3)
   282  )
   283  (assert_return
   284    (invoke "param-second-f32" (f32.const 2) (f32.const 3)) (f32.const 3)
   285  )
   286  (assert_return
   287    (invoke "param-second-f64" (f64.const 2) (f64.const 3)) (f64.const 3)
   288  )
   289  
   290  (assert_return
   291    (invoke "param-mixed"
   292      (f32.const 1) (i32.const 2) (i64.const 3)
   293      (i32.const 4) (f64.const 5.5) (i32.const 6)
   294    )
   295    (f64.const 5.5)
   296  )
   297  
   298  (assert_return (invoke "empty"))
   299  (assert_return (invoke "value-void"))
   300  (assert_return (invoke "value-i32") (i32.const 77))
   301  (assert_return (invoke "value-i64") (i64.const 7777))
   302  (assert_return (invoke "value-f32") (f32.const 77.7))
   303  (assert_return (invoke "value-f64") (f64.const 77.77))
   304  (assert_return (invoke "value-i32-f64") (i32.const 77) (f64.const 7))
   305  (assert_return (invoke "value-i32-i32-i32")
   306    (i32.const 1) (i32.const 2) (i32.const 3)
   307  )
   308  (assert_return (invoke "value-block-void"))
   309  (assert_return (invoke "value-block-i32") (i32.const 77))
   310  (assert_return (invoke "value-block-i32-i64") (i32.const 1) (i64.const 2))
   311  
   312  (assert_return (invoke "return-empty"))
   313  (assert_return (invoke "return-i32") (i32.const 78))
   314  (assert_return (invoke "return-i64") (i64.const 7878))
   315  (assert_return (invoke "return-f32") (f32.const 78.7))
   316  (assert_return (invoke "return-f64") (f64.const 78.78))
   317  (assert_return (invoke "return-i32-f64") (i32.const 78) (f64.const 78.78))
   318  (assert_return (invoke "return-i32-i32-i32")
   319    (i32.const 1) (i32.const 2) (i32.const 3)
   320  )
   321  (assert_return (invoke "return-block-i32") (i32.const 77))
   322  (assert_return (invoke "return-block-i32-i64") (i32.const 1) (i64.const 2))
   323  
   324  (assert_return (invoke "break-empty"))
   325  (assert_return (invoke "break-i32") (i32.const 79))
   326  (assert_return (invoke "break-i64") (i64.const 7979))
   327  (assert_return (invoke "break-f32") (f32.const 79.9))
   328  (assert_return (invoke "break-f64") (f64.const 79.79))
   329  (assert_return (invoke "break-i32-f64") (i32.const 79) (f64.const 79.79))
   330  (assert_return (invoke "break-i32-i32-i32")
   331    (i32.const 1) (i32.const 2) (i32.const 3)
   332  )
   333  (assert_return (invoke "break-block-i32") (i32.const 77))
   334  (assert_return (invoke "break-block-i32-i64") (i32.const 1) (i64.const 2))
   335  
   336  (assert_return (invoke "break-br_if-empty" (i32.const 0)))
   337  (assert_return (invoke "break-br_if-empty" (i32.const 2)))
   338  (assert_return (invoke "break-br_if-num" (i32.const 0)) (i32.const 51))
   339  (assert_return (invoke "break-br_if-num" (i32.const 1)) (i32.const 50))
   340  (assert_return (invoke "break-br_if-num-num" (i32.const 0))
   341    (i32.const 51) (i64.const 52)
   342  )
   343  (assert_return (invoke "break-br_if-num-num" (i32.const 1))
   344    (i32.const 50) (i64.const 51)
   345  )
   346  
   347  (assert_return (invoke "break-br_table-empty" (i32.const 0)))
   348  (assert_return (invoke "break-br_table-empty" (i32.const 1)))
   349  (assert_return (invoke "break-br_table-empty" (i32.const 5)))
   350  (assert_return (invoke "break-br_table-empty" (i32.const -1)))
   351  (assert_return (invoke "break-br_table-num" (i32.const 0)) (i32.const 50))
   352  (assert_return (invoke "break-br_table-num" (i32.const 1)) (i32.const 50))
   353  (assert_return (invoke "break-br_table-num" (i32.const 10)) (i32.const 50))
   354  (assert_return (invoke "break-br_table-num" (i32.const -100)) (i32.const 50))
   355  (assert_return (invoke "break-br_table-num-num" (i32.const 0))
   356    (i32.const 50) (i64.const 51)
   357  )
   358  (assert_return (invoke "break-br_table-num-num" (i32.const 1))
   359    (i32.const 50) (i64.const 51)
   360  )
   361  (assert_return (invoke "break-br_table-num-num" (i32.const 10))
   362    (i32.const 50) (i64.const 51)
   363  )
   364  (assert_return (invoke "break-br_table-num-num" (i32.const -100))
   365    (i32.const 50) (i64.const 51)
   366  )
   367  (assert_return (invoke "break-br_table-nested-empty" (i32.const 0)))
   368  (assert_return (invoke "break-br_table-nested-empty" (i32.const 1)))
   369  (assert_return (invoke "break-br_table-nested-empty" (i32.const 3)))
   370  (assert_return (invoke "break-br_table-nested-empty" (i32.const -2)))
   371  (assert_return
   372    (invoke "break-br_table-nested-num" (i32.const 0)) (i32.const 52)
   373  )
   374  (assert_return
   375    (invoke "break-br_table-nested-num" (i32.const 1)) (i32.const 50)
   376  )
   377  (assert_return
   378    (invoke "break-br_table-nested-num" (i32.const 2)) (i32.const 52)
   379  )
   380  (assert_return
   381    (invoke "break-br_table-nested-num" (i32.const -3)) (i32.const 52)
   382  )
   383  (assert_return
   384    (invoke "break-br_table-nested-num-num" (i32.const 0))
   385    (i32.const 101) (i32.const 52)
   386  )
   387  (assert_return
   388    (invoke "break-br_table-nested-num-num" (i32.const 1))
   389    (i32.const 50) (i32.const 51)
   390  )
   391  (assert_return
   392    (invoke "break-br_table-nested-num-num" (i32.const 2))
   393    (i32.const 101) (i32.const 52)
   394  )
   395  (assert_return
   396    (invoke "break-br_table-nested-num-num" (i32.const -3))
   397    (i32.const 101) (i32.const 52)
   398  )
   399  
   400  (assert_return
   401    (invoke "large-sig"
   402      (i32.const 0) (i64.const 1) (f32.const 2) (f32.const 3)
   403      (i32.const 4) (f64.const 5) (f32.const 6) (i32.const 7)
   404      (i32.const 8) (i32.const 9) (f32.const 10) (f64.const 11)
   405      (f64.const 12) (f64.const 13) (i32.const 14) (i32.const 15)
   406      (f32.const 16)
   407    )
   408    (f64.const 5) (f32.const 2) (i32.const 0) (i32.const 8)
   409    (i32.const 7) (i64.const 1) (f32.const 3) (i32.const 9)
   410    (i32.const 4) (f32.const 6) (f64.const 13) (f64.const 11)
   411    (i32.const 15) (f32.const 16) (i32.const 14) (f64.const 12)
   412  )
   413  
   414  (assert_return (invoke "init-local-i32") (i32.const 0))
   415  (assert_return (invoke "init-local-i64") (i64.const 0))
   416  (assert_return (invoke "init-local-f32") (f32.const 0))
   417  (assert_return (invoke "init-local-f64") (f64.const 0))
   418  
   419  
   420  ;; Expansion of inline function types
   421  
   422  (module
   423    (func $f (result f64) (f64.const 0))  ;; adds implicit type definition
   424    (func $g (param i32))                 ;; reuses explicit type definition
   425    (type $t (func (param i32)))
   426  
   427    (func $i32->void (type 0))                ;; (param i32)
   428    (func $void->f64 (type 1) (f64.const 0))  ;; (result f64)
   429    (func $check
   430      (call $i32->void (i32.const 0))
   431      (drop (call $void->f64))
   432    )
   433  )
   434  
   435  (assert_invalid
   436    (module
   437      (func $f (result f64) (f64.const 0))  ;; adds implicit type definition
   438      (func $g (param i32))                 ;; reuses explicit type definition
   439      (func $h (result f64) (f64.const 1))  ;; reuses implicit type definition
   440      (type $t (func (param i32)))
   441  
   442      (func (type 2))  ;; does not exist
   443    )
   444    "unknown type"
   445  )
   446  
   447  (assert_malformed
   448    (module quote
   449      "(func $f (result f64) (f64.const 0))"  ;; adds implicit type definition
   450      "(func $g (param i32))"                 ;; reuses explicit type definition
   451      "(func $h (result f64) (f64.const 1))"  ;; reuses implicit type definition
   452      "(type $t (func (param i32)))"
   453  
   454      "(func (type 2) (param i32))"  ;; does not exist
   455    )
   456    "unknown type"
   457  )
   458  
   459  (module
   460    (type $proc (func (result i32)))
   461    (type $sig (func (param i32) (result i32)))
   462  
   463    (func (export "f") (type $sig)
   464      (local $var i32)
   465      (local.get $var)
   466    )
   467  
   468    (func $g (type $sig)
   469      (local $var i32)
   470      (local.get $var)
   471    )
   472    (func (export "g") (type $sig)
   473      (call $g (local.get 0))
   474    )
   475  
   476    (func (export "p") (type $proc)
   477      (local $var i32)
   478      (local.set 0 (i32.const 42))
   479      (local.get $var)
   480    )
   481  )
   482  
   483  (assert_return (invoke "f" (i32.const 42)) (i32.const 0))
   484  (assert_return (invoke "g" (i32.const 42)) (i32.const 0))
   485  (assert_return (invoke "p") (i32.const 42))
   486  
   487  
   488  (module
   489    (type $sig (func))
   490  
   491    (func $empty-sig-1)  ;; should be assigned type $sig
   492    (func $complex-sig-1 (param f64 i64 f64 i64 f64 i64 f32 i32))
   493    (func $empty-sig-2)  ;; should be assigned type $sig
   494    (func $complex-sig-2 (param f64 i64 f64 i64 f64 i64 f32 i32))
   495    (func $complex-sig-3 (param f64 i64 f64 i64 f64 i64 f32 i32))
   496    (func $complex-sig-4 (param i64 i64 f64 i64 f64 i64 f32 i32))
   497    (func $complex-sig-5 (param i64 i64 f64 i64 f64 i64 f32 i32))
   498  
   499    (type $empty-sig-duplicate (func))
   500    (type $complex-sig-duplicate (func (param i64 i64 f64 i64 f64 i64 f32 i32)))
   501    (table funcref
   502      (elem
   503        $complex-sig-3 $empty-sig-2 $complex-sig-1 $complex-sig-3 $empty-sig-1
   504        $complex-sig-4 $complex-sig-5
   505      )
   506    )
   507  
   508    (func (export "signature-explicit-reused")
   509      (call_indirect (type $sig) (i32.const 1))
   510      (call_indirect (type $sig) (i32.const 4))
   511    )
   512  
   513    (func (export "signature-implicit-reused")
   514      ;; The implicit index 3 in this test depends on the function and
   515      ;; type definitions, and may need adapting if they change.
   516      (call_indirect (type 3)
   517        (f64.const 0) (i64.const 0) (f64.const 0) (i64.const 0)
   518        (f64.const 0) (i64.const 0) (f32.const 0) (i32.const 0)
   519        (i32.const 0)
   520      )
   521      (call_indirect (type 3)
   522        (f64.const 0) (i64.const 0) (f64.const 0) (i64.const 0)
   523        (f64.const 0) (i64.const 0) (f32.const 0) (i32.const 0)
   524        (i32.const 2)
   525      )
   526      (call_indirect (type 3)
   527        (f64.const 0) (i64.const 0) (f64.const 0) (i64.const 0)
   528        (f64.const 0) (i64.const 0) (f32.const 0) (i32.const 0)
   529        (i32.const 3)
   530      )
   531    )
   532  
   533    (func (export "signature-explicit-duplicate")
   534      (call_indirect (type $empty-sig-duplicate) (i32.const 1))
   535    )
   536  
   537    (func (export "signature-implicit-duplicate")
   538      (call_indirect (type $complex-sig-duplicate)
   539        (i64.const 0) (i64.const 0) (f64.const 0) (i64.const 0)
   540        (f64.const 0) (i64.const 0) (f32.const 0) (i32.const 0)
   541        (i32.const 5)
   542      )
   543      (call_indirect (type $complex-sig-duplicate)
   544        (i64.const 0) (i64.const 0) (f64.const 0) (i64.const 0)
   545        (f64.const 0) (i64.const 0) (f32.const 0) (i32.const 0)
   546        (i32.const 6)
   547      )
   548    )
   549  )
   550  
   551  (assert_return (invoke "signature-explicit-reused"))
   552  (assert_return (invoke "signature-implicit-reused"))
   553  (assert_return (invoke "signature-explicit-duplicate"))
   554  (assert_return (invoke "signature-implicit-duplicate"))
   555  
   556  
   557  ;; Malformed type use
   558  
   559  (assert_malformed
   560    (module quote
   561      "(type $sig (func (param i32) (result i32)))"
   562      "(func (type $sig) (result i32) (param i32) (i32.const 0))"
   563    )
   564    "unexpected token"
   565  )
   566  (assert_malformed
   567    (module quote
   568      "(type $sig (func (param i32) (result i32)))"
   569      "(func (param i32) (type $sig) (result i32) (i32.const 0))"
   570    )
   571    "unexpected token"
   572  )
   573  (assert_malformed
   574    (module quote
   575      "(type $sig (func (param i32) (result i32)))"
   576      "(func (param i32) (result i32) (type $sig) (i32.const 0))"
   577    )
   578    "unexpected token"
   579  )
   580  (assert_malformed
   581    (module quote
   582      "(type $sig (func (param i32) (result i32)))"
   583      "(func (result i32) (type $sig) (param i32) (i32.const 0))"
   584    )
   585    "unexpected token"
   586  )
   587  (assert_malformed
   588    (module quote
   589      "(type $sig (func (param i32) (result i32)))"
   590      "(func (result i32) (param i32) (type $sig) (i32.const 0))"
   591    )
   592    "unexpected token"
   593  )
   594  (assert_malformed
   595    (module quote
   596      "(func (result i32) (param i32) (i32.const 0))"
   597    )
   598    "unexpected token"
   599  )
   600  
   601  (assert_malformed
   602    (module quote
   603      "(type $sig (func))"
   604      "(func (type $sig) (result i32) (i32.const 0))"
   605    )
   606    "inline function type"
   607  )
   608  (assert_malformed
   609    (module quote
   610      "(type $sig (func (param i32) (result i32)))"
   611      "(func (type $sig) (result i32) (i32.const 0))"
   612    )
   613    "inline function type"
   614  )
   615  (assert_malformed
   616    (module quote
   617      "(type $sig (func (param i32) (result i32)))"
   618      "(func (type $sig) (param i32) (i32.const 0))"
   619    )
   620    "inline function type"
   621  )
   622  (assert_malformed
   623    (module quote
   624      "(type $sig (func (param i32 i32) (result i32)))"
   625      "(func (type $sig) (param i32) (result i32) (unreachable))"
   626    )
   627    "inline function type"
   628  )
   629  
   630  
   631  ;; Invalid typing of locals
   632  
   633  (assert_invalid
   634    (module (func $type-local-num-vs-num (result i64) (local i32) (local.get 0)))
   635    "type mismatch"
   636  )
   637  (assert_invalid
   638    (module (func $type-local-num-vs-num (local f32) (i32.eqz (local.get 0))))
   639    "type mismatch"
   640  )
   641  (assert_invalid
   642    (module (func $type-local-num-vs-num (local f64 i64) (f64.neg (local.get 1))))
   643    "type mismatch"
   644  )
   645  
   646  
   647  ;; Invalid typing of parameters
   648  
   649  (assert_invalid
   650    (module (func $type-param-num-vs-num (param i32) (result i64) (local.get 0)))
   651    "type mismatch"
   652  )
   653  (assert_invalid
   654    (module (func $type-param-num-vs-num (param f32) (i32.eqz (local.get 0))))
   655    "type mismatch"
   656  )
   657  (assert_invalid
   658    (module (func $type-param-num-vs-num (param f64 i64) (f64.neg (local.get 1))))
   659    "type mismatch"
   660  )
   661  
   662  
   663  ;; Invalid typing of result
   664  
   665  (assert_invalid
   666    (module (func $type-empty-i32 (result i32)))
   667    "type mismatch"
   668  )
   669  (assert_invalid
   670    (module (func $type-empty-i64 (result i64)))
   671    "type mismatch"
   672  )
   673  (assert_invalid
   674    (module (func $type-empty-f32 (result f32)))
   675    "type mismatch"
   676  )
   677  (assert_invalid
   678    (module (func $type-empty-f64 (result f64)))
   679    "type mismatch"
   680  )
   681  (assert_invalid
   682    (module (func $type-empty-f64-i32 (result f64 i32)))
   683    "type mismatch"
   684  )
   685  
   686  (assert_invalid
   687    (module (func $type-value-void-vs-num (result i32)
   688      (nop)
   689    ))
   690    "type mismatch"
   691  )
   692  (assert_invalid
   693    (module (func $type-value-void-vs-nums (result i32 i32)
   694      (nop)
   695    ))
   696    "type mismatch"
   697  )
   698  (assert_invalid
   699    (module (func $type-value-num-vs-void
   700      (i32.const 0)
   701    ))
   702    "type mismatch"
   703  )
   704  (assert_invalid
   705    (module (func $type-value-nums-vs-void
   706      (i32.const 0) (i64.const 0)
   707    ))
   708    "type mismatch"
   709  )
   710  (assert_invalid
   711    (module (func $type-value-num-vs-num (result i32)
   712      (f32.const 0)
   713    ))
   714    "type mismatch"
   715  )
   716  (assert_invalid
   717    (module (func $type-value-num-vs-nums (result f32 f32)
   718      (f32.const 0)
   719    ))
   720    "type mismatch"
   721  )
   722  (assert_invalid
   723    (module (func $type-value-nums-vs-num (result f32)
   724      (f32.const 0) (f32.const 0)
   725    ))
   726    "type mismatch"
   727  )
   728  
   729  (assert_invalid
   730    (module (func $type-return-last-empty-vs-num (result i32)
   731      (return)
   732    ))
   733    "type mismatch"
   734  )
   735  (assert_invalid
   736    (module (func $type-return-last-empty-vs-nums (result i32 i32)
   737      (return)
   738    ))
   739    "type mismatch"
   740  )
   741  (assert_invalid
   742    (module (func $type-return-last-void-vs-num (result i32)
   743      (return (nop))
   744    ))
   745    "type mismatch"
   746  )
   747  (assert_invalid
   748    (module (func $type-return-last-void-vs-nums (result i32 i64)
   749      (return (nop))
   750    ))
   751    "type mismatch"
   752  )
   753  (assert_invalid
   754    (module (func $type-return-last-num-vs-num (result i32)
   755      (return (i64.const 0))
   756    ))
   757    "type mismatch"
   758  )
   759  (assert_invalid
   760    (module (func $type-return-last-num-vs-nums (result i64 i64)
   761      (return (i64.const 0))
   762    ))
   763    "type mismatch"
   764  )
   765  
   766  (assert_invalid
   767    (module (func $type-return-empty-vs-num (result i32)
   768      (return) (i32.const 1)
   769    ))
   770    "type mismatch"
   771  )
   772  (assert_invalid
   773    (module (func $type-return-empty-vs-nums (result i32 i32)
   774      (return) (i32.const 1) (i32.const 2)
   775    ))
   776    "type mismatch"
   777  )
   778  (assert_invalid
   779    (module (func $type-return-partial-vs-nums (result i32 i32)
   780      (i32.const 1) (return) (i32.const 2)
   781    ))
   782    "type mismatch"
   783  )
   784  (assert_invalid
   785    (module (func $type-return-void-vs-num (result i32)
   786      (return (nop)) (i32.const 1)
   787    ))
   788    "type mismatch"
   789  )
   790  (assert_invalid
   791    (module (func $type-return-void-vs-nums (result i32 i32)
   792      (return (nop)) (i32.const 1)
   793    ))
   794    "type mismatch"
   795  )
   796  (assert_invalid
   797    (module (func $type-return-num-vs-num (result i32)
   798      (return (i64.const 1)) (i32.const 1)
   799    ))
   800    "type mismatch"
   801  )
   802  (assert_invalid
   803    (module (func $type-return-num-vs-nums (result i32 i32)
   804      (return (i64.const 1)) (i32.const 1) (i32.const 2)
   805    ))
   806    "type mismatch"
   807  )
   808  (assert_invalid
   809    (module (func $type-return-first-num-vs-num (result i32)
   810      (return (i64.const 1)) (return (i32.const 1))
   811    ))
   812    "type mismatch"
   813  )
   814  (assert_invalid
   815    (module (func $type-return-first-num-vs-nums (result i32 i32)
   816      (return (i32.const 1)) (return (i32.const 1) (i32.const 2))
   817    ))
   818    "type mismatch"
   819  )
   820  
   821  (assert_invalid
   822    (module (func $type-break-last-void-vs-num (result i32)
   823      (br 0)
   824    ))
   825    "type mismatch"
   826  )
   827  (assert_invalid
   828    (module (func $type-break-last-void-vs-nums (result i32 i32)
   829      (br 0)
   830    ))
   831    "type mismatch"
   832  )
   833  (assert_invalid
   834    (module (func $type-break-last-num-vs-num (result i32)
   835      (br 0 (f32.const 0))
   836    ))
   837    "type mismatch"
   838  )
   839  (assert_invalid
   840    (module (func $type-break-last-num-vs-nums (result i32 i32)
   841      (br 0 (i32.const 0))
   842    ))
   843    "type mismatch"
   844  )
   845  (assert_invalid
   846    (module (func $type-break-void-vs-num (result i32)
   847      (br 0) (i32.const 1)
   848    ))
   849    "type mismatch"
   850  )
   851  (assert_invalid
   852    (module (func $type-break-void-vs-nums (result i32 i32)
   853      (br 0) (i32.const 1) (i32.const 2)
   854    ))
   855    "type mismatch"
   856  )
   857  (assert_invalid
   858    (module (func $type-break-num-vs-num (result i32)
   859      (br 0 (i64.const 1)) (i32.const 1)
   860    ))
   861    "type mismatch"
   862  )
   863  (assert_invalid
   864    (module (func $type-break-num-vs-nums (result i32 i32)
   865      (br 0 (i32.const 1)) (i32.const 1) (i32.const 2)
   866    ))
   867    "type mismatch"
   868  )
   869  (assert_invalid
   870    (module (func $type-break-first-num-vs-num (result i32)
   871      (br 0 (i64.const 1)) (br 0 (i32.const 1))
   872    ))
   873    "type mismatch"
   874  )
   875  
   876  (assert_invalid
   877    (module (func $type-break-nested-empty-vs-num (result i32)
   878      (block (br 1)) (br 0 (i32.const 1))
   879    ))
   880    "type mismatch"
   881  )
   882  (assert_invalid
   883    (module (func $type-break-nested-empty-vs-nums (result i32 i32)
   884      (block (br 1)) (br 0 (i32.const 1) (i32.const 2))
   885    ))
   886    "type mismatch"
   887  )
   888  (assert_invalid
   889    (module (func $type-break-nested-void-vs-num (result i32)
   890      (block (br 1 (nop))) (br 0 (i32.const 1))
   891    ))
   892    "type mismatch"
   893  )
   894  (assert_invalid
   895    (module (func $type-break-nested-void-vs-nums (result i32 i32)
   896      (block (br 1 (nop))) (br 0 (i32.const 1) (i32.const 2))
   897    ))
   898    "type mismatch"
   899  )
   900  (assert_invalid
   901    (module (func $type-break-nested-num-vs-num (result i32)
   902      (block (br 1 (i64.const 1))) (br 0 (i32.const 1))
   903    ))
   904    "type mismatch"
   905  )
   906  (assert_invalid
   907    (module (func $type-break-nested-num-vs-nums (result i32 i32)
   908      (block (result i32) (br 1 (i32.const 1))) (br 0 (i32.const 1) (i32.const 2))
   909    ))
   910    "type mismatch"
   911  )
   912  
   913  
   914  ;; Syntax errors
   915  
   916  (assert_malformed
   917    (module quote "(func (nop) (local i32))")
   918    "unexpected token"
   919  )
   920  (assert_malformed
   921    (module quote "(func (nop) (param i32))")
   922    "unexpected token"
   923  )
   924  (assert_malformed
   925    (module quote "(func (nop) (result i32))")
   926    "unexpected token"
   927  )
   928  (assert_malformed
   929    (module quote "(func (local i32) (param i32))")
   930    "unexpected token"
   931  )
   932  (assert_malformed
   933    (module quote "(func (local i32) (result i32) (local.get 0))")
   934    "unexpected token"
   935  )
   936  (assert_malformed
   937    (module quote "(func (result i32) (param i32) (local.get 0))")
   938    "unexpected token"
   939  )
   940  
   941  ;; Duplicate name errors
   942  
   943  (assert_malformed (module quote
   944    "(func $foo)"
   945    "(func $foo)")
   946    "duplicate func")
   947  (assert_malformed (module quote
   948    "(import \"\" \"\" (func $foo))"
   949    "(func $foo)")
   950    "duplicate func")
   951  (assert_malformed (module quote
   952    "(import \"\" \"\" (func $foo))"
   953    "(import \"\" \"\" (func $foo))")
   954    "duplicate func")
   955  
   956  (assert_malformed (module quote "(func (param $foo i32) (param $foo i32))")
   957    "duplicate local")
   958  (assert_malformed (module quote "(func (param $foo i32) (local $foo i32))")
   959    "duplicate local")
   960  (assert_malformed (module quote "(func (local $foo i32) (local $foo i32))")
   961    "duplicate local")