Skip to content

Commit

Permalink
[Refactor] GetIntrinsic: remove the internal property salts, since …
Browse files Browse the repository at this point in the history
…% already handles that
  • Loading branch information
ljharb committed Dec 16, 2019
1 parent 84c50fb commit 3567ae9
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 110 deletions.
220 changes: 110 additions & 110 deletions GetIntrinsic.js
Expand Up @@ -44,115 +44,115 @@ var asyncGenIterator = asyncGen ? asyncGen() : undefined;
var TypedArray = typeof Uint8Array === 'undefined' ? undefined : getProto(Uint8Array);

var INTRINSICS = {
'$ %Array%': Array,
'$ %ArrayBuffer%': typeof ArrayBuffer === 'undefined' ? undefined : ArrayBuffer,
'$ %ArrayBufferPrototype%': typeof ArrayBuffer === 'undefined' ? undefined : ArrayBuffer.prototype,
'$ %ArrayIteratorPrototype%': hasSymbols ? getProto([][Symbol.iterator]()) : undefined,
'$ %ArrayPrototype%': Array.prototype,
'$ %ArrayProto_entries%': Array.prototype.entries,
'$ %ArrayProto_forEach%': Array.prototype.forEach,
'$ %ArrayProto_keys%': Array.prototype.keys,
'$ %ArrayProto_values%': Array.prototype.values,
'$ %AsyncFromSyncIteratorPrototype%': undefined,
'$ %AsyncFunction%': asyncFunction,
'$ %AsyncFunctionPrototype%': asyncFunction ? asyncFunction.prototype : undefined,
'$ %AsyncGenerator%': asyncGen ? getProto(asyncGenIterator) : undefined,
'$ %AsyncGeneratorFunction%': asyncGenFunction,
'$ %AsyncGeneratorPrototype%': asyncGenFunction ? asyncGenFunction.prototype : undefined,
'$ %AsyncIteratorPrototype%': asyncGenIterator && hasSymbols && Symbol.asyncIterator ? asyncGenIterator[Symbol.asyncIterator]() : undefined,
'$ %Atomics%': typeof Atomics === 'undefined' ? undefined : Atomics,
'$ %Boolean%': Boolean,
'$ %BooleanPrototype%': Boolean.prototype,
'$ %DataView%': typeof DataView === 'undefined' ? undefined : DataView,
'$ %DataViewPrototype%': typeof DataView === 'undefined' ? undefined : DataView.prototype,
'$ %Date%': Date,
'$ %DatePrototype%': Date.prototype,
'$ %decodeURI%': decodeURI,
'$ %decodeURIComponent%': decodeURIComponent,
'$ %encodeURI%': encodeURI,
'$ %encodeURIComponent%': encodeURIComponent,
'$ %Error%': Error,
'$ %ErrorPrototype%': Error.prototype,
'$ %eval%': eval, // eslint-disable-line no-eval
'$ %EvalError%': EvalError,
'$ %EvalErrorPrototype%': EvalError.prototype,
'$ %Float32Array%': typeof Float32Array === 'undefined' ? undefined : Float32Array,
'$ %Float32ArrayPrototype%': typeof Float32Array === 'undefined' ? undefined : Float32Array.prototype,
'$ %Float64Array%': typeof Float64Array === 'undefined' ? undefined : Float64Array,
'$ %Float64ArrayPrototype%': typeof Float64Array === 'undefined' ? undefined : Float64Array.prototype,
'$ %Function%': Function,
'$ %FunctionPrototype%': Function.prototype,
'$ %Generator%': generator ? getProto(generator()) : undefined,
'$ %GeneratorFunction%': generatorFunction,
'$ %GeneratorPrototype%': generatorFunction ? generatorFunction.prototype : undefined,
'$ %Int8Array%': typeof Int8Array === 'undefined' ? undefined : Int8Array,
'$ %Int8ArrayPrototype%': typeof Int8Array === 'undefined' ? undefined : Int8Array.prototype,
'$ %Int16Array%': typeof Int16Array === 'undefined' ? undefined : Int16Array,
'$ %Int16ArrayPrototype%': typeof Int16Array === 'undefined' ? undefined : Int8Array.prototype,
'$ %Int32Array%': typeof Int32Array === 'undefined' ? undefined : Int32Array,
'$ %Int32ArrayPrototype%': typeof Int32Array === 'undefined' ? undefined : Int32Array.prototype,
'$ %isFinite%': isFinite,
'$ %isNaN%': isNaN,
'$ %IteratorPrototype%': hasSymbols ? getProto(getProto([][Symbol.iterator]())) : undefined,
'$ %JSON%': typeof JSON === 'object' ? JSON : undefined,
'$ %JSONParse%': typeof JSON === 'object' ? JSON.parse : undefined,
'$ %Map%': typeof Map === 'undefined' ? undefined : Map,
'$ %MapIteratorPrototype%': typeof Map === 'undefined' || !hasSymbols ? undefined : getProto(new Map()[Symbol.iterator]()),
'$ %MapPrototype%': typeof Map === 'undefined' ? undefined : Map.prototype,
'$ %Math%': Math,
'$ %Number%': Number,
'$ %NumberPrototype%': Number.prototype,
'$ %Object%': Object,
'$ %ObjectPrototype%': Object.prototype,
'$ %ObjProto_toString%': Object.prototype.toString,
'$ %ObjProto_valueOf%': Object.prototype.valueOf,
'$ %parseFloat%': parseFloat,
'$ %parseInt%': parseInt,
'$ %Promise%': typeof Promise === 'undefined' ? undefined : Promise,
'$ %PromisePrototype%': typeof Promise === 'undefined' ? undefined : Promise.prototype,
'$ %PromiseProto_then%': typeof Promise === 'undefined' ? undefined : Promise.prototype.then,
'$ %Promise_all%': typeof Promise === 'undefined' ? undefined : Promise.all,
'$ %Promise_reject%': typeof Promise === 'undefined' ? undefined : Promise.reject,
'$ %Promise_resolve%': typeof Promise === 'undefined' ? undefined : Promise.resolve,
'$ %Proxy%': typeof Proxy === 'undefined' ? undefined : Proxy,
'$ %RangeError%': RangeError,
'$ %RangeErrorPrototype%': RangeError.prototype,
'$ %ReferenceError%': ReferenceError,
'$ %ReferenceErrorPrototype%': ReferenceError.prototype,
'$ %Reflect%': typeof Reflect === 'undefined' ? undefined : Reflect,
'$ %RegExp%': RegExp,
'$ %RegExpPrototype%': RegExp.prototype,
'$ %Set%': typeof Set === 'undefined' ? undefined : Set,
'$ %SetIteratorPrototype%': typeof Set === 'undefined' || !hasSymbols ? undefined : getProto(new Set()[Symbol.iterator]()),
'$ %SetPrototype%': typeof Set === 'undefined' ? undefined : Set.prototype,
'$ %SharedArrayBuffer%': typeof SharedArrayBuffer === 'undefined' ? undefined : SharedArrayBuffer,
'$ %SharedArrayBufferPrototype%': typeof SharedArrayBuffer === 'undefined' ? undefined : SharedArrayBuffer.prototype,
'$ %String%': String,
'$ %StringIteratorPrototype%': hasSymbols ? getProto(''[Symbol.iterator]()) : undefined,
'$ %StringPrototype%': String.prototype,
'$ %Symbol%': hasSymbols ? Symbol : undefined,
'$ %SymbolPrototype%': hasSymbols ? Symbol.prototype : undefined,
'$ %SyntaxError%': SyntaxError,
'$ %SyntaxErrorPrototype%': SyntaxError.prototype,
'$ %ThrowTypeError%': ThrowTypeError,
'$ %TypedArray%': TypedArray,
'$ %TypedArrayPrototype%': TypedArray ? TypedArray.prototype : undefined,
'$ %TypeError%': $TypeError,
'$ %TypeErrorPrototype%': $TypeError.prototype,
'$ %Uint8Array%': typeof Uint8Array === 'undefined' ? undefined : Uint8Array,
'$ %Uint8ArrayPrototype%': typeof Uint8Array === 'undefined' ? undefined : Uint8Array.prototype,
'$ %Uint8ClampedArray%': typeof Uint8ClampedArray === 'undefined' ? undefined : Uint8ClampedArray,
'$ %Uint8ClampedArrayPrototype%': typeof Uint8ClampedArray === 'undefined' ? undefined : Uint8ClampedArray.prototype,
'$ %Uint16Array%': typeof Uint16Array === 'undefined' ? undefined : Uint16Array,
'$ %Uint16ArrayPrototype%': typeof Uint16Array === 'undefined' ? undefined : Uint16Array.prototype,
'$ %Uint32Array%': typeof Uint32Array === 'undefined' ? undefined : Uint32Array,
'$ %Uint32ArrayPrototype%': typeof Uint32Array === 'undefined' ? undefined : Uint32Array.prototype,
'$ %URIError%': URIError,
'$ %URIErrorPrototype%': URIError.prototype,
'$ %WeakMap%': typeof WeakMap === 'undefined' ? undefined : WeakMap,
'$ %WeakMapPrototype%': typeof WeakMap === 'undefined' ? undefined : WeakMap.prototype,
'$ %WeakSet%': typeof WeakSet === 'undefined' ? undefined : WeakSet,
'$ %WeakSetPrototype%': typeof WeakSet === 'undefined' ? undefined : WeakSet.prototype
'%Array%': Array,
'%ArrayBuffer%': typeof ArrayBuffer === 'undefined' ? undefined : ArrayBuffer,
'%ArrayBufferPrototype%': typeof ArrayBuffer === 'undefined' ? undefined : ArrayBuffer.prototype,
'%ArrayIteratorPrototype%': hasSymbols ? getProto([][Symbol.iterator]()) : undefined,
'%ArrayPrototype%': Array.prototype,
'%ArrayProto_entries%': Array.prototype.entries,
'%ArrayProto_forEach%': Array.prototype.forEach,
'%ArrayProto_keys%': Array.prototype.keys,
'%ArrayProto_values%': Array.prototype.values,
'%AsyncFromSyncIteratorPrototype%': undefined,
'%AsyncFunction%': asyncFunction,
'%AsyncFunctionPrototype%': asyncFunction ? asyncFunction.prototype : undefined,
'%AsyncGenerator%': asyncGen ? getProto(asyncGenIterator) : undefined,
'%AsyncGeneratorFunction%': asyncGenFunction,
'%AsyncGeneratorPrototype%': asyncGenFunction ? asyncGenFunction.prototype : undefined,
'%AsyncIteratorPrototype%': asyncGenIterator && hasSymbols && Symbol.asyncIterator ? asyncGenIterator[Symbol.asyncIterator]() : undefined,
'%Atomics%': typeof Atomics === 'undefined' ? undefined : Atomics,
'%Boolean%': Boolean,
'%BooleanPrototype%': Boolean.prototype,
'%DataView%': typeof DataView === 'undefined' ? undefined : DataView,
'%DataViewPrototype%': typeof DataView === 'undefined' ? undefined : DataView.prototype,
'%Date%': Date,
'%DatePrototype%': Date.prototype,
'%decodeURI%': decodeURI,
'%decodeURIComponent%': decodeURIComponent,
'%encodeURI%': encodeURI,
'%encodeURIComponent%': encodeURIComponent,
'%Error%': Error,
'%ErrorPrototype%': Error.prototype,
'%eval%': eval, // eslint-disable-line no-eval
'%EvalError%': EvalError,
'%EvalErrorPrototype%': EvalError.prototype,
'%Float32Array%': typeof Float32Array === 'undefined' ? undefined : Float32Array,
'%Float32ArrayPrototype%': typeof Float32Array === 'undefined' ? undefined : Float32Array.prototype,
'%Float64Array%': typeof Float64Array === 'undefined' ? undefined : Float64Array,
'%Float64ArrayPrototype%': typeof Float64Array === 'undefined' ? undefined : Float64Array.prototype,
'%Function%': Function,
'%FunctionPrototype%': Function.prototype,
'%Generator%': generator ? getProto(generator()) : undefined,
'%GeneratorFunction%': generatorFunction,
'%GeneratorPrototype%': generatorFunction ? generatorFunction.prototype : undefined,
'%Int8Array%': typeof Int8Array === 'undefined' ? undefined : Int8Array,
'%Int8ArrayPrototype%': typeof Int8Array === 'undefined' ? undefined : Int8Array.prototype,
'%Int16Array%': typeof Int16Array === 'undefined' ? undefined : Int16Array,
'%Int16ArrayPrototype%': typeof Int16Array === 'undefined' ? undefined : Int8Array.prototype,
'%Int32Array%': typeof Int32Array === 'undefined' ? undefined : Int32Array,
'%Int32ArrayPrototype%': typeof Int32Array === 'undefined' ? undefined : Int32Array.prototype,
'%isFinite%': isFinite,
'%isNaN%': isNaN,
'%IteratorPrototype%': hasSymbols ? getProto(getProto([][Symbol.iterator]())) : undefined,
'%JSON%': typeof JSON === 'object' ? JSON : undefined,
'%JSONParse%': typeof JSON === 'object' ? JSON.parse : undefined,
'%Map%': typeof Map === 'undefined' ? undefined : Map,
'%MapIteratorPrototype%': typeof Map === 'undefined' || !hasSymbols ? undefined : getProto(new Map()[Symbol.iterator]()),
'%MapPrototype%': typeof Map === 'undefined' ? undefined : Map.prototype,
'%Math%': Math,
'%Number%': Number,
'%NumberPrototype%': Number.prototype,
'%Object%': Object,
'%ObjectPrototype%': Object.prototype,
'%ObjProto_toString%': Object.prototype.toString,
'%ObjProto_valueOf%': Object.prototype.valueOf,
'%parseFloat%': parseFloat,
'%parseInt%': parseInt,
'%Promise%': typeof Promise === 'undefined' ? undefined : Promise,
'%PromisePrototype%': typeof Promise === 'undefined' ? undefined : Promise.prototype,
'%PromiseProto_then%': typeof Promise === 'undefined' ? undefined : Promise.prototype.then,
'%Promise_all%': typeof Promise === 'undefined' ? undefined : Promise.all,
'%Promise_reject%': typeof Promise === 'undefined' ? undefined : Promise.reject,
'%Promise_resolve%': typeof Promise === 'undefined' ? undefined : Promise.resolve,
'%Proxy%': typeof Proxy === 'undefined' ? undefined : Proxy,
'%RangeError%': RangeError,
'%RangeErrorPrototype%': RangeError.prototype,
'%ReferenceError%': ReferenceError,
'%ReferenceErrorPrototype%': ReferenceError.prototype,
'%Reflect%': typeof Reflect === 'undefined' ? undefined : Reflect,
'%RegExp%': RegExp,
'%RegExpPrototype%': RegExp.prototype,
'%Set%': typeof Set === 'undefined' ? undefined : Set,
'%SetIteratorPrototype%': typeof Set === 'undefined' || !hasSymbols ? undefined : getProto(new Set()[Symbol.iterator]()),
'%SetPrototype%': typeof Set === 'undefined' ? undefined : Set.prototype,
'%SharedArrayBuffer%': typeof SharedArrayBuffer === 'undefined' ? undefined : SharedArrayBuffer,
'%SharedArrayBufferPrototype%': typeof SharedArrayBuffer === 'undefined' ? undefined : SharedArrayBuffer.prototype,
'%String%': String,
'%StringIteratorPrototype%': hasSymbols ? getProto(''[Symbol.iterator]()) : undefined,
'%StringPrototype%': String.prototype,
'%Symbol%': hasSymbols ? Symbol : undefined,
'%SymbolPrototype%': hasSymbols ? Symbol.prototype : undefined,
'%SyntaxError%': SyntaxError,
'%SyntaxErrorPrototype%': SyntaxError.prototype,
'%ThrowTypeError%': ThrowTypeError,
'%TypedArray%': TypedArray,
'%TypedArrayPrototype%': TypedArray ? TypedArray.prototype : undefined,
'%TypeError%': $TypeError,
'%TypeErrorPrototype%': $TypeError.prototype,
'%Uint8Array%': typeof Uint8Array === 'undefined' ? undefined : Uint8Array,
'%Uint8ArrayPrototype%': typeof Uint8Array === 'undefined' ? undefined : Uint8Array.prototype,
'%Uint8ClampedArray%': typeof Uint8ClampedArray === 'undefined' ? undefined : Uint8ClampedArray,
'%Uint8ClampedArrayPrototype%': typeof Uint8ClampedArray === 'undefined' ? undefined : Uint8ClampedArray.prototype,
'%Uint16Array%': typeof Uint16Array === 'undefined' ? undefined : Uint16Array,
'%Uint16ArrayPrototype%': typeof Uint16Array === 'undefined' ? undefined : Uint16Array.prototype,
'%Uint32Array%': typeof Uint32Array === 'undefined' ? undefined : Uint32Array,
'%Uint32ArrayPrototype%': typeof Uint32Array === 'undefined' ? undefined : Uint32Array.prototype,
'%URIError%': URIError,
'%URIErrorPrototype%': URIError.prototype,
'%WeakMap%': typeof WeakMap === 'undefined' ? undefined : WeakMap,
'%WeakMapPrototype%': typeof WeakMap === 'undefined' ? undefined : WeakMap.prototype,
'%WeakSet%': typeof WeakSet === 'undefined' ? undefined : WeakSet,
'%WeakSetPrototype%': typeof WeakSet === 'undefined' ? undefined : WeakSet.prototype
};

