From 2c8aa80362c270b90ccb16caae3807fded3101f2 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Thu, 3 Jun 2021 17:31:26 +0200 Subject: [PATCH] Polyfill: Add missing brand checks in TimeZone. --- polyfill/lib/timezone.mjs | 2 ++ .../prototype/getInstantFor/branding.js | 21 +++++++++++ .../prototype/getOffsetStringFor/branding.js | 21 +++++++++++ .../getOffsetStringFor/custom-timezone.js | 35 ------------------- polyfill/test/usertimezone.mjs | 6 ---- 5 files changed, 44 insertions(+), 41 deletions(-) create mode 100644 polyfill/test/TimeZone/prototype/getInstantFor/branding.js create mode 100644 polyfill/test/TimeZone/prototype/getOffsetStringFor/branding.js delete mode 100644 polyfill/test/TimeZone/prototype/getOffsetStringFor/custom-timezone.js diff --git a/polyfill/lib/timezone.mjs b/polyfill/lib/timezone.mjs index 69783ebce4..55c2f7c117 100644 --- a/polyfill/lib/timezone.mjs +++ b/polyfill/lib/timezone.mjs @@ -54,6 +54,7 @@ export class TimeZone { return ES.GetIANATimeZoneOffsetNanoseconds(GetSlot(instant, EPOCHNANOSECONDS), id); } getOffsetStringFor(instant) { + if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver'); instant = ES.ToTemporalInstant(instant); return ES.BuiltinTimeZoneGetOffsetStringFor(this, instant); } @@ -63,6 +64,7 @@ export class TimeZone { return ES.BuiltinTimeZoneGetPlainDateTimeFor(this, instant, calendar); } getInstantFor(dateTime, options = undefined) { + if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver'); dateTime = ES.ToTemporalDateTime(dateTime); options = ES.GetOptionsObject(options); const disambiguation = ES.ToTemporalDisambiguation(options); diff --git a/polyfill/test/TimeZone/prototype/getInstantFor/branding.js b/polyfill/test/TimeZone/prototype/getInstantFor/branding.js new file mode 100644 index 0000000000..38978a2266 --- /dev/null +++ b/polyfill/test/TimeZone/prototype/getInstantFor/branding.js @@ -0,0 +1,21 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.timezone.prototype.getinstantfor +features: [Symbol] +---*/ + +const getInstantFor = Temporal.TimeZone.prototype.getInstantFor; + +assert.sameValue(typeof getInstantFor, "function"); + +assert.throws(TypeError, () => getInstantFor.call(undefined), "undefined"); +assert.throws(TypeError, () => getInstantFor.call(null), "null"); +assert.throws(TypeError, () => getInstantFor.call(true), "true"); +assert.throws(TypeError, () => getInstantFor.call(""), "empty string"); +assert.throws(TypeError, () => getInstantFor.call(Symbol()), "symbol"); +assert.throws(TypeError, () => getInstantFor.call(1), "1"); +assert.throws(TypeError, () => getInstantFor.call({}), "plain object"); +assert.throws(TypeError, () => getInstantFor.call(Temporal.TimeZone), "Temporal.TimeZone"); +assert.throws(TypeError, () => getInstantFor.call(Temporal.TimeZone.prototype), "Temporal.TimeZone.prototype"); diff --git a/polyfill/test/TimeZone/prototype/getOffsetStringFor/branding.js b/polyfill/test/TimeZone/prototype/getOffsetStringFor/branding.js new file mode 100644 index 0000000000..8ade1f1215 --- /dev/null +++ b/polyfill/test/TimeZone/prototype/getOffsetStringFor/branding.js @@ -0,0 +1,21 @@ +// Copyright (C) 2021 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.timezone.prototype.getoffsetstringfor +features: [Symbol] +---*/ + +const getOffsetStringFor = Temporal.TimeZone.prototype.getOffsetStringFor; + +assert.sameValue(typeof getOffsetStringFor, "function"); + +assert.throws(TypeError, () => getOffsetStringFor.call(undefined), "undefined"); +assert.throws(TypeError, () => getOffsetStringFor.call(null), "null"); +assert.throws(TypeError, () => getOffsetStringFor.call(true), "true"); +assert.throws(TypeError, () => getOffsetStringFor.call(""), "empty string"); +assert.throws(TypeError, () => getOffsetStringFor.call(Symbol()), "symbol"); +assert.throws(TypeError, () => getOffsetStringFor.call(1), "1"); +assert.throws(TypeError, () => getOffsetStringFor.call({}), "plain object"); +assert.throws(TypeError, () => getOffsetStringFor.call(Temporal.TimeZone), "Temporal.TimeZone"); +assert.throws(TypeError, () => getOffsetStringFor.call(Temporal.TimeZone.prototype), "Temporal.TimeZone.prototype"); diff --git a/polyfill/test/TimeZone/prototype/getOffsetStringFor/custom-timezone.js b/polyfill/test/TimeZone/prototype/getOffsetStringFor/custom-timezone.js deleted file mode 100644 index dfc8f8a491..0000000000 --- a/polyfill/test/TimeZone/prototype/getOffsetStringFor/custom-timezone.js +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (C) 2020 Igalia, S.L. All rights reserved. -// This code is governed by the BSD license found in the LICENSE file. - -/*--- -esid: sec-temporal.timezone.prototype.getoffsetstringfor -includes: [compareArray.js] ----*/ - -const actual = []; -const expected = [ - "get timeZone.getOffsetNanosecondsFor", - "call timeZone.getOffsetNanosecondsFor", -]; - -const instant = Temporal.Instant.from("1975-02-02T14:25:36.123456789Z"); -const timeZone = new Proxy({ - getOffsetNanosecondsFor(instantArg) { - actual.push("call timeZone.getOffsetNanosecondsFor"); - assert.sameValue(instantArg, instant); - return 9876543210123; - }, -}, { - has(target, property) { - actual.push(`has timeZone.${property}`); - return property in target; - }, - get(target, property) { - actual.push(`get timeZone.${property}`); - return target[property]; - }, -}); - -const result = Temporal.TimeZone.prototype.getOffsetStringFor.call(timeZone, instant); -assert.sameValue(result, "+02:44:36.543210123"); -assert.compareArray(actual, expected); diff --git a/polyfill/test/usertimezone.mjs b/polyfill/test/usertimezone.mjs index 5c144d2c1f..2905780700 100644 --- a/polyfill/test/usertimezone.mjs +++ b/polyfill/test/usertimezone.mjs @@ -94,10 +94,7 @@ describe('Userland time zone', () => { }; const inst = Temporal.Instant.fromEpochNanoseconds(0n); - const dt = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 456, 789); - it('has offset string +00:00', () => - equal(Temporal.TimeZone.prototype.getOffsetStringFor.call(obj, inst), '+00:00')); it('converts to DateTime', () => { equal(`${Temporal.TimeZone.prototype.getPlainDateTimeFor.call(obj, inst)}`, '1970-01-01T00:00:00'); equal( @@ -105,9 +102,6 @@ describe('Userland time zone', () => { '1970-01-01T00:00:00[u-ca=gregory]' ); }); - it('converts to Instant', () => { - equal(`${Temporal.TimeZone.prototype.getInstantFor.call(obj, dt)}`, '1976-11-18T15:23:30.123456789Z'); - }); it('offset prints in instant.toString', () => equal(inst.toString({ timeZone: obj }), '1970-01-01T00:00:00+00:00')); it('prints in zdt.toString', () => { const zdt = new Temporal.ZonedDateTime(0n, obj);