test262/src
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
..
accessor-names Add cases for default identifier names in accessor-names 2019-08-14 17:02:03 -04:00
annex-b-fns Remove es6id from generation files 2019-08-05 10:54:37 -04:00
arguments Add templates for trailing comma in arguments list 2018-09-04 17:10:51 -04:00
assignment-target-type Add tests for Source Phase Imports (#3980) 2024-06-27 14:58:04 +02:00
async-functions Separate cases 2020-10-05 10:53:43 -04:00
async-generators Fix typo 2022-11-30 16:04:02 -08:00
class-elements Replace Object.hasOwnProperty.call with Object.prototype.hasOwnProperty.call 2022-11-30 16:04:02 -08:00
compound-assignment-private Add `Array.prototype.includes` and `exponentiation` features (#3799) 2023-03-23 10:51:08 +01:00
computed-property-names Add `Array.prototype.includes` and `exponentiation` features (#3799) 2023-03-23 10:51:08 +01:00
declarations naming 2018-12-20 12:53:31 -05:00
decorator Fixup template files 2023-10-05 10:23:12 -07:00
direct-eval-code Remove template that is generating duplicate tests 2020-10-29 13:48:50 -04:00
dstr-assignment Remove unused functions 2021-10-13 14:18:32 -04:00
dstr-assignment-for-await chore: migrate $ERROR -> throw new Test262Error in src/* 2021-07-22 10:56:15 -04:00
dstr-binding Array binding: add rest element direct binding test case 2020-08-20 12:48:08 -04:00
dstr-binding-for-await Rename to $DONOTEVALUATE per @leobalter's suggestion 2018-10-23 13:51:17 +02:00
dynamic-import Add tests for Source Phase Imports (#3980) 2024-06-27 14:58:04 +02:00
function-forms Refactor simple cases of (IIFE/async function).then(\$DONE, \$DONE); to use asyncTest 2023-02-21 11:04:09 -08:00
generators Replace Object.hasOwnProperty.call with Object.prototype.hasOwnProperty.call 2022-11-30 16:04:02 -08:00
identifier-names Fixed spelling error identifer -> identifier. 2022-06-21 10:14:17 +02:00
insignificant-input-elements Add a maintenance file for src/insignificant-input-elements 2021-07-16 09:49:17 -04:00
invalid-private-names Remove invalid private name templates 2019-01-30 06:44:10 -08:00
logical-assignment-private Fix tests for private reference with logical assignment 2022-02-21 15:38:59 -05:00
spread Replace Object.hasOwnProperty.call with Object.prototype.hasOwnProperty.call 2022-11-30 16:04:02 -08:00
statementList Remove superfluous EmptyStatement 2021-10-05 16:28:06 -04:00
subclass-builtins Compatibility with Hardened JavaScript (#4088) 2024-07-04 08:19:23 -07:00
top-level-await Add missing "flags: [module]" to top level await tests 2019-10-03 11:35:48 +02:00