var bind = require('function-bind');
Expand All @@ -171,7 +171,7 @@ var stringToPath = function stringToPath(string) {
/* end adaptation */

var getBaseIntrinsic = function getBaseIntrinsic(name, allowMissing) {
var key = '$ ' + name;
var key = $replace(name, /^%?([^%]*)%?$/, '%$1%');

This comment has been minimized.

Copy link
@lencioni

lencioni Dec 16, 2019

Why the replace here? I think this will change the behavior of the empty string case: 3567ae9#diff-0ab0e3a78017d714af58f14f659a3b86R194-R196

and is redundant for the other cases since the percentages are added elsewhere: 3567ae9#diff-0ab0e3a78017d714af58f14f659a3b86R198

This comment has been minimized.

Copy link
@ljharb

ljharb Dec 16, 2019

Author Owner

Tweaked further in 9be0385

if (!(key in INTRINSICS)) {
throw new SyntaxError('intrinsic ' + name + ' does not exist!');
}
Expand Down
17 changes: 17 additions & 0 deletions test/GetIntrinsic.js
Expand Up @@ -30,19 +30,35 @@ test('throws', function (t) {
);
});

forEach([
'toString',
'propertyIsEnumerable',
'hasOwnProperty'
], function (objectProtoMember) {
t['throws'](
function () { GetIntrinsic(objectProtoMember); },
SyntaxError,
debug(objectProtoMember) + ' is not an intrinsic'
);
});

t.end();
});

