262test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. "use strict";
  6. var $ = { global: this, createRealm(options) { options = options || {}; options.globals = options.globals || {}; var realm = WScript.LoadScript(this.source, 'samethread'); realm.$.source = this.source; realm.$.destroy = function () { if (options.destroy) { options.destroy(); } }; for(var glob in options.globals) { realm.$.global[glob] = options.globals[glob]; } return realm.$; }, evalScript(code) { try { WScript.LoadScript(code); return { type: 'normal', value: undefined }; } catch (e) { return { type: 'throw', value: e }; } }, getGlobal(name) { return this.global[name]; }, setGlobal(name, value) { this.global[name] = value; }, destroy() { /* noop */ }, source: "var $ = { global: this, createRealm(options) { options = options || {}; options.globals = options.globals || {}; var realm = WScript.LoadScript(this.source, 'samethread'); realm.$.source = this.source; realm.$.destroy = function () { if (options.destroy) { options.destroy(); } }; for(var glob in options.globals) { realm.$.global[glob] = options.globals[glob]; } return realm.$; }, evalScript(code) { try { WScript.LoadScript(code); return { type: 'normal', value: undefined }; } catch (e) { return { type: 'throw', value: e }; } }, getGlobal(name) { return this.global[name]; }, setGlobal(name, value) { this.global[name] = value; }, destroy() { /* noop */ }, source: \"\"};"};function Test262Error(message) {
  7. if (message) this.message = message;
  8. }
  9. Test262Error.prototype.name = "Test262Error";
  10. Test262Error.prototype.toString = function () {
  11. return "Test262Error: " + this.message;
  12. };
  13. function $ERROR(err) {
  14. if(typeof err === "object" && err !== null && "name" in err) {
  15. print('test262/error ' + err.name + ': ' + err.message);
  16. } else {
  17. print('test262/error Test262Error: ' + err);
  18. }
  19. }
  20. function $DONE(err) {
  21. if (err) {
  22. $ERROR(err);
  23. }
  24. print('PASS');
  25. $.destroy();
  26. }
  27. function $LOG(str) {
  28. print(str);
  29. }
  30. function assert(mustBeTrue, message) {
  31. if (mustBeTrue === true) {
  32. return;
  33. }
  34. if (message === undefined) {
  35. message = 'Expected true but got ' + String(mustBeTrue);
  36. }
  37. $ERROR(message);
  38. }
  39. assert._isSameValue = function (a, b) {
  40. if (a === b) {
  41. // Handle +/-0 vs. -/+0
  42. return a !== 0 || 1 / a === 1 / b;
  43. }
  44. // Handle NaN vs. NaN
  45. return a !== a && b !== b;
  46. };
  47. assert.sameValue = function (actual, expected, message) {
  48. if (assert._isSameValue(actual, expected)) {
  49. return;
  50. }
  51. if (message === undefined) {
  52. message = '';
  53. } else {
  54. message += ' ';
  55. }
  56. message += 'Expected SameValue(«' + String(actual) + '», «' + String(expected) + '») to be true';
  57. $ERROR(message);
  58. };
  59. assert.notSameValue = function (actual, unexpected, message) {
  60. if (!assert._isSameValue(actual, unexpected)) {
  61. return;
  62. }
  63. if (message === undefined) {
  64. message = '';
  65. } else {
  66. message += ' ';
  67. }
  68. message += 'Expected SameValue(«' + String(actual) + '», «' + String(unexpected) + '») to be false';
  69. $ERROR(message);
  70. };
  71. assert.throws = function (expectedErrorConstructor, func, message) {
  72. if (typeof func !== "function") {
  73. $ERROR('assert.throws requires two arguments: the error constructor ' +
  74. 'and a function to run');
  75. return;
  76. }
  77. if (message === undefined) {
  78. message = '';
  79. } else {
  80. message += ' ';
  81. }
  82. try {
  83. func();
  84. } catch (thrown) {
  85. if (typeof thrown !== 'object' || thrown === null) {
  86. message += 'Thrown value was not an object!';
  87. $ERROR(message);
  88. } else if (thrown.constructor !== expectedErrorConstructor) {
  89. message += 'Expected a ' + expectedErrorConstructor.name + ' but got a ' + thrown.constructor.name;
  90. $ERROR(message);
  91. }
  92. return;
  93. }
  94. message += 'Expected a ' + expectedErrorConstructor.name + ' to be thrown but no exception was thrown at all';
  95. $ERROR(message);
  96. };
  97. assert.throws.early = function(err, code) {
  98. let wrappedCode = `function wrapperFn() { ${code} }`;
  99. let ieval = eval;
  100. assert.throws(err, () => { Function(wrappedCode); }, `Function: ${code}`);
  101. };
  102. // Create workers and start them all spinning. We set atomic slots to make
  103. // them go into a wait, thus controlling the waiting order. Then we wake them
  104. // one by one and observe the wakeup order.
  105. for (var i = 0; i < 3; i++) {
  106. $262.agent.start(
  107. `
  108. $262.agent.receiveBroadcast(function (sab) {
  109. var ia = new Int32Array(sab);
  110. while (Atomics.load(ia, ${i}) == 0);
  111. // the one below should produce 'not-equal'.
  112. // because ia[i] is no longer 0! (see the loop above)
  113. // otherwise it would wait until timeout
  114. $262.agent.report(${i} + Atomics.wait(ia, ${i}, 0));
  115. $262.agent.leaving();
  116. })
  117. `);
  118. }
  119. var ia = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT * 3));
  120. $262.agent.broadcast(ia.buffer);
  121. // the `while` loop above waits until we store a value below
  122. for (var i = 0; i < 3; i++) {
  123. Atomics.store(ia, i, 1);
  124. $262.agent.sleep(500);
  125. }
  126. for (var i = 0; i < 3; i++) {
  127. assert.sameValue(Atomics.load(ia, i), 1);
  128. assert.sameValue(getReport(), i + "not-equal");
  129. }
  130. function getReport() {
  131. var r;
  132. while ((r = $262.agent.getReport()) == null)
  133. $262.agent.sleep(100);
  134. return r;
  135. }
  136. ;$DONE();
  137. ;$.destroy();