/* Copyright 2021 Mozilla Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
*/
if (!wasmIsSupported()) {
quit();
}
function bytes(type, bytes) { var typedBuffer = new Uint8Array(bytes); return wasmGlobalFromArrayBuffer(type, typedBuffer.buffer);
} function value(type, value) { returnnew WebAssembly.Global({
value: type,
mutable: false,
}, value);
}
function i8x16(elements) {
let typedBuffer = new Uint8Array(elements); return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer);
} function i16x8(elements) {
let typedBuffer = new Uint16Array(elements); return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer);
} function i32x4(elements) {
let typedBuffer = new Uint32Array(elements); return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer);
} function i64x2(elements) {
let typedBuffer = new BigUint64Array(elements); return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer);
} function f32x4(elements) {
let typedBuffer = new Float32Array(elements); return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer);
} function f64x2(elements) {
let typedBuffer = new Float64Array(elements); return wasmGlobalFromArrayBuffer("v128", typedBuffer.buffer);
}
function either(...arr) { returnnew EitherVariants(arr);
}
test(refGlobal) { try { new WebAssembly.Global({value: this.type}, refGlobal.value); returntrue;
} catch (err) {
assertEq(err instanceof TypeError, true, `wrong type of error when creating global: ${err}`);
assertEq(!!err.message.match(/can only pass/), true, `wrong type of error when creating global: ${err}`); returnfalse;
}
}
}
// ref.extern values created by spec tests will be JS objects of the form // { [externsym]: <number> }. Other externref values are possible to observe // if extern.convert_any is used.
let externsym = Symbol("externref"); function externref(s) { return { [externsym]: s };
} function is_externref(x) { return (x !== null && externsym in x) ? 1 : 0;
} function is_funcref(x) { returntypeof x === "function" ? 1 : 0;
} function eq_externref(x, y) { return x === y ? 1 : 0;
} function eq_funcref(x, y) { return x === y ? 1 : 0;
}
class ExternRefResult {
constructor(n) { this.n = n;
}
test(global) { // the global's value can either be an externref or just a plain old JS number
let result = global.value; if (typeof global.value === "object" && externsym in global.value) {
result = global.value[externsym];
} return result === this.n;
}
}
// ref.host values created by spectests will be whatever the JS API does to // convert the given value to anyref. It should implicitly be like any.convert_extern. function hostref(v) { const { internalizeNum } = new WebAssembly.Instance( new WebAssembly.Module(wasmTextToBinary(`(module
(func (import"test""coerce") (param i32) (result anyref))
(func (export "internalizeNum") (param i32) (result anyref)
(call 0 (local.get 0))
)
)`)),
{ "test": { "coerce": x => x } },
).exports; return internalizeNum(v);
}
class HostRefResult {
constructor(n) { this.n = n;
}
formatExpected() { return `ref.host ${this.n}`;
}
test(externrefGlobal) {
assertEq(externsym in externrefGlobal.value, true, `HostRefResult only works with externref inputs`); return externrefGlobal.value[externsym] === this.n;
}
}
function module(source) {
let bytecode = wasmTextToBinary(source);
let module = new WebAssembly.Module(bytecode); return module;
}
function instantiate(source) {
let bytecode = wasmTextToBinary(source);
let module = new WebAssembly.Module(bytecode);
let instance = new WebAssembly.Instance(module, linkage); return instance.exports;
}
function instantiateFromModule(module) {
let instance = new WebAssembly.Instance(module, linkage); return instance.exports;
}
function register(instance, name) {
linkage[name] = instance;
}
function invoke(instance, field, params) {
let func = instance[field];
assertEq(func instanceofFunction, true, "expected a function"); return wasmLosslessInvoke(func, ...params);
}
function get(instance, field) {
let global = instance[field];
assertEq(
global instanceof WebAssembly.Global, true, "expected a WebAssembly.Global",
); return global;
}
function compareResults(results, expected) { if (results.length !== expected.length) { returnfalse;
} for (let i in results) { if (expected[i] instanceof EitherVariants) { return expected[i].matches(results[i]);
} if (!compareResult(results[i], expected[i])) { returnfalse;
}
} returntrue;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.