File: basic.any.js

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (383 lines) | stat: -rw-r--r-- 9,709 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// META: global=window,dedicatedworker,jsshell
// META: script=/wasm/jsapi/assertions.js
// META: script=/wasm/jsapi/wasm-module-builder.js
// META: script=/wasm/jsapi/js-string/polyfill.js

// Generate two sets of exports, one from a polyfill implementation and another
// from the builtins provided by the host.
let polyfillExports;
let builtinExports;
setup(() => {
  // Compile a module that exports a function for each builtin that will call
  // it. We could just generate a module that re-exports the builtins, but that
  // would not catch any special codegen that could happen when direct calling
  // a known builtin function from wasm.
  const builder = new WasmModuleBuilder();
  const arrayIndex = builder.addArray(kWasmI16, true, kNoSuperType, true);
  const builtins = [
    {
      name: "test",
      params: [kWasmExternRef],
      results: [kWasmI32],
    },
    {
      name: "cast",
      params: [kWasmExternRef],
      results: [wasmRefType(kWasmExternRef)],
    },
    {
      name: "fromCharCodeArray",
      params: [wasmRefNullType(arrayIndex), kWasmI32, kWasmI32],
      results: [wasmRefType(kWasmExternRef)],
    },
    {
      name: "intoCharCodeArray",
      params: [kWasmExternRef, wasmRefNullType(arrayIndex), kWasmI32],
      results: [kWasmI32],
    },
    {
      name: "fromCharCode",
      params: [kWasmI32],
      results: [wasmRefType(kWasmExternRef)],
    },
    {
      name: "fromCodePoint",
      params: [kWasmI32],
      results: [wasmRefType(kWasmExternRef)],
    },
    {
      name: "charCodeAt",
      params: [kWasmExternRef, kWasmI32],
      results: [kWasmI32],
    },
    {
      name: "codePointAt",
      params: [kWasmExternRef, kWasmI32],
      results: [kWasmI32],
    },
    {
      name: "length",
      params: [kWasmExternRef],
      results: [kWasmI32],
    },
    {
      name: "concat",
      params: [kWasmExternRef, kWasmExternRef],
      results: [wasmRefType(kWasmExternRef)],
    },
    {
      name: "substring",
      params: [kWasmExternRef, kWasmI32, kWasmI32],
      results: [wasmRefType(kWasmExternRef)],
    },
    {
      name: "equals",
      params: [kWasmExternRef, kWasmExternRef],
      results: [kWasmI32],
    },
    {
      name: "compare",
      params: [kWasmExternRef, kWasmExternRef],
      results: [kWasmI32],
    },
  ];

  // Add a function type for each builtin
  for (let builtin of builtins) {
    builtin.type = builder.addType({
      params: builtin.params,
      results: builtin.results
    });
  }

  // Add an import for each builtin
  for (let builtin of builtins) {
    builtin.importFuncIndex = builder.addImport(
      "wasm:js-string",
      builtin.name,
      builtin.type);
  }

  // Generate an exported function to call the builtin
  for (let builtin of builtins) {
    let func = builder.addFunction(builtin.name + "Imp", builtin.type);
    func.addLocals(builtin.params.length);
    let body = [];
    for (let i = 0; i < builtin.params.length; i++) {
      body.push(kExprLocalGet);
      body.push(...wasmSignedLeb(i));
    }
    body.push(kExprCallFunction);
    body.push(...wasmSignedLeb(builtin.importFuncIndex));
    func.addBody(body);
    func.exportAs(builtin.name);
  }

  const buffer = builder.toBuffer();

  // Instantiate this module using the builtins from the host
  const builtinModule = new WebAssembly.Module(buffer, {
    builtins: ["js-string"]
  });
  const builtinInstance = new WebAssembly.Instance(builtinModule, {});
  builtinExports = builtinInstance.exports;

  // Instantiate this module using the polyfill module
  const polyfillModule = new WebAssembly.Module(buffer);
  const polyfillInstance = new WebAssembly.Instance(polyfillModule, {
    "wasm:js-string": polyfillImports
  });
  polyfillExports = polyfillInstance.exports;
});

// A helper function to assert that the behavior of two functions are the
// same.
function assert_same_behavior(funcA, funcB, ...params) {
  let resultA;
  let errA = null;
  try {
    resultA = funcA(...params);
  } catch (err) {
    errA = err;
  }

  let resultB;
  let errB = null;
  try {
    resultB = funcB(...params);
  } catch (err) {
    errB = err;
  }

  if (errA || errB) {
    assert_equals(errA === null, errB === null, errA ? errA.message : errB.message);
    assert_equals(Object.getPrototypeOf(errA), Object.getPrototypeOf(errB));
  }
  assert_equals(resultA, resultB);

  if (errA) {
    throw errA;
  }
  return resultA;
}

function assert_throws_if(func, shouldThrow, constructor) {
  let error = null;
  try {
    func();
  } catch (e) {
    error = e;
  }
  assert_equals(error !== null, shouldThrow, "shouldThrow mismatch");
  if (shouldThrow && error !== null) {
    assert_true(error instanceof constructor);
  }
}