test('base intrinsics', function (t) {
t.equal(GetIntrinsic('%Object%'), Object, '%Object% yields Object');
t.equal(GetIntrinsic('Object'), Object, 'Object yields Object');
t.equal(GetIntrinsic('%Array%'), Array, '%Array% yields Array');
t.equal(GetIntrinsic('Array'), Array, 'Array yields Array');

t.end();
});

test('dotted paths', function (t) {
t.equal(GetIntrinsic('%Object.prototype.toString%'), Object.prototype.toString, '%Object.prototype.toString% yields Object.prototype.toString');
t.equal(GetIntrinsic('Object.prototype.toString'), Object.prototype.toString, 'Object.prototype.toString yields Object.prototype.toString');
t.equal(GetIntrinsic('%Array.prototype.push%'), Array.prototype.push, '%Array.prototype.push% yields Array.prototype.push');
t.equal(GetIntrinsic('Array.prototype.push'), Array.prototype.push, 'Array.prototype.push yields Array.prototype.push');

t.end();
});
Expand All @@ -52,6 +68,7 @@ test('accessors', { skip: !Object.getOwnPropertyDescriptor || typeof Map !== 'fu
t.ok(actual, 'Map.prototype.size has a descriptor');
t.equal(typeof actual.get, 'function', 'Map.prototype.size has a getter function');
t.equal(GetIntrinsic('%Map.prototype.size%'), actual.get, '%Map.prototype.size% yields the getter for it');
t.equal(GetIntrinsic('Map.prototype.size'), actual.get, 'Map.prototype.size yields the getter for it');

t.end();
});

0 comments on commit 3567ae9

Please sign in to comment.