...

Text file src/github.com/tetratelabs/wazero/internal/integration_test/spectest/v1/testdata/globals.wast

Documentation: github.com/tetratelabs/wazero/internal/integration_test/spectest/v1/testdata

     1;; Test globals
     2
     3(module
     4  (global $a i32 (i32.const -2))
     5  (global (;1;) f32 (f32.const -3))
     6  (global (;2;) f64 (f64.const -4))
     7  (global $b i64 (i64.const -5))
     8
     9  (global $x (mut i32) (i32.const -12))
    10  (global (;5;) (mut f32) (f32.const -13))
    11  (global (;6;) (mut f64) (f64.const -14))
    12  (global $y (mut i64) (i64.const -15))
    13
    14  (func (export "get-a") (result i32) (global.get $a))
    15  (func (export "get-b") (result i64) (global.get $b))
    16  (func (export "get-x") (result i32) (global.get $x))
    17  (func (export "get-y") (result i64) (global.get $y))
    18  (func (export "set-x") (param i32) (global.set $x (local.get 0)))
    19  (func (export "set-y") (param i64) (global.set $y (local.get 0)))
    20
    21  (func (export "get-1") (result f32) (global.get 1))
    22  (func (export "get-2") (result f64) (global.get 2))
    23  (func (export "get-5") (result f32) (global.get 5))
    24  (func (export "get-6") (result f64) (global.get 6))
    25  (func (export "set-5") (param f32) (global.set 5 (local.get 0)))
    26  (func (export "set-6") (param f64) (global.set 6 (local.get 0)))
    27
    28  ;; As the argument of control constructs and instructions
    29
    30  (memory 1)
    31
    32  (func $dummy)
    33
    34  (func (export "as-select-first") (result i32)
    35    (select (global.get $x) (i32.const 2) (i32.const 3))
    36  )
    37  (func (export "as-select-mid") (result i32)
    38    (select (i32.const 2) (global.get $x) (i32.const 3))
    39  )
    40  (func (export "as-select-last") (result i32)
    41    (select (i32.const 2) (i32.const 3) (global.get $x))
    42  )
    43
    44  (func (export "as-loop-first") (result i32)
    45    (loop (result i32)
    46      (global.get $x) (call $dummy) (call $dummy)
    47    )
    48  )
    49  (func (export "as-loop-mid") (result i32)
    50    (loop (result i32)
    51      (call $dummy) (global.get $x) (call $dummy)
    52    )
    53  )
    54  (func (export "as-loop-last") (result i32)
    55    (loop (result i32)
    56      (call $dummy) (call $dummy) (global.get $x)
    57    )
    58  )
    59
    60  (func (export "as-if-condition") (result i32)
    61    (if (result i32) (global.get $x)
    62      (then (call $dummy) (i32.const 2))
    63      (else (call $dummy) (i32.const 3))
    64    )
    65  )
    66  (func (export "as-if-then") (result i32)
    67    (if (result i32) (i32.const 1)
    68      (then (global.get $x)) (else (i32.const 2))
    69    )
    70  )
    71  (func (export "as-if-else") (result i32)
    72    (if (result i32) (i32.const 0)
    73      (then (i32.const 2)) (else (global.get $x))
    74    )
    75  )
    76
    77  (func (export "as-br_if-first") (result i32)
    78    (block (result i32)
    79      (br_if 0 (global.get $x) (i32.const 2))
    80      (return (i32.const 3))
    81    )
    82  )
    83  (func (export "as-br_if-last") (result i32)
    84    (block (result i32)
    85      (br_if 0 (i32.const 2) (global.get $x))
    86      (return (i32.const 3))
    87    )
    88  )
    89
    90  (func (export "as-br_table-first") (result i32)
    91    (block (result i32)
    92      (global.get $x) (i32.const 2) (br_table 0 0)
    93    )
    94  )
    95  (func (export "as-br_table-last") (result i32)
    96    (block (result i32)
    97      (i32.const 2) (global.get $x) (br_table 0 0)
    98    )
    99  )
   100
   101  (func $func (param i32 i32) (result i32) (local.get 0))
   102  (type $check (func (param i32 i32) (result i32)))
   103  (table funcref (elem $func))
   104  (func (export "as-call_indirect-first") (result i32)
   105    (block (result i32)
   106      (call_indirect (type $check)
   107        (global.get $x) (i32.const 2) (i32.const 0)
   108      )
   109    )
   110  )
   111  (func (export "as-call_indirect-mid") (result i32)
   112    (block (result i32)
   113      (call_indirect (type $check)
   114        (i32.const 2) (global.get $x) (i32.const 0)
   115      )
   116    )
   117  )
   118 (func (export "as-call_indirect-last") (result i32)
   119    (block (result i32)
   120      (call_indirect (type $check)
   121        (i32.const 2) (i32.const 0) (global.get $x)
   122      )
   123    )
   124  )
   125
   126  (func (export "as-store-first")
   127    (global.get $x) (i32.const 1) (i32.store)
   128  )
   129  (func (export "as-store-last")
   130    (i32.const 0) (global.get $x) (i32.store)
   131  )
   132  (func (export "as-load-operand") (result i32)
   133    (i32.load (global.get $x))
   134  )
   135  (func (export "as-memory.grow-value") (result i32)
   136    (memory.grow (global.get $x))
   137  )
   138
   139  (func $f (param i32) (result i32) (local.get 0))
   140  (func (export "as-call-value") (result i32)
   141    (call $f (global.get $x))
   142  )
   143
   144  (func (export "as-return-value") (result i32)
   145    (global.get $x) (return)
   146  )
   147  (func (export "as-drop-operand")
   148    (drop (global.get $x))
   149  )
   150  (func (export "as-br-value") (result i32)
   151    (block (result i32) (br 0 (global.get $x)))
   152  )
   153
   154  (func (export "as-local.set-value") (param i32) (result i32)
   155    (local.set 0 (global.get $x))
   156    (local.get 0)
   157  )
   158  (func (export "as-local.tee-value") (param i32) (result i32)
   159    (local.tee 0 (global.get $x))
   160  )
   161  (func (export "as-global.set-value") (result i32)
   162    (global.set $x (global.get $x))
   163    (global.get $x)
   164  )
   165
   166  (func (export "as-unary-operand") (result i32)
   167    (i32.eqz (global.get $x))
   168  )
   169  (func (export "as-binary-operand") (result i32)
   170    (i32.mul
   171      (global.get $x) (global.get $x)
   172    )
   173  )
   174  (func (export "as-compare-operand") (result i32)
   175    (i32.gt_u
   176      (global.get 0) (i32.const 1)
   177    )
   178  )
   179)
   180
   181(assert_return (invoke "get-a") (i32.const -2))
   182(assert_return (invoke "get-b") (i64.const -5))
   183(assert_return (invoke "get-x") (i32.const -12))
   184(assert_return (invoke "get-y") (i64.const -15))
   185
   186(assert_return (invoke "get-1") (f32.const -3))
   187(assert_return (invoke "get-2") (f64.const -4))
   188(assert_return (invoke "get-5") (f32.const -13))
   189(assert_return (invoke "get-6") (f64.const -14))
   190
   191(assert_return (invoke "set-x" (i32.const 6)))
   192(assert_return (invoke "set-y" (i64.const 7)))
   193(assert_return (invoke "set-5" (f32.const 8)))
   194(assert_return (invoke "set-6" (f64.const 9)))
   195
   196(assert_return (invoke "get-x") (i32.const 6))
   197(assert_return (invoke "get-y") (i64.const 7))
   198(assert_return (invoke "get-5") (f32.const 8))
   199(assert_return (invoke "get-6") (f64.const 9))
   200
   201(assert_return (invoke "as-select-first") (i32.const 6))
   202(assert_return (invoke "as-select-mid") (i32.const 2))
   203(assert_return (invoke "as-select-last") (i32.const 2))
   204
   205(assert_return (invoke "as-loop-first") (i32.const 6))
   206(assert_return (invoke "as-loop-mid") (i32.const 6))
   207(assert_return (invoke "as-loop-last") (i32.const 6))
   208
   209(assert_return (invoke "as-if-condition") (i32.const 2))
   210(assert_return (invoke "as-if-then") (i32.const 6))
   211(assert_return (invoke "as-if-else") (i32.const 6))
   212
   213(assert_return (invoke "as-br_if-first") (i32.const 6))
   214(assert_return (invoke "as-br_if-last") (i32.const 2))
   215
   216(assert_return (invoke "as-br_table-first") (i32.const 6))
   217(assert_return (invoke "as-br_table-last") (i32.const 2))
   218
   219(assert_return (invoke "as-call_indirect-first") (i32.const 6))
   220(assert_return (invoke "as-call_indirect-mid") (i32.const 2))
   221(assert_trap (invoke "as-call_indirect-last") "undefined element")
   222
   223(assert_return (invoke "as-store-first"))
   224(assert_return (invoke "as-store-last"))
   225(assert_return (invoke "as-load-operand") (i32.const 1))
   226(assert_return (invoke "as-memory.grow-value") (i32.const 1))
   227
   228(assert_return (invoke "as-call-value") (i32.const 6))
   229
   230(assert_return (invoke "as-return-value") (i32.const 6))
   231(assert_return (invoke "as-drop-operand"))
   232(assert_return (invoke "as-br-value") (i32.const 6))
   233
   234(assert_return (invoke "as-local.set-value" (i32.const 1)) (i32.const 6))
   235(assert_return (invoke "as-local.tee-value" (i32.const 1)) (i32.const 6))
   236(assert_return (invoke "as-global.set-value") (i32.const 6))
   237
   238(assert_return (invoke "as-unary-operand") (i32.const 0))
   239(assert_return (invoke "as-binary-operand") (i32.const 36))
   240(assert_return (invoke "as-compare-operand") (i32.const 1))
   241
   242(assert_invalid
   243  (module (global f32 (f32.const 0)) (func (global.set 0 (f32.const 1))))
   244  "global is immutable"
   245)
   246
   247;; mutable globals can be exported
   248(module (global (mut f32) (f32.const 0)) (export "a" (global 0)))
   249(module (global (export "a") (mut f32) (f32.const 0)))
   250
   251(assert_invalid
   252  (module (global f32 (f32.neg (f32.const 0))))
   253  "constant expression required"
   254)
   255
   256(assert_invalid
   257  (module (global f32 (local.get 0)))
   258  "constant expression required"
   259)
   260
   261(assert_invalid
   262  (module (global f32 (f32.neg (f32.const 1))))
   263  "constant expression required"
   264)
   265
   266(assert_invalid
   267  (module (global i32 (i32.const 0) (nop)))
   268  "constant expression required"
   269)
   270
   271(assert_invalid
   272  (module (global i32 (nop)))
   273  "constant expression required"
   274)
   275
   276(assert_invalid
   277  (module (global i32 (f32.const 0)))
   278  "type mismatch"
   279)
   280
   281(assert_invalid
   282  (module (global i32 (i32.const 0) (i32.const 0)))
   283  "type mismatch"
   284)
   285
   286(assert_invalid
   287  (module (global i32 (;empty instruction sequence;)))
   288  "type mismatch"
   289)
   290
   291(assert_invalid
   292  (module (global i32 (global.get 0)))
   293  "unknown global"
   294)
   295
   296(assert_invalid
   297  (module (global i32 (global.get 1)) (global i32 (i32.const 0)))
   298  "unknown global"
   299)
   300
   301(module
   302  (import "spectest" "global_i32" (global i32))
   303)
   304(assert_malformed
   305  (module binary
   306    "\00asm" "\01\00\00\00"
   307    "\02\98\80\80\80\00"             ;; import section
   308      "\01"                          ;; length 1
   309      "\08\73\70\65\63\74\65\73\74"  ;; "spectest"
   310      "\0a\67\6c\6f\62\61\6c\5f\69\33\32" ;; "global_i32"
   311      "\03"                          ;; GlobalImport
   312      "\7f"                          ;; i32
   313      "\02"                          ;; invalid mutability
   314  )
   315  "invalid mutability"
   316)
   317(assert_malformed
   318  (module binary
   319    "\00asm" "\01\00\00\00"
   320    "\02\98\80\80\80\00"             ;; import section
   321      "\01"                          ;; length 1
   322      "\08\73\70\65\63\74\65\73\74"  ;; "spectest"
   323      "\0a\67\6c\6f\62\61\6c\5f\69\33\32" ;; "global_i32"
   324      "\03"                          ;; GlobalImport
   325      "\7f"                          ;; i32
   326      "\ff"                          ;; invalid mutability
   327  )
   328  "invalid mutability"
   329)
   330
   331(module
   332  (global i32 (i32.const 0))
   333)
   334(assert_malformed
   335  (module binary
   336    "\00asm" "\01\00\00\00"
   337    "\06\86\80\80\80\00"  ;; global section
   338      "\01"               ;; length 1
   339      "\7f"               ;; i32
   340      "\02"               ;; invalid mutability
   341      "\41\00"            ;; i32.const 0
   342      "\0b"               ;; end
   343  )
   344  "invalid mutability"
   345)
   346(assert_malformed
   347  (module binary
   348    "\00asm" "\01\00\00\00"
   349    "\06\86\80\80\80\00"  ;; global section
   350      "\01"               ;; length 1
   351      "\7f"               ;; i32
   352      "\ff"               ;; invalid mutability
   353      "\41\00"            ;; i32.const 0
   354      "\0b"               ;; end
   355  )
   356  "invalid mutability"
   357)
   358
   359
   360(assert_invalid
   361  (module
   362    (global $x (mut i32) (i32.const 0))
   363    (func $type-global.set-value-empty
   364      (global.set $x)
   365    )
   366  )
   367  "type mismatch"
   368)
   369(assert_invalid
   370  (module
   371    (global $x (mut i32) (i32.const 0))
   372    (func $type-global.set-value-empty-in-block
   373      (i32.const 0)
   374      (block (global.set $x))
   375    )
   376  )
   377  "type mismatch"
   378)
   379(assert_invalid
   380  (module
   381    (global $x (mut i32) (i32.const 0))
   382    (func $type-global.set-value-empty-in-loop
   383      (i32.const 0)
   384      (loop (global.set $x))
   385    )
   386  )
   387  "type mismatch"
   388)
   389(assert_invalid
   390  (module
   391    (global $x (mut i32) (i32.const 0))
   392    (func $type-global.set-value-empty-in-then
   393      (i32.const 0) (i32.const 0)
   394      (if (then (global.set $x)))
   395    )
   396  )
   397  "type mismatch"
   398)
   399(assert_invalid
   400  (module
   401    (global $x (mut i32) (i32.const 0))
   402    (func $type-global.set-value-empty-in-else
   403      (i32.const 0) (i32.const 0)
   404      (if (result i32) (then (i32.const 0)) (else (global.set $x)))
   405    )
   406  )
   407  "type mismatch"
   408)
   409(assert_invalid
   410  (module
   411    (global $x (mut i32) (i32.const 0))
   412    (func $type-global.set-value-empty-in-br
   413      (i32.const 0)
   414      (block (br 0 (global.set $x)))
   415    )
   416  )
   417  "type mismatch"
   418)
   419(assert_invalid
   420  (module
   421    (global $x (mut i32) (i32.const 0))
   422    (func $type-global.set-value-empty-in-br_if
   423      (i32.const 0)
   424      (block (br_if 0 (global.set $x)))
   425    )
   426  )
   427  "type mismatch"
   428)
   429(assert_invalid
   430  (module
   431    (global $x (mut i32) (i32.const 0))
   432    (func $type-global.set-value-empty-in-br_table
   433      (i32.const 0)
   434      (block (br_table 0 (global.set $x)))
   435    )
   436  )
   437  "type mismatch"
   438)
   439(assert_invalid
   440  (module
   441    (global $x (mut i32) (i32.const 0))
   442    (func $type-global.set-value-empty-in-return
   443      (return (global.set $x))
   444    )
   445  )
   446  "type mismatch"
   447)
   448(assert_invalid
   449  (module
   450    (global $x (mut i32) (i32.const 0))
   451    (func $type-global.set-value-empty-in-select
   452      (select (global.set $x) (i32.const 1) (i32.const 2))
   453    )
   454  )
   455  "type mismatch"
   456)
   457(assert_invalid
   458  (module
   459    (global $x (mut i32) (i32.const 0))
   460    (func $type-global.set-value-empty-in-call
   461      (call 1 (global.set $x))
   462    )
   463    (func (param i32) (result i32) (local.get 0))
   464  )
   465  "type mismatch"
   466)
   467(assert_invalid
   468  (module
   469    (global $x (mut i32) (i32.const 0))
   470    (func $f (param i32) (result i32) (local.get 0))
   471    (type $sig (func (param i32) (result i32)))
   472    (table funcref (elem $f))
   473    (func $type-global.set-value-empty-in-call_indirect
   474      (block (result i32)
   475        (call_indirect (type $sig)
   476          (global.set $x) (i32.const 0)
   477        )
   478      )
   479    )
   480  )
   481  "type mismatch"
   482)

View as plain text