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

     1  ;; Auxiliary module to import from
     2  
     3  (module
     4    (func (export "func"))
     5    (func (export "func-i32") (param i32))
     6    (func (export "func-f32") (param f32))
     7    (func (export "func->i32") (result i32) (i32.const 22))
     8    (func (export "func->f32") (result f32) (f32.const 11))
     9    (func (export "func-i32->i32") (param i32) (result i32) (local.get 0))
    10    (func (export "func-i64->i64") (param i64) (result i64) (local.get 0))
    11    (global (export "global-i32") i32 (i32.const 55))
    12    (global (export "global-f32") f32 (f32.const 44))
    13    (table (export "table-10-inf") 10 funcref)
    14    ;; (table (export "table-10-20") 10 20 funcref)
    15    (memory (export "memory-2-inf") 2)
    16    ;; (memory (export "memory-2-4") 2 4)
    17  )
    18  
    19  (register "test")
    20  
    21  
    22  ;; Functions
    23  
    24  (module
    25    (type $func_i32 (func (param i32)))
    26    (type $func_i64 (func (param i64)))
    27    (type $func_f32 (func (param f32)))
    28    (type $func_f64 (func (param f64)))
    29  
    30    (import "spectest" "print_i32" (func (param i32)))
    31    ;; JavaScript can't handle i64 yet.
    32    ;; (func (import "spectest" "print_i64") (param i64))
    33    (import "spectest" "print_i32" (func $print_i32 (param i32)))
    34    ;; JavaScript can't handle i64 yet.
    35    ;; (import "spectest" "print_i64" (func $print_i64 (param i64)))
    36    (import "spectest" "print_f32" (func $print_f32 (param f32)))
    37    (import "spectest" "print_f64" (func $print_f64 (param f64)))
    38    (import "spectest" "print_i32_f32" (func $print_i32_f32 (param i32 f32)))
    39    (import "spectest" "print_f64_f64" (func $print_f64_f64 (param f64 f64)))
    40    (func $print_i32-2 (import "spectest" "print_i32") (param i32))
    41    (func $print_f64-2 (import "spectest" "print_f64") (param f64))
    42    (import "test" "func-i64->i64" (func $i64->i64 (param i64) (result i64)))
    43  
    44    (func (export "p1") (import "spectest" "print_i32") (param i32))
    45    (func $p (export "p2") (import "spectest" "print_i32") (param i32))
    46    (func (export "p3") (export "p4") (import "spectest" "print_i32") (param i32))
    47    (func (export "p5") (import "spectest" "print_i32") (type 0))
    48    (func (export "p6") (import "spectest" "print_i32") (type 0) (param i32) (result))
    49  
    50    (import "spectest" "print_i32" (func (type $forward)))
    51    (func (import "spectest" "print_i32") (type $forward))
    52    (type $forward (func (param i32)))
    53  
    54    (table funcref (elem $print_i32 $print_f64))
    55  
    56    (func (export "print32") (param $i i32)
    57      (local $x f32)
    58      (local.set $x (f32.convert_i32_s (local.get $i)))
    59      (call 0 (local.get $i))
    60      (call $print_i32_f32
    61        (i32.add (local.get $i) (i32.const 1))
    62        (f32.const 42)
    63      )
    64      (call $print_i32 (local.get $i))
    65      (call $print_i32-2 (local.get $i))
    66      (call $print_f32 (local.get $x))
    67      (call_indirect (type $func_i32) (local.get $i) (i32.const 0))
    68    )
    69  
    70    (func (export "print64") (param $i i64)
    71      (local $x f64)
    72      (local.set $x (f64.convert_i64_s (call $i64->i64 (local.get $i))))
    73      ;; JavaScript can't handle i64 yet.
    74      ;; (call 1 (local.get $i))
    75      (call $print_f64_f64
    76        (f64.add (local.get $x) (f64.const 1))
    77        (f64.const 53)
    78      )
    79      ;; JavaScript can't handle i64 yet.
    80      ;; (call $print_i64 (local.get $i))
    81      (call $print_f64 (local.get $x))
    82      (call $print_f64-2 (local.get $x))
    83      (call_indirect (type $func_f64) (local.get $x) (i32.const 1))
    84    )
    85  )
    86  
    87  (assert_return (invoke "print32" (i32.const 13)))
    88  (assert_return (invoke "print64" (i64.const 24)))
    89  
    90  (assert_invalid
    91    (module 
    92      (type (func (result i32)))
    93      (import "test" "func" (func (type 1)))
    94    )
    95    "unknown type"
    96  )
    97  
    98  (module (import "test" "func" (func)))
    99  (module (import "test" "func-i32" (func (param i32))))
   100  (module (import "test" "func-f32" (func (param f32))))
   101  (module (import "test" "func->i32" (func (result i32))))
   102  (module (import "test" "func->f32" (func (result f32))))
   103  (module (import "test" "func-i32->i32" (func (param i32) (result i32))))
   104  (module (import "test" "func-i64->i64" (func (param i64) (result i64))))
   105  
   106  (assert_unlinkable
   107    (module (import "test" "unknown" (func)))
   108    "unknown import"
   109  )
   110  (assert_unlinkable
   111    (module (import "spectest" "unknown" (func)))
   112    "unknown import"
   113  )
   114  
   115  (assert_unlinkable
   116    (module (import "test" "func" (func (param i32))))
   117    "incompatible import type"
   118  )
   119  (assert_unlinkable
   120    (module (import "test" "func" (func (result i32))))
   121    "incompatible import type"
   122  )
   123  (assert_unlinkable
   124    (module (import "test" "func" (func (param i32) (result i32))))
   125    "incompatible import type"
   126  )
   127  (assert_unlinkable
   128    (module (import "test" "func-i32" (func)))
   129    "incompatible import type"
   130  )
   131  (assert_unlinkable
   132    (module (import "test" "func-i32" (func (result i32))))
   133    "incompatible import type"
   134  )
   135  (assert_unlinkable
   136    (module (import "test" "func-i32" (func (param f32))))
   137    "incompatible import type"
   138  )
   139  (assert_unlinkable
   140    (module (import "test" "func-i32" (func (param i64))))
   141    "incompatible import type"
   142  )
   143  (assert_unlinkable
   144    (module (import "test" "func-i32" (func (param i32) (result i32))))
   145    "incompatible import type"
   146  )
   147  (assert_unlinkable
   148    (module (import "test" "func->i32" (func)))
   149    "incompatible import type"
   150  )
   151  (assert_unlinkable
   152    (module (import "test" "func->i32" (func (param i32))))
   153    "incompatible import type"
   154  )
   155  (assert_unlinkable
   156    (module (import "test" "func->i32" (func (result f32))))
   157    "incompatible import type"
   158  )
   159  (assert_unlinkable
   160    (module (import "test" "func->i32" (func (result i64))))
   161    "incompatible import type"
   162  )
   163  (assert_unlinkable
   164    (module (import "test" "func->i32" (func (param i32) (result i32))))
   165    "incompatible import type"
   166  )
   167  (assert_unlinkable
   168    (module (import "test" "func-i32->i32" (func)))
   169    "incompatible import type"
   170  )
   171  (assert_unlinkable
   172    (module (import "test" "func-i32->i32" (func (param i32))))
   173    "incompatible import type"
   174  )
   175  (assert_unlinkable
   176    (module (import "test" "func-i32->i32" (func (result i32))))
   177    "incompatible import type"
   178  )
   179  
   180  (assert_unlinkable
   181    (module (import "test" "global-i32" (func (result i32))))
   182    "incompatible import type"
   183  )
   184  (assert_unlinkable
   185    (module (import "test" "table-10-inf" (func)))
   186    "incompatible import type"
   187  )
   188  (assert_unlinkable
   189    (module (import "test" "memory-2-inf" (func)))
   190    "incompatible import type"
   191  )
   192  (assert_unlinkable
   193    (module (import "spectest" "global_i32" (func)))
   194    "incompatible import type"
   195  )
   196  (assert_unlinkable
   197    (module (import "spectest" "table" (func)))
   198    "incompatible import type"
   199  )
   200  (assert_unlinkable
   201    (module (import "spectest" "memory" (func)))
   202    "incompatible import type"
   203  )
   204  
   205  
   206  ;; Globals
   207  
   208  (module
   209    (import "spectest" "global_i32" (global i32))
   210    (global (import "spectest" "global_i32") i32)
   211  
   212    (import "spectest" "global_i32" (global $x i32))
   213    (global $y (import "spectest" "global_i32") i32)
   214  
   215    ;; JavaScript can't handle i64 yet.
   216    ;; (import "spectest" "global_i64" (global i64))
   217    (import "spectest" "global_f32" (global f32))
   218    (import "spectest" "global_f64" (global f64))
   219  
   220    (func (export "get-0") (result i32) (global.get 0))
   221    (func (export "get-1") (result i32) (global.get 1))
   222    (func (export "get-x") (result i32) (global.get $x))
   223    (func (export "get-y") (result i32) (global.get $y))
   224  )
   225  
   226  (assert_return (invoke "get-0") (i32.const 666))
   227  (assert_return (invoke "get-1") (i32.const 666))
   228  (assert_return (invoke "get-x") (i32.const 666))
   229  (assert_return (invoke "get-y") (i32.const 666))
   230  
   231  (module (import "test" "global-i32" (global i32)))
   232  (module (import "test" "global-f32" (global f32)))
   233  
   234  (assert_unlinkable
   235    (module (import "test" "unknown" (global i32)))
   236    "unknown import"
   237  )
   238  (assert_unlinkable
   239    (module (import "spectest" "unknown" (global i32)))
   240    "unknown import"
   241  )
   242  
   243  (assert_unlinkable
   244    (module (import "test" "func" (global i32)))
   245    "incompatible import type"
   246  )
   247  (assert_unlinkable
   248    (module (import "test" "table-10-inf" (global i32)))
   249    "incompatible import type"
   250  )
   251  (assert_unlinkable
   252    (module (import "test" "memory-2-inf" (global i32)))
   253    "incompatible import type"
   254  )
   255  (assert_unlinkable
   256    (module (import "spectest" "print_i32" (global i32)))
   257    "incompatible import type"
   258  )
   259  (assert_unlinkable
   260    (module (import "spectest" "table" (global i32)))
   261    "incompatible import type"
   262  )
   263  (assert_unlinkable
   264    (module (import "spectest" "memory" (global i32)))
   265    "incompatible import type"
   266  )
   267  
   268  
   269  ;; Tables
   270  
   271  (module
   272    (type (func (result i32)))
   273    (import "spectest" "table" (table 10 20 funcref))
   274    (elem 0 (i32.const 1) $f $g)
   275  
   276    (func (export "call") (param i32) (result i32)
   277      (call_indirect (type 0) (local.get 0))
   278    )
   279    (func $f (result i32) (i32.const 11))
   280    (func $g (result i32) (i32.const 22))
   281  )
   282  
   283  (assert_trap (invoke "call" (i32.const 0)) "uninitialized element")
   284  (assert_return (invoke "call" (i32.const 1)) (i32.const 11))
   285  (assert_return (invoke "call" (i32.const 2)) (i32.const 22))
   286  (assert_trap (invoke "call" (i32.const 3)) "uninitialized element")
   287  (assert_trap (invoke "call" (i32.const 100)) "undefined element")
   288  
   289  
   290  (module
   291    (type (func (result i32)))
   292    (table (import "spectest" "table") 10 20 funcref)
   293    (elem 0 (i32.const 1) $f $g)
   294  
   295    (func (export "call") (param i32) (result i32)
   296      (call_indirect (type 0) (local.get 0))
   297    )
   298    (func $f (result i32) (i32.const 11))
   299    (func $g (result i32) (i32.const 22))
   300  )
   301  
   302  (assert_trap (invoke "call" (i32.const 0)) "uninitialized element")
   303  (assert_return (invoke "call" (i32.const 1)) (i32.const 11))
   304  (assert_return (invoke "call" (i32.const 2)) (i32.const 22))
   305  (assert_trap (invoke "call" (i32.const 3)) "uninitialized element")
   306  (assert_trap (invoke "call" (i32.const 100)) "undefined element")
   307  
   308  
   309  (assert_invalid
   310    (module (import "" "" (table 10 funcref)) (import "" "" (table 10 funcref)))
   311    "multiple tables"
   312  )
   313  (assert_invalid
   314    (module (import "" "" (table 10 funcref)) (table 10 funcref))
   315    "multiple tables"
   316  )
   317  (assert_invalid
   318    (module (table 10 funcref) (table 10 funcref))
   319    "multiple tables"
   320  )
   321  
   322  (module (import "test" "table-10-inf" (table 10 funcref)))
   323  (module (import "test" "table-10-inf" (table 5 funcref)))
   324  (module (import "test" "table-10-inf" (table 0 funcref)))
   325  (module (import "spectest" "table" (table 10 funcref)))
   326  (module (import "spectest" "table" (table 5 funcref)))
   327  (module (import "spectest" "table" (table 0 funcref)))
   328  (module (import "spectest" "table" (table 10 20 funcref)))
   329  (module (import "spectest" "table" (table 5 20 funcref)))
   330  (module (import "spectest" "table" (table 0 20 funcref)))
   331  (module (import "spectest" "table" (table 10 25 funcref)))
   332  (module (import "spectest" "table" (table 5 25 funcref)))
   333  
   334  (assert_unlinkable
   335    (module (import "test" "unknown" (table 10 funcref)))
   336    "unknown import"
   337  )
   338  (assert_unlinkable
   339    (module (import "spectest" "unknown" (table 10 funcref)))
   340    "unknown import"
   341  )
   342  
   343  (assert_unlinkable
   344    (module (import "test" "table-10-inf" (table 12 funcref)))
   345    "incompatible import type"
   346  )
   347  (assert_unlinkable
   348    (module (import "test" "table-10-inf" (table 10 20 funcref)))
   349    "incompatible import type"
   350  )
   351  (assert_unlinkable
   352    (module (import "spectest" "table" (table 12 funcref)))
   353    "incompatible import type"
   354  )
   355  (assert_unlinkable
   356    (module (import "spectest" "table" (table 10 15 funcref)))
   357    "incompatible import type"
   358  )
   359  
   360  (assert_unlinkable
   361    (module (import "test" "func" (table 10 funcref)))
   362    "incompatible import type"
   363  )
   364  (assert_unlinkable
   365    (module (import "test" "global-i32" (table 10 funcref)))
   366    "incompatible import type"
   367  )
   368  (assert_unlinkable
   369    (module (import "test" "memory-2-inf" (table 10 funcref)))
   370    "incompatible import type"
   371  )
   372  (assert_unlinkable
   373    (module (import "spectest" "print_i32" (table 10 funcref)))
   374    "incompatible import type"
   375  )
   376  
   377  
   378  
   379  ;; Memories
   380  
   381  (module
   382    (import "spectest" "memory" (memory 1 2))
   383    (data 0 (i32.const 10) "\10")
   384  
   385    (func (export "load") (param i32) (result i32) (i32.load (local.get 0)))
   386  )
   387  
   388  (assert_return (invoke "load" (i32.const 0)) (i32.const 0))
   389  (assert_return (invoke "load" (i32.const 10)) (i32.const 16))
   390  (assert_return (invoke "load" (i32.const 8)) (i32.const 0x100000))
   391  (assert_trap (invoke "load" (i32.const 1000000)) "out of bounds memory access")
   392  
   393  (module
   394    (memory (import "spectest" "memory") 1 2)
   395    (data 0 (i32.const 10) "\10")
   396  
   397    (func (export "load") (param i32) (result i32) (i32.load (local.get 0)))
   398  )
   399  (assert_return (invoke "load" (i32.const 0)) (i32.const 0))
   400  (assert_return (invoke "load" (i32.const 10)) (i32.const 16))
   401  (assert_return (invoke "load" (i32.const 8)) (i32.const 0x100000))
   402  (assert_trap (invoke "load" (i32.const 1000000)) "out of bounds memory access")
   403  
   404  (assert_invalid
   405    (module (import "" "" (memory 1)) (import "" "" (memory 1)))
   406    "multiple memories"
   407  )
   408  (assert_invalid
   409    (module (import "" "" (memory 1)) (memory 0))
   410    "multiple memories"
   411  )
   412  (assert_invalid
   413    (module (memory 0) (memory 0))
   414    "multiple memories"
   415  )
   416  
   417  (module (import "test" "memory-2-inf" (memory 2)))
   418  (module (import "test" "memory-2-inf" (memory 1)))
   419  (module (import "test" "memory-2-inf" (memory 0)))
   420  (module (import "spectest" "memory" (memory 1)))
   421  (module (import "spectest" "memory" (memory 0)))
   422  (module (import "spectest" "memory" (memory 1 2)))
   423  (module (import "spectest" "memory" (memory 0 2)))
   424  (module (import "spectest" "memory" (memory 1 3)))
   425  (module (import "spectest" "memory" (memory 0 3)))
   426  
   427  (assert_unlinkable
   428    (module (import "test" "unknown" (memory 1)))
   429    "unknown import"
   430  )
   431  (assert_unlinkable
   432    (module (import "spectest" "unknown" (memory 1)))
   433    "unknown import"
   434  )
   435  
   436  (assert_unlinkable
   437    (module (import "test" "memory-2-inf" (memory 3)))
   438    "incompatible import type"
   439  )
   440  (assert_unlinkable
   441    (module (import "test" "memory-2-inf" (memory 2 3)))
   442    "incompatible import type"
   443  )
   444  (assert_unlinkable
   445    (module (import "spectest" "memory" (memory 2)))
   446    "incompatible import type"
   447  )
   448  (assert_unlinkable
   449    (module (import "spectest" "memory" (memory 1 1)))
   450    "incompatible import type"
   451  )
   452  
   453  (assert_unlinkable
   454    (module (import "test" "func-i32" (memory 1)))
   455    "incompatible import type"
   456  )
   457  (assert_unlinkable
   458    (module (import "test" "global-i32" (memory 1)))
   459    "incompatible import type"
   460  )
   461  (assert_unlinkable
   462    (module (import "test" "table-10-inf" (memory 1)))
   463    "incompatible import type"
   464  )
   465  (assert_unlinkable
   466    (module (import "spectest" "print_i32" (memory 1)))
   467    "incompatible import type"
   468  )
   469  (assert_unlinkable
   470    (module (import "spectest" "global_i32" (memory 1)))
   471    "incompatible import type"
   472  )
   473  (assert_unlinkable
   474    (module (import "spectest" "table" (memory 1)))
   475    "incompatible import type"
   476  )
   477  
   478  (assert_unlinkable
   479    (module (import "spectest" "memory" (memory 2)))
   480    "incompatible import type"
   481  )
   482  (assert_unlinkable
   483    (module (import "spectest" "memory" (memory 1 1)))
   484    "incompatible import type"
   485  )
   486  
   487  (module
   488    (import "spectest" "memory" (memory 0 3))  ;; actual has max size 2
   489    (func (export "grow") (param i32) (result i32) (memory.grow (local.get 0)))
   490  )
   491  (assert_return (invoke "grow" (i32.const 0)) (i32.const 1))
   492  (assert_return (invoke "grow" (i32.const 1)) (i32.const 1))
   493  (assert_return (invoke "grow" (i32.const 0)) (i32.const 2))
   494  (assert_return (invoke "grow" (i32.const 1)) (i32.const -1))
   495  (assert_return (invoke "grow" (i32.const 0)) (i32.const 2))
   496  
   497  
   498  ;; Syntax errors
   499  
   500  (assert_malformed
   501    (module quote "(func) (import \"\" \"\" (func))")
   502    "import after function"
   503  )
   504  (assert_malformed
   505    (module quote "(func) (import \"\" \"\" (global i64))")
   506    "import after function"
   507  )
   508  (assert_malformed
   509    (module quote "(func) (import \"\" \"\" (table 0 funcref))")
   510    "import after function"
   511  )
   512  (assert_malformed
   513    (module quote "(func) (import \"\" \"\" (memory 0))")
   514    "import after function"
   515  )
   516  
   517  (assert_malformed
   518    (module quote "(global i64 (i64.const 0)) (import \"\" \"\" (func))")
   519    "import after global"
   520  )
   521  (assert_malformed
   522    (module quote "(global i64 (i64.const 0)) (import \"\" \"\" (global f32))")
   523    "import after global"
   524  )
   525  (assert_malformed
   526    (module quote "(global i64 (i64.const 0)) (import \"\" \"\" (table 0 funcref))")
   527    "import after global"
   528  )
   529  (assert_malformed
   530    (module quote "(global i64 (i64.const 0)) (import \"\" \"\" (memory 0))")
   531    "import after global"
   532  )
   533  
   534  (assert_malformed
   535    (module quote "(table 0 funcref) (import \"\" \"\" (func))")
   536    "import after table"
   537  )
   538  (assert_malformed
   539    (module quote "(table 0 funcref) (import \"\" \"\" (global i32))")
   540    "import after table"
   541  )
   542  (assert_malformed
   543    (module quote "(table 0 funcref) (import \"\" \"\" (table 0 funcref))")
   544    "import after table"
   545  )
   546  (assert_malformed
   547    (module quote "(table 0 funcref) (import \"\" \"\" (memory 0))")
   548    "import after table"
   549  )
   550  
   551  (assert_malformed
   552    (module quote "(memory 0) (import \"\" \"\" (func))")
   553    "import after memory"
   554  )
   555  (assert_malformed
   556    (module quote "(memory 0) (import \"\" \"\" (global i32))")
   557    "import after memory"
   558  )
   559  (assert_malformed
   560    (module quote "(memory 0) (import \"\" \"\" (table 1 3 funcref))")
   561    "import after memory"
   562  )
   563  (assert_malformed
   564    (module quote "(memory 0) (import \"\" \"\" (memory 1 2))")
   565    "import after memory"
   566  )
   567  
   568  ;; This module is required to validate, regardless of whether it can be
   569  ;; linked. Overloading is not possible in wasm itself, but it is possible
   570  ;; in modules from which wasm can import.
   571  (module)
   572  (register "not wasm")
   573  (assert_unlinkable
   574    (module
   575      (import "not wasm" "overloaded" (func))
   576      (import "not wasm" "overloaded" (func (param i32)))
   577      (import "not wasm" "overloaded" (func (param i32 i32)))
   578      (import "not wasm" "overloaded" (func (param i64)))
   579      (import "not wasm" "overloaded" (func (param f32)))
   580      (import "not wasm" "overloaded" (func (param f64)))
   581      (import "not wasm" "overloaded" (func (result i32)))
   582      (import "not wasm" "overloaded" (func (result i64)))
   583      (import "not wasm" "overloaded" (func (result f32)))
   584      (import "not wasm" "overloaded" (func (result f64)))
   585      (import "not wasm" "overloaded" (global i32))
   586      (import "not wasm" "overloaded" (global i64))
   587      (import "not wasm" "overloaded" (global f32))
   588      (import "not wasm" "overloaded" (global f64))
   589      (import "not wasm" "overloaded" (table 0 funcref))
   590      (import "not wasm" "overloaded" (memory 0))
   591    )
   592    "unknown import"
   593  )