assertions.js 5.3 KB

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