// Constant values used in the tests below
const testStrings = [
  "",
  "a",
  "1",
  "ab",
  "hello, world",
  "\n",
  "☺",
  "☺☺",
  String.fromCodePoint(0x10000, 0x10001)
];
const testCharCodes = [1, 2, 3, 10, 0x7f, 0xff, 0xfffe, 0xffff];
const testCodePoints = [1, 2, 3, 10, 0x7f, 0xff, 0xfffe, 0xffff, 0x10000, 0x10001];
const testExternRefValues = [
  null,
  undefined,
  true,
  false,
  {x:1337},
  ["abracadabra"],
  13.37,
  -0,
  0x7fffffff + 0.1,
  -0x7fffffff - 0.1,
  0x80000000 + 0.1,
  -0x80000000 - 0.1,
  0xffffffff + 0.1,
  -0xffffffff - 0.1,
  Number.EPSILON,
  Number.MAX_SAFE_INTEGER,
  Number.MIN_SAFE_INTEGER,
  Number.MIN_VALUE,
  Number.MAX_VALUE,
  Number.NaN,
  "hi",
  37n,
  new Number(42),
  new Boolean(true),
  Symbol("status"),
  () => 1337,
];

// Test that `test` and `cast` work on various JS values. Run all the
// other builtins and assert that they also perform equivalent type
// checks.
test(() => {
  for (let a of testExternRefValues) {
    let isString = assert_same_behavior(
      builtinExports['test'],
      polyfillExports['test'],
      a
    );

    assert_throws_if(() => assert_same_behavior(
        builtinExports['cast'],
        polyfillExports['cast'],
        a
      ), !isString, WebAssembly.RuntimeError);

    let arrayMutI16 = helperExports.createArrayMutI16(10);
    assert_throws_if(() => assert_same_behavior(
        builtinExports['intoCharCodeArray'],
        polyfillExports['intoCharCodeArray'],
        a, arrayMutI16, 0
      ), !isString, WebAssembly.RuntimeError);

    assert_throws_if(() => assert_same_behavior(
        builtinExports['charCodeAt'],
        polyfillExports['charCodeAt'],
        a, 0
      ), !isString, WebAssembly.RuntimeError);

    assert_throws_if(() => assert_same_behavior(
        builtinExports['codePointAt'],
        polyfillExports['codePointAt'],
        a, 0
      ), !isString, WebAssembly.RuntimeError);

    assert_throws_if(() => assert_same_behavior(
        builtinExports['length'],
        polyfillExports['length'],
        a
      ), !isString, WebAssembly.RuntimeError);

    assert_throws_if(() => assert_same_behavior(
        builtinExports['concat'],
        polyfillExports['concat'],
        a, a
      ), !isString, WebAssembly.RuntimeError);

    assert_throws_if(() => assert_same_behavior(
        builtinExports['substring'],
        polyfillExports['substring'],
        a, 0, 0
      ), !isString, WebAssembly.RuntimeError);

    assert_throws_if(() => assert_same_behavior(
        builtinExports['equals'],
        polyfillExports['equals'],
        a, a
      ), a !== null && !isString, WebAssembly.RuntimeError);

    assert_throws_if(() => assert_same_behavior(
        builtinExports['compare'],
        polyfillExports['compare'],
        a, a
      ), !isString, WebAssembly.RuntimeError);
  }
});

// Test that `fromCharCode` works on various char codes
test(() => {
  for (let a of testCharCodes) {
    assert_same_behavior(
      builtinExports['fromCharCode'],
      polyfillExports['fromCharCode'],
      a
    );
  }
});

// Test that `fromCodePoint` works on various code points
test(() => {
  for (let a of testCodePoints) {
    assert_same_behavior(
      builtinExports['fromCodePoint'],
      polyfillExports['fromCodePoint'],
      a
    );
  }
});

// Perform tests on various strings
test(() => {
  for (let a of testStrings) {
    let length = assert_same_behavior(
      builtinExports['length'],
      polyfillExports['length'],
      a
    );

    for (let i = 0; i < length; i++) {
      let charCode = assert_same_behavior(
        builtinExports['charCodeAt'],
        polyfillExports['charCodeAt'],
        a, i
      );
    }

    for (let i = 0; i < length; i++) {
      let charCode = assert_same_behavior(
        builtinExports['codePointAt'],
        polyfillExports['codePointAt'],
        a, i
      );
    }

    let arrayMutI16 = helperExports.createArrayMutI16(length);
    assert_same_behavior(
      builtinExports['intoCharCodeArray'],
      polyfillExports['intoCharCodeArray'],
      a, arrayMutI16, 0
    );

    assert_same_behavior(
      builtinExports['fromCharCodeArray'],
      polyfillExports['fromCharCodeArray'],
      arrayMutI16, 0, length
    );

    for (let i = 0; i < length; i++) {
      for (let j = 0; j < length; j++) {
        assert_same_behavior(
          builtinExports['substring'],
          polyfillExports['substring'],
          a, i, j
        );
      }
    }
  }
});

// Test various binary operations
test(() => {
  for (let a of testStrings) {
    for (let b of testStrings) {
      assert_same_behavior(
        builtinExports['concat'],
        polyfillExports['concat'],
        a, b
      );

      assert_same_behavior(
        builtinExports['equals'],
        polyfillExports['equals'],
        a, b
      );

      assert_same_behavior(
        builtinExports['compare'],
        polyfillExports['compare'],
        a, b
      );
    }
  }
});