test262/test/language
Peter Hoddie e4f4abdcb2
Compatibility with Hardened JavaScript (#4088)
This PR proposes changes to existing test262 tests to allow them to pass under Hardened JavaScript (see Secure ECMAScript proposal and Hardened JavaScript). Moddable uses Hardened JavaScript for JavaScript runtimes on resource constrained embedded devices, including those targeted by ECMA-419.

The changes fall into four groups:

1. Replace use of new Date() with new Date(1970). Scripts running inside a Compartment cannot retrieve the current time, so new Date() throws but new Date(1970) succeeds. Very few tests need the current time, but instead simply need a Date instance.
2. Use Object.defineProperty instead of setting existing built-in properties directly, such as toString and toValue. In Hardened JavaScript, prototypes of built-in objects are frozen. Consequently, setting properties of an instance that exist on the prototype throw (Hardened JavaScript is always in strict mode).
3. Eliminate use of Math.random(). Scripts running inside a Compartment cannot generate random numbers. One test identified so far uses Math.random() in a way that can easily be replaced with a counter.
4. Narrow the scope of exception tests. Consider the following

assert.throws(TypeError, () => {
  var s1 = new Date();
  s1.toString = Boolean.prototype.toString;
  s1.toString();
});

This test passes, but only because new Date() fails by throwing a TypeError. If the invocation of the Date constructor is resolved by (1) above, then the assignment to toString fails as per (2) above. The script should be modified as below to ensure that assert.throws only tests the intended statement, s1.toString(). The modified script tests the intended functionality and passes under Hardened JavaScript

var s1 = new Date(1970);
Object.defineProperty(s1, "toString", {
  value: Boolean.prototype.toString
});
assert.throws(TypeError, () => {
  s1.toString();
});

This is an initial PR to begin the process of adapting test262 for use with Hardened JavaScript. Further changes are expected, with the vast majority likely to fall into the four groups described above.

Thank you to gibson042, kriskowal, and erights for their advice on this work.
2024-07-04 08:19:23 -07:00
..
arguments-object Remove excess leading whitespace 2023-09-13 16:05:40 +02:00
asi chore: migrate $ERROR -> throw new Test262Error in test/language/a* (#3095) 2021-07-21 13:42:51 -07:00
block-scope Remove harness file, arrayContains.js 2021-11-01 12:55:10 -04:00
comments Throw distinctive errors for different failures 2021-09-02 10:36:00 -04:00
computed-property-names Transform compareArray -> assert.compareArray: test/language/**/*.js (#3239) 2021-10-15 20:35:01 -04:00
destructuring/binding Improved metadata 2020-11-18 16:44:55 -05:00
directive-prologue Fix existing linting errors, update error phase, and add a new flags linter (#2086) 2019-03-06 12:24:44 -05:00
eval-code Use verifyProperty in language/eval-code tests 2023-09-13 16:05:40 +02:00
export Update "info" field (#2941) 2021-01-28 16:03:01 -08:00
expressions Compatibility with Hardened JavaScript (#4088) 2024-07-04 08:19:23 -07:00
function-code Remove excess leading whitespace 2023-09-13 16:05:40 +02:00
future-reserved-words Strengthen sloppy-mode tests for future reserved words. 2021-04-20 09:54:23 -04:00
global-code Allow global var-via-eval be declared 2024-04-11 17:22:43 -07:00
identifier-resolution chore: migrate $ERROR -> throw new Test262Error in test/language/i* (#3102) 2021-07-28 13:46:58 -07:00
identifiers Add Unicode v15.1.0-sensitive `IdentifierPart` tests (#3833) 2023-09-15 12:27:42 +02:00
import Add readmes 2023-10-26 18:21:19 -07:00
keywords Rename to $DONOTEVALUATE per @leobalter's suggestion 2018-10-23 13:51:17 +02:00
line-terminators chore: migrate $ERROR -> throw new Test262Error in test/language/line-terminators/* (#3103) 2021-07-28 13:46:23 -07:00
literals Test parsing error when using unicode escape sequences to express i, m, s 2024-03-05 12:56:48 -08:00
module-code Avoid duplicate binding import-source-binding-name.js (#4121) 2024-06-28 15:14:23 +02:00
punctuators Rename to $DONOTEVALUATE per @leobalter's suggestion 2018-10-23 13:51:17 +02:00
reserved-words Rename to $DONOTEVALUATE per @leobalter's suggestion 2018-10-23 13:51:17 +02:00
rest-parameters Transform compareArray -> assert.compareArray: test/language/**/*.js (#3239) 2021-10-15 20:35:01 -04:00
source-text chore: migrate $ERROR -> throw new Test262Error in test/language/s* 2021-07-29 10:38:53 -04:00
statementList Remove superfluous EmptyStatement 2021-10-05 16:28:06 -04:00
statements Compatibility with Hardened JavaScript (#4088) 2024-07-04 08:19:23 -07:00
types Line Terminator test description corrections 2022-11-08 10:37:37 -08:00
white-space chore: migrate $ERROR -> throw new Test262Error in test/language/white-space/* (#3107) 2021-07-28 13:43:40 -07:00