tor-browser

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

constructor.any.js (4807B)


      1 // META: global=window,dedicatedworker,jsshell,shadowrealm
      2 // META: script=/wasm/jsapi/assertions.js
      3 
      4 function assert_Global(actual, expected) {
      5  assert_equals(Object.getPrototypeOf(actual), WebAssembly.Global.prototype,
      6                "prototype");
      7  assert_true(Object.isExtensible(actual), "extensible");
      8 
      9  assert_equals(actual.value, expected, "value");
     10  assert_equals(actual.valueOf(), expected, "valueOf");
     11 }
     12 
     13 test(() => {
     14  assert_function_name(WebAssembly.Global, "Global", "WebAssembly.Global");
     15 }, "name");
     16 
     17 test(() => {
     18  assert_function_length(WebAssembly.Global, 1, "WebAssembly.Global");
     19 }, "length");
     20 
     21 test(() => {
     22  assert_throws_js(TypeError, () => new WebAssembly.Global());
     23 }, "No arguments");
     24 
     25 test(() => {
     26  const argument = { "value": "i32" };
     27  assert_throws_js(TypeError, () => WebAssembly.Global(argument));
     28 }, "Calling");
     29 
     30 test(() => {
     31  const order = [];
     32 
     33  new WebAssembly.Global({
     34    get value() {
     35      order.push("descriptor value");
     36      return {
     37        toString() {
     38          order.push("descriptor value toString");
     39          return "f64";
     40        },
     41      };
     42    },
     43 
     44    get mutable() {
     45      order.push("descriptor mutable");
     46      return false;
     47    },
     48  }, {
     49    valueOf() {
     50      order.push("value valueOf()");
     51    }
     52  });
     53 
     54  assert_array_equals(order, [
     55    "descriptor mutable",
     56    "descriptor value",
     57    "descriptor value toString",
     58    "value valueOf()",
     59  ]);
     60 }, "Order of evaluation");
     61 
     62 test(() => {
     63  const invalidArguments = [
     64    undefined,
     65    null,
     66    false,
     67    true,
     68    "",
     69    "test",
     70    Symbol(),
     71    1,
     72    NaN,
     73    {},
     74  ];
     75  for (const invalidArgument of invalidArguments) {
     76    assert_throws_js(TypeError,
     77                     () => new WebAssembly.Global(invalidArgument),
     78                     `new Global(${format_value(invalidArgument)})`);
     79  }
     80 }, "Invalid descriptor argument");
     81 
     82 test(() => {
     83  const invalidTypes = ["i16", "i128", "f16", "f128", "u32", "u64", "i32\0"];
     84  for (const value of invalidTypes) {
     85    const argument = { value };
     86    assert_throws_js(TypeError, () => new WebAssembly.Global(argument));
     87  }
     88 }, "Invalid type argument");
     89 
     90 test(() => {
     91  const argument = { "value": "v128" };
     92  assert_throws_js(TypeError, () => new WebAssembly.Global(argument));
     93 }, "Construct v128 global");
     94 
     95 test(() => {
     96  const argument = { "value": "i64" };
     97  const global = new WebAssembly.Global(argument);
     98  assert_Global(global, 0n);
     99 }, "i64 with default");
    100 
    101 for (const type of ["i32", "f32", "f64"]) {
    102  test(() => {
    103    const argument = { "value": type };
    104    const global = new WebAssembly.Global(argument);
    105    assert_Global(global, 0);
    106  }, `Default value for type ${type}`);
    107 
    108  const valueArguments = [
    109    [undefined, 0],
    110    [null, 0],
    111    [true, 1],
    112    [false, 0],
    113    [2, 2],
    114    ["3", 3],
    115    [{ toString() { return "5" } }, 5, "object with toString returning string"],
    116    [{ valueOf() { return "8" } }, 8, "object with valueOf returning string"],
    117    [{ toString() { return 6 } }, 6, "object with toString returning number"],
    118    [{ valueOf() { return 9 } }, 9, "object with valueOf returning number"],
    119  ];
    120  for (const [value, expected, name = format_value(value)] of valueArguments) {
    121    test(() => {
    122      const argument = { "value": type };
    123      const global = new WebAssembly.Global(argument, value);
    124      assert_Global(global, expected);
    125    }, `Explicit value ${name} for type ${type}`);
    126  }
    127 
    128  test(() => {
    129    const argument = { "value": type };
    130    assert_throws_js(TypeError, () => new WebAssembly.Global(argument, 0n));
    131  }, `BigInt value for type ${type}`);
    132 }
    133 
    134 const valueArguments = [
    135  [undefined, 0n],
    136  [true, 1n],
    137  [false, 0n],
    138  ["3", 3n],
    139  [123n, 123n],
    140  [{ toString() { return "5" } }, 5n, "object with toString returning string"],
    141  [{ valueOf() { return "8" } }, 8n, "object with valueOf returning string"],
    142  [{ toString() { return 6n } }, 6n, "object with toString returning bigint"],
    143  [{ valueOf() { return 9n } }, 9n, "object with valueOf returning bigint"],
    144 ];
    145 for (const [value, expected, name = format_value(value)] of valueArguments) {
    146  test(() => {
    147    const argument = { "value": "i64" };
    148    const global = new WebAssembly.Global(argument, value);
    149    assert_Global(global, expected);
    150  }, `Explicit value ${name} for type i64`);
    151 }
    152 
    153 const invalidBigints = [
    154  null,
    155  666,
    156  { toString() { return 5 } },
    157  { valueOf() { return 8 } },
    158  Symbol(),
    159 ];
    160 for (const invalidBigint of invalidBigints) {
    161  test(() => {
    162    var argument = { "value": "i64" };
    163    assert_throws_js(TypeError, () => new WebAssembly.Global(argument, invalidBigint));
    164  }, `Pass non-bigint as i64 Global value: ${format_value(invalidBigint)}`);
    165 }
    166 
    167 test(() => {
    168  const argument = { "value": "i32" };
    169  const global = new WebAssembly.Global(argument, 0, {});
    170  assert_Global(global, 0);
    171 }, "Stray argument");