assertions.js (5464B)
1 function assert_function_name(fn, name, description) { 2 const propdesc = Object.getOwnPropertyDescriptor(fn, "name"); 3 assert_equals(typeof propdesc, "object", `${description} should have name property`); 4 assert_false(propdesc.writable, "writable", `${description} name should not be writable`); 5 assert_false(propdesc.enumerable, "enumerable", `${description} name should not be enumerable`); 6 assert_true(propdesc.configurable, "configurable", `${description} name should be configurable`); 7 assert_equals(propdesc.value, name, `${description} name should be ${name}`); 8 } 9 globalThis.assert_function_name = assert_function_name; 10 11 function assert_function_length(fn, length, description) { 12 const propdesc = Object.getOwnPropertyDescriptor(fn, "length"); 13 assert_equals(typeof propdesc, "object", `${description} should have length property`); 14 assert_false(propdesc.writable, "writable", `${description} length should not be writable`); 15 assert_false(propdesc.enumerable, "enumerable", `${description} length should not be enumerable`); 16 assert_true(propdesc.configurable, "configurable", `${description} length should be configurable`); 17 assert_equals(propdesc.value, length, `${description} length should be ${length}`); 18 } 19 globalThis.assert_function_length = assert_function_length; 20 21 function assert_exported_function(fn, { name, length }, description) { 22 if (WebAssembly.Function === undefined) { 23 assert_equals(Object.getPrototypeOf(fn), Function.prototype, 24 `${description}: prototype`); 25 } else { 26 assert_equals(Object.getPrototypeOf(fn), WebAssembly.Function.prototype, 27 `${description}: prototype`); 28 } 29 30 assert_function_name(fn, name, description); 31 assert_function_length(fn, length, description); 32 } 33 globalThis.assert_exported_function = assert_exported_function; 34 35 function assert_Instance(instance, expected_exports) { 36 assert_equals(Object.getPrototypeOf(instance), WebAssembly.Instance.prototype, 37 "prototype"); 38 assert_true(Object.isExtensible(instance), "extensible"); 39 40 assert_equals(instance.exports, instance.exports, "exports should be idempotent"); 41 const exports = instance.exports; 42 43 assert_equals(Object.getPrototypeOf(exports), null, "exports prototype"); 44 assert_false(Object.isExtensible(exports), "extensible exports"); 45 assert_array_equals(Object.keys(exports), Object.keys(expected_exports), "matching export keys"); 46 for (const [key, expected] of Object.entries(expected_exports)) { 47 const property = Object.getOwnPropertyDescriptor(exports, key); 48 assert_equals(typeof property, "object", `${key} should be present`); 49 assert_false(property.writable, `${key}: writable`); 50 assert_true(property.enumerable, `${key}: enumerable`); 51 assert_false(property.configurable, `${key}: configurable`); 52 const actual = property.value; 53 assert_true(Object.isExtensible(actual), `${key}: extensible`); 54 55 switch (expected.kind) { 56 case "function": 57 assert_exported_function(actual, expected, `value of ${key}`); 58 break; 59 case "global": 60 assert_equals(Object.getPrototypeOf(actual), WebAssembly.Global.prototype, 61 `value of ${key}: prototype`); 62 assert_equals(actual.value, expected.value, `value of ${key}: value`); 63 assert_equals(actual.valueOf(), expected.value, `value of ${key}: valueOf()`); 64 break; 65 case "memory": 66 assert_equals(Object.getPrototypeOf(actual), WebAssembly.Memory.prototype, 67 `value of ${key}: prototype`); 68 assert_equals(Object.getPrototypeOf(actual.buffer), ArrayBuffer.prototype, 69 `value of ${key}: prototype of buffer`); 70 assert_equals(actual.buffer.byteLength, 0x10000 * expected.size, `value of ${key}: size of buffer`); 71 const array = new Uint8Array(actual.buffer); 72 assert_equals(array[0], 0, `value of ${key}: first element of buffer`); 73 assert_equals(array[array.byteLength - 1], 0, `value of ${key}: last element of buffer`); 74 break; 75 case "table": 76 assert_equals(Object.getPrototypeOf(actual), WebAssembly.Table.prototype, 77 `value of ${key}: prototype`); 78 assert_equals(actual.length, expected.length, `value of ${key}: length of table`); 79 break; 80 } 81 } 82 } 83 globalThis.assert_Instance = assert_Instance; 84 85 function assert_WebAssemblyInstantiatedSource(actual, expected_exports={}) { 86 assert_equals(Object.getPrototypeOf(actual), Object.prototype, 87 "Prototype"); 88 assert_true(Object.isExtensible(actual), "Extensibility"); 89 90 const module = Object.getOwnPropertyDescriptor(actual, "module"); 91 assert_equals(typeof module, "object", "module: type of descriptor"); 92 assert_true(module.writable, "module: writable"); 93 assert_true(module.enumerable, "module: enumerable"); 94 assert_true(module.configurable, "module: configurable"); 95 assert_equals(Object.getPrototypeOf(module.value), WebAssembly.Module.prototype, 96 "module: prototype"); 97 98 const instance = Object.getOwnPropertyDescriptor(actual, "instance"); 99 assert_equals(typeof instance, "object", "instance: type of descriptor"); 100 assert_true(instance.writable, "instance: writable"); 101 assert_true(instance.enumerable, "instance: enumerable"); 102 assert_true(instance.configurable, "instance: configurable"); 103 assert_Instance(instance.value, expected_exports); 104 } 105 globalThis.assert_WebAssemblyInstantiatedSource = assert_WebAssemblyInstantiatedSource;