reflect-metadata
Advanced tools
Comparing version 0.1.4 to 0.1.5
{ | ||
"name": "reflect-metadata", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Polyfill for Metadata Reflection API", | ||
@@ -11,5 +11,5 @@ "main": "Reflect.js", | ||
"scripts": { | ||
"build": "./node_modules/.bin/tsc.cmd", | ||
"build-test": "./node_modules/.bin/tsc.cmd --project ./test/", | ||
"test": "npm run-script build-test & node ./temp/test/run.js" | ||
"build": "tsc", | ||
"build-test": "tsc --project ./test/", | ||
"test": "npm run build-test & node ./temp/test/run.js" | ||
}, | ||
@@ -16,0 +16,0 @@ "repository": { |
@@ -9,3 +9,3 @@ Proposal to add Decorators to ES7, along with a prototype for an ES7 Reflection API for Decorator Metadata | ||
* [Decorators][] add the ability to augment a class and its members as the class is defined, through a declarative syntax. | ||
* Decorators add the ability to augment a class and its members as the class is defined, through a declarative syntax. | ||
* Traceur attaches annotations to a static property on the class. | ||
@@ -29,3 +29,3 @@ * Languages like C# (.NET), and Java support attributes or annotations that add metadata to types, along with a reflective API for reading metadata. | ||
@Reflect.metadata(metadataKey, metadataValue) | ||
method() { | ||
method() { | ||
} | ||
@@ -49,3 +49,3 @@ } | ||
* Object has a new \[\[Metadata\]\] internal property that will contain a Map whose keys are property keys (or **undefined**) and whose values are Maps of metadata keys to metadata values. | ||
* Object will have a number of new internal methods for \[\[DefineOwnMetadata\]\], \[\[GetOwnMetadata\]\], \[\[HasOwnMetadata\]\], etc. | ||
* Object will have a number of new internal methods for \[\[DefineOwnMetadata\]\], \[\[GetOwnMetadata\]\], \[\[HasOwnMetadata\]\], etc. | ||
* These internal methods can be overridden by a Proxy to support additional traps. | ||
@@ -55,3 +55,3 @@ * These internal methods will by default call a set of abstract operations to define and read metadata. | ||
* Metadata defined on class declaration *C* is stored in *C*.\[\[Metadata\]\], with **undefined** as the key. | ||
* Metadata defined on static members of class declaration *C* are stored in *C*.\[\[Metadata\]\], with the property key as the key. | ||
* Metadata defined on static members of class declaration *C* are stored in *C*.\[\[Metadata\]\], with the property key as the key. | ||
* Metadata defined on instance members of class declaration *C* are stored in *C*.prototype.\[\[Metadata\]\], with the property key as the key. | ||
@@ -99,3 +99,3 @@ | ||
@Reflect.metadata(metadataKey, metadataValue) | ||
method() { | ||
method() { | ||
} | ||
@@ -175,3 +175,2 @@ } | ||
[Metadata-Spec]: https://github.com/jonathandturner/decorators/blob/master/specs/metadata.md | ||
[Decorators]: https://github.com/jonathandturner/decorators/blob/master/README.md | ||
[Metadata-Spec]: https://github.com/rbuckton/ReflectDecorators/blob/master/spec/metadata.md |
236
Reflect.js
@@ -18,2 +18,26 @@ /*! ***************************************************************************** | ||
"use strict"; | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
// feature test for Object.create support | ||
var supportsCreate = typeof Object.create === "function"; | ||
// feature test for __proto__ support | ||
var supportsProto = (function () { | ||
var sentinel = {}; | ||
function __() { } | ||
__.prototype = sentinel; | ||
var instance = new __(); | ||
return instance.__proto__ === sentinel; | ||
})(); | ||
var createDictionary = supportsCreate ? function () { return MakeDictionary(Object.create(null)); } : | ||
supportsProto ? function () { return MakeDictionary({ __proto__: null }); } : | ||
function () { return MakeDictionary({}); }; | ||
var HashMap; | ||
(function (HashMap) { | ||
var downLevel = !supportsCreate && !supportsProto; | ||
HashMap.has = downLevel | ||
? function (map, key) { return hasOwn.call(map, key); } | ||
: function (map, key) { return key in map; }; | ||
HashMap.get = downLevel | ||
? function (map, key) { return hasOwn.call(map, key) ? map[key] : undefined; } | ||
: function (map, key) { return map[key]; }; | ||
})(HashMap || (HashMap = {})); | ||
// Load global or shim versions of Map, Set, and WeakMap | ||
@@ -647,4 +671,4 @@ var functionPrototype = Object.getPrototypeOf(Function); | ||
var keys = []; | ||
for (var _i = 0, ownKeys_1 = ownKeys; _i < ownKeys_1.length; _i++) { | ||
var key = ownKeys_1[_i]; | ||
for (var _i = 0; _i < ownKeys.length; _i++) { | ||
var key = ownKeys[_i]; | ||
var hasKey = set.has(key); | ||
@@ -656,4 +680,4 @@ if (!hasKey) { | ||
} | ||
for (var _a = 0, parentKeys_1 = parentKeys; _a < parentKeys_1.length; _a++) { | ||
var key = parentKeys_1[_a]; | ||
for (var _a = 0; _a < parentKeys.length; _a++) { | ||
var key = parentKeys[_a]; | ||
var hasKey = set.has(key); | ||
@@ -739,52 +763,66 @@ if (!hasKey) { | ||
var cacheSentinel = {}; | ||
function Map() { | ||
this._keys = []; | ||
this._values = []; | ||
this._cache = cacheSentinel; | ||
} | ||
Map.prototype = { | ||
get size() { | ||
return this._keys.length; | ||
}, | ||
has: function (key) { | ||
if (key === this._cache) { | ||
return true; | ||
} | ||
return (function () { | ||
function Map() { | ||
this._keys = []; | ||
this._values = []; | ||
this._cache = cacheSentinel; | ||
this._cacheIndex = -2; | ||
} | ||
Object.defineProperty(Map.prototype, "size", { | ||
get: function () { | ||
return this._keys.length; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Map.prototype.has = function (key) { | ||
if (this._find(key) >= 0) { | ||
this._cache = key; | ||
return true; | ||
} | ||
return false; | ||
}, | ||
get: function (key) { | ||
}; | ||
Map.prototype.get = function (key) { | ||
var index = this._find(key); | ||
if (index >= 0) { | ||
this._cache = key; | ||
return this._values[index]; | ||
} | ||
return undefined; | ||
}, | ||
set: function (key, value) { | ||
this.delete(key); | ||
this._keys.push(key); | ||
this._values.push(value); | ||
this._cache = key; | ||
}; | ||
Map.prototype.set = function (key, value) { | ||
var index = this._find(key); | ||
if (index >= 0) { | ||
this._keys[index] = key; | ||
this._values[index] = value; | ||
} | ||
else { | ||
this._keys.push(key); | ||
this._values.push(value); | ||
this._cache = key; | ||
this._cacheIndex = this._keys.length - 1; | ||
} | ||
return this; | ||
}, | ||
delete: function (key) { | ||
}; | ||
Map.prototype.delete = function (key) { | ||
var index = this._find(key); | ||
if (index >= 0) { | ||
this._keys.splice(index, 1); | ||
this._values.splice(index, 1); | ||
var size = this._keys.length; | ||
for (var i = index + 1; i < size; i++) { | ||
this._keys[i - 1] = this._keys[i]; | ||
this._values[i - 1] = this._values[i]; | ||
} | ||
this._keys.length--; | ||
this._values.length--; | ||
this._cache = cacheSentinel; | ||
this._cacheIndex = -2; | ||
return true; | ||
} | ||
return false; | ||
}, | ||
clear: function () { | ||
}; | ||
Map.prototype.clear = function () { | ||
this._keys.length = 0; | ||
this._values.length = 0; | ||
this._cache = cacheSentinel; | ||
}, | ||
forEach: function (callback, thisArg) { | ||
this._cacheIndex = -2; | ||
}; | ||
Map.prototype.forEach = function (callback, thisArg) { | ||
var size = this.size; | ||
@@ -794,7 +832,9 @@ for (var i = 0; i < size; ++i) { | ||
var value = this._values[i]; | ||
this._cache = key; | ||
callback.call(this, value, key, this); | ||
callback.call(thisArg, value, key, this); | ||
} | ||
}, | ||
_find: function (key) { | ||
}; | ||
Map.prototype._find = function (key) { | ||
if (this._cache === key) { | ||
return this._cacheIndex; | ||
} | ||
var keys = this._keys; | ||
@@ -804,38 +844,41 @@ var size = keys.length; | ||
if (keys[i] === key) { | ||
return i; | ||
return this._cache = key, this._cacheIndex = i; | ||
} | ||
} | ||
return -1; | ||
} | ||
}; | ||
return Map; | ||
return this._cache = key, this._cacheIndex = -1; | ||
}; | ||
return Map; | ||
})(); | ||
} | ||
// naive Set shim | ||
function CreateSetPolyfill() { | ||
var cacheSentinel = {}; | ||
function Set() { | ||
this._map = new _Map(); | ||
} | ||
Set.prototype = { | ||
get size() { | ||
return this._map.length; | ||
}, | ||
has: function (value) { | ||
return (function () { | ||
function Set() { | ||
this._map = new _Map(); | ||
} | ||
Object.defineProperty(Set.prototype, "size", { | ||
get: function () { | ||
return this._map.size; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
Set.prototype.has = function (value) { | ||
return this._map.has(value); | ||
}, | ||
add: function (value) { | ||
}; | ||
Set.prototype.add = function (value) { | ||
this._map.set(value, value); | ||
return this; | ||
}, | ||
delete: function (value) { | ||
}; | ||
Set.prototype.delete = function (value) { | ||
return this._map.delete(value); | ||
}, | ||
clear: function () { | ||
}; | ||
Set.prototype.clear = function () { | ||
this._map.clear(); | ||
}, | ||
forEach: function (callback, thisArg) { | ||
}; | ||
Set.prototype.forEach = function (callback, thisArg) { | ||
this._map.forEach(callback, thisArg); | ||
} | ||
}; | ||
return Set; | ||
}; | ||
return Set; | ||
})(); | ||
} | ||
@@ -845,45 +888,40 @@ // naive WeakMap shim | ||
var UUID_SIZE = 16; | ||
var isNode = typeof global !== "undefined" && Object.prototype.toString.call(global.process) === '[object process]'; | ||
var nodeCrypto = isNode && function () { try { | ||
return (void 0, require)("crypto"); | ||
} | ||
catch (e) { } }(); | ||
var hasOwn = Object.prototype.hasOwnProperty; | ||
var keys = {}; | ||
var keys = createDictionary(); | ||
var rootKey = CreateUniqueKey(); | ||
function WeakMap() { | ||
this._key = CreateUniqueKey(); | ||
} | ||
WeakMap.prototype = { | ||
has: function (target) { | ||
return (function () { | ||
function WeakMap() { | ||
this._key = CreateUniqueKey(); | ||
} | ||
WeakMap.prototype.has = function (target) { | ||
var table = GetOrCreateWeakMapTable(target, /*create*/ false); | ||
if (table) { | ||
return this._key in table; | ||
return HashMap.has(table, this._key); | ||
} | ||
return false; | ||
}, | ||
get: function (target) { | ||
}; | ||
WeakMap.prototype.get = function (target) { | ||
var table = GetOrCreateWeakMapTable(target, /*create*/ false); | ||
if (table) { | ||
return table[this._key]; | ||
return HashMap.get(table, this._key); | ||
} | ||
return undefined; | ||
}, | ||
set: function (target, value) { | ||
}; | ||
WeakMap.prototype.set = function (target, value) { | ||
var table = GetOrCreateWeakMapTable(target, /*create*/ true); | ||
table[this._key] = value; | ||
return this; | ||
}, | ||
delete: function (target) { | ||
}; | ||
WeakMap.prototype.delete = function (target) { | ||
var table = GetOrCreateWeakMapTable(target, /*create*/ false); | ||
if (table && this._key in table) { | ||
if (table && HashMap.has(table, this._key)) { | ||
return delete table[this._key]; | ||
} | ||
return false; | ||
}, | ||
clear: function () { | ||
}; | ||
WeakMap.prototype.clear = function () { | ||
// NOTE: not a real clear, just makes the previous data unreachable | ||
this._key = CreateUniqueKey(); | ||
} | ||
}; | ||
}; | ||
return WeakMap; | ||
})(); | ||
function FillRandomBytes(buffer, size) { | ||
@@ -895,7 +933,3 @@ for (var i = 0; i < size; ++i) { | ||
function GenRandomBytes(size) { | ||
if (nodeCrypto) { | ||
var data = nodeCrypto.randomBytes(size); | ||
return data; | ||
} | ||
else if (typeof Uint8Array === "function") { | ||
if (typeof Uint8Array === "function") { | ||
var data = new Uint8Array(size); | ||
@@ -941,3 +975,3 @@ if (typeof crypto !== "undefined") { | ||
key = "@@WeakMap@@" + CreateUUID(); | ||
} while (hasOwn.call(keys, key)); | ||
} while (HashMap.has(keys, key)); | ||
keys[key] = true; | ||
@@ -951,8 +985,12 @@ return key; | ||
} | ||
Object.defineProperty(target, rootKey, { value: Object.create(null) }); | ||
Object.defineProperty(target, rootKey, { value: createDictionary() }); | ||
} | ||
return target[rootKey]; | ||
} | ||
return WeakMap; | ||
} | ||
function MakeDictionary(obj) { | ||
obj.__DICTIONARY_MODE__ = 1; | ||
delete obj.____DICTIONARY_MODE__; | ||
return obj; | ||
} | ||
// hook global Reflect | ||
@@ -963,3 +1001,5 @@ (function (__global) { | ||
for (var p in Reflect) { | ||
__global.Reflect[p] = Reflect[p]; | ||
if (hasOwn.call(Reflect, p)) { | ||
__global.Reflect[p] = Reflect[p]; | ||
} | ||
} | ||
@@ -966,0 +1006,0 @@ } |
791
Reflect.ts
@@ -18,11 +18,34 @@ /*! ***************************************************************************** | ||
interface HashMap<V> { | ||
[key: string]: V; | ||
} | ||
interface BufferLike { | ||
[offset: number]: number; | ||
length: number; | ||
} | ||
interface IteratorResult<T> { | ||
value?: T; | ||
done?: boolean; | ||
} | ||
interface Iterator<T> { | ||
next(value?: any): IteratorResult<T>; | ||
throw?(value: any): IteratorResult<T>; | ||
return?(value?: T): IteratorResult<T>; | ||
} | ||
interface Map<K, V> { | ||
size: number; | ||
clear(): void; | ||
delete(key:K): boolean; | ||
forEach(callbackfn:(value:V, index:K, map:Map<K, V>) => void, thisArg?:any): void; | ||
get(key:K): V; | ||
has(key:K): boolean; | ||
set(key:K, value?:V): Map<K, V>; | ||
size: number; | ||
delete(key: K): boolean; | ||
get(key: K): V; | ||
has(key: K): boolean; | ||
set(key: K, value?: V): Map<K, V>; | ||
keys?(): Iterator<K>; | ||
values?(): Iterator<V>; | ||
entries?(): Iterator<[K, V]>; | ||
} | ||
interface MapConstructor { | ||
@@ -34,2 +57,19 @@ new (): Map<any, any>; | ||
interface Set<T> { | ||
size: number; | ||
add(value: T): Set<T>; | ||
clear(): void; | ||
delete(value: T): boolean; | ||
has(value: T): boolean; | ||
keys?(): Iterator<T>; | ||
values?(): Iterator<T>; | ||
entries?(): Iterator<[T, T]>; | ||
} | ||
interface SetConstructor { | ||
new (): Set<any>; | ||
new <T>(): Set<T>; | ||
prototype: Set<any>; | ||
} | ||
interface WeakMap<K, V> { | ||
@@ -49,28 +89,47 @@ clear(): void; | ||
interface Set<T> { | ||
add(value: T): Set<T>; | ||
clear(): void; | ||
delete(value: T): boolean; | ||
forEach(callbackfn: (value: T, index: T, set: Set<T>) => void, thisArg?: any): void; | ||
has(value: T): boolean; | ||
size: number; | ||
interface ForEachable<K, V> { | ||
forEach?(callbackfn: (value: V, index: K, map: ForEachable<K, V>) => void, thisArg?: any): void; | ||
entries?(): Iterator<[K, V]>; | ||
} | ||
interface SetConstructor { | ||
new (): Set<any>; | ||
new <T>(): Set<T>; | ||
prototype: Set<any>; | ||
} | ||
declare const Set: SetConstructor; | ||
declare const WeakMap: WeakMapConstructor; | ||
declare const Map: MapConstructor; | ||
declare const global: any; | ||
declare const WorkerGlobalScope: any; | ||
declare const module: any; | ||
declare const crypto: Crypto; | ||
declare const msCrypto: Crypto; | ||
declare const require: Function; | ||
const hasOwn = Object.prototype.hasOwnProperty; | ||
declare var Set: SetConstructor; | ||
declare var WeakMap: WeakMapConstructor; | ||
declare var Map:MapConstructor; | ||
declare var global: any; | ||
declare var WorkerGlobalScope: any; | ||
declare var module: any; | ||
declare var crypto: Crypto; | ||
declare var msCrypto: Crypto; | ||
declare var require: Function; | ||
// feature test for Object.create support | ||
const supportsCreate = typeof Object.create === "function"; | ||
// feature test for __proto__ support | ||
const supportsProto = (function () { | ||
const sentinel = {}; | ||
function __() { } | ||
__.prototype = sentinel; | ||
const instance = new (<any>__)(); | ||
return instance.__proto__ === sentinel; | ||
})(); | ||
// create an object in dictionary mode (a.k.a. "slow" mode in v8) | ||
const createDictionary = | ||
supportsCreate ? <V>() => MakeDictionary(Object.create(null) as HashMap<V>) : | ||
supportsProto ? <V>() => MakeDictionary({ __proto__: null } as HashMap<V>) : | ||
<V>() => MakeDictionary({} as HashMap<V>); | ||
namespace HashMap { | ||
const downLevel = !supportsCreate && !supportsProto; | ||
export const has = downLevel | ||
? <V>(map: HashMap<V>, key: string | number) => hasOwn.call(map, key) | ||
: <V>(map: HashMap<V>, key: string | number) => key in map; | ||
export const get = downLevel | ||
? <V>(map: HashMap<V>, key: string | number): V => hasOwn.call(map, key) ? map[key] : undefined | ||
: <V>(map: HashMap<V>, key: string | number): V => map[key]; | ||
} | ||
// Load global or shim versions of Map, Set, and WeakMap | ||
@@ -83,3 +142,3 @@ const functionPrototype = Object.getPrototypeOf(Function); | ||
// [[Metadata]] internal slot | ||
const __Metadata__ = new _WeakMap<Object, Map<string | symbol, Map<any, any>>>(); | ||
const Metadata = new _WeakMap<Object, Map<string | symbol, Map<any, any>>>(); | ||
@@ -180,15 +239,6 @@ /** | ||
if (!IsUndefined(targetDescriptor)) { | ||
if (!IsArray(decorators)) { | ||
throw new TypeError(); | ||
} | ||
else if (!IsObject(target)) { | ||
throw new TypeError(); | ||
} | ||
else if (IsUndefined(targetKey)) { | ||
throw new TypeError(); | ||
} | ||
else if (!IsObject(targetDescriptor)) { | ||
throw new TypeError(); | ||
} | ||
if (!IsArray(decorators)) throw new TypeError(); | ||
if (!IsObject(target)) throw new TypeError(); | ||
if (IsUndefined(targetKey)) throw new TypeError(); | ||
if (!IsObject(targetDescriptor)) throw new TypeError(); | ||
targetKey = ToPropertyKey(targetKey); | ||
@@ -198,9 +248,4 @@ return DecoratePropertyWithDescriptor(<MethodDecorator[]>decorators, target, targetKey, targetDescriptor); | ||
else if (!IsUndefined(targetKey)) { | ||
if (!IsArray(decorators)) { | ||
throw new TypeError(); | ||
} | ||
else if (!IsObject(target)) { | ||
throw new TypeError(); | ||
} | ||
if (!IsArray(decorators)) throw new TypeError(); | ||
if (!IsObject(target)) throw new TypeError(); | ||
targetKey = ToPropertyKey(targetKey); | ||
@@ -210,9 +255,4 @@ return DecoratePropertyWithoutDescriptor(<PropertyDecorator[]>decorators, target, targetKey); | ||
else { | ||
if (!IsArray(decorators)) { | ||
throw new TypeError(); | ||
} | ||
else if (!IsConstructor(target)) { | ||
throw new TypeError(); | ||
} | ||
if (!IsArray(decorators)) throw new TypeError(); | ||
if (!IsConstructor(target)) throw new TypeError(); | ||
return DecorateConstructor(<ClassDecorator[]>decorators, <Function>target); | ||
@@ -267,6 +307,3 @@ } | ||
if (!IsUndefined(targetKey)) { | ||
if (!IsObject(target)) { | ||
throw new TypeError(); | ||
} | ||
if (!IsObject(target)) throw new TypeError(); | ||
targetKey = ToPropertyKey(targetKey); | ||
@@ -276,10 +313,6 @@ OrdinaryDefineOwnMetadata(metadataKey, metadataValue, target, targetKey); | ||
else { | ||
if (!IsConstructor(target)) { | ||
throw new TypeError(); | ||
} | ||
if (!IsConstructor(target)) throw new TypeError(); | ||
OrdinaryDefineOwnMetadata(metadataKey, metadataValue, target, /*targetKey*/ undefined); | ||
} | ||
} | ||
return decorator; | ||
@@ -386,9 +419,4 @@ } | ||
export function defineMetadata(metadataKey: any, metadataValue: any, target: Object, targetKey?: string | symbol): void { | ||
if (!IsObject(target)) { | ||
throw new TypeError(); | ||
} | ||
else if (!IsUndefined(targetKey)) { | ||
targetKey = ToPropertyKey(targetKey); | ||
} | ||
if (!IsObject(target)) throw new TypeError(); | ||
if (!IsUndefined(targetKey)) targetKey = ToPropertyKey(targetKey); | ||
return OrdinaryDefineOwnMetadata(metadataKey, metadataValue, target, targetKey); | ||
@@ -480,9 +508,4 @@ } | ||
export function hasMetadata(metadataKey: any, target: Object, targetKey?: string | symbol): boolean { | ||
if (!IsObject(target)) { | ||
throw new TypeError(); | ||
} | ||
else if (!IsUndefined(targetKey)) { | ||
targetKey = ToPropertyKey(targetKey); | ||
} | ||
if (!IsObject(target)) throw new TypeError(); | ||
if (!IsUndefined(targetKey)) targetKey = ToPropertyKey(targetKey); | ||
return OrdinaryHasMetadata(metadataKey, target, targetKey); | ||
@@ -574,9 +597,4 @@ } | ||
export function hasOwnMetadata(metadataKey: any, target: Object, targetKey?: string | symbol): boolean { | ||
if (!IsObject(target)) { | ||
throw new TypeError(); | ||
} | ||
else if (!IsUndefined(targetKey)) { | ||
targetKey = ToPropertyKey(targetKey); | ||
} | ||
if (!IsObject(target)) throw new TypeError(); | ||
if (!IsUndefined(targetKey)) targetKey = ToPropertyKey(targetKey); | ||
return OrdinaryHasOwnMetadata(metadataKey, target, targetKey); | ||
@@ -668,9 +686,4 @@ } | ||
export function getMetadata(metadataKey: any, target: Object, targetKey?: string | symbol): any { | ||
if (!IsObject(target)) { | ||
throw new TypeError(); | ||
} | ||
else if (!IsUndefined(targetKey)) { | ||
targetKey = ToPropertyKey(targetKey); | ||
} | ||
if (!IsObject(target)) throw new TypeError(); | ||
if (!IsUndefined(targetKey)) targetKey = ToPropertyKey(targetKey); | ||
return OrdinaryGetMetadata(metadataKey, target, targetKey); | ||
@@ -762,9 +775,4 @@ } | ||
export function getOwnMetadata(metadataKey: any, target: Object, targetKey?: string | symbol): any { | ||
if (!IsObject(target)) { | ||
throw new TypeError(); | ||
} | ||
else if (!IsUndefined(targetKey)) { | ||
targetKey = ToPropertyKey(targetKey); | ||
} | ||
if (!IsObject(target)) throw new TypeError(); | ||
if (!IsUndefined(targetKey)) targetKey = ToPropertyKey(targetKey); | ||
return OrdinaryGetOwnMetadata(metadataKey, target, targetKey); | ||
@@ -853,9 +861,4 @@ } | ||
export function getMetadataKeys(target: Object, targetKey?: string | symbol): any[] { | ||
if (!IsObject(target)) { | ||
throw new TypeError(); | ||
} | ||
else if (!IsUndefined(targetKey)) { | ||
targetKey = ToPropertyKey(targetKey); | ||
} | ||
if (!IsObject(target)) throw new TypeError(); | ||
if (!IsUndefined(targetKey)) targetKey = ToPropertyKey(targetKey); | ||
return OrdinaryMetadataKeys(target, targetKey); | ||
@@ -944,9 +947,4 @@ } | ||
export function getOwnMetadataKeys(target: Object, targetKey?: string | symbol): any[] { | ||
if (!IsObject(target)) { | ||
throw new TypeError(); | ||
} | ||
else if (!IsUndefined(targetKey)) { | ||
targetKey = ToPropertyKey(targetKey); | ||
} | ||
if (!IsObject(target)) throw new TypeError(); | ||
if (!IsUndefined(targetKey)) targetKey = ToPropertyKey(targetKey); | ||
return OrdinaryOwnMetadataKeys(target, targetKey); | ||
@@ -1038,30 +1036,13 @@ } | ||
export function deleteMetadata(metadataKey: any, target: Object, targetKey?: string | symbol): boolean { | ||
if (!IsObject(target)) { | ||
throw new TypeError(); | ||
} | ||
else if (!IsUndefined(targetKey)) { | ||
targetKey = ToPropertyKey(targetKey); | ||
} | ||
// https://github.com/jonathandturner/decorators/blob/master/specs/metadata.md#deletemetadata-metadatakey-p- | ||
let metadataMap = GetOrCreateMetadataMap(target, targetKey, /*create*/ false); | ||
if (IsUndefined(metadataMap)) { | ||
return false; | ||
} | ||
if (!metadataMap.delete(metadataKey)) { | ||
return false; | ||
} | ||
if (metadataMap.size > 0) { | ||
return true; | ||
} | ||
let targetMetadata = __Metadata__.get(target); | ||
// https://github.com/rbuckton/ReflectDecorators/blob/master/spec/metadata.md#deletemetadata-metadatakey-p- | ||
if (!IsObject(target)) throw new TypeError(); | ||
if (!IsUndefined(targetKey)) targetKey = ToPropertyKey(targetKey); | ||
const metadataMap = GetOrCreateMetadataMap(target, targetKey, /*create*/ false); | ||
if (IsUndefined(metadataMap)) return false; | ||
if (!metadataMap.delete(metadataKey)) return false; | ||
if (metadataMap.size > 0) return true; | ||
const targetMetadata = Metadata.get(target); | ||
targetMetadata.delete(targetKey); | ||
if (targetMetadata.size > 0) { | ||
return true; | ||
} | ||
__Metadata__.delete(target); | ||
if (targetMetadata.size > 0) return true; | ||
Metadata.delete(target); | ||
return true; | ||
@@ -1072,8 +1053,6 @@ } | ||
for (let i = decorators.length - 1; i >= 0; --i) { | ||
let decorator = decorators[i]; | ||
let decorated = decorator(target); | ||
const decorator = decorators[i]; | ||
const decorated = decorator(target); | ||
if (!IsUndefined(decorated)) { | ||
if (!IsConstructor(decorated)) { | ||
throw new TypeError(); | ||
} | ||
if (!IsConstructor(decorated)) throw new TypeError(); | ||
target = <Function>decorated; | ||
@@ -1087,8 +1066,6 @@ } | ||
for (let i = decorators.length - 1; i >= 0; --i) { | ||
let decorator = decorators[i]; | ||
let decorated = decorator(target, propertyKey, descriptor); | ||
const decorator = decorators[i]; | ||
const decorated = decorator(target, propertyKey, descriptor); | ||
if (!IsUndefined(decorated)) { | ||
if (!IsObject(decorated)) { | ||
throw new TypeError(); | ||
} | ||
if (!IsObject(decorated)) throw new TypeError(); | ||
descriptor = <PropertyDescriptor>decorated; | ||
@@ -1102,3 +1079,3 @@ } | ||
for (let i = decorators.length - 1; i >= 0; --i) { | ||
let decorator = decorators[i]; | ||
const decorator = decorators[i]; | ||
decorator(target, propertyKey); | ||
@@ -1108,127 +1085,72 @@ } | ||
// https://github.com/jonathandturner/decorators/blob/master/specs/metadata.md#getorcreatemetadatamap--o-p-create- | ||
// https://github.com/rbuckton/ReflectDecorators/blob/master/spec/metadata.md#getorcreatemetadatamap--o-p-create- | ||
function GetOrCreateMetadataMap(target: Object, targetKey: string | symbol, create: boolean): Map<any, any> { | ||
let targetMetadata = __Metadata__.get(target); | ||
let targetMetadata = Metadata.get(target); | ||
if (!targetMetadata) { | ||
if (!create) { | ||
return undefined; | ||
} | ||
if (!create) return undefined; | ||
targetMetadata = new _Map<string | symbol, Map<any, any>>(); | ||
__Metadata__.set(target, targetMetadata); | ||
Metadata.set(target, targetMetadata); | ||
} | ||
let keyMetadata = targetMetadata.get(targetKey); | ||
if (!keyMetadata) { | ||
if (!create) { | ||
return undefined; | ||
} | ||
if (!create) return undefined; | ||
keyMetadata = new _Map<any, any>(); | ||
targetMetadata.set(targetKey, keyMetadata); | ||
} | ||
return keyMetadata; | ||
} | ||
// https://github.com/jonathandturner/decorators/blob/master/specs/metadata.md#ordinaryhasmetadata--metadatakey-o-p- | ||
// https://github.com/rbuckton/ReflectDecorators/blob/master/spec/metadata.md#ordinaryhasmetadata--metadatakey-o-p- | ||
function OrdinaryHasMetadata(MetadataKey: any, O: Object, P: string | symbol): boolean { | ||
let hasOwn = OrdinaryHasOwnMetadata(MetadataKey, O, P); | ||
if (hasOwn) { | ||
return true; | ||
} | ||
let parent = GetPrototypeOf(O); | ||
if (parent !== null) { | ||
return OrdinaryHasMetadata(MetadataKey, parent, P); | ||
} | ||
return false; | ||
const hasOwn = OrdinaryHasOwnMetadata(MetadataKey, O, P); | ||
if (hasOwn) return true; | ||
const parent = GetPrototypeOf(O); | ||
return parent !== null ? OrdinaryHasMetadata(MetadataKey, parent, P) : false; | ||
} | ||
// https://github.com/jonathandturner/decorators/blob/master/specs/metadata.md#ordinaryhasownmetadata--metadatakey-o-p- | ||
// https://github.com/rbuckton/ReflectDecorators/blob/master/spec/metadata.md#ordinaryhasownmetadata--metadatakey-o-p- | ||
function OrdinaryHasOwnMetadata(MetadataKey: any, O: Object, P: string | symbol): boolean { | ||
let metadataMap = GetOrCreateMetadataMap(O, P, /*create*/ false); | ||
if (metadataMap === undefined) { | ||
return false; | ||
} | ||
return Boolean(metadataMap.has(MetadataKey)); | ||
const metadataMap = GetOrCreateMetadataMap(O, P, /*create*/ false); | ||
return metadataMap !== undefined && Boolean(metadataMap.has(MetadataKey)); | ||
} | ||
// https://github.com/jonathandturner/decorators/blob/master/specs/metadata.md#ordinarygetmetadata--metadatakey-o-p- | ||
// https://github.com/rbuckton/ReflectDecorators/blob/master/spec/metadata.md#ordinarygetmetadata--metadatakey-o-p- | ||
function OrdinaryGetMetadata(MetadataKey: any, O: Object, P: string | symbol): any { | ||
let hasOwn = OrdinaryHasOwnMetadata(MetadataKey, O, P); | ||
if (hasOwn) { | ||
return OrdinaryGetOwnMetadata(MetadataKey, O, P); | ||
} | ||
let parent = GetPrototypeOf(O); | ||
if (parent !== null) { | ||
return OrdinaryGetMetadata(MetadataKey, parent, P); | ||
} | ||
return undefined; | ||
const hasOwn = OrdinaryHasOwnMetadata(MetadataKey, O, P); | ||
if (hasOwn) return OrdinaryGetOwnMetadata(MetadataKey, O, P); | ||
const parent = GetPrototypeOf(O); | ||
return parent !== null ? OrdinaryGetMetadata(MetadataKey, parent, P) : undefined; | ||
} | ||
// https://github.com/jonathandturner/decorators/blob/master/specs/metadata.md#ordinarygetownmetadata--metadatakey-o-p- | ||
// https://github.com/rbuckton/ReflectDecorators/blob/master/spec/metadata.md#ordinarygetownmetadata--metadatakey-o-p- | ||
function OrdinaryGetOwnMetadata(MetadataKey: any, O: Object, P: string | symbol): any { | ||
let metadataMap = GetOrCreateMetadataMap(O, P, /*create*/ false); | ||
if (metadataMap === undefined) { | ||
return undefined; | ||
} | ||
return metadataMap.get(MetadataKey); | ||
const metadataMap = GetOrCreateMetadataMap(O, P, /*create*/ false); | ||
return metadataMap === undefined ? undefined : metadataMap.get(MetadataKey); | ||
} | ||
// https://github.com/jonathandturner/decorators/blob/master/specs/metadata.md#ordinarydefineownmetadata--metadatakey-metadatavalue-o-p- | ||
// https://github.com/rbuckton/ReflectDecorators/blob/master/spec/metadata.md#ordinarydefineownmetadata--metadatakey-metadatavalue-o-p- | ||
function OrdinaryDefineOwnMetadata(MetadataKey: any, MetadataValue: any, O: Object, P: string | symbol): void { | ||
let metadataMap = GetOrCreateMetadataMap(O, P, /*create*/ true); | ||
const metadataMap = GetOrCreateMetadataMap(O, P, /*create*/ true); | ||
metadataMap.set(MetadataKey, MetadataValue); | ||
} | ||
// https://github.com/jonathandturner/decorators/blob/master/specs/metadata.md#ordinarymetadatakeys--o-p- | ||
// https://github.com/rbuckton/ReflectDecorators/blob/master/spec/metadata.md#ordinarymetadatakeys--o-p- | ||
function OrdinaryMetadataKeys(O: Object, P: string | symbol): any[] { | ||
let ownKeys = OrdinaryOwnMetadataKeys(O, P); | ||
let parent = GetPrototypeOf(O); | ||
if (parent === null) { | ||
return ownKeys; | ||
} | ||
let parentKeys = OrdinaryMetadataKeys(parent, P); | ||
if (parentKeys.length <= 0) { | ||
return ownKeys; | ||
} | ||
if (ownKeys.length <= 0) { | ||
return parentKeys; | ||
} | ||
let set = new _Set<any>(); | ||
let keys: any[] = []; | ||
for (let key of ownKeys) { | ||
let hasKey = set.has(key); | ||
if (!hasKey) { | ||
set.add(key); | ||
keys.push(key); | ||
} | ||
} | ||
for (let key of parentKeys) { | ||
let hasKey = set.has(key); | ||
if (!hasKey) { | ||
set.add(key); | ||
keys.push(key); | ||
} | ||
} | ||
return keys; | ||
const ownKeys = OrdinaryOwnMetadataKeys(O, P); | ||
const parent = GetPrototypeOf(O); | ||
if (parent === null) return ownKeys; | ||
const parentKeys = OrdinaryMetadataKeys(parent, P); | ||
if (parentKeys.length <= 0) return ownKeys; | ||
if (ownKeys.length <= 0) return parentKeys; | ||
const keys = new _Set<any>(); | ||
for (const key of ownKeys) keys.add(key); | ||
for (const key of parentKeys) keys.add(key); | ||
return getKeys(keys); | ||
} | ||
// https://github.com/jonathandturner/decorators/blob/master/specs/metadata.md#ordinaryownmetadatakeys--o-p- | ||
// https://github.com/rbuckton/ReflectDecorators/blob/master/spec/metadata.md#ordinaryownmetadatakeys--o-p- | ||
function OrdinaryOwnMetadataKeys(target: Object, targetKey: string | symbol): any[] { | ||
let metadataMap = GetOrCreateMetadataMap(target, targetKey, /*create*/ false); | ||
let keys: any[] = []; | ||
if (metadataMap) { | ||
metadataMap.forEach((_, key) => keys.push(key)); | ||
} | ||
const metadataMap = GetOrCreateMetadataMap(target, targetKey, /*create*/ false); | ||
const keys: any[] = []; | ||
if (metadataMap) forEach(metadataMap, (_, key) => keys.push(key)); | ||
return keys; | ||
@@ -1244,3 +1166,3 @@ } | ||
function IsArray(x: any): boolean { | ||
return Array.isArray(x); | ||
return Array.isArray ? Array.isArray(x) : x instanceof Array || Object.prototype.toString.call(x) === "[object Array]"; | ||
} | ||
@@ -1265,13 +1187,8 @@ | ||
function ToPropertyKey(value: any): string | symbol { | ||
if (IsSymbol(value)) { | ||
return <symbol>value; | ||
} | ||
return String(value); | ||
return IsSymbol(value) ? <symbol>value : String(value); | ||
} | ||
function GetPrototypeOf(O: any): Object { | ||
let proto = Object.getPrototypeOf(O); | ||
if (typeof O !== "function" || O === functionPrototype) { | ||
return proto; | ||
} | ||
const proto = Object.getPrototypeOf(O); | ||
if (typeof O !== "function" || O === functionPrototype) return proto; | ||
@@ -1286,23 +1203,15 @@ // TypeScript doesn't set __proto__ in ES5, as it's non-standard. | ||
// This is the case when in ES6 or when using __proto__ in a compatible browser. | ||
if (proto !== functionPrototype) { | ||
return proto; | ||
} | ||
if (proto !== functionPrototype) return proto; | ||
// If the super prototype is Object.prototype, null, or undefined, then we cannot determine the heritage. | ||
let prototype = O.prototype; | ||
let prototypeProto = prototype && Object.getPrototypeOf(prototype); | ||
if (prototypeProto == null || prototypeProto === Object.prototype) { | ||
return proto; | ||
} | ||
const prototype = O.prototype; | ||
const prototypeProto = prototype && Object.getPrototypeOf(prototype); | ||
if (prototypeProto == null || prototypeProto === Object.prototype) return proto; | ||
// if the constructor was not a function, then we cannot determine the heritage. | ||
let constructor = prototypeProto.constructor; | ||
if (typeof constructor !== "function") { | ||
return proto; | ||
} | ||
// If the constructor was not a function, then we cannot determine the heritage. | ||
const constructor = prototypeProto.constructor; | ||
if (typeof constructor !== "function") return proto; | ||
// if we have some kind of self-reference, then we cannot determine the heritage. | ||
if (constructor === O) { | ||
return proto; | ||
} | ||
// If we have some kind of self-reference, then we cannot determine the heritage. | ||
if (constructor === O) return proto; | ||
@@ -1313,146 +1222,169 @@ // we have a pretty good guess at the heritage. | ||
// naive Map shim | ||
function CreateMapPolyfill() { | ||
const cacheSentinel = {}; | ||
function Map() { | ||
this._keys = []; | ||
this._values = []; | ||
this._cache = cacheSentinel; | ||
function IteratorStep<T>(iterator: Iterator<T>): IteratorResult<T> { | ||
const result = iterator.next(); | ||
return result.done ? undefined : result; | ||
} | ||
function IteratorClose<T>(iterator: Iterator<T>) { | ||
const f = iterator["return"]; | ||
if (f) f.call(iterator); | ||
} | ||
function forEach<K, V>(source: ForEachable<K, V>, callback: (value: V, key: K, source: ForEachable<K, V>) => void, thisArg?: any) { | ||
const entries = source.entries; | ||
if (typeof entries === "function") { | ||
const iterator: Iterator<[K, V]> = entries.call(source); | ||
let result: IteratorResult<[K, V]>; | ||
try { | ||
while (result = IteratorStep(iterator)) { | ||
const [key, value] = result.value; | ||
callback.call(thisArg, value, key, source); | ||
} | ||
} | ||
finally { if (result) IteratorClose(iterator); } | ||
} | ||
Map.prototype = { | ||
get size() { | ||
return this._keys.length; | ||
else { | ||
const forEach = source.forEach; | ||
if (typeof forEach === "function") { | ||
forEach.call(source, callback, thisArg); | ||
} | ||
} | ||
} | ||
function getKeys<K, V>(source: ForEachable<K, V>) { | ||
const keys: K[] = []; | ||
forEach(source, (_, key) => { keys.push(key); }); | ||
return keys; | ||
} | ||
// naive MapIterator shim | ||
function CreateMapIterator<K, V>(keys: K[], values: V[], kind: string): Iterator<K | V | [K, V]> { | ||
let index = 0; | ||
return { | ||
next() { | ||
if ((keys || values) && index < (keys || values).length) { | ||
const current = index++; | ||
switch (kind) { | ||
case "key": return { value: keys[current], done: false }; | ||
case "value": return { value: values[current], done: false }; | ||
case "key+value": return { value: [keys[current], values[current]], done: false }; | ||
} | ||
} | ||
keys = undefined; | ||
values = undefined; | ||
return { value: undefined, done: true }; | ||
}, | ||
has(key: any): boolean { | ||
if (key === this._cache) { | ||
return true; | ||
"throw"(error: any): any { | ||
if (keys || values) { | ||
keys = undefined; | ||
values = undefined; | ||
} | ||
if (this._find(key) >= 0) { | ||
this._cache = key; | ||
return true; | ||
} | ||
return false; | ||
throw error; | ||
}, | ||
get(key: any): any { | ||
let index = this._find(key); | ||
if (index >= 0) { | ||
this._cache = key; | ||
return this._values[index]; | ||
"return"(value: any) { | ||
if (keys || values) { | ||
keys = undefined; | ||
values = undefined; | ||
} | ||
return undefined; | ||
}, | ||
set(key: any, value: any): Map<any, any> { | ||
this.delete(key); | ||
this._keys.push(key); | ||
this._values.push(value); | ||
this._cache = key; | ||
return { value, done: true }; | ||
} | ||
}; | ||
} | ||
// naive Map shim | ||
function CreateMapPolyfill(): MapConstructor { | ||
const cacheSentinel = {}; | ||
return class Map<K, V> { | ||
private _keys: K[] = []; | ||
private _values: V[] = []; | ||
private _cacheKey = cacheSentinel; | ||
private _cacheIndex = -2; | ||
get size() { return this._keys.length; } | ||
has(key: K): boolean { return this._find(key, /*insert*/ false) >= 0; } | ||
get(key: K): V { | ||
const index = this._find(key, /*insert*/ false); | ||
return index >= 0 ? this._values[index] : undefined; | ||
} | ||
set(key: K, value: V): Map<K, V> { | ||
const index = this._find(key, /*insert*/ true); | ||
this._values[index] = value; | ||
return this; | ||
}, | ||
delete(key: any): boolean { | ||
let index = this._find(key); | ||
} | ||
delete(key: K): boolean { | ||
const index = this._find(key, /*insert*/ false); | ||
if (index >= 0) { | ||
this._keys.splice(index, 1); | ||
this._values.splice(index, 1); | ||
this._cache = cacheSentinel; | ||
const size = this._keys.length; | ||
for (let i = index + 1; i < size; i++) { | ||
this._keys[i - 1] = this._keys[i]; | ||
this._values[i - 1] = this._values[i]; | ||
} | ||
this._keys.length--; | ||
this._values.length--; | ||
this._cacheKey = cacheSentinel; | ||
this._cacheIndex = -2; | ||
return true; | ||
} | ||
return false; | ||
}, | ||
} | ||
clear(): void { | ||
this._keys.length = 0; | ||
this._values.length = 0; | ||
this._cache = cacheSentinel; | ||
}, | ||
forEach(callback: (value: any, key: any, map: Map<any, any>) => void, thisArg?: any): void { | ||
let size = this.size; | ||
for (let i = 0; i < size; ++i) { | ||
let key = this._keys[i]; | ||
let value = this._values[i]; | ||
this._cache = key; | ||
callback.call(this, value, key, this); | ||
this._cacheKey = cacheSentinel; | ||
this._cacheIndex = -2; | ||
} | ||
keys() { return CreateMapIterator(this._keys, /*values*/ undefined, "key") as Iterator<K>; } | ||
values() { return CreateMapIterator(/*keys*/ undefined, this._values, "value") as Iterator<V>; } | ||
entries() { return CreateMapIterator(this._keys, this._values, "key+value") as Iterator<[K, V]>; } | ||
private _find(key: K, insert?: boolean): number { | ||
if (this._cacheKey === key) return this._cacheIndex; | ||
let index = this._keys.indexOf(key); | ||
if (index < 0 && insert) { | ||
index = this._keys.length; | ||
this._keys.push(key); | ||
this._values.push(undefined); | ||
} | ||
}, | ||
_find(key: any): number { | ||
const keys = this._keys; | ||
const size = keys.length; | ||
for (let i = 0; i < size; ++i) { | ||
if (keys[i] === key) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
return this._cacheKey = key, this._cacheIndex = index; | ||
} | ||
}; | ||
return <any>Map; | ||
} | ||
// naive Set shim | ||
function CreateSetPolyfill() { | ||
const cacheSentinel = {}; | ||
function Set() { | ||
this._map = new _Map<any, any>(); | ||
} | ||
Set.prototype = { | ||
get size() { | ||
return this._map.length; | ||
}, | ||
has(value: any): boolean { | ||
return this._map.has(value); | ||
}, | ||
add(value: any): Set<any> { | ||
this._map.set(value, value); | ||
return this; | ||
}, | ||
delete(value: any): boolean { | ||
return this._map.delete(value); | ||
}, | ||
clear(): void { | ||
this._map.clear(); | ||
}, | ||
forEach(callback: (value: any, key: any, set: Set<any>) => void, thisArg?: any): void { | ||
this._map.forEach(callback, thisArg); | ||
} | ||
function CreateSetPolyfill(): SetConstructor { | ||
return class Set<T> { | ||
private _map = new _Map<any, any>(); | ||
get size() { return this._map.size; } | ||
has(value: T): boolean { return this._map.has(value); } | ||
add(value: T): Set<T> { return this._map.set(value, value), this; } | ||
delete(value: T): boolean { return this._map.delete(value); } | ||
clear(): void { this._map.clear(); } | ||
keys() { return this._map.keys(); } | ||
values() { return this._map.values(); } | ||
entries() { return this._map.entries(); } | ||
}; | ||
return <any>Set; | ||
} | ||
// naive WeakMap shim | ||
function CreateWeakMapPolyfill() { | ||
function CreateWeakMapPolyfill(): WeakMapConstructor { | ||
const UUID_SIZE = 16; | ||
const isNode = typeof global !== "undefined" && Object.prototype.toString.call(global.process) === '[object process]'; | ||
const nodeCrypto = isNode && function () { try { return (void 0, require)("crypto"); } catch (e) { } }(); | ||
const hasOwn = Object.prototype.hasOwnProperty; | ||
const keys: { [key: string]: boolean; } = {}; | ||
const keys = createDictionary(); | ||
const rootKey = CreateUniqueKey(); | ||
function WeakMap() { | ||
this._key = CreateUniqueKey(); | ||
} | ||
WeakMap.prototype = { | ||
has(target: Object): boolean { | ||
let table = GetOrCreateWeakMapTable(target, /*create*/ false); | ||
if (table) { | ||
return this._key in table; | ||
} | ||
return false; | ||
}, | ||
get(target: Object): any { | ||
let table = GetOrCreateWeakMapTable(target, /*create*/ false); | ||
if (table) { | ||
return table[this._key]; | ||
} | ||
return undefined; | ||
}, | ||
set(target: Object, value: any): WeakMap<any, any> { | ||
let table = GetOrCreateWeakMapTable(target, /*create*/ true); | ||
return class WeakMap<K, V> { | ||
private _key = CreateUniqueKey(); | ||
has(target: K): boolean { | ||
const table = GetOrCreateWeakMapTable<K>(target, /*create*/ false); | ||
return table !== undefined ? HashMap.has(table, this._key) : false; | ||
} | ||
get(target: K): V { | ||
const table = GetOrCreateWeakMapTable<K>(target, /*create*/ false); | ||
return table !== undefined ? HashMap.get(table, this._key) : undefined; | ||
} | ||
set(target: K, value: V): WeakMap<K, V> { | ||
const table = GetOrCreateWeakMapTable<K>(target, /*create*/ true); | ||
table[this._key] = value; | ||
return this; | ||
}, | ||
delete(target: Object): boolean { | ||
let table = GetOrCreateWeakMapTable(target, /*create*/ false); | ||
if (table && this._key in table) { | ||
return delete table[this._key]; | ||
} | ||
return false; | ||
}, | ||
} | ||
delete(target: K): boolean { | ||
const table = GetOrCreateWeakMapTable<K>(target, /*create*/ false); | ||
return table !== undefined ? delete table[this._key] : false; | ||
} | ||
clear(): void { | ||
@@ -1462,54 +1394,30 @@ // NOTE: not a real clear, just makes the previous data unreachable | ||
} | ||
} | ||
}; | ||
function FillRandomBytes(buffer: BufferLike, size: number): void { | ||
for (var i = 0; i < size; ++i) { | ||
buffer[i] = Math.random() * 255 | 0; | ||
} | ||
function FillRandomBytes(buffer: BufferLike, size: number): BufferLike { | ||
for (let i = 0; i < size; ++i) buffer[i] = Math.random() * 0xff | 0; | ||
return buffer; | ||
} | ||
function GenRandomBytes(size: number): BufferLike { | ||
if (nodeCrypto) { | ||
let data = nodeCrypto.randomBytes(size); | ||
return data; | ||
if (typeof Uint8Array === "function") { | ||
if (typeof crypto !== "undefined") return crypto.getRandomValues(new Uint8Array(size)) as Uint8Array; | ||
if (typeof msCrypto !== "undefined") return msCrypto.getRandomValues(new Uint8Array(size)) as Uint8Array; | ||
return FillRandomBytes(new Uint8Array(size), size); | ||
} | ||
else if (typeof Uint8Array === "function") { | ||
let data = new Uint8Array(size); | ||
if (typeof crypto !== "undefined") { | ||
crypto.getRandomValues(<Uint8Array>data); | ||
} | ||
else if (typeof msCrypto !== "undefined") { | ||
msCrypto.getRandomValues(<Uint8Array>data); | ||
} | ||
else { | ||
FillRandomBytes(data, size); | ||
} | ||
return data; | ||
} | ||
else { | ||
let data = new Array(size); | ||
FillRandomBytes(data, size); | ||
return data; | ||
} | ||
return FillRandomBytes(new Array(size), size); | ||
} | ||
function CreateUUID() { | ||
let data = GenRandomBytes(UUID_SIZE); | ||
const data = GenRandomBytes(UUID_SIZE); | ||
// mark as random - RFC 4122 § 4.4 | ||
data[6] = data[6] & 0x4f | 0x40; | ||
data[8] = data[8] & 0xbf | 0x80; | ||
let result = ""; | ||
for (let offset = 0; offset < UUID_SIZE; ++offset) { | ||
let byte = data[offset]; | ||
if (offset === 4 || offset === 6 || offset === 8) { | ||
result += "-"; | ||
} | ||
if (byte < 16) { | ||
result += "0"; | ||
} | ||
const byte = data[offset]; | ||
if (offset === 4 || offset === 6 || offset === 8) result += "-"; | ||
if (byte < 16) result += "0"; | ||
result += byte.toString(16).toLowerCase(); | ||
} | ||
return result; | ||
@@ -1520,6 +1428,4 @@ } | ||
let key: string; | ||
do { | ||
key = "@@WeakMap@@" + CreateUUID(); | ||
} | ||
while (hasOwn.call(keys, key)); | ||
do key = "@@WeakMap@@" + CreateUUID(); | ||
while (HashMap.has(keys, key)); | ||
keys[key] = true; | ||
@@ -1529,27 +1435,26 @@ return key; | ||
function GetOrCreateWeakMapTable(target: Object, create: boolean): { [key: string]: any; } { | ||
function GetOrCreateWeakMapTable<K>(target: K, create: boolean): HashMap<any> { | ||
if (!hasOwn.call(target, rootKey)) { | ||
if (!create) { | ||
return undefined; | ||
} | ||
Object.defineProperty(target, rootKey, { value: Object.create(null) }); | ||
if (!create) return undefined; | ||
Object.defineProperty(target, rootKey, { value: createDictionary<any>() }); | ||
} | ||
return (<any>target)[rootKey]; | ||
} | ||
return <any>WeakMap; | ||
} | ||
interface BufferLike { | ||
[offset: number]: number; | ||
length: number; | ||
// uses a heuristic used by v8 and chakra to force an object into dictionary mode. | ||
function MakeDictionary<T>(obj: T): T { | ||
(<any>obj).__DICTIONARY_MODE__ = 1; | ||
delete (<any>obj).____DICTIONARY_MODE__; | ||
return obj; | ||
} | ||
// hook global Reflect | ||
(function(__global: any) { | ||
// patch global Reflect | ||
(function (__global: any) { | ||
if (typeof __global.Reflect !== "undefined") { | ||
if (__global.Reflect !== Reflect) { | ||
for (var p in Reflect) { | ||
__global.Reflect[p] = (<any>Reflect)[p]; | ||
for (const p in Reflect) { | ||
if (hasOwn.call(Reflect, p)) { | ||
__global.Reflect[p] = (<any>Reflect)[p]; | ||
} | ||
} | ||
@@ -1556,0 +1461,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
549927
62
5434
0
0
170