Skip to content

Commit

Permalink
Fix util import errors in Test262
Browse files Browse the repository at this point in the history
tc39#715 broke Test262 tests (specifically the ability to
`import * from 'es-abstract'), and I couldn't figure out how
to import individual es-abstract js functions from the PoC
TypeScript code, so I gave up and hacked up the three es-abstract
methods (ToString, ToObject, and ToInteger) that the PoC uses.
A real JS implementation will simply depend on ecmascript.js, so it
wasn't worth doing a real implementation. I apologize for how bad
this placeholder code is. ;-(
  • Loading branch information
justingrant committed Jul 8, 2020
1 parent 2206102 commit df40949
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
24 changes: 20 additions & 4 deletions polyfill/lib/localdatetime.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import ES2019 from 'es-abstract/es2019.js';
import { GetIntrinsic, MakeIntrinsicClass } from './intrinsicclass.mjs';

const Temporal = {
Expand Down Expand Up @@ -1110,9 +1109,26 @@ function toLargestTemporalUnit(options, fallback, disallowedStrings = []) {
}

const ES = {
ToInteger: ES2019.ToInteger,
ToString: ES2019.ToString,
ToObject: ES2019.ToObject,
// The three hacky conversion implementations below are placeholders because I
// couldn't figure out how to import es-abstract methods in TypeScript without
// triggering failures in test262 scripts caused by `util` being missing. And
// I was too lazy to copy/paste the es-abstract implementations. The real
// implementation in JS won't have these problems.
ToInteger: (value) => {
if (typeof value === 'symbol') throw new TypeError('Cannot convert a Symbol value to a number');
if (typeof value === 'boolean') throw new TypeError('Cannot convert a boolean value to an integer');
if (typeof value === 'undefined') return 0;
if (value === 'null') throw new TypeError('Cannot convert a null value to an integer');
const n = Number(value);
if (isNaN(n)) throw new TypeError('Cannot convert value to an integer');
return Math.floor(n);
},
ToString: (s) => Object(s).toString(),
ToObject: (s) => {
if (s == null) throw new TypeError(`Cannot coerce ${s} to Object`);
if (typeof s !== 'object') throw new TypeError(`Expected object, received ${typeof s}`);
return s;
},

// replace this with public parsing API after it lands
ParseFullISOString: (isoString) => {
Expand Down
25 changes: 21 additions & 4 deletions polyfill/lib/poc/LocalDateTime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Temporal } from '../..';
import * as ES2019 from 'es-abstract';
// import { ToInteger, ToObject, ToString } from 'es-abstract';

export type LocalDateTimeLike = Temporal.DateTimeLike & {
/**`Temporal.TimeZone`, IANA time zone identifier, or offset string */
Expand Down Expand Up @@ -1295,9 +1295,26 @@ function toLargestTemporalUnit<V extends keyof Temporal.DurationLike, F extends
}

const ES = {
ToInteger: ES2019.ToInteger,
ToString: ES2019.ToString,
ToObject: ES2019.ToObject,
// The three hacky conversion implementations below are placeholders because I
// couldn't figure out how to import es-abstract methods in TypeScript without
// triggering failures in test262 scripts caused by `util` being missing. And
// I was too lazy to copy/paste the es-abstract implementations. The real
// implementation in JS won't have these problems.
ToInteger: (value: unknown) => {
if (typeof value === 'symbol') throw new TypeError('Cannot convert a Symbol value to a number');
if (typeof value === 'boolean') throw new TypeError('Cannot convert a boolean value to an integer');
if (typeof value === 'undefined') return 0;
if (value === 'null') throw new TypeError('Cannot convert a null value to an integer');
const n = Number(value);
if (isNaN(n)) throw new TypeError('Cannot convert value to an integer');
return Math.floor(n);
},
ToString: (s: unknown) => Object(s).toString(),
ToObject: (s: unknown) => {
if (s == null) throw new TypeError(`Cannot coerce ${s} to Object`);
if (typeof s !== 'object') throw new TypeError(`Expected object, received ${typeof s}`);
return s;
},

// replace this with public parsing API after it lands
ParseFullISOString: (isoString: string) => {
Expand Down
2 changes: 1 addition & 1 deletion polyfill/lib/poc/postProcess.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function processDts(file, original, final) {
function processMjs(file) {
let lines = readAllLines(file);
const prepend = [
"import ES2019 from 'es-abstract/es2019.js';",
// "import ES2019 from 'es-abstract/es2019.js';",
"import { GetIntrinsic, MakeIntrinsicClass } from './intrinsicclass.mjs'",
'',
'const Temporal = {',
Expand Down

0 comments on commit df40949

Please sign in to comment.