Skip to content

Commit

Permalink
fixup! Polyfill: Be more robust against late-run primordial manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
justingrant committed Mar 5, 2024
1 parent eccd27f commit d55f393
Showing 1 changed file with 11 additions and 24 deletions.
35 changes: 11 additions & 24 deletions lib/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,9 @@ const MathAbs = Math.abs;
const MathFloor = Math.floor;
const ObjectCreate = Object.create;
const ObjectEntries = Object.entries;
const OriginalMap = Map;
const OriginalSet = Set;
const OriginalWeakMap = WeakMap;
const ReflectOwnKeys = Reflect.ownKeys;
const MapPrototypeEntries = Map.prototype.entries;
const MapPrototypeGet = Map.prototype.get;
const MapPrototypeSet = Map.prototype.set;
const SetPrototypeAdd = Set.prototype.add;
const WeakMapPrototypeGet = WeakMap.prototype.get;
const WeakMapPrototypeSet = WeakMap.prototype.set;

const MapIterator = ES.Call(MapPrototypeEntries, new Map(), []);
const MapIteratorPrototypeNext = MapIterator.next;

function arrayFromSet<T>(src: Set<T>): T[] {
return [...src];
Expand Down Expand Up @@ -194,7 +184,7 @@ export class Calendar implements Temporal.Calendar {
fields(fields: Params['fields'][0]): Return['fields'] {
if (!ES.IsTemporalCalendar(this)) throw new TypeError('invalid receiver');
const fieldsArray = [] as string[];
const allowed = new Set(['year', 'month', 'monthCode', 'day']);
const allowed = new OriginalSet<string>(['year', 'month', 'monthCode', 'day']);
for (const name of fields) {
if (typeof name !== 'string') throw new TypeError('invalid fields');
if (!allowed.has(name)) throw new RangeError(`invalid field name ${name}`);
Expand Down Expand Up @@ -619,7 +609,7 @@ type CachedTypes = Temporal.PlainYearMonth | Temporal.PlainDate | Temporal.Plain
* because each object's cache is thrown away when the object is GC-ed.
*/
class OneObjectCache {
map = new OriginalMap();
map = new Map();
calls = 0;
now: number;
hits = 0;
Expand All @@ -628,17 +618,14 @@ class OneObjectCache {
this.now = globalThis.performance ? globalThis.performance.now() : Date.now();
if (cacheToClone !== undefined) {
let i = 0;
const entriesIterator = ES.Call(MapPrototypeEntries, cacheToClone.map, []);
for (;;) {
const iterResult = ES.Call(MapIteratorPrototypeNext, entriesIterator, []);
if (iterResult.done) break;
for (const entry of cacheToClone.map.entries()) {
if (++i > OneObjectCache.MAX_CACHE_ENTRIES) break;
ES.Call(MapPrototypeSet, this.map, iterResult.value);
this.map.set(...entry);
}
}
}
get(key: string) {
const result = ES.Call(MapPrototypeGet, this.map, [key]);
const result = this.map.get(key);
if (result) {
this.hits++;
this.report();
Expand All @@ -647,7 +634,7 @@ class OneObjectCache {
return result;
}
set(key: string, value: unknown) {
ES.Call(MapPrototypeSet, this.map, [key, value]);
this.map.set(key, value);
this.misses++;
this.report();
}
Expand All @@ -660,12 +647,12 @@ class OneObjectCache {
*/
}
setObject(obj: CachedTypes) {
if (ES.Call(WeakMapPrototypeGet, OneObjectCache.objectMap, [obj])) throw new RangeError('object already cached');
ES.Call(WeakMapPrototypeSet, OneObjectCache.objectMap, [obj, this]);
if (OneObjectCache.objectMap.get(obj)) throw new RangeError('object already cached');
OneObjectCache.objectMap.set(obj, this);
this.report();
}

static objectMap = new OriginalWeakMap();
static objectMap = new WeakMap();
static MAX_CACHE_ENTRIES = 1000;

/**
Expand All @@ -675,10 +662,10 @@ class OneObjectCache {
* @param obj - object to associate with the cache
*/
static getCacheForObject(obj: CachedTypes) {
let cache = ES.Call(WeakMapPrototypeGet, OneObjectCache.objectMap, [obj]);
let cache = OneObjectCache.objectMap.get(obj);
if (!cache) {
cache = new OneObjectCache();
ES.Call(WeakMapPrototypeSet, OneObjectCache.objectMap, [obj, cache]);
OneObjectCache.objectMap.set(obj, cache);
}
return cache;
}
Expand Down

0 comments on commit d55f393

Please sign in to comment.