tor-browser

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

instantiate.any.js (4213B)


      1 // META: global=window,dedicatedworker,jsshell,shadowrealm
      2 // META: script=/wasm/jsapi/wasm-module-builder.js
      3 // META: script=/wasm/jsapi/assertions.js
      4 // META: script=/wasm/jsapi/instanceTestFactory.js
      5 
      6 let emptyModuleBinary;
      7 setup(() => {
      8  emptyModuleBinary = new WasmModuleBuilder().toBuffer();
      9 });
     10 
     11 promise_test(t => {
     12  return promise_rejects_js(t, TypeError, WebAssembly.instantiate());
     13 }, "Missing arguments");
     14 
     15 promise_test(() => {
     16  const fn = WebAssembly.instantiate;
     17  const thisValues = [
     18    undefined,
     19    null,
     20    true,
     21    "",
     22    Symbol(),
     23    1,
     24    {},
     25    WebAssembly,
     26  ];
     27  return Promise.all(thisValues.map(thisValue => {
     28    return fn.call(thisValue, emptyModuleBinary).then(assert_WebAssemblyInstantiatedSource);
     29  }));
     30 }, "Branding");
     31 
     32 promise_test(t => {
     33  const invalidArguments = [
     34    undefined,
     35    null,
     36    true,
     37    "",
     38    Symbol(),
     39    1,
     40    {},
     41    WebAssembly.Module,
     42    WebAssembly.Module.prototype,
     43    ArrayBuffer,
     44    ArrayBuffer.prototype,
     45    Array.from(emptyModuleBinary),
     46  ];
     47  return Promise.all(invalidArguments.map(argument => {
     48    return promise_rejects_js(t, TypeError, WebAssembly.instantiate(argument),
     49                           `instantiate(${format_value(argument)})`);
     50  }));
     51 }, "Invalid arguments");
     52 
     53 test(() => {
     54  const promise = WebAssembly.instantiate(emptyModuleBinary);
     55  assert_equals(Object.getPrototypeOf(promise), Promise.prototype, "prototype");
     56  assert_true(Object.isExtensible(promise), "extensibility");
     57 }, "Promise type");
     58 
     59 for (const [name, fn] of instanceTestFactory) {
     60  promise_test(() => {
     61    const { buffer, args, exports, verify } = fn();
     62    return WebAssembly.instantiate(buffer, ...args).then(result => {
     63      assert_WebAssemblyInstantiatedSource(result, exports);
     64      verify(result.instance);
     65    });
     66  }, `${name}: BufferSource argument`);
     67 
     68  promise_test(() => {
     69    const { buffer, args, exports, verify } = fn();
     70    const module = new WebAssembly.Module(buffer);
     71    return WebAssembly.instantiate(module, ...args).then(instance => {
     72      assert_Instance(instance, exports);
     73      verify(instance);
     74    });
     75  }, `${name}: Module argument`);
     76 }
     77 
     78 promise_test(() => {
     79  const builder = new WasmModuleBuilder();
     80  builder.addImportedGlobal("module", "global", kWasmI32);
     81  const buffer = builder.toBuffer();
     82  const order = [];
     83 
     84  const imports = {
     85    get module() {
     86      order.push("module getter");
     87      return {
     88        get global() {
     89          order.push("global getter");
     90          return 0;
     91        },
     92      }
     93    },
     94  };
     95 
     96  const expected = [
     97    "module getter",
     98    "global getter",
     99  ];
    100  const p = WebAssembly.instantiate(buffer, imports);
    101  assert_array_equals(order, []);
    102  return p.then(result => {
    103    assert_WebAssemblyInstantiatedSource(result);
    104    assert_array_equals(order, expected);
    105  });
    106 }, "Synchronous options handling: Buffer argument");
    107 
    108 promise_test(() => {
    109  const builder = new WasmModuleBuilder();
    110  builder.addImportedGlobal("module", "global", kWasmI32);
    111  const buffer = builder.toBuffer();
    112  const module = new WebAssembly.Module(buffer);
    113  const order = [];
    114 
    115  const imports = {
    116    get module() {
    117      order.push("module getter");
    118      return {
    119        get global() {
    120          order.push("global getter");
    121          return 0;
    122        },
    123      }
    124    },
    125  };
    126 
    127  const expected = [
    128    "module getter",
    129    "global getter",
    130  ];
    131  const p = WebAssembly.instantiate(module, imports);
    132  assert_array_equals(order, expected);
    133  return p.then(instance => assert_Instance(instance, {}));
    134 }, "Synchronous options handling: Module argument");
    135 
    136 promise_test(t => {
    137  const buffer = new Uint8Array();
    138  return promise_rejects_js(t, WebAssembly.CompileError, WebAssembly.instantiate(buffer));
    139 }, "Empty buffer");
    140 
    141 promise_test(t => {
    142  const buffer = new Uint8Array(Array.from(emptyModuleBinary).concat([0, 0]));
    143  return promise_rejects_js(t, WebAssembly.CompileError, WebAssembly.instantiate(buffer));
    144 }, "Invalid code");
    145 
    146 promise_test(() => {
    147  const buffer = new WasmModuleBuilder().toBuffer();
    148  assert_equals(buffer[0], 0);
    149  const promise = WebAssembly.instantiate(buffer);
    150  buffer[0] = 1;
    151  return promise.then(assert_WebAssemblyInstantiatedSource);
    152 }, "Changing the buffer");