tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

call_ref.js (2423B)


      1 let { plusOne } = wasmEvalText(`(module
      2  (; forward declaration so that ref.func works ;)
      3  (elem declare func $plusOneRef)
      4  (type $t (func (param i32) (result i32)))
      5 
      6  (func $plusOneRef (param i32) (result i32)
      7    (i32.add
      8      local.get 0
      9      i32.const 1)
     10  )
     11 
     12  (func (export "plusOne") (param i32) (result i32)
     13    local.get 0
     14    ref.func $plusOneRef
     15    call_ref $t
     16  )
     17 )`).exports;
     18 
     19 assertEq(plusOne(3), 4);
     20 
     21 // pass non-funcref type
     22 wasmFailValidateText(`(module
     23  (type $t (func (param i32)))
     24  (func (param $a i32)
     25    local.get $a
     26    call_ref $t
     27  )
     28 )`, /type mismatch: expression has type i32 but expected \(ref null \d+\)/);
     29 
     30 wasmFailValidateText(`(module
     31  (type $t (func (param externref)))
     32  (func (param $a (ref extern))
     33    local.get $a
     34    call_ref $t
     35  )
     36 )`, /type mismatch: expression has type \(ref extern\) but expected \(ref null \d+\)/);
     37 
     38 // pass (non-subtype of) funcref
     39 wasmFailValidateText(`(module
     40  (type $t (func (param i32) (result i32)))
     41  (func (param funcref)
     42    local.get 0
     43    call_ref $t
     44  )
     45 )`, /type mismatch: expression has type funcref but expected \(ref null \d+\)/);  
     46  
     47 // signature mismatch
     48 wasmFailValidateText(`(module
     49  (type $t (func (param i32) (result i32)))
     50  (elem declare func $plusOneRef)
     51  (func $plusOneRef (param f32) (result f32)
     52    (f32.add
     53      local.get 0
     54      f32.const 1.0)
     55  )
     56 
     57  (func (export "plusOne") (param i32) (result i32)
     58    local.get 0
     59    ref.func $plusOneRef
     60    call_ref $t
     61  )
     62 )`, /type mismatch: expression has type \(ref \d+\) but expected \(ref null \d+\)/);
     63 
     64 // Cross-instance calls
     65 let { loadInt } = wasmEvalText(`(module
     66  (memory 1 1)
     67  (data (i32.const 0) "\\04\\00\\00\\00")
     68  (func (export "loadInt") (result i32)
     69    i32.const 0
     70    i32.load offset=0
     71  )
     72 )`).exports;
     73 
     74 let { callLoadInt } = wasmEvalText(`(module
     75  (type $t (func (result i32)))
     76  (elem declare func 0)
     77  (import "" "loadInt" (func (result i32)))
     78  (func (export "callLoadInt") (result i32)
     79    ref.func 0
     80    call_ref $t
     81  )
     82 )`, {"": { loadInt, }}).exports;
     83 
     84 assertEq(loadInt(), 4);
     85 assertEq(callLoadInt(), 4);
     86 
     87 // Null call.
     88 assertErrorMessage(function() {
     89  let { nullCall } = wasmEvalText(`(module
     90    (type $t (func (param i32) (result i32)))
     91    (func (export "nullCall") (param i32) (result i32)
     92      local.get 0
     93      ref.null $t
     94      call_ref $t
     95    )
     96  )`).exports;
     97  nullCall(3);
     98 }, WebAssembly.RuntimeError, /dereferencing null pointer/);