diff --git a/.gitignore b/.gitignore index 005715f..fa2f5e6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,10 @@ .vscode/ node_modules/ test/**/*.js -test/**/*.js.map \ No newline at end of file +test/**/*.js.map +index.d.mts +no-conflict.d.mts +*.js +*.js.map +*.mjs +*.mjs.map diff --git a/.npmignore b/.npmignore index 53a9e9b..55d2120 100644 --- a/.npmignore +++ b/.npmignore @@ -1,14 +1,20 @@ .vscode node_modules out +docs spec temp test typings bower.json gulpfile.js +globals.d.ts Reflect.ts Reflect.js.map +ReflectLite.ts +ReflectLite.js.map +ReflectNoConflict.ts +ReflectNoConflict.js.map spec.html tsconfig.json tsconfig-release.json \ No newline at end of file diff --git a/README.md b/README.md index be056dc..8f6feb5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,11 @@ # Metadata Reflection API +NOTE: Now that both [Decorators](https://github.com/tc39/proposal-decorators) and +[Decorator Metadata](https://github.com/tc39/proposal-decorator-metadata) have achieved Stage 3 within TC39, the API +proposed below is no longer being considered for standardization. However, this package will continue to support +projects that leverage TypeScript's legacy `--experimentalDecorators` option as some projects may not be able to migrate +to use standard decorators. + * [Detailed proposal][Metadata-Spec] ## Installation @@ -8,6 +14,53 @@ npm install reflect-metadata ``` +## Usage + +### ES Modules in NodeJS/Browser, TypeScript/Babel, Bundlers +```ts +// - Modifies global `Reflect` object (or defines one in ES5 runtimes). +// - Supports ESM and CommonJS. +// - Contains internal polyfills for `Map`, `Set`, and `WeakMap` for older runtimes. +import "reflect-metadata"; + +// - Modifies global `Reflect` object (or defines one in ES5 runtimes). +// - Supports ESM and CommonJS. +// - Requires runtime support for `"exports"` in `package.json`. +// - Does not include internal polyfills. +import "reflect-metadata/lite"; +``` + +### CommonJS +```ts +// - Modifies global `Reflect` object (or defines one in ES5 runtimes). +// - Contains internal polyfills for `Map`, `Set`, and `WeakMap` for older runtimes. +require("reflect-metadata"); + +// - Modifies global `Reflect` object (or defines one in ES5 runtimes). +// - Requires runtime support for `"exports"` in `package.json`. +// - Does not include internal polyfills. +require("reflect-metadata/lite"); +``` + +### In the Browser via ` + + + + +``` + +**Script** +```js +// - Makes types available in your editor. +/// + +``` + ## Background * Decorators add the ability to augment a class and its members as the class is defined, through a declarative syntax. diff --git a/Reflect.d.ts b/Reflect.d.ts deleted file mode 100644 index 7cce59f..0000000 --- a/Reflect.d.ts +++ /dev/null @@ -1,494 +0,0 @@ -/*! ***************************************************************************** -Copyright (C) Microsoft. All rights reserved. -Licensed under the Apache License, Version 2.0 (the "License"); you may not use -this file except in compliance with the License. You may obtain a copy of the -License at http://www.apache.org/licenses/LICENSE-2.0 - -THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED -WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, -MERCHANTABLITY OR NON-INFRINGEMENT. - -See the Apache Version 2.0 License for specific language governing permissions -and limitations under the License. -***************************************************************************** */ - -// NOTE: This file is obsolete and may be removed in a later release. -// For CommonJS/AMD/UMD/SystemJS declarations please use 'index.d.ts'. -// For standalone browser declarations, please use 'standalone.d.ts'. - -declare module "reflect-metadata" { - // The "reflect-metadata" module has no imports or exports, but can be used by modules to load the polyfill. -} - -declare namespace Reflect { - /** - * Applies a set of decorators to a target object. - * @param decorators An array of decorators. - * @param target The target object. - * @returns The result of applying the provided decorators. - * @remarks Decorators are applied in reverse order of their positions in the array. - * @example - * - * class Example { } - * - * // constructor - * Example = Reflect.decorate(decoratorsArray, Example); - * - */ - function decorate(decorators: ClassDecorator[], target: Function): Function; - /** - * Applies a set of decorators to a property of a target object. - * @param decorators An array of decorators. - * @param target The target object. - * @param targetKey The property key to decorate. - * @param descriptor A property descriptor - * @remarks Decorators are applied in reverse order. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod() { } - * method() { } - * } - * - * // property (on constructor) - * Reflect.decorate(decoratorsArray, Example, "staticProperty"); - * - * // property (on prototype) - * Reflect.decorate(decoratorsArray, Example.prototype, "property"); - * - * // method (on constructor) - * Object.defineProperty(Example, "staticMethod", - * Reflect.decorate(decoratorsArray, Example, "staticMethod", - * Object.getOwnPropertyDescriptor(Example, "staticMethod"))); - * - * // method (on prototype) - * Object.defineProperty(Example.prototype, "method", - * Reflect.decorate(decoratorsArray, Example.prototype, "method", - * Object.getOwnPropertyDescriptor(Example.prototype, "method"))); - * - */ - function decorate(decorators: (PropertyDecorator | MethodDecorator)[], target: Object, targetKey: string | symbol, descriptor?: PropertyDescriptor): PropertyDescriptor; - /** - * A default metadata decorator factory that can be used on a class, class member, or parameter. - * @param metadataKey The key for the metadata entry. - * @param metadataValue The value for the metadata entry. - * @returns A decorator function. - * @remarks - * If `metadataKey` is already defined for the target and target key, the - * metadataValue for that key will be overwritten. - * @example - * - * // constructor - * @Reflect.metadata(key, value) - * class Example { - * } - * - * // property (on constructor, TypeScript only) - * class Example { - * @Reflect.metadata(key, value) - * static staticProperty; - * } - * - * // property (on prototype, TypeScript only) - * class Example { - * @Reflect.metadata(key, value) - * property; - * } - * - * // method (on constructor) - * class Example { - * @Reflect.metadata(key, value) - * static staticMethod() { } - * } - * - * // method (on prototype) - * class Example { - * @Reflect.metadata(key, value) - * method() { } - * } - * - */ - function metadata(metadataKey: any, metadataValue: any): { - (target: Function): void; - (target: Object, targetKey: string | symbol): void; - }; - /** - * Define a unique metadata entry on the target. - * @param metadataKey A key used to store and retrieve metadata. - * @param metadataValue A value that contains attached metadata. - * @param target The target object on which to define metadata. - * @example - * - * class Example { - * } - * - * // constructor - * Reflect.defineMetadata("custom:annotation", options, Example); - * - * // decorator factory as metadata-producing annotation. - * function MyAnnotation(options): ClassDecorator { - * return target => Reflect.defineMetadata("custom:annotation", options, target); - * } - * - */ - function defineMetadata(metadataKey: any, metadataValue: any, target: Object): void; - /** - * Define a unique metadata entry on the target. - * @param metadataKey A key used to store and retrieve metadata. - * @param metadataValue A value that contains attached metadata. - * @param target The target object on which to define metadata. - * @param targetKey The property key for the target. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // property (on constructor) - * Reflect.defineMetadata("custom:annotation", Number, Example, "staticProperty"); - * - * // property (on prototype) - * Reflect.defineMetadata("custom:annotation", Number, Example.prototype, "property"); - * - * // method (on constructor) - * Reflect.defineMetadata("custom:annotation", Number, Example, "staticMethod"); - * - * // method (on prototype) - * Reflect.defineMetadata("custom:annotation", Number, Example.prototype, "method"); - * - * // decorator factory as metadata-producing annotation. - * function MyAnnotation(options): PropertyDecorator { - * return (target, key) => Reflect.defineMetadata("custom:annotation", options, target, key); - * } - * - */ - function defineMetadata(metadataKey: any, metadataValue: any, target: Object, targetKey: string | symbol): void; - /** - * Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined. - * @param metadataKey A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`. - * @example - * - * class Example { - * } - * - * // constructor - * result = Reflect.hasMetadata("custom:annotation", Example); - * - */ - function hasMetadata(metadataKey: any, target: Object): boolean; - /** - * Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined. - * @param metadataKey A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @param targetKey The property key for the target. - * @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // property (on constructor) - * result = Reflect.hasMetadata("custom:annotation", Example, "staticProperty"); - * - * // property (on prototype) - * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "property"); - * - * // method (on constructor) - * result = Reflect.hasMetadata("custom:annotation", Example, "staticMethod"); - * - * // method (on prototype) - * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "method"); - * - */ - function hasMetadata(metadataKey: any, target: Object, targetKey: string | symbol): boolean; - /** - * Gets a value indicating whether the target object has the provided metadata key defined. - * @param metadataKey A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @returns `true` if the metadata key was defined on the target object; otherwise, `false`. - * @example - * - * class Example { - * } - * - * // constructor - * result = Reflect.hasOwnMetadata("custom:annotation", Example); - * - */ - function hasOwnMetadata(metadataKey: any, target: Object): boolean; - /** - * Gets a value indicating whether the target object has the provided metadata key defined. - * @param metadataKey A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @param targetKey The property key for the target. - * @returns `true` if the metadata key was defined on the target object; otherwise, `false`. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // property (on constructor) - * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticProperty"); - * - * // property (on prototype) - * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "property"); - * - * // method (on constructor) - * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticMethod"); - * - * // method (on prototype) - * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "method"); - * - */ - function hasOwnMetadata(metadataKey: any, target: Object, targetKey: string | symbol): boolean; - /** - * Gets the metadata value for the provided metadata key on the target object or its prototype chain. - * @param metadataKey A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @returns The metadata value for the metadata key if found; otherwise, `undefined`. - * @example - * - * class Example { - * } - * - * // constructor - * result = Reflect.getMetadata("custom:annotation", Example); - * - */ - function getMetadata(metadataKey: any, target: Object): any; - /** - * Gets the metadata value for the provided metadata key on the target object or its prototype chain. - * @param metadataKey A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @param targetKey The property key for the target. - * @returns The metadata value for the metadata key if found; otherwise, `undefined`. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // property (on constructor) - * result = Reflect.getMetadata("custom:annotation", Example, "staticProperty"); - * - * // property (on prototype) - * result = Reflect.getMetadata("custom:annotation", Example.prototype, "property"); - * - * // method (on constructor) - * result = Reflect.getMetadata("custom:annotation", Example, "staticMethod"); - * - * // method (on prototype) - * result = Reflect.getMetadata("custom:annotation", Example.prototype, "method"); - * - */ - function getMetadata(metadataKey: any, target: Object, targetKey: string | symbol): any; - /** - * Gets the metadata value for the provided metadata key on the target object. - * @param metadataKey A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @returns The metadata value for the metadata key if found; otherwise, `undefined`. - * @example - * - * class Example { - * } - * - * // constructor - * result = Reflect.getOwnMetadata("custom:annotation", Example); - * - */ - function getOwnMetadata(metadataKey: any, target: Object): any; - /** - * Gets the metadata value for the provided metadata key on the target object. - * @param metadataKey A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @param targetKey The property key for the target. - * @returns The metadata value for the metadata key if found; otherwise, `undefined`. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // property (on constructor) - * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticProperty"); - * - * // property (on prototype) - * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "property"); - * - * // method (on constructor) - * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticMethod"); - * - * // method (on prototype) - * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "method"); - * - */ - function getOwnMetadata(metadataKey: any, target: Object, targetKey: string | symbol): any; - /** - * Gets the metadata keys defined on the target object or its prototype chain. - * @param target The target object on which the metadata is defined. - * @returns An array of unique metadata keys. - * @example - * - * class Example { - * } - * - * // constructor - * result = Reflect.getMetadataKeys(Example); - * - */ - function getMetadataKeys(target: Object): any[]; - /** - * Gets the metadata keys defined on the target object or its prototype chain. - * @param target The target object on which the metadata is defined. - * @param targetKey The property key for the target. - * @returns An array of unique metadata keys. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // property (on constructor) - * result = Reflect.getMetadataKeys(Example, "staticProperty"); - * - * // property (on prototype) - * result = Reflect.getMetadataKeys(Example.prototype, "property"); - * - * // method (on constructor) - * result = Reflect.getMetadataKeys(Example, "staticMethod"); - * - * // method (on prototype) - * result = Reflect.getMetadataKeys(Example.prototype, "method"); - * - */ - function getMetadataKeys(target: Object, targetKey: string | symbol): any[]; - /** - * Gets the unique metadata keys defined on the target object. - * @param target The target object on which the metadata is defined. - * @returns An array of unique metadata keys. - * @example - * - * class Example { - * } - * - * // constructor - * result = Reflect.getOwnMetadataKeys(Example); - * - */ - function getOwnMetadataKeys(target: Object): any[]; - /** - * Gets the unique metadata keys defined on the target object. - * @param target The target object on which the metadata is defined. - * @param targetKey The property key for the target. - * @returns An array of unique metadata keys. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // property (on constructor) - * result = Reflect.getOwnMetadataKeys(Example, "staticProperty"); - * - * // property (on prototype) - * result = Reflect.getOwnMetadataKeys(Example.prototype, "property"); - * - * // method (on constructor) - * result = Reflect.getOwnMetadataKeys(Example, "staticMethod"); - * - * // method (on prototype) - * result = Reflect.getOwnMetadataKeys(Example.prototype, "method"); - * - */ - function getOwnMetadataKeys(target: Object, targetKey: string | symbol): any[]; - /** - * Deletes the metadata entry from the target object with the provided key. - * @param metadataKey A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @returns `true` if the metadata entry was found and deleted; otherwise, false. - * @example - * - * class Example { - * } - * - * // constructor - * result = Reflect.deleteMetadata("custom:annotation", Example); - * - */ - function deleteMetadata(metadataKey: any, target: Object): boolean; - /** - * Deletes the metadata entry from the target object with the provided key. - * @param metadataKey A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @param targetKey The property key for the target. - * @returns `true` if the metadata entry was found and deleted; otherwise, false. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * static staticMethod(p) { } - * method(p) { } - * } - * - * // property (on constructor) - * result = Reflect.deleteMetadata("custom:annotation", Example, "staticProperty"); - * - * // property (on prototype) - * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "property"); - * - * // method (on constructor) - * result = Reflect.deleteMetadata("custom:annotation", Example, "staticMethod"); - * - * // method (on prototype) - * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "method"); - * - */ - function deleteMetadata(metadataKey: any, target: Object, targetKey: string | symbol): boolean; -} \ No newline at end of file diff --git a/Reflect.js b/Reflect.js deleted file mode 100644 index c1ff58f..0000000 --- a/Reflect.js +++ /dev/null @@ -1,1141 +0,0 @@ -/*! ***************************************************************************** -Copyright (C) Microsoft. All rights reserved. -Licensed under the Apache License, Version 2.0 (the "License"); you may not use -this file except in compliance with the License. You may obtain a copy of the -License at http://www.apache.org/licenses/LICENSE-2.0 - -THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED -WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, -MERCHANTABLITY OR NON-INFRINGEMENT. - -See the Apache Version 2.0 License for specific language governing permissions -and limitations under the License. -***************************************************************************** */ -var Reflect; -(function (Reflect) { - // Metadata Proposal - // https://rbuckton.github.io/reflect-metadata/ - (function (factory) { - var root = typeof global === "object" ? global : - typeof self === "object" ? self : - typeof this === "object" ? this : - Function("return this;")(); - var exporter = makeExporter(Reflect); - if (typeof root.Reflect === "undefined") { - root.Reflect = Reflect; - } - else { - exporter = makeExporter(root.Reflect, exporter); - } - factory(exporter); - function makeExporter(target, previous) { - return function (key, value) { - if (typeof target[key] !== "function") { - Object.defineProperty(target, key, { configurable: true, writable: true, value: value }); - } - if (previous) - previous(key, value); - }; - } - })(function (exporter) { - var hasOwn = Object.prototype.hasOwnProperty; - // feature test for Symbol support - var supportsSymbol = typeof Symbol === "function"; - var toPrimitiveSymbol = supportsSymbol && typeof Symbol.toPrimitive !== "undefined" ? Symbol.toPrimitive : "@@toPrimitive"; - var iteratorSymbol = supportsSymbol && typeof Symbol.iterator !== "undefined" ? Symbol.iterator : "@@iterator"; - var supportsCreate = typeof Object.create === "function"; // feature test for Object.create support - var supportsProto = { __proto__: [] } instanceof Array; // feature test for __proto__ support - var downLevel = !supportsCreate && !supportsProto; - var HashMap = { - // create an object in dictionary mode (a.k.a. "slow" mode in v8) - create: supportsCreate - ? function () { return MakeDictionary(Object.create(null)); } - : supportsProto - ? function () { return MakeDictionary({ __proto__: null }); } - : function () { return MakeDictionary({}); }, - has: downLevel - ? function (map, key) { return hasOwn.call(map, key); } - : function (map, key) { return key in map; }, - get: downLevel - ? function (map, key) { return hasOwn.call(map, key) ? map[key] : undefined; } - : function (map, key) { return map[key]; }, - }; - // Load global or shim versions of Map, Set, and WeakMap - var functionPrototype = Object.getPrototypeOf(Function); - var usePolyfill = typeof process === "object" && process["env" + ""] && process["env" + ""]["REFLECT_METADATA_USE_MAP_POLYFILL"] === "true"; - var _Map = !usePolyfill && typeof Map === "function" && typeof Map.prototype.entries === "function" ? Map : CreateMapPolyfill(); - var _Set = !usePolyfill && typeof Set === "function" && typeof Set.prototype.entries === "function" ? Set : CreateSetPolyfill(); - var _WeakMap = !usePolyfill && typeof WeakMap === "function" ? WeakMap : CreateWeakMapPolyfill(); - // [[Metadata]] internal slot - // https://rbuckton.github.io/reflect-metadata/#ordinary-object-internal-methods-and-internal-slots - var Metadata = new _WeakMap(); - /** - * Applies a set of decorators to a property of a target object. - * @param decorators An array of decorators. - * @param target The target object. - * @param propertyKey (Optional) The property key to decorate. - * @param attributes (Optional) The property descriptor for the target key. - * @remarks Decorators are applied in reverse order. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * constructor(p) { } - * static staticMethod(p) { } - * method(p) { } - * } - * - * // constructor - * Example = Reflect.decorate(decoratorsArray, Example); - * - * // property (on constructor) - * Reflect.decorate(decoratorsArray, Example, "staticProperty"); - * - * // property (on prototype) - * Reflect.decorate(decoratorsArray, Example.prototype, "property"); - * - * // method (on constructor) - * Object.defineProperty(Example, "staticMethod", - * Reflect.decorate(decoratorsArray, Example, "staticMethod", - * Object.getOwnPropertyDescriptor(Example, "staticMethod"))); - * - * // method (on prototype) - * Object.defineProperty(Example.prototype, "method", - * Reflect.decorate(decoratorsArray, Example.prototype, "method", - * Object.getOwnPropertyDescriptor(Example.prototype, "method"))); - * - */ - function decorate(decorators, target, propertyKey, attributes) { - if (!IsUndefined(propertyKey)) { - if (!IsArray(decorators)) - throw new TypeError(); - if (!IsObject(target)) - throw new TypeError(); - if (!IsObject(attributes) && !IsUndefined(attributes) && !IsNull(attributes)) - throw new TypeError(); - if (IsNull(attributes)) - attributes = undefined; - propertyKey = ToPropertyKey(propertyKey); - return DecorateProperty(decorators, target, propertyKey, attributes); - } - else { - if (!IsArray(decorators)) - throw new TypeError(); - if (!IsConstructor(target)) - throw new TypeError(); - return DecorateConstructor(decorators, target); - } - } - exporter("decorate", decorate); - // 4.1.2 Reflect.metadata(metadataKey, metadataValue) - // https://rbuckton.github.io/reflect-metadata/#reflect.metadata - /** - * A default metadata decorator factory that can be used on a class, class member, or parameter. - * @param metadataKey The key for the metadata entry. - * @param metadataValue The value for the metadata entry. - * @returns A decorator function. - * @remarks - * If `metadataKey` is already defined for the target and target key, the - * metadataValue for that key will be overwritten. - * @example - * - * // constructor - * @Reflect.metadata(key, value) - * class Example { - * } - * - * // property (on constructor, TypeScript only) - * class Example { - * @Reflect.metadata(key, value) - * static staticProperty; - * } - * - * // property (on prototype, TypeScript only) - * class Example { - * @Reflect.metadata(key, value) - * property; - * } - * - * // method (on constructor) - * class Example { - * @Reflect.metadata(key, value) - * static staticMethod() { } - * } - * - * // method (on prototype) - * class Example { - * @Reflect.metadata(key, value) - * method() { } - * } - * - */ - function metadata(metadataKey, metadataValue) { - function decorator(target, propertyKey) { - if (!IsObject(target)) - throw new TypeError(); - if (!IsUndefined(propertyKey) && !IsPropertyKey(propertyKey)) - throw new TypeError(); - OrdinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey); - } - return decorator; - } - exporter("metadata", metadata); - /** - * Define a unique metadata entry on the target. - * @param metadataKey A key used to store and retrieve metadata. - * @param metadataValue A value that contains attached metadata. - * @param target The target object on which to define metadata. - * @param propertyKey (Optional) The property key for the target. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * constructor(p) { } - * static staticMethod(p) { } - * method(p) { } - * } - * - * // constructor - * Reflect.defineMetadata("custom:annotation", options, Example); - * - * // property (on constructor) - * Reflect.defineMetadata("custom:annotation", options, Example, "staticProperty"); - * - * // property (on prototype) - * Reflect.defineMetadata("custom:annotation", options, Example.prototype, "property"); - * - * // method (on constructor) - * Reflect.defineMetadata("custom:annotation", options, Example, "staticMethod"); - * - * // method (on prototype) - * Reflect.defineMetadata("custom:annotation", options, Example.prototype, "method"); - * - * // decorator factory as metadata-producing annotation. - * function MyAnnotation(options): Decorator { - * return (target, key?) => Reflect.defineMetadata("custom:annotation", options, target, key); - * } - * - */ - function defineMetadata(metadataKey, metadataValue, target, propertyKey) { - if (!IsObject(target)) - throw new TypeError(); - if (!IsUndefined(propertyKey)) - propertyKey = ToPropertyKey(propertyKey); - return OrdinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey); - } - exporter("defineMetadata", defineMetadata); - /** - * Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined. - * @param metadataKey A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @param propertyKey (Optional) The property key for the target. - * @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * constructor(p) { } - * static staticMethod(p) { } - * method(p) { } - * } - * - * // constructor - * result = Reflect.hasMetadata("custom:annotation", Example); - * - * // property (on constructor) - * result = Reflect.hasMetadata("custom:annotation", Example, "staticProperty"); - * - * // property (on prototype) - * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "property"); - * - * // method (on constructor) - * result = Reflect.hasMetadata("custom:annotation", Example, "staticMethod"); - * - * // method (on prototype) - * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "method"); - * - */ - function hasMetadata(metadataKey, target, propertyKey) { - if (!IsObject(target)) - throw new TypeError(); - if (!IsUndefined(propertyKey)) - propertyKey = ToPropertyKey(propertyKey); - return OrdinaryHasMetadata(metadataKey, target, propertyKey); - } - exporter("hasMetadata", hasMetadata); - /** - * Gets a value indicating whether the target object has the provided metadata key defined. - * @param metadataKey A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @param propertyKey (Optional) The property key for the target. - * @returns `true` if the metadata key was defined on the target object; otherwise, `false`. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * constructor(p) { } - * static staticMethod(p) { } - * method(p) { } - * } - * - * // constructor - * result = Reflect.hasOwnMetadata("custom:annotation", Example); - * - * // property (on constructor) - * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticProperty"); - * - * // property (on prototype) - * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "property"); - * - * // method (on constructor) - * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticMethod"); - * - * // method (on prototype) - * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "method"); - * - */ - function hasOwnMetadata(metadataKey, target, propertyKey) { - if (!IsObject(target)) - throw new TypeError(); - if (!IsUndefined(propertyKey)) - propertyKey = ToPropertyKey(propertyKey); - return OrdinaryHasOwnMetadata(metadataKey, target, propertyKey); - } - exporter("hasOwnMetadata", hasOwnMetadata); - /** - * Gets the metadata value for the provided metadata key on the target object or its prototype chain. - * @param metadataKey A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @param propertyKey (Optional) The property key for the target. - * @returns The metadata value for the metadata key if found; otherwise, `undefined`. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * constructor(p) { } - * static staticMethod(p) { } - * method(p) { } - * } - * - * // constructor - * result = Reflect.getMetadata("custom:annotation", Example); - * - * // property (on constructor) - * result = Reflect.getMetadata("custom:annotation", Example, "staticProperty"); - * - * // property (on prototype) - * result = Reflect.getMetadata("custom:annotation", Example.prototype, "property"); - * - * // method (on constructor) - * result = Reflect.getMetadata("custom:annotation", Example, "staticMethod"); - * - * // method (on prototype) - * result = Reflect.getMetadata("custom:annotation", Example.prototype, "method"); - * - */ - function getMetadata(metadataKey, target, propertyKey) { - if (!IsObject(target)) - throw new TypeError(); - if (!IsUndefined(propertyKey)) - propertyKey = ToPropertyKey(propertyKey); - return OrdinaryGetMetadata(metadataKey, target, propertyKey); - } - exporter("getMetadata", getMetadata); - /** - * Gets the metadata value for the provided metadata key on the target object. - * @param metadataKey A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @param propertyKey (Optional) The property key for the target. - * @returns The metadata value for the metadata key if found; otherwise, `undefined`. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * constructor(p) { } - * static staticMethod(p) { } - * method(p) { } - * } - * - * // constructor - * result = Reflect.getOwnMetadata("custom:annotation", Example); - * - * // property (on constructor) - * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticProperty"); - * - * // property (on prototype) - * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "property"); - * - * // method (on constructor) - * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticMethod"); - * - * // method (on prototype) - * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "method"); - * - */ - function getOwnMetadata(metadataKey, target, propertyKey) { - if (!IsObject(target)) - throw new TypeError(); - if (!IsUndefined(propertyKey)) - propertyKey = ToPropertyKey(propertyKey); - return OrdinaryGetOwnMetadata(metadataKey, target, propertyKey); - } - exporter("getOwnMetadata", getOwnMetadata); - /** - * Gets the metadata keys defined on the target object or its prototype chain. - * @param target The target object on which the metadata is defined. - * @param propertyKey (Optional) The property key for the target. - * @returns An array of unique metadata keys. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * constructor(p) { } - * static staticMethod(p) { } - * method(p) { } - * } - * - * // constructor - * result = Reflect.getMetadataKeys(Example); - * - * // property (on constructor) - * result = Reflect.getMetadataKeys(Example, "staticProperty"); - * - * // property (on prototype) - * result = Reflect.getMetadataKeys(Example.prototype, "property"); - * - * // method (on constructor) - * result = Reflect.getMetadataKeys(Example, "staticMethod"); - * - * // method (on prototype) - * result = Reflect.getMetadataKeys(Example.prototype, "method"); - * - */ - function getMetadataKeys(target, propertyKey) { - if (!IsObject(target)) - throw new TypeError(); - if (!IsUndefined(propertyKey)) - propertyKey = ToPropertyKey(propertyKey); - return OrdinaryMetadataKeys(target, propertyKey); - } - exporter("getMetadataKeys", getMetadataKeys); - /** - * Gets the unique metadata keys defined on the target object. - * @param target The target object on which the metadata is defined. - * @param propertyKey (Optional) The property key for the target. - * @returns An array of unique metadata keys. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * constructor(p) { } - * static staticMethod(p) { } - * method(p) { } - * } - * - * // constructor - * result = Reflect.getOwnMetadataKeys(Example); - * - * // property (on constructor) - * result = Reflect.getOwnMetadataKeys(Example, "staticProperty"); - * - * // property (on prototype) - * result = Reflect.getOwnMetadataKeys(Example.prototype, "property"); - * - * // method (on constructor) - * result = Reflect.getOwnMetadataKeys(Example, "staticMethod"); - * - * // method (on prototype) - * result = Reflect.getOwnMetadataKeys(Example.prototype, "method"); - * - */ - function getOwnMetadataKeys(target, propertyKey) { - if (!IsObject(target)) - throw new TypeError(); - if (!IsUndefined(propertyKey)) - propertyKey = ToPropertyKey(propertyKey); - return OrdinaryOwnMetadataKeys(target, propertyKey); - } - exporter("getOwnMetadataKeys", getOwnMetadataKeys); - /** - * Deletes the metadata entry from the target object with the provided key. - * @param metadataKey A key used to store and retrieve metadata. - * @param target The target object on which the metadata is defined. - * @param propertyKey (Optional) The property key for the target. - * @returns `true` if the metadata entry was found and deleted; otherwise, false. - * @example - * - * class Example { - * // property declarations are not part of ES6, though they are valid in TypeScript: - * // static staticProperty; - * // property; - * - * constructor(p) { } - * static staticMethod(p) { } - * method(p) { } - * } - * - * // constructor - * result = Reflect.deleteMetadata("custom:annotation", Example); - * - * // property (on constructor) - * result = Reflect.deleteMetadata("custom:annotation", Example, "staticProperty"); - * - * // property (on prototype) - * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "property"); - * - * // method (on constructor) - * result = Reflect.deleteMetadata("custom:annotation", Example, "staticMethod"); - * - * // method (on prototype) - * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "method"); - * - */ - function deleteMetadata(metadataKey, target, propertyKey) { - if (!IsObject(target)) - throw new TypeError(); - if (!IsUndefined(propertyKey)) - propertyKey = ToPropertyKey(propertyKey); - var metadataMap = GetOrCreateMetadataMap(target, propertyKey, /*Create*/ false); - if (IsUndefined(metadataMap)) - return false; - if (!metadataMap.delete(metadataKey)) - return false; - if (metadataMap.size > 0) - return true; - var targetMetadata = Metadata.get(target); - targetMetadata.delete(propertyKey); - if (targetMetadata.size > 0) - return true; - Metadata.delete(target); - return true; - } - exporter("deleteMetadata", deleteMetadata); - function DecorateConstructor(decorators, target) { - for (var i = decorators.length - 1; i >= 0; --i) { - var decorator = decorators[i]; - var decorated = decorator(target); - if (!IsUndefined(decorated) && !IsNull(decorated)) { - if (!IsConstructor(decorated)) - throw new TypeError(); - target = decorated; - } - } - return target; - } - function DecorateProperty(decorators, target, propertyKey, descriptor) { - for (var i = decorators.length - 1; i >= 0; --i) { - var decorator = decorators[i]; - var decorated = decorator(target, propertyKey, descriptor); - if (!IsUndefined(decorated) && !IsNull(decorated)) { - if (!IsObject(decorated)) - throw new TypeError(); - descriptor = decorated; - } - } - return descriptor; - } - function GetOrCreateMetadataMap(O, P, Create) { - var targetMetadata = Metadata.get(O); - if (IsUndefined(targetMetadata)) { - if (!Create) - return undefined; - targetMetadata = new _Map(); - Metadata.set(O, targetMetadata); - } - var metadataMap = targetMetadata.get(P); - if (IsUndefined(metadataMap)) { - if (!Create) - return undefined; - metadataMap = new _Map(); - targetMetadata.set(P, metadataMap); - } - return metadataMap; - } - // 3.1.1.1 OrdinaryHasMetadata(MetadataKey, O, P) - // https://rbuckton.github.io/reflect-metadata/#ordinaryhasmetadata - function OrdinaryHasMetadata(MetadataKey, O, P) { - var hasOwn = OrdinaryHasOwnMetadata(MetadataKey, O, P); - if (hasOwn) - return true; - var parent = OrdinaryGetPrototypeOf(O); - if (!IsNull(parent)) - return OrdinaryHasMetadata(MetadataKey, parent, P); - return false; - } - // 3.1.2.1 OrdinaryHasOwnMetadata(MetadataKey, O, P) - // https://rbuckton.github.io/reflect-metadata/#ordinaryhasownmetadata - function OrdinaryHasOwnMetadata(MetadataKey, O, P) { - var metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); - if (IsUndefined(metadataMap)) - return false; - return ToBoolean(metadataMap.has(MetadataKey)); - } - // 3.1.3.1 OrdinaryGetMetadata(MetadataKey, O, P) - // https://rbuckton.github.io/reflect-metadata/#ordinarygetmetadata - function OrdinaryGetMetadata(MetadataKey, O, P) { - var hasOwn = OrdinaryHasOwnMetadata(MetadataKey, O, P); - if (hasOwn) - return OrdinaryGetOwnMetadata(MetadataKey, O, P); - var parent = OrdinaryGetPrototypeOf(O); - if (!IsNull(parent)) - return OrdinaryGetMetadata(MetadataKey, parent, P); - return undefined; - } - // 3.1.4.1 OrdinaryGetOwnMetadata(MetadataKey, O, P) - // https://rbuckton.github.io/reflect-metadata/#ordinarygetownmetadata - function OrdinaryGetOwnMetadata(MetadataKey, O, P) { - var metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); - if (IsUndefined(metadataMap)) - return undefined; - return metadataMap.get(MetadataKey); - } - // 3.1.5.1 OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P) - // https://rbuckton.github.io/reflect-metadata/#ordinarydefineownmetadata - function OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P) { - var metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ true); - metadataMap.set(MetadataKey, MetadataValue); - } - // 3.1.6.1 OrdinaryMetadataKeys(O, P) - // https://rbuckton.github.io/reflect-metadata/#ordinarymetadatakeys - function OrdinaryMetadataKeys(O, P) { - var ownKeys = OrdinaryOwnMetadataKeys(O, P); - var parent = OrdinaryGetPrototypeOf(O); - if (parent === null) - return ownKeys; - var parentKeys = OrdinaryMetadataKeys(parent, P); - if (parentKeys.length <= 0) - return ownKeys; - if (ownKeys.length <= 0) - return parentKeys; - var set = new _Set(); - var keys = []; - for (var _i = 0, ownKeys_1 = ownKeys; _i < ownKeys_1.length; _i++) { - var key = ownKeys_1[_i]; - var hasKey = set.has(key); - if (!hasKey) { - set.add(key); - keys.push(key); - } - } - for (var _a = 0, parentKeys_1 = parentKeys; _a < parentKeys_1.length; _a++) { - var key = parentKeys_1[_a]; - var hasKey = set.has(key); - if (!hasKey) { - set.add(key); - keys.push(key); - } - } - return keys; - } - // 3.1.7.1 OrdinaryOwnMetadataKeys(O, P) - // https://rbuckton.github.io/reflect-metadata/#ordinaryownmetadatakeys - function OrdinaryOwnMetadataKeys(O, P) { - var keys = []; - var metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); - if (IsUndefined(metadataMap)) - return keys; - var keysObj = metadataMap.keys(); - var iterator = GetIterator(keysObj); - var k = 0; - while (true) { - var next = IteratorStep(iterator); - if (!next) { - keys.length = k; - return keys; - } - var nextValue = IteratorValue(next); - try { - keys[k] = nextValue; - } - catch (e) { - try { - IteratorClose(iterator); - } - finally { - throw e; - } - } - k++; - } - } - // 6 ECMAScript Data Typ0es and Values - // https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values - function Type(x) { - if (x === null) - return 1 /* Null */; - switch (typeof x) { - case "undefined": return 0 /* Undefined */; - case "boolean": return 2 /* Boolean */; - case "string": return 3 /* String */; - case "symbol": return 4 /* Symbol */; - case "number": return 5 /* Number */; - case "object": return x === null ? 1 /* Null */ : 6 /* Object */; - default: return 6 /* Object */; - } - } - // 6.1.1 The Undefined Type - // https://tc39.github.io/ecma262/#sec-ecmascript-language-types-undefined-type - function IsUndefined(x) { - return x === undefined; - } - // 6.1.2 The Null Type - // https://tc39.github.io/ecma262/#sec-ecmascript-language-types-null-type - function IsNull(x) { - return x === null; - } - // 6.1.5 The Symbol Type - // https://tc39.github.io/ecma262/#sec-ecmascript-language-types-symbol-type - function IsSymbol(x) { - return typeof x === "symbol"; - } - // 6.1.7 The Object Type - // https://tc39.github.io/ecma262/#sec-object-type - function IsObject(x) { - return typeof x === "object" ? x !== null : typeof x === "function"; - } - // 7.1 Type Conversion - // https://tc39.github.io/ecma262/#sec-type-conversion - // 7.1.1 ToPrimitive(input [, PreferredType]) - // https://tc39.github.io/ecma262/#sec-toprimitive - function ToPrimitive(input, PreferredType) { - switch (Type(input)) { - case 0 /* Undefined */: return input; - case 1 /* Null */: return input; - case 2 /* Boolean */: return input; - case 3 /* String */: return input; - case 4 /* Symbol */: return input; - case 5 /* Number */: return input; - } - var hint = PreferredType === 3 /* String */ ? "string" : PreferredType === 5 /* Number */ ? "number" : "default"; - var exoticToPrim = GetMethod(input, toPrimitiveSymbol); - if (exoticToPrim !== undefined) { - var result = exoticToPrim.call(input, hint); - if (IsObject(result)) - throw new TypeError(); - return result; - } - return OrdinaryToPrimitive(input, hint === "default" ? "number" : hint); - } - // 7.1.1.1 OrdinaryToPrimitive(O, hint) - // https://tc39.github.io/ecma262/#sec-ordinarytoprimitive - function OrdinaryToPrimitive(O, hint) { - if (hint === "string") { - var toString_1 = O.toString; - if (IsCallable(toString_1)) { - var result = toString_1.call(O); - if (!IsObject(result)) - return result; - } - var valueOf = O.valueOf; - if (IsCallable(valueOf)) { - var result = valueOf.call(O); - if (!IsObject(result)) - return result; - } - } - else { - var valueOf = O.valueOf; - if (IsCallable(valueOf)) { - var result = valueOf.call(O); - if (!IsObject(result)) - return result; - } - var toString_2 = O.toString; - if (IsCallable(toString_2)) { - var result = toString_2.call(O); - if (!IsObject(result)) - return result; - } - } - throw new TypeError(); - } - // 7.1.2 ToBoolean(argument) - // https://tc39.github.io/ecma262/2016/#sec-toboolean - function ToBoolean(argument) { - return !!argument; - } - // 7.1.12 ToString(argument) - // https://tc39.github.io/ecma262/#sec-tostring - function ToString(argument) { - return "" + argument; - } - // 7.1.14 ToPropertyKey(argument) - // https://tc39.github.io/ecma262/#sec-topropertykey - function ToPropertyKey(argument) { - var key = ToPrimitive(argument, 3 /* String */); - if (IsSymbol(key)) - return key; - return ToString(key); - } - // 7.2 Testing and Comparison Operations - // https://tc39.github.io/ecma262/#sec-testing-and-comparison-operations - // 7.2.2 IsArray(argument) - // https://tc39.github.io/ecma262/#sec-isarray - function IsArray(argument) { - return Array.isArray - ? Array.isArray(argument) - : argument instanceof Object - ? argument instanceof Array - : Object.prototype.toString.call(argument) === "[object Array]"; - } - // 7.2.3 IsCallable(argument) - // https://tc39.github.io/ecma262/#sec-iscallable - function IsCallable(argument) { - // NOTE: This is an approximation as we cannot check for [[Call]] internal method. - return typeof argument === "function"; - } - // 7.2.4 IsConstructor(argument) - // https://tc39.github.io/ecma262/#sec-isconstructor - function IsConstructor(argument) { - // NOTE: This is an approximation as we cannot check for [[Construct]] internal method. - return typeof argument === "function"; - } - // 7.2.7 IsPropertyKey(argument) - // https://tc39.github.io/ecma262/#sec-ispropertykey - function IsPropertyKey(argument) { - switch (Type(argument)) { - case 3 /* String */: return true; - case 4 /* Symbol */: return true; - default: return false; - } - } - function SameValueZero(x, y) { - return x === y || x !== x && y !== y; - } - // 7.3 Operations on Objects - // https://tc39.github.io/ecma262/#sec-operations-on-objects - // 7.3.9 GetMethod(V, P) - // https://tc39.github.io/ecma262/#sec-getmethod - function GetMethod(V, P) { - var func = V[P]; - if (func === undefined || func === null) - return undefined; - if (!IsCallable(func)) - throw new TypeError(); - return func; - } - // 7.4 Operations on Iterator Objects - // https://tc39.github.io/ecma262/#sec-operations-on-iterator-objects - function GetIterator(obj) { - var method = GetMethod(obj, iteratorSymbol); - if (!IsCallable(method)) - throw new TypeError(); // from Call - var iterator = method.call(obj); - if (!IsObject(iterator)) - throw new TypeError(); - return iterator; - } - // 7.4.4 IteratorValue(iterResult) - // https://tc39.github.io/ecma262/2016/#sec-iteratorvalue - function IteratorValue(iterResult) { - return iterResult.value; - } - // 7.4.5 IteratorStep(iterator) - // https://tc39.github.io/ecma262/#sec-iteratorstep - function IteratorStep(iterator) { - var result = iterator.next(); - return result.done ? false : result; - } - // 7.4.6 IteratorClose(iterator, completion) - // https://tc39.github.io/ecma262/#sec-iteratorclose - function IteratorClose(iterator) { - var f = iterator["return"]; - if (f) - f.call(iterator); - } - // 9.1 Ordinary Object Internal Methods and Internal Slots - // https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots - // 9.1.1.1 OrdinaryGetPrototypeOf(O) - // https://tc39.github.io/ecma262/#sec-ordinarygetprototypeof - function OrdinaryGetPrototypeOf(O) { - var proto = Object.getPrototypeOf(O); - if (typeof O !== "function" || O === functionPrototype) - return proto; - // TypeScript doesn't set __proto__ in ES5, as it's non-standard. - // Try to determine the superclass constructor. Compatible implementations - // must either set __proto__ on a subclass constructor to the superclass constructor, - // or ensure each class has a valid `constructor` property on its prototype that - // points back to the constructor. - // If this is not the same as Function.[[Prototype]], then this is definately inherited. - // This is the case when in ES6 or when using __proto__ in a compatible browser. - if (proto !== functionPrototype) - return proto; - // If the super prototype is Object.prototype, null, or undefined, then we cannot determine the heritage. - var prototype = O.prototype; - var 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. - var 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; - // we have a pretty good guess at the heritage. - return constructor; - } - // naive Map shim - function CreateMapPolyfill() { - var cacheSentinel = {}; - var arraySentinel = []; - var MapIterator = /** @class */ (function () { - function MapIterator(keys, values, selector) { - this._index = 0; - this._keys = keys; - this._values = values; - this._selector = selector; - } - MapIterator.prototype["@@iterator"] = function () { return this; }; - MapIterator.prototype[iteratorSymbol] = function () { return this; }; - MapIterator.prototype.next = function () { - var index = this._index; - if (index >= 0 && index < this._keys.length) { - var result = this._selector(this._keys[index], this._values[index]); - if (index + 1 >= this._keys.length) { - this._index = -1; - this._keys = arraySentinel; - this._values = arraySentinel; - } - else { - this._index++; - } - return { value: result, done: false }; - } - return { value: undefined, done: true }; - }; - MapIterator.prototype.throw = function (error) { - if (this._index >= 0) { - this._index = -1; - this._keys = arraySentinel; - this._values = arraySentinel; - } - throw error; - }; - MapIterator.prototype.return = function (value) { - if (this._index >= 0) { - this._index = -1; - this._keys = arraySentinel; - this._values = arraySentinel; - } - return { value: value, done: true }; - }; - return MapIterator; - }()); - return /** @class */ (function () { - function Map() { - this._keys = []; - this._values = []; - this._cacheKey = cacheSentinel; - this._cacheIndex = -2; - } - Object.defineProperty(Map.prototype, "size", { - get: function () { return this._keys.length; }, - enumerable: true, - configurable: true - }); - Map.prototype.has = function (key) { return this._find(key, /*insert*/ false) >= 0; }; - Map.prototype.get = function (key) { - var index = this._find(key, /*insert*/ false); - return index >= 0 ? this._values[index] : undefined; - }; - Map.prototype.set = function (key, value) { - var index = this._find(key, /*insert*/ true); - this._values[index] = value; - return this; - }; - Map.prototype.delete = function (key) { - var index = this._find(key, /*insert*/ false); - if (index >= 0) { - 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--; - if (SameValueZero(key, this._cacheKey)) { - this._cacheKey = cacheSentinel; - this._cacheIndex = -2; - } - return true; - } - return false; - }; - Map.prototype.clear = function () { - this._keys.length = 0; - this._values.length = 0; - this._cacheKey = cacheSentinel; - this._cacheIndex = -2; - }; - Map.prototype.keys = function () { return new MapIterator(this._keys, this._values, getKey); }; - Map.prototype.values = function () { return new MapIterator(this._keys, this._values, getValue); }; - Map.prototype.entries = function () { return new MapIterator(this._keys, this._values, getEntry); }; - Map.prototype["@@iterator"] = function () { return this.entries(); }; - Map.prototype[iteratorSymbol] = function () { return this.entries(); }; - Map.prototype._find = function (key, insert) { - if (!SameValueZero(this._cacheKey, key)) { - this._cacheIndex = -1; - for (var i = 0; i < this._keys.length; i++) { - if (SameValueZero(this._keys[i], key)) { - this._cacheIndex = i; - break; - } - } - } - if (this._cacheIndex < 0 && insert) { - this._cacheIndex = this._keys.length; - this._keys.push(key); - this._values.push(undefined); - } - return this._cacheIndex; - }; - return Map; - }()); - function getKey(key, _) { - return key; - } - function getValue(_, value) { - return value; - } - function getEntry(key, value) { - return [key, value]; - } - } - // naive Set shim - function CreateSetPolyfill() { - return /** @class */ (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); }; - Set.prototype.add = function (value) { return this._map.set(value, value), this; }; - Set.prototype.delete = function (value) { return this._map.delete(value); }; - Set.prototype.clear = function () { this._map.clear(); }; - Set.prototype.keys = function () { return this._map.keys(); }; - Set.prototype.values = function () { return this._map.keys(); }; - Set.prototype.entries = function () { return this._map.keys(); }; - Set.prototype["@@iterator"] = function () { return this.keys(); }; - Set.prototype[iteratorSymbol] = function () { return this.keys(); }; - return Set; - }()); - } - // naive WeakMap shim - function CreateWeakMapPolyfill() { - var UUID_SIZE = 16; - var keys = HashMap.create(); - var rootKey = CreateUniqueKey(); - return /** @class */ (function () { - function WeakMap() { - this._key = CreateUniqueKey(); - } - WeakMap.prototype.has = function (target) { - var table = GetOrCreateWeakMapTable(target, /*create*/ false); - return table !== undefined ? HashMap.has(table, this._key) : false; - }; - WeakMap.prototype.get = function (target) { - var table = GetOrCreateWeakMapTable(target, /*create*/ false); - return table !== undefined ? HashMap.get(table, this._key) : undefined; - }; - WeakMap.prototype.set = function (target, value) { - var table = GetOrCreateWeakMapTable(target, /*create*/ true); - table[this._key] = value; - return this; - }; - WeakMap.prototype.delete = function (target) { - var table = GetOrCreateWeakMapTable(target, /*create*/ false); - return table !== undefined ? delete table[this._key] : false; - }; - WeakMap.prototype.clear = function () { - // NOTE: not a real clear, just makes the previous data unreachable - this._key = CreateUniqueKey(); - }; - return WeakMap; - }()); - function CreateUniqueKey() { - var key; - do - key = "@@WeakMap@@" + CreateUUID(); - while (HashMap.has(keys, key)); - keys[key] = true; - return key; - } - function GetOrCreateWeakMapTable(target, create) { - if (!hasOwn.call(target, rootKey)) { - if (!create) - return undefined; - Object.defineProperty(target, rootKey, { value: HashMap.create() }); - } - return target[rootKey]; - } - function FillRandomBytes(buffer, size) { - for (var i = 0; i < size; ++i) - buffer[i] = Math.random() * 0xff | 0; - return buffer; - } - function GenRandomBytes(size) { - if (typeof Uint8Array === "function") { - if (typeof crypto !== "undefined") - return crypto.getRandomValues(new Uint8Array(size)); - if (typeof msCrypto !== "undefined") - return msCrypto.getRandomValues(new Uint8Array(size)); - return FillRandomBytes(new Uint8Array(size), size); - } - return FillRandomBytes(new Array(size), size); - } - function CreateUUID() { - var data = GenRandomBytes(UUID_SIZE); - // mark as random - RFC 4122 § 4.4 - data[6] = data[6] & 0x4f | 0x40; - data[8] = data[8] & 0xbf | 0x80; - var result = ""; - for (var offset = 0; offset < UUID_SIZE; ++offset) { - var byte = data[offset]; - if (offset === 4 || offset === 6 || offset === 8) - result += "-"; - if (byte < 16) - result += "0"; - result += byte.toString(16).toLowerCase(); - } - return result; - } - } - // uses a heuristic used by v8 and chakra to force an object into dictionary mode. - function MakeDictionary(obj) { - obj.__ = undefined; - delete obj.__; - return obj; - } - }); -})(Reflect || (Reflect = {})); -//# sourceMappingURL=Reflect.js.map \ No newline at end of file diff --git a/Reflect.js.map b/Reflect.js.map deleted file mode 100644 index 466a91b..0000000 --- a/Reflect.js.map +++ /dev/null @@ -1 +0,0 @@ -{"version":3,"file":"Reflect.js","sourceRoot":"","sources":["Reflect.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;gFAagF;AAChF,IAAU,OAAO,CA4wDhB;AA5wDD,WAAU,OAAO;IACb,oBAAoB;IACpB,+CAA+C;IA8lB/C,CAAC,UAAqB,OAAuG;QACzH,IAAM,IAAI,GAAG,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;YAC9C,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;gBACjC,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;oBACjC,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC;QAE/B,IAAI,QAAQ,GAAG,YAAY,CAAC,OAAO,CAAC,CAAC;QACrC,IAAI,OAAO,IAAI,CAAC,OAAO,KAAK,WAAW,EAAE;YACrC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;SAC1B;aACI;YACD,QAAQ,GAAG,YAAY,CAAC,IAAI,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;SACnD;QAED,OAAO,CAAC,QAAQ,CAAC,CAAC;QAElB,sBAAsB,MAAsB,EAAE,QAAqF;YAC/H,OAAO,UAAiC,GAAM,EAAE,KAAwB;gBACpE,IAAI,OAAO,MAAM,CAAC,GAAG,CAAC,KAAK,UAAU,EAAE;oBACn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AG,CAAC;oBAChE,IAAI,IAAI,GAAG,EAAE;wBAAE,MAAM,IAAI,GAAG,CAAC;oBAC7B,MAAM,IAAI,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;iBAC7C;gBACD,OAAO,MAAM,CAAC;YAClB,CAAC;QACL,CAAC;QAED,kFAAkF;QAClF,wBAA2B,GAAM;YACvB,GAAI,CAAC,EAAE,GAAG,SAAS,CAAC;YAC1B,OAAa,GAAI,CAAC,EAAE,CAAC;YACrB,OAAO,GAAG,CAAC;QACf,CAAC;IACL,CAAC,CAAC,CAAC;AACP,CAAC,EA5wDS,OAAO,KAAP,OAAO,QA4wDhB"} \ No newline at end of file diff --git a/Reflect.ts b/Reflect.ts index b571c0d..190d386 100644 --- a/Reflect.ts +++ b/Reflect.ts @@ -12,6 +12,7 @@ MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ + namespace Reflect { // Metadata Proposal // https://rbuckton.github.io/reflect-metadata/ @@ -23,80 +24,12 @@ namespace Reflect { length: number; } - type IteratorResult = { value: T, done: false } | { value: never, done: true }; - - interface Iterator { - next(value?: any): IteratorResult; - throw?(value: any): IteratorResult; - return?(value?: T): IteratorResult; - } - - interface Iterable { - "@@iterator"(): Iterator; - } - - interface IterableIterator extends Iterator { - "@@iterator"(): IterableIterator; - } - - interface Map extends Iterable<[K, V]> { - size: number; - has(key: K): boolean; - get(key: K): V; - set(key: K, value?: V): this; - delete(key: K): boolean; - clear(): void; - keys(): IterableIterator; - values(): IterableIterator; - entries(): IterableIterator<[K, V]>; - } - - interface MapConstructor { - new (): Map; - new (): Map; - prototype: Map; - } - - interface Set extends Iterable { - size: number; - has(value: T): boolean; - add(value: T): this; - delete(value: T): boolean; - clear(): void; - keys(): IterableIterator; - values(): IterableIterator; - entries(): IterableIterator<[T, T]>; - } - - interface SetConstructor { - new (): Set; - new (): Set; - prototype: Set; - } - - interface WeakMap { - clear(): void; - delete(key: K): boolean; - get(key: K): V; - has(key: K): boolean; - set(key: K, value?: V): WeakMap; - } - - interface WeakMapConstructor { - new (): WeakMap; - new (): WeakMap; - prototype: WeakMap; - } + type MemberDecorator = (target: Object, propertyKey: string | symbol, descriptor?: PropertyDescriptor) => PropertyDescriptor | void; - type MemberDecorator = (target: Object, propertyKey: string | symbol, descriptor?: TypedPropertyDescriptor) => TypedPropertyDescriptor | void; - declare const Symbol: { iterator: symbol, toPrimitive: symbol }; - declare const Set: SetConstructor; - declare const WeakMap: WeakMapConstructor; - declare const Map: MapConstructor; declare const global: any; + declare const globalThis: any; declare const crypto: Crypto; declare const msCrypto: Crypto; - declare const process: any; /** * Applies a set of decorators to a target object. @@ -620,32 +553,45 @@ namespace Reflect { */ export declare function deleteMetadata(metadataKey: any, target: any, propertyKey: string | symbol): boolean; - (function (this: any, factory: (exporter: (key: K, value: typeof Reflect[K]) => void) => void) { - const root = typeof global === "object" ? global : + (function (this: any, factory: (exporter: (key: K, value: typeof Reflect[K]) => void, root: any) => void) { + const root = + typeof globalThis === "object" ? globalThis : + typeof global === "object" ? global : typeof self === "object" ? self : typeof this === "object" ? this : - Function("return this;")(); + sloppyModeThis(); let exporter = makeExporter(Reflect); - if (typeof root.Reflect === "undefined") { - root.Reflect = Reflect; - } - else { + if (typeof root.Reflect !== "undefined") { exporter = makeExporter(root.Reflect, exporter); } - factory(exporter); + factory(exporter, root); + + if (typeof root.Reflect === "undefined") { + root.Reflect = Reflect; + } function makeExporter(target: typeof Reflect, previous?: (key: K, value: typeof Reflect[K]) => void) { return (key: K, value: typeof Reflect[K]) => { - if (typeof target[key] !== "function") { - Object.defineProperty(target, key, { configurable: true, writable: true, value }); - } + Object.defineProperty(target, key, { configurable: true, writable: true, value }); if (previous) previous(key, value); }; } + + function functionThis() { + try { return Function("return this;")(); } catch (_) { } + } + + function indirectEvalThis() { + try { return (void 0, eval)("(function() { return this; })()"); } catch (_) { } + } + + function sloppyModeThis() { + return functionThis() || indirectEvalThis(); + } }) - (function (exporter) { + (function (exporter, root) { const hasOwn = Object.prototype.hasOwnProperty; // feature test for Symbol support @@ -675,14 +621,12 @@ namespace Reflect { // Load global or shim versions of Map, Set, and WeakMap const functionPrototype = Object.getPrototypeOf(Function); - const usePolyfill = typeof process === "object" && process["env" + ""] && process["env" + ""]["REFLECT_METADATA_USE_MAP_POLYFILL"] === "true"; - const _Map: typeof Map = !usePolyfill && typeof Map === "function" && typeof Map.prototype.entries === "function" ? Map : CreateMapPolyfill(); - const _Set: typeof Set = !usePolyfill && typeof Set === "function" && typeof Set.prototype.entries === "function" ? Set : CreateSetPolyfill(); - const _WeakMap: typeof WeakMap = !usePolyfill && typeof WeakMap === "function" ? WeakMap : CreateWeakMapPolyfill(); - - // [[Metadata]] internal slot - // https://rbuckton.github.io/reflect-metadata/#ordinary-object-internal-methods-and-internal-slots - const Metadata = new _WeakMap>>(); + const _Map: typeof Map = typeof Map === "function" && typeof Map.prototype.entries === "function" ? Map : CreateMapPolyfill(); + const _Set: typeof Set = typeof Set === "function" && typeof Set.prototype.entries === "function" ? Set : CreateSetPolyfill(); + const _WeakMap: typeof WeakMap = typeof WeakMap === "function" ? WeakMap : CreateWeakMapPolyfill(); + const registrySymbol = supportsSymbol ? Symbol.for("@reflect-metadata:registry") : undefined; + const metadataRegistry = GetOrCreateMetadataRegistry(); + const metadataProvider = CreateMetadataProvider(metadataRegistry); function decorate(decorators: ClassDecorator[], target: Function): Function; function decorate(decorators: (PropertyDecorator | MethodDecorator)[], target: any, propertyKey: string | symbol, attributes?: PropertyDescriptor | null): PropertyDescriptor | undefined; @@ -1183,15 +1127,11 @@ namespace Reflect { function deleteMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): boolean { if (!IsObject(target)) throw new TypeError(); if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); - const metadataMap = GetOrCreateMetadataMap(target, propertyKey, /*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(propertyKey); - if (targetMetadata.size > 0) return true; - Metadata.delete(target); - return true; + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + const provider = GetMetadataProvider(target, propertyKey, /*Create*/ false); + if (IsUndefined(provider)) return false; + return provider.OrdinaryDeleteMetadata(metadataKey, target, propertyKey); } exporter("deleteMetadata", deleteMetadata); @@ -1220,26 +1160,6 @@ namespace Reflect { return descriptor; } - // 2.1.1 GetOrCreateMetadataMap(O, P, Create) - // https://rbuckton.github.io/reflect-metadata/#getorcreatemetadatamap - function GetOrCreateMetadataMap(O: any, P: string | symbol | undefined, Create: true): Map; - function GetOrCreateMetadataMap(O: any, P: string | symbol | undefined, Create: false): Map | undefined; - function GetOrCreateMetadataMap(O: any, P: string | symbol | undefined, Create: boolean): Map | undefined { - let targetMetadata = Metadata.get(O); - if (IsUndefined(targetMetadata)) { - if (!Create) return undefined; - targetMetadata = new _Map>(); - Metadata.set(O, targetMetadata); - } - let metadataMap = targetMetadata.get(P); - if (IsUndefined(metadataMap)) { - if (!Create) return undefined; - metadataMap = new _Map(); - targetMetadata.set(P, metadataMap); - } - return metadataMap; - } - // 3.1.1.1 OrdinaryHasMetadata(MetadataKey, O, P) // https://rbuckton.github.io/reflect-metadata/#ordinaryhasmetadata function OrdinaryHasMetadata(MetadataKey: any, O: any, P: string | symbol | undefined): boolean { @@ -1253,9 +1173,9 @@ namespace Reflect { // 3.1.2.1 OrdinaryHasOwnMetadata(MetadataKey, O, P) // https://rbuckton.github.io/reflect-metadata/#ordinaryhasownmetadata function OrdinaryHasOwnMetadata(MetadataKey: any, O: any, P: string | symbol | undefined): boolean { - const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); - if (IsUndefined(metadataMap)) return false; - return ToBoolean(metadataMap.has(MetadataKey)); + const provider = GetMetadataProvider(O, P, /*Create*/ false); + if (IsUndefined(provider)) return false; + return ToBoolean(provider.OrdinaryHasOwnMetadata(MetadataKey, O, P)); } // 3.1.3.1 OrdinaryGetMetadata(MetadataKey, O, P) @@ -1271,16 +1191,16 @@ namespace Reflect { // 3.1.4.1 OrdinaryGetOwnMetadata(MetadataKey, O, P) // https://rbuckton.github.io/reflect-metadata/#ordinarygetownmetadata function OrdinaryGetOwnMetadata(MetadataKey: any, O: any, P: string | symbol | undefined): any { - const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); - if (IsUndefined(metadataMap)) return undefined; - return metadataMap.get(MetadataKey); + const provider = GetMetadataProvider(O, P, /*Create*/ false); + if (IsUndefined(provider)) return; + return provider.OrdinaryGetOwnMetadata(MetadataKey, O, P); } // 3.1.5.1 OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P) // https://rbuckton.github.io/reflect-metadata/#ordinarydefineownmetadata function OrdinaryDefineOwnMetadata(MetadataKey: any, MetadataValue: any, O: any, P: string | symbol | undefined): void { - const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ true); - metadataMap.set(MetadataKey, MetadataValue); + const provider = GetMetadataProvider(O, P, /*Create*/ true); + provider.OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P); } // 3.1.6.1 OrdinaryMetadataKeys(O, P) @@ -1314,32 +1234,11 @@ namespace Reflect { // 3.1.7.1 OrdinaryOwnMetadataKeys(O, P) // https://rbuckton.github.io/reflect-metadata/#ordinaryownmetadatakeys function OrdinaryOwnMetadataKeys(O: any, P: string | symbol | undefined): any[] { - const keys: any[] = []; - const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); - if (IsUndefined(metadataMap)) return keys; - const keysObj = metadataMap.keys(); - const iterator = GetIterator(keysObj); - let k = 0; - while (true) { - const next = IteratorStep(iterator); - if (!next) { - keys.length = k; - return keys; - } - const nextValue = IteratorValue(next); - try { - keys[k] = nextValue; - } - catch (e) { - try { - IteratorClose(iterator); - } - finally { - throw e; - } - } - k++; + const provider = GetMetadataProvider(O, P, /*create*/ false); + if (!provider) { + return []; } + return provider.OrdinaryOwnMetadataKeys(O, P); } // 6 ECMAScript Data Typ0es and Values @@ -1533,13 +1432,13 @@ namespace Reflect { // 7.4.4 IteratorValue(iterResult) // https://tc39.github.io/ecma262/2016/#sec-iteratorvalue - function IteratorValue(iterResult: IteratorResult): T { + function IteratorValue(iterResult: __IteratorResult): T { return iterResult.value; } // 7.4.5 IteratorStep(iterator) // https://tc39.github.io/ecma262/#sec-iteratorstep - function IteratorStep(iterator: Iterator): IteratorResult | false { + function IteratorStep(iterator: Iterator): __IteratorResult | false { const result = iterator.next(); return result.done ? false : result; } @@ -1586,6 +1485,312 @@ namespace Reflect { return constructor; } + // Global metadata registry + // - Allows `import "reflect-metadata"` and `import "reflect-metadata/no-conflict"` to interoperate. + // - Uses isolated metadata if `Reflect` is frozen before the registry can be installed. + + /** + * Creates a registry used to allow multiple `reflect-metadata` providers. + */ + function CreateMetadataRegistry(): MetadataRegistry { + let fallback: MetadataProvider | undefined; + if (!IsUndefined(registrySymbol) && + typeof root.Reflect !== "undefined" && + !(registrySymbol in root.Reflect) && + typeof root.Reflect.defineMetadata === "function") { + // interoperate with older version of `reflect-metadata` that did not support a registry. + fallback = CreateFallbackProvider(root.Reflect); + } + + let first: MetadataProvider | undefined; + let second: MetadataProvider | undefined; + let rest: Set | undefined; + const targetProviderMap = new _WeakMap>(); + const registry: MetadataRegistry = { + registerProvider, + getProvider, + setProvider, + }; + return registry; + + function registerProvider(provider: MetadataProvider) { + if (!Object.isExtensible(registry)) { + throw new Error("Cannot add provider to a frozen registry."); + } + switch (true) { + case fallback === provider: break; + case IsUndefined(first): first = provider; break; + case first === provider: break; + case IsUndefined(second): second = provider; break; + case second === provider: break; + default: + if (rest === undefined) rest = new _Set(); + rest.add(provider); + break; + } + } + + function getProviderNoCache(O: object, P: string | symbol | undefined) { + if (!IsUndefined(first)) { + if (first.isProviderFor(O, P)) return first; + if (!IsUndefined(second)) { + if (second.isProviderFor(O, P)) return first; + if (!IsUndefined(rest)) { + const iterator = GetIterator(rest); + while (true) { + const next = IteratorStep(iterator); + if (!next) { + return undefined; + } + const provider = IteratorValue(next); + if (provider.isProviderFor(O, P)) { + IteratorClose(iterator); + return provider; + } + } + } + } + } + if (!IsUndefined(fallback) && fallback.isProviderFor(O, P)) { + return fallback; + } + return undefined; + } + + function getProvider(O: object, P: string | symbol | undefined) { + let providerMap = targetProviderMap.get(O); + let provider: MetadataProvider | undefined; + if (!IsUndefined(providerMap)) { + provider = providerMap.get(P); + } + if (!IsUndefined(provider)) { + return provider; + } + + provider = getProviderNoCache(O, P); + if (!IsUndefined(provider)) { + if (IsUndefined(providerMap)) { + providerMap = new _Map(); + targetProviderMap.set(O, providerMap); + } + providerMap.set(P, provider); + } + return provider; + } + + function hasProvider(provider: MetadataProvider) { + if (IsUndefined(provider)) throw new TypeError(); + return first === provider || second === provider || !IsUndefined(rest) && rest.has(provider); + } + + function setProvider(O: object, P: string | symbol | undefined, provider: MetadataProvider) { + if (!hasProvider(provider)) { + throw new Error("Metadata provider not registered."); + } + const existingProvider = getProvider(O, P); + if (existingProvider !== provider) { + if (!IsUndefined(existingProvider)) { + return false; + } + let providerMap = targetProviderMap.get(O); + if (IsUndefined(providerMap)) { + providerMap = new _Map(); + targetProviderMap.set(O, providerMap); + } + providerMap.set(P, provider); + } + return true; + } + } + + /** + * Gets or creates the shared registry of metadata providers. + */ + function GetOrCreateMetadataRegistry(): MetadataRegistry { + let metadataRegistry: MetadataRegistry | undefined; + if (!IsUndefined(registrySymbol) && IsObject(root.Reflect) && Object.isExtensible(root.Reflect)) { + metadataRegistry = (root.Reflect as any)[registrySymbol] as MetadataRegistry | undefined; + } + if (IsUndefined(metadataRegistry)) { + metadataRegistry = CreateMetadataRegistry(); + } + if (!IsUndefined(registrySymbol) && IsObject(root.Reflect) && Object.isExtensible(root.Reflect)) { + Object.defineProperty(root.Reflect, registrySymbol, { + enumerable: false, + configurable: false, + writable: false, + value: metadataRegistry + }); + } + return metadataRegistry; + } + + function CreateMetadataProvider(registry: MetadataRegistry): MetadataProvider { + // [[Metadata]] internal slot + // https://rbuckton.github.io/reflect-metadata/#ordinary-object-internal-methods-and-internal-slots + const metadata = new _WeakMap>>(); + const provider: MetadataProvider = { + isProviderFor(O, P) { + const targetMetadata = metadata.get(O); + if (IsUndefined(targetMetadata)) return false; + return targetMetadata.has(P); + }, + OrdinaryDefineOwnMetadata: OrdinaryDefineOwnMetadata, + OrdinaryHasOwnMetadata: OrdinaryHasOwnMetadata, + OrdinaryGetOwnMetadata: OrdinaryGetOwnMetadata, + OrdinaryOwnMetadataKeys: OrdinaryOwnMetadataKeys, + OrdinaryDeleteMetadata: OrdinaryDeleteMetadata, + }; + metadataRegistry.registerProvider(provider); + return provider; + + // 2.1.1 GetOrCreateMetadataMap(O, P, Create) + // https://rbuckton.github.io/reflect-metadata/#getorcreatemetadatamap + function GetOrCreateMetadataMap(O: object, P: string | symbol | undefined, Create: true): Map; + function GetOrCreateMetadataMap(O: object, P: string | symbol | undefined, Create: false): Map | undefined; + function GetOrCreateMetadataMap(O: object, P: string | symbol | undefined, Create: boolean) { + let targetMetadata = metadata.get(O); + let createdTargetMetadata = false; + if (IsUndefined(targetMetadata)) { + if (!Create) return undefined; + targetMetadata = new _Map>(); + metadata.set(O, targetMetadata); + createdTargetMetadata = true; + } + let metadataMap = targetMetadata.get(P); + if (IsUndefined(metadataMap)) { + if (!Create) return undefined; + metadataMap = new _Map(); + targetMetadata.set(P, metadataMap); + if (!registry.setProvider(O, P, provider)) { + targetMetadata.delete(P); + if (createdTargetMetadata) { + metadata.delete(O); + } + throw new Error("Wrong provider for target."); + } + } + return metadataMap; + } + + // 3.1.2.1 OrdinaryHasOwnMetadata(MetadataKey, O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinaryhasownmetadata + function OrdinaryHasOwnMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): boolean { + const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); + if (IsUndefined(metadataMap)) return false; + return ToBoolean(metadataMap.has(MetadataKey)); + } + + // 3.1.4.1 OrdinaryGetOwnMetadata(MetadataKey, O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinarygetownmetadata + function OrdinaryGetOwnMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): any { + const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); + if (IsUndefined(metadataMap)) return undefined; + return metadataMap.get(MetadataKey); + } + + // 3.1.5.1 OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinarydefineownmetadata + function OrdinaryDefineOwnMetadata(MetadataKey: any, MetadataValue: any, O: object, P: string | symbol | undefined): void { + const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ true); + metadataMap.set(MetadataKey, MetadataValue); + } + + // 3.1.7.1 OrdinaryOwnMetadataKeys(O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinaryownmetadatakeys + function OrdinaryOwnMetadataKeys(O: any, P: string | symbol | undefined): any[] { + const keys: any[] = []; + const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); + if (IsUndefined(metadataMap)) return keys; + const keysObj = metadataMap.keys(); + const iterator = GetIterator(keysObj); + let k = 0; + while (true) { + const next = IteratorStep(iterator); + if (!next) { + keys.length = k; + return keys; + } + const nextValue = IteratorValue(next); + try { + keys[k] = nextValue; + } + catch (e) { + try { + IteratorClose(iterator); + } + finally { + throw e; + } + } + k++; + } + } + + function OrdinaryDeleteMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): boolean { + const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); + if (IsUndefined(metadataMap)) return false; + if (!metadataMap.delete(MetadataKey)) return false; + if (metadataMap.size === 0) { + const targetMetadata = metadata.get(O); + if (!IsUndefined(targetMetadata)) { + targetMetadata.delete(P); + if (targetMetadata.size === 0) { + metadata.delete(targetMetadata); + } + } + } + return true; + } + } + + function CreateFallbackProvider(reflect: typeof Reflect): MetadataProvider { + const metadataOwner = new _WeakMap>(); + const provider: MetadataProvider = { + isProviderFor(O, P) { + let metadataPropertySet = metadataOwner.get(O); + if (!IsUndefined(metadataPropertySet)) { + return metadataPropertySet.has(P); + } + if (reflect.getOwnMetadataKeys(O, P!).length) { + if (IsUndefined(metadataPropertySet)) { + metadataPropertySet = new _Set(); + metadataOwner.set(O, metadataPropertySet); + } + metadataPropertySet.add(P); + return true; + } + return false; + }, + OrdinaryDefineOwnMetadata: reflect.defineMetadata, + OrdinaryHasOwnMetadata: reflect.hasOwnMetadata, + OrdinaryGetOwnMetadata: reflect.getOwnMetadata, + OrdinaryOwnMetadataKeys: reflect.getOwnMetadataKeys, + OrdinaryDeleteMetadata: reflect.deleteMetadata, + }; + return provider; + } + + function GetMetadataProvider(O: object, P: string | symbol | undefined, Create: true): MetadataProvider; + function GetMetadataProvider(O: object, P: string | symbol | undefined, Create: false): MetadataProvider | undefined; + /** + * Gets the metadata provider for an object. If the object has no metadata provider and this is for a create operation, + * then this module's metadata provider is assigned to the object. + */ + function GetMetadataProvider(O: object, P: string | symbol | undefined, Create: boolean): MetadataProvider | undefined { + const registeredProvider = metadataRegistry.getProvider(O, P); + if (!IsUndefined(registeredProvider)) { + return registeredProvider; + } + if (Create) { + if (metadataRegistry.setProvider(O, P, metadataProvider)) { + return metadataProvider; + } + throw new Error("Illegal state."); + } + return undefined; + } + // naive Map shim function CreateMapPolyfill(): MapConstructor { const cacheSentinel = {}; @@ -1601,9 +1806,9 @@ namespace Reflect { this._values = values; this._selector = selector; } - "@@iterator"() { return this; } - [iteratorSymbol]() { return this; } - next(): IteratorResult { + "@@iterator"(): IterableIterator { return this; } + [iteratorSymbol](): IterableIterator { return this; } + next(): __IteratorResult { const index = this._index; if (index >= 0 && index < this._keys.length) { const result = this._selector(this._keys[index], this._values[index]); @@ -1619,7 +1824,7 @@ namespace Reflect { } return { value: undefined, done: true }; } - throw(error: any): IteratorResult { + throw(error: any): __IteratorResult { if (this._index >= 0) { this._index = -1; this._keys = arraySentinel; @@ -1627,7 +1832,7 @@ namespace Reflect { } throw error; } - return(value?: R): IteratorResult { + return(value?: R): __IteratorResult { if (this._index >= 0) { this._index = -1; this._keys = arraySentinel; @@ -1637,10 +1842,14 @@ namespace Reflect { } } - return class Map { + interface MapIterator { + [Symbol.iterator](): IterableIterator; + } + + class Map { private _keys: K[] = []; private _values: (V | undefined)[] = []; - private _cacheKey = cacheSentinel; + private _cacheKey: any = cacheSentinel; private _cacheIndex = -2; get size() { return this._keys.length; } has(key: K): boolean { return this._find(key, /*insert*/ false) >= 0; } @@ -1699,7 +1908,13 @@ namespace Reflect { } return this._cacheIndex; } - }; + } + + interface Map { + [Symbol.iterator](): IterableIterator<[K, V]>; + } + + return Map; function getKey(key: K, _: V) { return key; @@ -1716,7 +1931,7 @@ namespace Reflect { // naive Set shim function CreateSetPolyfill(): SetConstructor { - return class Set { + class Set { private _map = new _Map(); get size() { return this._map.size; } has(value: T): boolean { return this._map.has(value); } @@ -1728,7 +1943,11 @@ namespace Reflect { entries() { return this._map.entries(); } "@@iterator"() { return this.keys(); } [iteratorSymbol]() { return this.keys(); } - }; + } + interface Set { + [Symbol.iterator](): IterableIterator; + } + return Set; } // naive WeakMap shim @@ -1816,4 +2035,4 @@ namespace Reflect { return obj; } }); -} \ No newline at end of file +} diff --git a/ReflectLite.ts b/ReflectLite.ts new file mode 100644 index 0000000..81f71e8 --- /dev/null +++ b/ReflectLite.ts @@ -0,0 +1,1762 @@ +/*! ***************************************************************************** +Copyright (C) Microsoft. All rights reserved. +Licensed under the Apache License, Version 2.0 (the "License"); you may not use +this file except in compliance with the License. You may obtain a copy of the +License at http://www.apache.org/licenses/LICENSE-2.0 + +THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, +MERCHANTABLITY OR NON-INFRINGEMENT. + +See the Apache Version 2.0 License for specific language governing permissions +and limitations under the License. +***************************************************************************** */ + +namespace Reflect { + // Metadata Proposal + // https://rbuckton.github.io/reflect-metadata/ + + type MemberDecorator = (target: Object, propertyKey: string | symbol, descriptor?: PropertyDescriptor) => PropertyDescriptor | void; + + declare const global: any; + declare const globalThis: any; + declare const self: any; + + /** + * Applies a set of decorators to a target object. + * @param decorators An array of decorators. + * @param target The target object. + * @returns The result of applying the provided decorators. + * @remarks Decorators are applied in reverse order of their positions in the array. + * @example + * + * class Example { } + * + * // constructor + * Example = Reflect.decorate(decoratorsArray, Example); + * + */ + export declare function decorate(decorators: ClassDecorator[], target: Function): Function; + + /** + * Applies a set of decorators to a property of a target object. + * @param decorators An array of decorators. + * @param target The target object. + * @param propertyKey The property key to decorate. + * @param attributes A property descriptor. + * @remarks Decorators are applied in reverse order. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod() { } + * method() { } + * } + * + * // property (on constructor) + * Reflect.decorate(decoratorsArray, Example, "staticProperty"); + * + * // property (on prototype) + * Reflect.decorate(decoratorsArray, Example.prototype, "property"); + * + * // method (on constructor) + * Object.defineProperty(Example, "staticMethod", + * Reflect.decorate(decoratorsArray, Example, "staticMethod", + * Object.getOwnPropertyDescriptor(Example, "staticMethod"))); + * + * // method (on prototype) + * Object.defineProperty(Example.prototype, "method", + * Reflect.decorate(decoratorsArray, Example.prototype, "method", + * Object.getOwnPropertyDescriptor(Example.prototype, "method"))); + * + */ + export declare function decorate(decorators: (PropertyDecorator | MethodDecorator)[], target: any, propertyKey: string | symbol, attributes?: PropertyDescriptor | null): PropertyDescriptor | undefined; + + /** + * Applies a set of decorators to a property of a target object. + * @param decorators An array of decorators. + * @param target The target object. + * @param propertyKey The property key to decorate. + * @param attributes A property descriptor. + * @remarks Decorators are applied in reverse order. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod() { } + * method() { } + * } + * + * // property (on constructor) + * Reflect.decorate(decoratorsArray, Example, "staticProperty"); + * + * // property (on prototype) + * Reflect.decorate(decoratorsArray, Example.prototype, "property"); + * + * // method (on constructor) + * Object.defineProperty(Example, "staticMethod", + * Reflect.decorate(decoratorsArray, Example, "staticMethod", + * Object.getOwnPropertyDescriptor(Example, "staticMethod"))); + * + * // method (on prototype) + * Object.defineProperty(Example.prototype, "method", + * Reflect.decorate(decoratorsArray, Example.prototype, "method", + * Object.getOwnPropertyDescriptor(Example.prototype, "method"))); + * + */ + export declare function decorate(decorators: (PropertyDecorator | MethodDecorator)[], target: any, propertyKey: string | symbol, attributes: PropertyDescriptor): PropertyDescriptor; + + /** + * A default metadata decorator factory that can be used on a class, class member, or parameter. + * @param metadataKey The key for the metadata entry. + * @param metadataValue The value for the metadata entry. + * @returns A decorator function. + * @remarks + * If `metadataKey` is already defined for the target and target key, the + * metadataValue for that key will be overwritten. + * @example + * + * // constructor + * @Reflect.metadata(key, value) + * class Example { + * } + * + * // property (on constructor, TypeScript only) + * class Example { + * @Reflect.metadata(key, value) + * static staticProperty; + * } + * + * // property (on prototype, TypeScript only) + * class Example { + * @Reflect.metadata(key, value) + * property; + * } + * + * // method (on constructor) + * class Example { + * @Reflect.metadata(key, value) + * static staticMethod() { } + * } + * + * // method (on prototype) + * class Example { + * @Reflect.metadata(key, value) + * method() { } + * } + * + */ + export declare function metadata(metadataKey: any, metadataValue: any): { (target: Function): void; (target: any, propertyKey: string | symbol): void; }; + + /** + * Define a unique metadata entry on the target. + * @param metadataKey A key used to store and retrieve metadata. + * @param metadataValue A value that contains attached metadata. + * @param target The target object on which to define metadata. + * @example + * + * class Example { + * } + * + * // constructor + * Reflect.defineMetadata("custom:annotation", options, Example); + * + * // decorator factory as metadata-producing annotation. + * function MyAnnotation(options): ClassDecorator { + * return target => Reflect.defineMetadata("custom:annotation", options, target); + * } + * + */ + export declare function defineMetadata(metadataKey: any, metadataValue: any, target: any): void; + + /** + * Define a unique metadata entry on the target. + * @param metadataKey A key used to store and retrieve metadata. + * @param metadataValue A value that contains attached metadata. + * @param target The target object on which to define metadata. + * @param propertyKey The property key for the target. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * Reflect.defineMetadata("custom:annotation", Number, Example, "staticProperty"); + * + * // property (on prototype) + * Reflect.defineMetadata("custom:annotation", Number, Example.prototype, "property"); + * + * // method (on constructor) + * Reflect.defineMetadata("custom:annotation", Number, Example, "staticMethod"); + * + * // method (on prototype) + * Reflect.defineMetadata("custom:annotation", Number, Example.prototype, "method"); + * + * // decorator factory as metadata-producing annotation. + * function MyAnnotation(options): PropertyDecorator { + * return (target, key) => Reflect.defineMetadata("custom:annotation", options, target, key); + * } + * + */ + export declare function defineMetadata(metadataKey: any, metadataValue: any, target: any, propertyKey: string | symbol): void; + + /** + * Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.hasMetadata("custom:annotation", Example); + * + */ + export declare function hasMetadata(metadataKey: any, target: any): boolean; + + /** + * Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.hasMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.hasMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "method"); + * + */ + export declare function hasMetadata(metadataKey: any, target: any, propertyKey: string | symbol): boolean; + + /** + * Gets a value indicating whether the target object has the provided metadata key defined. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @returns `true` if the metadata key was defined on the target object; otherwise, `false`. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.hasOwnMetadata("custom:annotation", Example); + * + */ + export declare function hasOwnMetadata(metadataKey: any, target: any): boolean; + + /** + * Gets a value indicating whether the target object has the provided metadata key defined. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns `true` if the metadata key was defined on the target object; otherwise, `false`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "method"); + * + */ + export declare function hasOwnMetadata(metadataKey: any, target: any, propertyKey: string | symbol): boolean; + + /** + * Gets the metadata value for the provided metadata key on the target object or its prototype chain. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @returns The metadata value for the metadata key if found; otherwise, `undefined`. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.getMetadata("custom:annotation", Example); + * + */ + export declare function getMetadata(metadataKey: any, target: any): any; + + /** + * Gets the metadata value for the provided metadata key on the target object or its prototype chain. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns The metadata value for the metadata key if found; otherwise, `undefined`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.getMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getMetadata("custom:annotation", Example.prototype, "method"); + * + */ + export declare function getMetadata(metadataKey: any, target: any, propertyKey: string | symbol): any; + + /** + * Gets the metadata value for the provided metadata key on the target object. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @returns The metadata value for the metadata key if found; otherwise, `undefined`. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.getOwnMetadata("custom:annotation", Example); + * + */ + export declare function getOwnMetadata(metadataKey: any, target: any): any; + + /** + * Gets the metadata value for the provided metadata key on the target object. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns The metadata value for the metadata key if found; otherwise, `undefined`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "method"); + * + */ + export declare function getOwnMetadata(metadataKey: any, target: any, propertyKey: string | symbol): any; + + /** + * Gets the metadata keys defined on the target object or its prototype chain. + * @param target The target object on which the metadata is defined. + * @returns An array of unique metadata keys. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.getMetadataKeys(Example); + * + */ + export declare function getMetadataKeys(target: any): any[]; + + /** + * Gets the metadata keys defined on the target object or its prototype chain. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns An array of unique metadata keys. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.getMetadataKeys(Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getMetadataKeys(Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getMetadataKeys(Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getMetadataKeys(Example.prototype, "method"); + * + */ + export declare function getMetadataKeys(target: any, propertyKey: string | symbol): any[]; + + /** + * Gets the unique metadata keys defined on the target object. + * @param target The target object on which the metadata is defined. + * @returns An array of unique metadata keys. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.getOwnMetadataKeys(Example); + * + */ + export declare function getOwnMetadataKeys(target: any): any[]; + + /** + * Gets the unique metadata keys defined on the target object. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns An array of unique metadata keys. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.getOwnMetadataKeys(Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getOwnMetadataKeys(Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getOwnMetadataKeys(Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getOwnMetadataKeys(Example.prototype, "method"); + * + */ + export declare function getOwnMetadataKeys(target: any, propertyKey: string | symbol): any[]; + + /** + * Deletes the metadata entry from the target object with the provided key. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @returns `true` if the metadata entry was found and deleted; otherwise, false. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.deleteMetadata("custom:annotation", Example); + * + */ + export declare function deleteMetadata(metadataKey: any, target: any): boolean; + + /** + * Deletes the metadata entry from the target object with the provided key. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns `true` if the metadata entry was found and deleted; otherwise, false. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.deleteMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.deleteMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "method"); + * + */ + export declare function deleteMetadata(metadataKey: any, target: any, propertyKey: string | symbol): boolean; + + (function (this: any, factory: (exporter: (key: K, value: typeof Reflect[K]) => void, root: any) => void) { + const root = + typeof globalThis === "object" ? globalThis : + typeof global === "object" ? global : + typeof self === "object" ? self : + typeof this === "object" ? this : + sloppyModeThis(); + + let exporter = makeExporter(Reflect); + if (typeof root.Reflect !== "undefined") { + exporter = makeExporter(root.Reflect, exporter); + } + + factory(exporter, root); + + if (typeof root.Reflect === "undefined") { + root.Reflect = Reflect; + } + + function makeExporter(target: typeof Reflect, previous?: (key: K, value: typeof Reflect[K]) => void) { + return (key: K, value: typeof Reflect[K]) => { + Object.defineProperty(target, key, { configurable: true, writable: true, value }); + if (previous) previous(key, value); + }; + } + + function functionThis() { + try { return Function("return this;")(); } catch (_) { } + } + + function indirectEvalThis() { + try { return (void 0, eval)("(function() { return this; })()"); } catch (_) { } + } + + function sloppyModeThis() { + return functionThis() || indirectEvalThis(); + } + }) + (function (exporter, root) { + // feature test for Symbol support + const supportsSymbol = typeof Symbol === "function"; + const toPrimitiveSymbol = supportsSymbol && typeof Symbol.toPrimitive !== "undefined" ? Symbol.toPrimitive : fail("Symbol.toPrimitive not found."); + const iteratorSymbol = supportsSymbol && typeof Symbol.iterator !== "undefined" ? Symbol.iterator : fail("Symbol.iterator not found."); + + // Load global or shim versions of Map, Set, and WeakMap + const functionPrototype = Object.getPrototypeOf(Function); + const _Map: typeof Map = typeof Map === "function" && typeof Map.prototype.entries === "function" ? Map : fail("A valid Map constructor could not be found."); + const _Set: typeof Set = typeof Set === "function" && typeof Set.prototype.entries === "function" ? Set : fail("A valid Set constructor could not be found."); + const _WeakMap: typeof WeakMap = typeof WeakMap === "function" ? WeakMap : fail("A valid WeakMap constructor could not be found."); + const registrySymbol = supportsSymbol ? Symbol.for("@reflect-metadata:registry") : undefined; + const metadataRegistry = GetOrCreateMetadataRegistry(); + const metadataProvider = CreateMetadataProvider(metadataRegistry); + + function decorate(decorators: ClassDecorator[], target: Function): Function; + function decorate(decorators: (PropertyDecorator | MethodDecorator)[], target: any, propertyKey: string | symbol, attributes?: PropertyDescriptor | null): PropertyDescriptor | undefined; + function decorate(decorators: (PropertyDecorator | MethodDecorator)[], target: any, propertyKey: string | symbol, attributes: PropertyDescriptor): PropertyDescriptor; + + /** + * Applies a set of decorators to a property of a target object. + * @param decorators An array of decorators. + * @param target The target object. + * @param propertyKey (Optional) The property key to decorate. + * @param attributes (Optional) The property descriptor for the target key. + * @remarks Decorators are applied in reverse order. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * Example = Reflect.decorate(decoratorsArray, Example); + * + * // property (on constructor) + * Reflect.decorate(decoratorsArray, Example, "staticProperty"); + * + * // property (on prototype) + * Reflect.decorate(decoratorsArray, Example.prototype, "property"); + * + * // method (on constructor) + * Object.defineProperty(Example, "staticMethod", + * Reflect.decorate(decoratorsArray, Example, "staticMethod", + * Object.getOwnPropertyDescriptor(Example, "staticMethod"))); + * + * // method (on prototype) + * Object.defineProperty(Example.prototype, "method", + * Reflect.decorate(decoratorsArray, Example.prototype, "method", + * Object.getOwnPropertyDescriptor(Example.prototype, "method"))); + * + */ + function decorate(decorators: (ClassDecorator | MemberDecorator)[], target: any, propertyKey?: string | symbol, attributes?: PropertyDescriptor | null): PropertyDescriptor | Function | undefined { + if (!IsUndefined(propertyKey)) { + if (!IsArray(decorators)) throw new TypeError(); + if (!IsObject(target)) throw new TypeError(); + if (!IsObject(attributes) && !IsUndefined(attributes) && !IsNull(attributes)) throw new TypeError(); + if (IsNull(attributes)) attributes = undefined; + propertyKey = ToPropertyKey(propertyKey); + return DecorateProperty(decorators, target, propertyKey, attributes); + } + else { + if (!IsArray(decorators)) throw new TypeError(); + if (!IsConstructor(target)) throw new TypeError(); + return DecorateConstructor(decorators, target); + } + } + + exporter("decorate", decorate); + + // 4.1.2 Reflect.metadata(metadataKey, metadataValue) + // https://rbuckton.github.io/reflect-metadata/#reflect.metadata + + /** + * A default metadata decorator factory that can be used on a class, class member, or parameter. + * @param metadataKey The key for the metadata entry. + * @param metadataValue The value for the metadata entry. + * @returns A decorator function. + * @remarks + * If `metadataKey` is already defined for the target and target key, the + * metadataValue for that key will be overwritten. + * @example + * + * // constructor + * @Reflect.metadata(key, value) + * class Example { + * } + * + * // property (on constructor, TypeScript only) + * class Example { + * @Reflect.metadata(key, value) + * static staticProperty; + * } + * + * // property (on prototype, TypeScript only) + * class Example { + * @Reflect.metadata(key, value) + * property; + * } + * + * // method (on constructor) + * class Example { + * @Reflect.metadata(key, value) + * static staticMethod() { } + * } + * + * // method (on prototype) + * class Example { + * @Reflect.metadata(key, value) + * method() { } + * } + * + */ + function metadata(metadataKey: any, metadataValue: any) { + function decorator(target: Function): void; + function decorator(target: any, propertyKey: string | symbol): void; + function decorator(target: any, propertyKey?: string | symbol): void { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey) && !IsPropertyKey(propertyKey)) throw new TypeError(); + OrdinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey); + } + return decorator; + } + + exporter("metadata", metadata); + + // 4.1.3 Reflect.defineMetadata(metadataKey, metadataValue, target [, propertyKey]) + // https://rbuckton.github.io/reflect-metadata/#reflect.definemetadata + + function defineMetadata(metadataKey: any, metadataValue: any, target: any): void; + function defineMetadata(metadataKey: any, metadataValue: any, target: any, propertyKey: string | symbol): void; + + /** + * Define a unique metadata entry on the target. + * @param metadataKey A key used to store and retrieve metadata. + * @param metadataValue A value that contains attached metadata. + * @param target The target object on which to define metadata. + * @param propertyKey (Optional) The property key for the target. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * Reflect.defineMetadata("custom:annotation", options, Example); + * + * // property (on constructor) + * Reflect.defineMetadata("custom:annotation", options, Example, "staticProperty"); + * + * // property (on prototype) + * Reflect.defineMetadata("custom:annotation", options, Example.prototype, "property"); + * + * // method (on constructor) + * Reflect.defineMetadata("custom:annotation", options, Example, "staticMethod"); + * + * // method (on prototype) + * Reflect.defineMetadata("custom:annotation", options, Example.prototype, "method"); + * + * // decorator factory as metadata-producing annotation. + * function MyAnnotation(options): Decorator { + * return (target, key?) => Reflect.defineMetadata("custom:annotation", options, target, key); + * } + * + */ + function defineMetadata(metadataKey: any, metadataValue: any, target: any, propertyKey?: string | symbol): void { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + return OrdinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey); + } + + exporter("defineMetadata", defineMetadata); + + // 4.1.4 Reflect.hasMetadata(metadataKey, target [, propertyKey]) + // https://rbuckton.github.io/reflect-metadata/#reflect.hasmetadata + + function hasMetadata(metadataKey: any, target: any): boolean; + function hasMetadata(metadataKey: any, target: any, propertyKey: string | symbol): boolean; + + /** + * Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey (Optional) The property key for the target. + * @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * result = Reflect.hasMetadata("custom:annotation", Example); + * + * // property (on constructor) + * result = Reflect.hasMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.hasMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "method"); + * + */ + function hasMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): boolean { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + return OrdinaryHasMetadata(metadataKey, target, propertyKey); + } + + exporter("hasMetadata", hasMetadata); + + // 4.1.5 Reflect.hasOwnMetadata(metadataKey, target [, propertyKey]) + // https://rbuckton.github.io/reflect-metadata/#reflect-hasownmetadata + + function hasOwnMetadata(metadataKey: any, target: any): boolean; + function hasOwnMetadata(metadataKey: any, target: any, propertyKey: string | symbol): boolean; + + /** + * Gets a value indicating whether the target object has the provided metadata key defined. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey (Optional) The property key for the target. + * @returns `true` if the metadata key was defined on the target object; otherwise, `false`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * result = Reflect.hasOwnMetadata("custom:annotation", Example); + * + * // property (on constructor) + * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "method"); + * + */ + function hasOwnMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): boolean { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + return OrdinaryHasOwnMetadata(metadataKey, target, propertyKey); + } + + exporter("hasOwnMetadata", hasOwnMetadata); + + // 4.1.6 Reflect.getMetadata(metadataKey, target [, propertyKey]) + // https://rbuckton.github.io/reflect-metadata/#reflect-getmetadata + + function getMetadata(metadataKey: any, target: any): any; + function getMetadata(metadataKey: any, target: any, propertyKey: string | symbol): any; + + /** + * Gets the metadata value for the provided metadata key on the target object or its prototype chain. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey (Optional) The property key for the target. + * @returns The metadata value for the metadata key if found; otherwise, `undefined`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * result = Reflect.getMetadata("custom:annotation", Example); + * + * // property (on constructor) + * result = Reflect.getMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getMetadata("custom:annotation", Example.prototype, "method"); + * + */ + function getMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): any { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + return OrdinaryGetMetadata(metadataKey, target, propertyKey); + } + + exporter("getMetadata", getMetadata); + + // 4.1.7 Reflect.getOwnMetadata(metadataKey, target [, propertyKey]) + // https://rbuckton.github.io/reflect-metadata/#reflect-getownmetadata + + function getOwnMetadata(metadataKey: any, target: any): any; + function getOwnMetadata(metadataKey: any, target: any, propertyKey: string | symbol): any; + + /** + * Gets the metadata value for the provided metadata key on the target object. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey (Optional) The property key for the target. + * @returns The metadata value for the metadata key if found; otherwise, `undefined`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * result = Reflect.getOwnMetadata("custom:annotation", Example); + * + * // property (on constructor) + * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "method"); + * + */ + function getOwnMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): any { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + return OrdinaryGetOwnMetadata(metadataKey, target, propertyKey); + } + + exporter("getOwnMetadata", getOwnMetadata); + + // 4.1.8 Reflect.getMetadataKeys(target [, propertyKey]) + // https://rbuckton.github.io/reflect-metadata/#reflect-getmetadatakeys + + function getMetadataKeys(target: any): any[]; + function getMetadataKeys(target: any, propertyKey: string | symbol): any[]; + + /** + * Gets the metadata keys defined on the target object or its prototype chain. + * @param target The target object on which the metadata is defined. + * @param propertyKey (Optional) The property key for the target. + * @returns An array of unique metadata keys. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * result = Reflect.getMetadataKeys(Example); + * + * // property (on constructor) + * result = Reflect.getMetadataKeys(Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getMetadataKeys(Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getMetadataKeys(Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getMetadataKeys(Example.prototype, "method"); + * + */ + function getMetadataKeys(target: any, propertyKey?: string | symbol): any[] { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + return OrdinaryMetadataKeys(target, propertyKey); + } + + exporter("getMetadataKeys", getMetadataKeys); + + // 4.1.9 Reflect.getOwnMetadataKeys(target [, propertyKey]) + // https://rbuckton.github.io/reflect-metadata/#reflect-getownmetadata + + function getOwnMetadataKeys(target: any): any[]; + function getOwnMetadataKeys(target: any, propertyKey: string | symbol): any[]; + + /** + * Gets the unique metadata keys defined on the target object. + * @param target The target object on which the metadata is defined. + * @param propertyKey (Optional) The property key for the target. + * @returns An array of unique metadata keys. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * result = Reflect.getOwnMetadataKeys(Example); + * + * // property (on constructor) + * result = Reflect.getOwnMetadataKeys(Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getOwnMetadataKeys(Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getOwnMetadataKeys(Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getOwnMetadataKeys(Example.prototype, "method"); + * + */ + function getOwnMetadataKeys(target: any, propertyKey?: string | symbol): any[] { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + return OrdinaryOwnMetadataKeys(target, propertyKey); + } + + exporter("getOwnMetadataKeys", getOwnMetadataKeys); + + // 4.1.10 Reflect.deleteMetadata(metadataKey, target [, propertyKey]) + // https://rbuckton.github.io/reflect-metadata/#reflect-deletemetadata + + function deleteMetadata(metadataKey: any, target: any): boolean; + function deleteMetadata(metadataKey: any, target: any, propertyKey: string | symbol): boolean; + + /** + * Deletes the metadata entry from the target object with the provided key. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey (Optional) The property key for the target. + * @returns `true` if the metadata entry was found and deleted; otherwise, false. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * result = Reflect.deleteMetadata("custom:annotation", Example); + * + * // property (on constructor) + * result = Reflect.deleteMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.deleteMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "method"); + * + */ + function deleteMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): boolean { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + const provider = GetMetadataProvider(target, propertyKey, /*Create*/ false); + if (IsUndefined(provider)) return false; + return provider.OrdinaryDeleteMetadata(metadataKey, target, propertyKey); + } + + exporter("deleteMetadata", deleteMetadata); + + function DecorateConstructor(decorators: ClassDecorator[], target: Function): Function { + for (let i = decorators.length - 1; i >= 0; --i) { + const decorator = decorators[i]; + const decorated = decorator(target); + if (!IsUndefined(decorated) && !IsNull(decorated)) { + if (!IsConstructor(decorated)) throw new TypeError(); + target = decorated; + } + } + return target; + } + + function DecorateProperty(decorators: MemberDecorator[], target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor | undefined): PropertyDescriptor | undefined { + for (let i = decorators.length - 1; i >= 0; --i) { + const decorator = decorators[i]; + const decorated = decorator(target, propertyKey, descriptor); + if (!IsUndefined(decorated) && !IsNull(decorated)) { + if (!IsObject(decorated)) throw new TypeError(); + descriptor = decorated; + } + } + return descriptor; + } + + // 3.1.1.1 OrdinaryHasMetadata(MetadataKey, O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinaryhasmetadata + function OrdinaryHasMetadata(MetadataKey: any, O: any, P: string | symbol | undefined): boolean { + const hasOwn = OrdinaryHasOwnMetadata(MetadataKey, O, P); + if (hasOwn) return true; + const parent = OrdinaryGetPrototypeOf(O); + if (!IsNull(parent)) return OrdinaryHasMetadata(MetadataKey, parent, P); + return false; + } + + // 3.1.2.1 OrdinaryHasOwnMetadata(MetadataKey, O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinaryhasownmetadata + function OrdinaryHasOwnMetadata(MetadataKey: any, O: any, P: string | symbol | undefined): boolean { + const provider = GetMetadataProvider(O, P, /*Create*/ false); + if (IsUndefined(provider)) return false; + return ToBoolean(provider.OrdinaryHasOwnMetadata(MetadataKey, O, P)); + } + + // 3.1.3.1 OrdinaryGetMetadata(MetadataKey, O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinarygetmetadata + function OrdinaryGetMetadata(MetadataKey: any, O: any, P: string | symbol | undefined): any { + const hasOwn = OrdinaryHasOwnMetadata(MetadataKey, O, P); + if (hasOwn) return OrdinaryGetOwnMetadata(MetadataKey, O, P); + const parent = OrdinaryGetPrototypeOf(O); + if (!IsNull(parent)) return OrdinaryGetMetadata(MetadataKey, parent, P); + return undefined; + } + + // 3.1.4.1 OrdinaryGetOwnMetadata(MetadataKey, O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinarygetownmetadata + function OrdinaryGetOwnMetadata(MetadataKey: any, O: any, P: string | symbol | undefined): any { + const provider = GetMetadataProvider(O, P, /*Create*/ false); + if (IsUndefined(provider)) return; + return provider.OrdinaryGetOwnMetadata(MetadataKey, O, P); + } + + // 3.1.5.1 OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinarydefineownmetadata + function OrdinaryDefineOwnMetadata(MetadataKey: any, MetadataValue: any, O: any, P: string | symbol | undefined): void { + const provider = GetMetadataProvider(O, P, /*Create*/ true); + provider.OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P); + } + + // 3.1.6.1 OrdinaryMetadataKeys(O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinarymetadatakeys + function OrdinaryMetadataKeys(O: any, P: string | symbol | undefined): any[] { + const ownKeys = OrdinaryOwnMetadataKeys(O, P); + const parent = OrdinaryGetPrototypeOf(O); + if (parent === null) return ownKeys; + const parentKeys = OrdinaryMetadataKeys(parent, P); + if (parentKeys.length <= 0) return ownKeys; + if (ownKeys.length <= 0) return parentKeys; + const set = new _Set(); + const keys: any[] = []; + for (const key of ownKeys) { + const hasKey = set.has(key); + if (!hasKey) { + set.add(key); + keys.push(key); + } + } + for (const key of parentKeys) { + const hasKey = set.has(key); + if (!hasKey) { + set.add(key); + keys.push(key); + } + } + return keys; + } + + // 3.1.7.1 OrdinaryOwnMetadataKeys(O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinaryownmetadatakeys + function OrdinaryOwnMetadataKeys(O: any, P: string | symbol | undefined): any[] { + const provider = GetMetadataProvider(O, P, /*create*/ false); + if (!provider) { + return []; + } + return provider.OrdinaryOwnMetadataKeys(O, P); + } + + // 6 ECMAScript Data Typ0es and Values + // https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values + function Type(x: any): Tag { + if (x === null) return Tag.Null; + switch (typeof x) { + case "undefined": return Tag.Undefined; + case "boolean": return Tag.Boolean; + case "string": return Tag.String; + case "symbol": return Tag.Symbol; + case "number": return Tag.Number; + case "object": return x === null ? Tag.Null : Tag.Object; + default: return Tag.Object; + } + } + + // 6.1 ECMAScript Language Types + // https://tc39.github.io/ecma262/#sec-ecmascript-language-types + const enum Tag { + Undefined, + Null, + Boolean, + String, + Symbol, + Number, + Object + } + + // 6.1.1 The Undefined Type + // https://tc39.github.io/ecma262/#sec-ecmascript-language-types-undefined-type + function IsUndefined(x: any): x is undefined { + return x === undefined; + } + + // 6.1.2 The Null Type + // https://tc39.github.io/ecma262/#sec-ecmascript-language-types-null-type + function IsNull(x: any): x is null { + return x === null; + } + + // 6.1.5 The Symbol Type + // https://tc39.github.io/ecma262/#sec-ecmascript-language-types-symbol-type + function IsSymbol(x: any): x is symbol { + return typeof x === "symbol"; + } + + // 6.1.7 The Object Type + // https://tc39.github.io/ecma262/#sec-object-type + function IsObject(x: T | undefined | null | boolean | string | symbol | number): x is T { + return typeof x === "object" ? x !== null : typeof x === "function"; + } + + // 7.1 Type Conversion + // https://tc39.github.io/ecma262/#sec-type-conversion + + // 7.1.1 ToPrimitive(input [, PreferredType]) + // https://tc39.github.io/ecma262/#sec-toprimitive + function ToPrimitive(input: any, PreferredType?: Tag): undefined | null | boolean | string | symbol | number { + switch (Type(input)) { + case Tag.Undefined: return input; + case Tag.Null: return input; + case Tag.Boolean: return input; + case Tag.String: return input; + case Tag.Symbol: return input; + case Tag.Number: return input; + } + const hint: "string" | "number" | "default" = PreferredType === Tag.String ? "string" : PreferredType === Tag.Number ? "number" : "default"; + const exoticToPrim = GetMethod(input, toPrimitiveSymbol); + if (exoticToPrim !== undefined) { + const result = exoticToPrim.call(input, hint); + if (IsObject(result)) throw new TypeError(); + return result; + } + return OrdinaryToPrimitive(input, hint === "default" ? "number" : hint); + } + + // 7.1.1.1 OrdinaryToPrimitive(O, hint) + // https://tc39.github.io/ecma262/#sec-ordinarytoprimitive + function OrdinaryToPrimitive(O: any, hint: "string" | "number"): undefined | null | boolean | string | symbol | number { + if (hint === "string") { + const toString = O.toString; + if (IsCallable(toString)) { + const result = toString.call(O); + if (!IsObject(result)) return result; + } + const valueOf = O.valueOf; + if (IsCallable(valueOf)) { + const result = valueOf.call(O); + if (!IsObject(result)) return result; + } + } + else { + const valueOf = O.valueOf; + if (IsCallable(valueOf)) { + const result = valueOf.call(O); + if (!IsObject(result)) return result; + } + const toString = O.toString; + if (IsCallable(toString)) { + const result = toString.call(O); + if (!IsObject(result)) return result; + } + } + throw new TypeError(); + } + + // 7.1.2 ToBoolean(argument) + // https://tc39.github.io/ecma262/2016/#sec-toboolean + function ToBoolean(argument: any): boolean { + return !!argument; + } + + // 7.1.12 ToString(argument) + // https://tc39.github.io/ecma262/#sec-tostring + function ToString(argument: any): string { + return "" + argument; + } + + // 7.1.14 ToPropertyKey(argument) + // https://tc39.github.io/ecma262/#sec-topropertykey + function ToPropertyKey(argument: any): string | symbol { + const key = ToPrimitive(argument, Tag.String); + if (IsSymbol(key)) return key; + return ToString(key); + } + + // 7.2 Testing and Comparison Operations + // https://tc39.github.io/ecma262/#sec-testing-and-comparison-operations + + // 7.2.2 IsArray(argument) + // https://tc39.github.io/ecma262/#sec-isarray + function IsArray(argument: any): argument is any[] { + return Array.isArray + ? Array.isArray(argument) + : argument instanceof Object + ? argument instanceof Array + : Object.prototype.toString.call(argument) === "[object Array]"; + } + + // 7.2.3 IsCallable(argument) + // https://tc39.github.io/ecma262/#sec-iscallable + function IsCallable(argument: any): argument is Function { + // NOTE: This is an approximation as we cannot check for [[Call]] internal method. + return typeof argument === "function"; + } + + // 7.2.4 IsConstructor(argument) + // https://tc39.github.io/ecma262/#sec-isconstructor + function IsConstructor(argument: any): argument is Function { + // NOTE: This is an approximation as we cannot check for [[Construct]] internal method. + return typeof argument === "function"; + } + + // 7.2.7 IsPropertyKey(argument) + // https://tc39.github.io/ecma262/#sec-ispropertykey + function IsPropertyKey(argument: any): argument is string | symbol { + switch (Type(argument)) { + case Tag.String: return true; + case Tag.Symbol: return true; + default: return false; + } + } + + // 7.3 Operations on Objects + // https://tc39.github.io/ecma262/#sec-operations-on-objects + + // 7.3.9 GetMethod(V, P) + // https://tc39.github.io/ecma262/#sec-getmethod + function GetMethod(V: any, P: any): Function | undefined { + const func = V[P]; + if (func === undefined || func === null) return undefined; + if (!IsCallable(func)) throw new TypeError(); + return func; + } + + // 7.4 Operations on Iterator Objects + // https://tc39.github.io/ecma262/#sec-operations-on-iterator-objects + + function GetIterator(obj: Iterable): Iterator { + const method = GetMethod(obj, iteratorSymbol); + if (!IsCallable(method)) throw new TypeError(); // from Call + const iterator = method.call(obj); + if (!IsObject(iterator)) throw new TypeError(); + return iterator; + } + + // 7.4.4 IteratorValue(iterResult) + // https://tc39.github.io/ecma262/2016/#sec-iteratorvalue + function IteratorValue(iterResult: __IteratorResult): T { + return iterResult.value; + } + + // 7.4.5 IteratorStep(iterator) + // https://tc39.github.io/ecma262/#sec-iteratorstep + function IteratorStep(iterator: Iterator): __IteratorResult | false { + const result = iterator.next(); + return result.done ? false : result; + } + + // 7.4.6 IteratorClose(iterator, completion) + // https://tc39.github.io/ecma262/#sec-iteratorclose + function IteratorClose(iterator: Iterator) { + const f = iterator["return"]; + if (f) f.call(iterator); + } + + // 9.1 Ordinary Object Internal Methods and Internal Slots + // https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots + + // 9.1.1.1 OrdinaryGetPrototypeOf(O) + // https://tc39.github.io/ecma262/#sec-ordinarygetprototypeof + function OrdinaryGetPrototypeOf(O: any): any { + const proto = Object.getPrototypeOf(O); + if (typeof O !== "function" || O === functionPrototype) return proto; + + // TypeScript doesn't set __proto__ in ES5, as it's non-standard. + // Try to determine the superclass constructor. Compatible implementations + // must either set __proto__ on a subclass constructor to the superclass constructor, + // or ensure each class has a valid `constructor` property on its prototype that + // points back to the constructor. + + // If this is not the same as Function.[[Prototype]], then this is definately inherited. + // This is the case when in ES6 or when using __proto__ in a compatible browser. + if (proto !== functionPrototype) return proto; + + // If the super prototype is Object.prototype, null, or undefined, then we cannot determine the heritage. + 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. + 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; + + // we have a pretty good guess at the heritage. + return constructor; + } + + function fail(e: any): never { + throw e; + } + + // Global metadata registry + // - Allows `import "reflect-metadata"` and `import "reflect-metadata/no-conflict"` to interoperate. + // - Uses isolated metadata if `Reflect` is frozen before the registry can be installed. + + /** + * Creates a registry used to allow multiple `reflect-metadata` providers. + */ + function CreateMetadataRegistry(): MetadataRegistry { + let fallback: MetadataProvider | undefined; + if (!IsUndefined(registrySymbol) && + typeof root.Reflect !== "undefined" && + !(registrySymbol in root.Reflect) && + typeof root.Reflect.defineMetadata === "function") { + // interoperate with older version of `reflect-metadata` that did not support a registry. + fallback = CreateFallbackProvider(root.Reflect); + } + + let first: MetadataProvider | undefined; + let second: MetadataProvider | undefined; + let rest: Set | undefined; + const targetProviderMap = new _WeakMap>(); + const registry: MetadataRegistry = { + registerProvider, + getProvider, + setProvider, + }; + return registry; + + function registerProvider(provider: MetadataProvider) { + if (!Object.isExtensible(registry)) { + throw new Error("Cannot add provider to a frozen registry."); + } + switch (true) { + case fallback === provider: break; + case IsUndefined(first): first = provider; break; + case first === provider: break; + case IsUndefined(second): second = provider; break; + case second === provider: break; + default: + if (rest === undefined) rest = new _Set(); + rest.add(provider); + break; + } + } + + function getProviderNoCache(O: object, P: string | symbol | undefined) { + if (!IsUndefined(first)) { + if (first.isProviderFor(O, P)) return first; + if (!IsUndefined(second)) { + if (second.isProviderFor(O, P)) return first; + if (!IsUndefined(rest)) { + const iterator = GetIterator(rest); + while (true) { + const next = IteratorStep(iterator); + if (!next) { + return undefined; + } + const provider = IteratorValue(next); + if (provider.isProviderFor(O, P)) { + IteratorClose(iterator); + return provider; + } + } + } + } + } + if (!IsUndefined(fallback) && fallback.isProviderFor(O, P)) { + return fallback; + } + return undefined; + } + + function getProvider(O: object, P: string | symbol | undefined) { + let providerMap = targetProviderMap.get(O); + let provider: MetadataProvider | undefined; + if (!IsUndefined(providerMap)) { + provider = providerMap.get(P); + } + if (!IsUndefined(provider)) { + return provider; + } + + provider = getProviderNoCache(O, P); + if (!IsUndefined(provider)) { + if (IsUndefined(providerMap)) { + providerMap = new _Map(); + targetProviderMap.set(O, providerMap); + } + providerMap.set(P, provider); + } + return provider; + } + + function hasProvider(provider: MetadataProvider) { + if (IsUndefined(provider)) throw new TypeError(); + return first === provider || second === provider || !IsUndefined(rest) && rest.has(provider); + } + + function setProvider(O: object, P: string | symbol | undefined, provider: MetadataProvider) { + if (!hasProvider(provider)) { + throw new Error("Metadata provider not registered."); + } + const existingProvider = getProvider(O, P); + if (existingProvider !== provider) { + if (!IsUndefined(existingProvider)) { + return false; + } + let providerMap = targetProviderMap.get(O); + if (IsUndefined(providerMap)) { + providerMap = new _Map(); + targetProviderMap.set(O, providerMap); + } + providerMap.set(P, provider); + } + return true; + } + } + + /** + * Gets or creates the shared registry of metadata providers. + */ + function GetOrCreateMetadataRegistry(): MetadataRegistry { + let metadataRegistry: MetadataRegistry | undefined; + if (!IsUndefined(registrySymbol) && IsObject(root.Reflect) && Object.isExtensible(root.Reflect)) { + metadataRegistry = (root.Reflect as any)[registrySymbol] as MetadataRegistry | undefined; + } + if (IsUndefined(metadataRegistry)) { + metadataRegistry = CreateMetadataRegistry(); + } + if (!IsUndefined(registrySymbol) && IsObject(root.Reflect) && Object.isExtensible(root.Reflect)) { + Object.defineProperty(root.Reflect, registrySymbol, { + enumerable: false, + configurable: false, + writable: false, + value: metadataRegistry + }); + } + return metadataRegistry; + } + + function CreateMetadataProvider(registry: MetadataRegistry): MetadataProvider { + // [[Metadata]] internal slot + // https://rbuckton.github.io/reflect-metadata/#ordinary-object-internal-methods-and-internal-slots + const metadata = new _WeakMap>>(); + const provider: MetadataProvider = { + isProviderFor(O, P) { + const targetMetadata = metadata.get(O); + if (IsUndefined(targetMetadata)) return false; + return targetMetadata.has(P); + }, + OrdinaryDefineOwnMetadata: OrdinaryDefineOwnMetadata, + OrdinaryHasOwnMetadata: OrdinaryHasOwnMetadata, + OrdinaryGetOwnMetadata: OrdinaryGetOwnMetadata, + OrdinaryOwnMetadataKeys: OrdinaryOwnMetadataKeys, + OrdinaryDeleteMetadata: OrdinaryDeleteMetadata, + }; + metadataRegistry.registerProvider(provider); + return provider; + + // 2.1.1 GetOrCreateMetadataMap(O, P, Create) + // https://rbuckton.github.io/reflect-metadata/#getorcreatemetadatamap + function GetOrCreateMetadataMap(O: object, P: string | symbol | undefined, Create: true): Map; + function GetOrCreateMetadataMap(O: object, P: string | symbol | undefined, Create: false): Map | undefined; + function GetOrCreateMetadataMap(O: object, P: string | symbol | undefined, Create: boolean) { + let targetMetadata = metadata.get(O); + let createdTargetMetadata = false; + if (IsUndefined(targetMetadata)) { + if (!Create) return undefined; + targetMetadata = new _Map>(); + metadata.set(O, targetMetadata); + createdTargetMetadata = true; + } + let metadataMap = targetMetadata.get(P); + if (IsUndefined(metadataMap)) { + if (!Create) return undefined; + metadataMap = new _Map(); + targetMetadata.set(P, metadataMap); + if (!registry.setProvider(O, P, provider)) { + targetMetadata.delete(P); + if (createdTargetMetadata) { + metadata.delete(O); + } + throw new Error("Wrong provider for target."); + } + } + return metadataMap; + } + + // 3.1.2.1 OrdinaryHasOwnMetadata(MetadataKey, O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinaryhasownmetadata + function OrdinaryHasOwnMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): boolean { + const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); + if (IsUndefined(metadataMap)) return false; + return ToBoolean(metadataMap.has(MetadataKey)); + } + + // 3.1.4.1 OrdinaryGetOwnMetadata(MetadataKey, O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinarygetownmetadata + function OrdinaryGetOwnMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): any { + const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); + if (IsUndefined(metadataMap)) return undefined; + return metadataMap.get(MetadataKey); + } + + // 3.1.5.1 OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinarydefineownmetadata + function OrdinaryDefineOwnMetadata(MetadataKey: any, MetadataValue: any, O: object, P: string | symbol | undefined): void { + const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ true); + metadataMap.set(MetadataKey, MetadataValue); + } + + // 3.1.7.1 OrdinaryOwnMetadataKeys(O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinaryownmetadatakeys + function OrdinaryOwnMetadataKeys(O: any, P: string | symbol | undefined): any[] { + const keys: any[] = []; + const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); + if (IsUndefined(metadataMap)) return keys; + const keysObj = metadataMap.keys(); + const iterator = GetIterator(keysObj); + let k = 0; + while (true) { + const next = IteratorStep(iterator); + if (!next) { + keys.length = k; + return keys; + } + const nextValue = IteratorValue(next); + try { + keys[k] = nextValue; + } + catch (e) { + try { + IteratorClose(iterator); + } + finally { + throw e; + } + } + k++; + } + } + + function OrdinaryDeleteMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): boolean { + const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); + if (IsUndefined(metadataMap)) return false; + if (!metadataMap.delete(MetadataKey)) return false; + if (metadataMap.size === 0) { + const targetMetadata = metadata.get(O); + if (!IsUndefined(targetMetadata)) { + targetMetadata.delete(P); + if (targetMetadata.size === 0) { + metadata.delete(targetMetadata); + } + } + } + return true; + } + } + + function CreateFallbackProvider(reflect: typeof Reflect): MetadataProvider { + const metadataOwner = new _WeakMap>(); + const provider: MetadataProvider = { + isProviderFor(O, P) { + let metadataPropertySet = metadataOwner.get(O); + if (!IsUndefined(metadataPropertySet)) { + return metadataPropertySet.has(P); + } + if (reflect.getOwnMetadataKeys(O, P!).length) { + if (IsUndefined(metadataPropertySet)) { + metadataPropertySet = new _Set(); + metadataOwner.set(O, metadataPropertySet); + } + metadataPropertySet.add(P); + return true; + } + return false; + }, + OrdinaryDefineOwnMetadata: reflect.defineMetadata, + OrdinaryHasOwnMetadata: reflect.hasOwnMetadata, + OrdinaryGetOwnMetadata: reflect.getOwnMetadata, + OrdinaryOwnMetadataKeys: reflect.getOwnMetadataKeys, + OrdinaryDeleteMetadata: reflect.deleteMetadata, + }; + return provider; + } + + function GetMetadataProvider(O: object, P: string | symbol | undefined, Create: true): MetadataProvider; + function GetMetadataProvider(O: object, P: string | symbol | undefined, Create: false): MetadataProvider | undefined; + /** + * Gets the metadata provider for an object. If the object has no metadata provider and this is for a create operation, + * then this module's metadata provider is assigned to the object. + */ + function GetMetadataProvider(O: object, P: string | symbol | undefined, Create: boolean): MetadataProvider | undefined { + const registeredProvider = metadataRegistry.getProvider(O, P); + if (!IsUndefined(registeredProvider)) { + return registeredProvider; + } + if (Create) { + if (metadataRegistry.setProvider(O, P, metadataProvider)) { + return metadataProvider; + } + throw new Error("Illegal state."); + } + return undefined; + } + }); +} diff --git a/ReflectNoConflict.ts b/ReflectNoConflict.ts new file mode 100644 index 0000000..59e993c --- /dev/null +++ b/ReflectNoConflict.ts @@ -0,0 +1,1629 @@ +/*! ***************************************************************************** +Copyright (C) Microsoft. All rights reserved. +Licensed under the Apache License, Version 2.0 (the "License"); you may not use +this file except in compliance with the License. You may obtain a copy of the +License at http://www.apache.org/licenses/LICENSE-2.0 + +THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, +MERCHANTABLITY OR NON-INFRINGEMENT. + +See the Apache Version 2.0 License for specific language governing permissions +and limitations under the License. +***************************************************************************** */ + +// Metadata Proposal +// https://rbuckton.github.io/reflect-metadata/ + +type MemberDecorator = (target: Object, propertyKey: string | symbol, descriptor?: PropertyDescriptor) => PropertyDescriptor | void; + +// feature test for Symbol support +const supportsSymbol = typeof Symbol === "function"; +const toPrimitiveSymbol = supportsSymbol && typeof Symbol.toPrimitive !== "undefined" ? Symbol.toPrimitive : fail("Symbol.toPrimitive not found."); +const iteratorSymbol = supportsSymbol && typeof Symbol.iterator !== "undefined" ? Symbol.iterator : fail("Symbol.iterator not found."); + +// Load global or shim versions of Map, Set, and WeakMap +const functionPrototype = Object.getPrototypeOf(Function); +const _Map: typeof Map = typeof Map === "function" && typeof Map.prototype.entries === "function" ? Map : fail("A valid Map constructor could not be found."); +const _Set: typeof Set = typeof Set === "function" && typeof Set.prototype.entries === "function" ? Set : fail("A valid Set constructor could not be found."); +const _WeakMap: typeof WeakMap = typeof WeakMap === "function" ? WeakMap : fail("A valid WeakMap constructor could not be found."); +const registrySymbol = supportsSymbol ? Symbol.for("@reflect-metadata:registry") : undefined; +const metadataRegistry = GetOrCreateMetadataRegistry(); +const metadataProvider = CreateMetadataProvider(metadataRegistry); + +/** + * Applies a set of decorators to a target object. + * @param decorators An array of decorators. + * @param target The target object. + * @returns The result of applying the provided decorators. + * @remarks Decorators are applied in reverse order of their positions in the array. + * @example + * + * class Example { } + * + * // constructor + * Example = Reflect.decorate(decoratorsArray, Example); + * + */ +export function decorate(decorators: ClassDecorator[], target: Function): Function; + +/** + * Applies a set of decorators to a property of a target object. + * @param decorators An array of decorators. + * @param target The target object. + * @param propertyKey The property key to decorate. + * @param attributes A property descriptor. + * @remarks Decorators are applied in reverse order. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod() { } + * method() { } + * } + * + * // property (on constructor) + * Reflect.decorate(decoratorsArray, Example, "staticProperty"); + * + * // property (on prototype) + * Reflect.decorate(decoratorsArray, Example.prototype, "property"); + * + * // method (on constructor) + * Object.defineProperty(Example, "staticMethod", + * Reflect.decorate(decoratorsArray, Example, "staticMethod", + * Object.getOwnPropertyDescriptor(Example, "staticMethod"))); + * + * // method (on prototype) + * Object.defineProperty(Example.prototype, "method", + * Reflect.decorate(decoratorsArray, Example.prototype, "method", + * Object.getOwnPropertyDescriptor(Example.prototype, "method"))); + * + */ +export function decorate(decorators: (PropertyDecorator | MethodDecorator)[], target: any, propertyKey: string | symbol, attributes?: PropertyDescriptor | null): PropertyDescriptor | undefined; + +/** + * Applies a set of decorators to a property of a target object. + * @param decorators An array of decorators. + * @param target The target object. + * @param propertyKey The property key to decorate. + * @param attributes A property descriptor. + * @remarks Decorators are applied in reverse order. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod() { } + * method() { } + * } + * + * // property (on constructor) + * Reflect.decorate(decoratorsArray, Example, "staticProperty"); + * + * // property (on prototype) + * Reflect.decorate(decoratorsArray, Example.prototype, "property"); + * + * // method (on constructor) + * Object.defineProperty(Example, "staticMethod", + * Reflect.decorate(decoratorsArray, Example, "staticMethod", + * Object.getOwnPropertyDescriptor(Example, "staticMethod"))); + * + * // method (on prototype) + * Object.defineProperty(Example.prototype, "method", + * Reflect.decorate(decoratorsArray, Example.prototype, "method", + * Object.getOwnPropertyDescriptor(Example.prototype, "method"))); + * + */ +export function decorate(decorators: (PropertyDecorator | MethodDecorator)[], target: any, propertyKey: string | symbol, attributes: PropertyDescriptor): PropertyDescriptor; + +/** + * Applies a set of decorators to a property of a target object. + * @param decorators An array of decorators. + * @param target The target object. + * @param propertyKey (Optional) The property key to decorate. + * @param attributes (Optional) The property descriptor for the target key. + * @remarks Decorators are applied in reverse order. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * Example = Reflect.decorate(decoratorsArray, Example); + * + * // property (on constructor) + * Reflect.decorate(decoratorsArray, Example, "staticProperty"); + * + * // property (on prototype) + * Reflect.decorate(decoratorsArray, Example.prototype, "property"); + * + * // method (on constructor) + * Object.defineProperty(Example, "staticMethod", + * Reflect.decorate(decoratorsArray, Example, "staticMethod", + * Object.getOwnPropertyDescriptor(Example, "staticMethod"))); + * + * // method (on prototype) + * Object.defineProperty(Example.prototype, "method", + * Reflect.decorate(decoratorsArray, Example.prototype, "method", + * Object.getOwnPropertyDescriptor(Example.prototype, "method"))); + * + */ +export function decorate(decorators: (ClassDecorator | MemberDecorator)[], target: any, propertyKey?: string | symbol, attributes?: PropertyDescriptor | null): PropertyDescriptor | Function | undefined { + if (!IsUndefined(propertyKey)) { + if (!IsArray(decorators)) throw new TypeError(); + if (!IsObject(target)) throw new TypeError(); + if (!IsObject(attributes) && !IsUndefined(attributes) && !IsNull(attributes)) throw new TypeError(); + if (IsNull(attributes)) attributes = undefined; + propertyKey = ToPropertyKey(propertyKey); + return DecorateProperty(decorators, target, propertyKey, attributes); + } + else { + if (!IsArray(decorators)) throw new TypeError(); + if (!IsConstructor(target)) throw new TypeError(); + return DecorateConstructor(decorators, target); + } +} + +// 4.1.2 Reflect.metadata(metadataKey, metadataValue) +// https://rbuckton.github.io/reflect-metadata/#reflect.metadata + +/** + * A default metadata decorator factory that can be used on a class, class member, or parameter. + * @param metadataKey The key for the metadata entry. + * @param metadataValue The value for the metadata entry. + * @returns A decorator function. + * @remarks + * If `metadataKey` is already defined for the target and target key, the + * metadataValue for that key will be overwritten. + * @example + * + * // constructor + * @Reflect.metadata(key, value) + * class Example { + * } + * + * // property (on constructor, TypeScript only) + * class Example { + * @Reflect.metadata(key, value) + * static staticProperty; + * } + * + * // property (on prototype, TypeScript only) + * class Example { + * @Reflect.metadata(key, value) + * property; + * } + * + * // method (on constructor) + * class Example { + * @Reflect.metadata(key, value) + * static staticMethod() { } + * } + * + * // method (on prototype) + * class Example { + * @Reflect.metadata(key, value) + * method() { } + * } + * + */ +export function metadata(metadataKey: any, metadataValue: any): { (target: Function): void; (target: any, propertyKey: string | symbol): void; } { + if (typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata !== metadata) { + return Reflect.metadata(metadataKey, metadataValue); + } + function decorator(target: Function): void; + function decorator(target: any, propertyKey: string | symbol): void; + function decorator(target: any, propertyKey?: string | symbol): void { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey) && !IsPropertyKey(propertyKey)) throw new TypeError(); + OrdinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey); + } + return decorator; +} + +// 4.1.3 Reflect.defineMetadata(metadataKey, metadataValue, target [, propertyKey]) +// https://rbuckton.github.io/reflect-metadata/#reflect.definemetadata + +/** + * Define a unique metadata entry on the target. + * @param metadataKey A key used to store and retrieve metadata. + * @param metadataValue A value that contains attached metadata. + * @param target The target object on which to define metadata. + * @example + * + * class Example { + * } + * + * // constructor + * Reflect.defineMetadata("custom:annotation", options, Example); + * + * // decorator factory as metadata-producing annotation. + * function MyAnnotation(options): ClassDecorator { + * return target => Reflect.defineMetadata("custom:annotation", options, target); + * } + * + */ +export function defineMetadata(metadataKey: any, metadataValue: any, target: any): void; + +/** + * Define a unique metadata entry on the target. + * @param metadataKey A key used to store and retrieve metadata. + * @param metadataValue A value that contains attached metadata. + * @param target The target object on which to define metadata. + * @param propertyKey The property key for the target. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * Reflect.defineMetadata("custom:annotation", Number, Example, "staticProperty"); + * + * // property (on prototype) + * Reflect.defineMetadata("custom:annotation", Number, Example.prototype, "property"); + * + * // method (on constructor) + * Reflect.defineMetadata("custom:annotation", Number, Example, "staticMethod"); + * + * // method (on prototype) + * Reflect.defineMetadata("custom:annotation", Number, Example.prototype, "method"); + * + * // decorator factory as metadata-producing annotation. + * function MyAnnotation(options): PropertyDecorator { + * return (target, key) => Reflect.defineMetadata("custom:annotation", options, target, key); + * } + * + */ +export function defineMetadata(metadataKey: any, metadataValue: any, target: any, propertyKey: string | symbol): void; + +/** + * Define a unique metadata entry on the target. + * @param metadataKey A key used to store and retrieve metadata. + * @param metadataValue A value that contains attached metadata. + * @param target The target object on which to define metadata. + * @param propertyKey (Optional) The property key for the target. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * Reflect.defineMetadata("custom:annotation", options, Example); + * + * // property (on constructor) + * Reflect.defineMetadata("custom:annotation", options, Example, "staticProperty"); + * + * // property (on prototype) + * Reflect.defineMetadata("custom:annotation", options, Example.prototype, "property"); + * + * // method (on constructor) + * Reflect.defineMetadata("custom:annotation", options, Example, "staticMethod"); + * + * // method (on prototype) + * Reflect.defineMetadata("custom:annotation", options, Example.prototype, "method"); + * + * // decorator factory as metadata-producing annotation. + * function MyAnnotation(options): Decorator { + * return (target, key?) => Reflect.defineMetadata("custom:annotation", options, target, key); + * } + * + */ +export function defineMetadata(metadataKey: any, metadataValue: any, target: any, propertyKey?: string | symbol): void { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + return OrdinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey); +} + +// 4.1.4 Reflect.hasMetadata(metadataKey, target [, propertyKey]) +// https://rbuckton.github.io/reflect-metadata/#reflect.hasmetadata + +/** + * Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.hasMetadata("custom:annotation", Example); + * + */ +export function hasMetadata(metadataKey: any, target: any): boolean; + +/** + * Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.hasMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.hasMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "method"); + * + */ +export function hasMetadata(metadataKey: any, target: any, propertyKey: string | symbol): boolean; + +/** + * Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey (Optional) The property key for the target. + * @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * result = Reflect.hasMetadata("custom:annotation", Example); + * + * // property (on constructor) + * result = Reflect.hasMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.hasMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "method"); + * + */ +export function hasMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): boolean { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + return OrdinaryHasMetadata(metadataKey, target, propertyKey); +} + +// 4.1.5 Reflect.hasOwnMetadata(metadataKey, target [, propertyKey]) +// https://rbuckton.github.io/reflect-metadata/#reflect-hasownmetadata + +/** + * Gets a value indicating whether the target object has the provided metadata key defined. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @returns `true` if the metadata key was defined on the target object; otherwise, `false`. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.hasOwnMetadata("custom:annotation", Example); + * + */ +export function hasOwnMetadata(metadataKey: any, target: any): boolean; + +/** + * Gets a value indicating whether the target object has the provided metadata key defined. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns `true` if the metadata key was defined on the target object; otherwise, `false`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "method"); + * + */ +export function hasOwnMetadata(metadataKey: any, target: any, propertyKey: string | symbol): boolean; + +/** + * Gets a value indicating whether the target object has the provided metadata key defined. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey (Optional) The property key for the target. + * @returns `true` if the metadata key was defined on the target object; otherwise, `false`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * result = Reflect.hasOwnMetadata("custom:annotation", Example); + * + * // property (on constructor) + * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "method"); + * + */ +export function hasOwnMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): boolean { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + return OrdinaryHasOwnMetadata(metadataKey, target, propertyKey); +} + +// 4.1.6 Reflect.getMetadata(metadataKey, target [, propertyKey]) +// https://rbuckton.github.io/reflect-metadata/#reflect-getmetadata + +/** + * Gets the metadata value for the provided metadata key on the target object or its prototype chain. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @returns The metadata value for the metadata key if found; otherwise, `undefined`. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.getMetadata("custom:annotation", Example); + * + */ +export function getMetadata(metadataKey: any, target: any): any; + +/** + * Gets the metadata value for the provided metadata key on the target object or its prototype chain. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns The metadata value for the metadata key if found; otherwise, `undefined`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.getMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getMetadata("custom:annotation", Example.prototype, "method"); + * + */ +export function getMetadata(metadataKey: any, target: any, propertyKey: string | symbol): any; + +/** + * Gets the metadata value for the provided metadata key on the target object or its prototype chain. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey (Optional) The property key for the target. + * @returns The metadata value for the metadata key if found; otherwise, `undefined`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * result = Reflect.getMetadata("custom:annotation", Example); + * + * // property (on constructor) + * result = Reflect.getMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getMetadata("custom:annotation", Example.prototype, "method"); + * + */ +export function getMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): any { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + return OrdinaryGetMetadata(metadataKey, target, propertyKey); +} + +// 4.1.7 Reflect.getOwnMetadata(metadataKey, target [, propertyKey]) +// https://rbuckton.github.io/reflect-metadata/#reflect-getownmetadata + +/** + * Gets the metadata value for the provided metadata key on the target object. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @returns The metadata value for the metadata key if found; otherwise, `undefined`. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.getOwnMetadata("custom:annotation", Example); + * + */ +export function getOwnMetadata(metadataKey: any, target: any): any; + +/** + * Gets the metadata value for the provided metadata key on the target object. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns The metadata value for the metadata key if found; otherwise, `undefined`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "method"); + * + */ +export function getOwnMetadata(metadataKey: any, target: any, propertyKey: string | symbol): any; + +/** + * Gets the metadata value for the provided metadata key on the target object. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey (Optional) The property key for the target. + * @returns The metadata value for the metadata key if found; otherwise, `undefined`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * result = Reflect.getOwnMetadata("custom:annotation", Example); + * + * // property (on constructor) + * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "method"); + * + */ +export function getOwnMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): any { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + return OrdinaryGetOwnMetadata(metadataKey, target, propertyKey); +} + +// 4.1.8 Reflect.getMetadataKeys(target [, propertyKey]) +// https://rbuckton.github.io/reflect-metadata/#reflect-getmetadatakeys + +/** + * Gets the metadata keys defined on the target object or its prototype chain. + * @param target The target object on which the metadata is defined. + * @returns An array of unique metadata keys. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.getMetadataKeys(Example); + * + */ +export function getMetadataKeys(target: any): any[]; + +/** + * Gets the metadata keys defined on the target object or its prototype chain. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns An array of unique metadata keys. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.getMetadataKeys(Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getMetadataKeys(Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getMetadataKeys(Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getMetadataKeys(Example.prototype, "method"); + * + */ +export function getMetadataKeys(target: any, propertyKey: string | symbol): any[]; + +/** + * Gets the metadata keys defined on the target object or its prototype chain. + * @param target The target object on which the metadata is defined. + * @param propertyKey (Optional) The property key for the target. + * @returns An array of unique metadata keys. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * result = Reflect.getMetadataKeys(Example); + * + * // property (on constructor) + * result = Reflect.getMetadataKeys(Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getMetadataKeys(Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getMetadataKeys(Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getMetadataKeys(Example.prototype, "method"); + * + */ +export function getMetadataKeys(target: any, propertyKey?: string | symbol): any[] { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + return OrdinaryMetadataKeys(target, propertyKey); +} + +// 4.1.9 Reflect.getOwnMetadataKeys(target [, propertyKey]) +// https://rbuckton.github.io/reflect-metadata/#reflect-getownmetadata + +/** + * Gets the unique metadata keys defined on the target object. + * @param target The target object on which the metadata is defined. + * @returns An array of unique metadata keys. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.getOwnMetadataKeys(Example); + * + */ +export function getOwnMetadataKeys(target: any): any[]; + +/** + * Gets the unique metadata keys defined on the target object. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns An array of unique metadata keys. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.getOwnMetadataKeys(Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getOwnMetadataKeys(Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getOwnMetadataKeys(Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getOwnMetadataKeys(Example.prototype, "method"); + * + */ +export function getOwnMetadataKeys(target: any, propertyKey: string | symbol): any[]; + +/** + * Gets the unique metadata keys defined on the target object. + * @param target The target object on which the metadata is defined. + * @param propertyKey (Optional) The property key for the target. + * @returns An array of unique metadata keys. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * result = Reflect.getOwnMetadataKeys(Example); + * + * // property (on constructor) + * result = Reflect.getOwnMetadataKeys(Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getOwnMetadataKeys(Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getOwnMetadataKeys(Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getOwnMetadataKeys(Example.prototype, "method"); + * + */ +export function getOwnMetadataKeys(target: any, propertyKey?: string | symbol): any[] { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + return OrdinaryOwnMetadataKeys(target, propertyKey); +} + +// 4.1.10 Reflect.deleteMetadata(metadataKey, target [, propertyKey]) +// https://rbuckton.github.io/reflect-metadata/#reflect-deletemetadata + +/** + * Deletes the metadata entry from the target object with the provided key. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @returns `true` if the metadata entry was found and deleted; otherwise, false. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.deleteMetadata("custom:annotation", Example); + * + */ +export function deleteMetadata(metadataKey: any, target: any): boolean; + +/** + * Deletes the metadata entry from the target object with the provided key. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns `true` if the metadata entry was found and deleted; otherwise, false. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.deleteMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.deleteMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "method"); + * + */ +export function deleteMetadata(metadataKey: any, target: any, propertyKey: string | symbol): boolean; + +/** + * Deletes the metadata entry from the target object with the provided key. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey (Optional) The property key for the target. + * @returns `true` if the metadata entry was found and deleted; otherwise, false. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * constructor(p) { } + * static staticMethod(p) { } + * method(p) { } + * } + * + * // constructor + * result = Reflect.deleteMetadata("custom:annotation", Example); + * + * // property (on constructor) + * result = Reflect.deleteMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.deleteMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "method"); + * + */ +export function deleteMetadata(metadataKey: any, target: any, propertyKey?: string | symbol): boolean { + if (!IsObject(target)) throw new TypeError(); + if (!IsUndefined(propertyKey)) propertyKey = ToPropertyKey(propertyKey); + const provider = GetMetadataProvider(target, propertyKey, /*Create*/ false); + if (IsUndefined(provider)) return false; + return provider.OrdinaryDeleteMetadata(metadataKey, target, propertyKey); +} + +function DecorateConstructor(decorators: ClassDecorator[], target: Function): Function { + for (let i = decorators.length - 1; i >= 0; --i) { + const decorator = decorators[i]; + const decorated = decorator(target); + if (!IsUndefined(decorated) && !IsNull(decorated)) { + if (!IsConstructor(decorated)) throw new TypeError(); + target = decorated; + } + } + return target; +} + +function DecorateProperty(decorators: MemberDecorator[], target: any, propertyKey: string | symbol, descriptor: PropertyDescriptor | undefined): PropertyDescriptor | undefined { + for (let i = decorators.length - 1; i >= 0; --i) { + const decorator = decorators[i]; + const decorated = decorator(target, propertyKey, descriptor); + if (!IsUndefined(decorated) && !IsNull(decorated)) { + if (!IsObject(decorated)) throw new TypeError(); + descriptor = decorated; + } + } + return descriptor; +} + +// 3.1.1.1 OrdinaryHasMetadata(MetadataKey, O, P) +// https://rbuckton.github.io/reflect-metadata/#ordinaryhasmetadata +function OrdinaryHasMetadata(MetadataKey: any, O: any, P: string | symbol | undefined): boolean { + const hasOwn = OrdinaryHasOwnMetadata(MetadataKey, O, P); + if (hasOwn) return true; + const parent = OrdinaryGetPrototypeOf(O); + if (!IsNull(parent)) return OrdinaryHasMetadata(MetadataKey, parent, P); + return false; +} + +// 3.1.2.1 OrdinaryHasOwnMetadata(MetadataKey, O, P) +// https://rbuckton.github.io/reflect-metadata/#ordinaryhasownmetadata +function OrdinaryHasOwnMetadata(MetadataKey: any, O: any, P: string | symbol | undefined): boolean { + const provider = GetMetadataProvider(O, P, /*Create*/ false); + if (IsUndefined(provider)) return false; + return ToBoolean(provider.OrdinaryHasOwnMetadata(MetadataKey, O, P)); +} + +// 3.1.3.1 OrdinaryGetMetadata(MetadataKey, O, P) +// https://rbuckton.github.io/reflect-metadata/#ordinarygetmetadata +function OrdinaryGetMetadata(MetadataKey: any, O: any, P: string | symbol | undefined): any { + const hasOwn = OrdinaryHasOwnMetadata(MetadataKey, O, P); + if (hasOwn) return OrdinaryGetOwnMetadata(MetadataKey, O, P); + const parent = OrdinaryGetPrototypeOf(O); + if (!IsNull(parent)) return OrdinaryGetMetadata(MetadataKey, parent, P); + return undefined; +} + +// 3.1.4.1 OrdinaryGetOwnMetadata(MetadataKey, O, P) +// https://rbuckton.github.io/reflect-metadata/#ordinarygetownmetadata +function OrdinaryGetOwnMetadata(MetadataKey: any, O: any, P: string | symbol | undefined): any { + const provider = GetMetadataProvider(O, P, /*Create*/ false); + if (IsUndefined(provider)) return; + return provider.OrdinaryGetOwnMetadata(MetadataKey, O, P); +} + +// 3.1.5.1 OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P) +// https://rbuckton.github.io/reflect-metadata/#ordinarydefineownmetadata +function OrdinaryDefineOwnMetadata(MetadataKey: any, MetadataValue: any, O: any, P: string | symbol | undefined): void { + const provider = GetMetadataProvider(O, P, /*Create*/ true); + provider.OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P); +} + +// 3.1.6.1 OrdinaryMetadataKeys(O, P) +// https://rbuckton.github.io/reflect-metadata/#ordinarymetadatakeys +function OrdinaryMetadataKeys(O: any, P: string | symbol | undefined): any[] { + const ownKeys = OrdinaryOwnMetadataKeys(O, P); + const parent = OrdinaryGetPrototypeOf(O); + if (parent === null) return ownKeys; + const parentKeys = OrdinaryMetadataKeys(parent, P); + if (parentKeys.length <= 0) return ownKeys; + if (ownKeys.length <= 0) return parentKeys; + const set = new _Set(); + const keys: any[] = []; + for (const key of ownKeys) { + const hasKey = set.has(key); + if (!hasKey) { + set.add(key); + keys.push(key); + } + } + for (const key of parentKeys) { + const hasKey = set.has(key); + if (!hasKey) { + set.add(key); + keys.push(key); + } + } + return keys; +} + +// 3.1.7.1 OrdinaryOwnMetadataKeys(O, P) +// https://rbuckton.github.io/reflect-metadata/#ordinaryownmetadatakeys +function OrdinaryOwnMetadataKeys(O: any, P: string | symbol | undefined): any[] { + const provider = GetMetadataProvider(O, P, /*create*/ false); + if (!provider) { + return []; + } + return provider.OrdinaryOwnMetadataKeys(O, P); +} + +// 6 ECMAScript Data Typ0es and Values +// https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values +function Type(x: any): Tag { + if (x === null) return Tag.Null; + switch (typeof x) { + case "undefined": return Tag.Undefined; + case "boolean": return Tag.Boolean; + case "string": return Tag.String; + case "symbol": return Tag.Symbol; + case "number": return Tag.Number; + case "object": return x === null ? Tag.Null : Tag.Object; + default: return Tag.Object; + } +} + +// 6.1 ECMAScript Language Types +// https://tc39.github.io/ecma262/#sec-ecmascript-language-types +const enum Tag { + Undefined, + Null, + Boolean, + String, + Symbol, + Number, + Object +} + +// 6.1.1 The Undefined Type +// https://tc39.github.io/ecma262/#sec-ecmascript-language-types-undefined-type +function IsUndefined(x: any): x is undefined { + return x === undefined; +} + +// 6.1.2 The Null Type +// https://tc39.github.io/ecma262/#sec-ecmascript-language-types-null-type +function IsNull(x: any): x is null { + return x === null; +} + +// 6.1.5 The Symbol Type +// https://tc39.github.io/ecma262/#sec-ecmascript-language-types-symbol-type +function IsSymbol(x: any): x is symbol { + return typeof x === "symbol"; +} + +// 6.1.7 The Object Type +// https://tc39.github.io/ecma262/#sec-object-type +function IsObject(x: T | undefined | null | boolean | string | symbol | number): x is T { + return typeof x === "object" ? x !== null : typeof x === "function"; +} + +// 7.1 Type Conversion +// https://tc39.github.io/ecma262/#sec-type-conversion + +// 7.1.1 ToPrimitive(input [, PreferredType]) +// https://tc39.github.io/ecma262/#sec-toprimitive +function ToPrimitive(input: any, PreferredType?: Tag): undefined | null | boolean | string | symbol | number { + switch (Type(input)) { + case Tag.Undefined: return input; + case Tag.Null: return input; + case Tag.Boolean: return input; + case Tag.String: return input; + case Tag.Symbol: return input; + case Tag.Number: return input; + } + const hint: "string" | "number" | "default" = PreferredType === Tag.String ? "string" : PreferredType === Tag.Number ? "number" : "default"; + const exoticToPrim = GetMethod(input, toPrimitiveSymbol); + if (exoticToPrim !== undefined) { + const result = exoticToPrim.call(input, hint); + if (IsObject(result)) throw new TypeError(); + return result; + } + return OrdinaryToPrimitive(input, hint === "default" ? "number" : hint); +} + +// 7.1.1.1 OrdinaryToPrimitive(O, hint) +// https://tc39.github.io/ecma262/#sec-ordinarytoprimitive +function OrdinaryToPrimitive(O: any, hint: "string" | "number"): undefined | null | boolean | string | symbol | number { + if (hint === "string") { + const toString = O.toString; + if (IsCallable(toString)) { + const result = toString.call(O); + if (!IsObject(result)) return result; + } + const valueOf = O.valueOf; + if (IsCallable(valueOf)) { + const result = valueOf.call(O); + if (!IsObject(result)) return result; + } + } + else { + const valueOf = O.valueOf; + if (IsCallable(valueOf)) { + const result = valueOf.call(O); + if (!IsObject(result)) return result; + } + const toString = O.toString; + if (IsCallable(toString)) { + const result = toString.call(O); + if (!IsObject(result)) return result; + } + } + throw new TypeError(); +} + +// 7.1.2 ToBoolean(argument) +// https://tc39.github.io/ecma262/2016/#sec-toboolean +function ToBoolean(argument: any): boolean { + return !!argument; +} + +// 7.1.12 ToString(argument) +// https://tc39.github.io/ecma262/#sec-tostring +function ToString(argument: any): string { + return "" + argument; +} + +// 7.1.14 ToPropertyKey(argument) +// https://tc39.github.io/ecma262/#sec-topropertykey +function ToPropertyKey(argument: any): string | symbol { + const key = ToPrimitive(argument, Tag.String); + if (IsSymbol(key)) return key; + return ToString(key); +} + +// 7.2 Testing and Comparison Operations +// https://tc39.github.io/ecma262/#sec-testing-and-comparison-operations + +// 7.2.2 IsArray(argument) +// https://tc39.github.io/ecma262/#sec-isarray +function IsArray(argument: any): argument is any[] { + return Array.isArray + ? Array.isArray(argument) + : argument instanceof Object + ? argument instanceof Array + : Object.prototype.toString.call(argument) === "[object Array]"; +} + +// 7.2.3 IsCallable(argument) +// https://tc39.github.io/ecma262/#sec-iscallable +function IsCallable(argument: any): argument is Function { + // NOTE: This is an approximation as we cannot check for [[Call]] internal method. + return typeof argument === "function"; +} + +// 7.2.4 IsConstructor(argument) +// https://tc39.github.io/ecma262/#sec-isconstructor +function IsConstructor(argument: any): argument is Function { + // NOTE: This is an approximation as we cannot check for [[Construct]] internal method. + return typeof argument === "function"; +} + +// 7.2.7 IsPropertyKey(argument) +// https://tc39.github.io/ecma262/#sec-ispropertykey +function IsPropertyKey(argument: any): argument is string | symbol { + switch (Type(argument)) { + case Tag.String: return true; + case Tag.Symbol: return true; + default: return false; + } +} + +// 7.3 Operations on Objects +// https://tc39.github.io/ecma262/#sec-operations-on-objects + +// 7.3.9 GetMethod(V, P) +// https://tc39.github.io/ecma262/#sec-getmethod +function GetMethod(V: any, P: any): Function | undefined { + const func = V[P]; + if (func === undefined || func === null) return undefined; + if (!IsCallable(func)) throw new TypeError(); + return func; +} + +// 7.4 Operations on Iterator Objects +// https://tc39.github.io/ecma262/#sec-operations-on-iterator-objects + +function GetIterator(obj: Iterable): Iterator { + const method = GetMethod(obj, iteratorSymbol); + if (!IsCallable(method)) throw new TypeError(); // from Call + const iterator = method.call(obj); + if (!IsObject(iterator)) throw new TypeError(); + return iterator; +} + +// 7.4.4 IteratorValue(iterResult) +// https://tc39.github.io/ecma262/2016/#sec-iteratorvalue +function IteratorValue(iterResult: __IteratorResult): T { + return iterResult.value; +} + +// 7.4.5 IteratorStep(iterator) +// https://tc39.github.io/ecma262/#sec-iteratorstep +function IteratorStep(iterator: Iterator): __IteratorResult | false { + const result = iterator.next(); + return result.done ? false : result; +} + +// 7.4.6 IteratorClose(iterator, completion) +// https://tc39.github.io/ecma262/#sec-iteratorclose +function IteratorClose(iterator: Iterator) { + const f = iterator["return"]; + if (f) f.call(iterator); +} + +// 9.1 Ordinary Object Internal Methods and Internal Slots +// https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots + +// 9.1.1.1 OrdinaryGetPrototypeOf(O) +// https://tc39.github.io/ecma262/#sec-ordinarygetprototypeof +function OrdinaryGetPrototypeOf(O: any): any { + const proto = Object.getPrototypeOf(O); + if (typeof O !== "function" || O === functionPrototype) return proto; + + // TypeScript doesn't set __proto__ in ES5, as it's non-standard. + // Try to determine the superclass constructor. Compatible implementations + // must either set __proto__ on a subclass constructor to the superclass constructor, + // or ensure each class has a valid `constructor` property on its prototype that + // points back to the constructor. + + // If this is not the same as Function.[[Prototype]], then this is definately inherited. + // This is the case when in ES6 or when using __proto__ in a compatible browser. + if (proto !== functionPrototype) return proto; + + // If the super prototype is Object.prototype, null, or undefined, then we cannot determine the heritage. + 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. + 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; + + // we have a pretty good guess at the heritage. + return constructor; +} + +function fail(e: any): never { + throw e; +} + +// Global metadata registry +// - Allows `import "reflect-metadata"` and `import "reflect-metadata/no-conflict"` to interoperate. +// - Uses isolated metadata if `Reflect` is frozen before the registry can be installed. + +/** + * Creates a registry used to allow multiple `reflect-metadata` providers. + */ +function CreateMetadataRegistry(): MetadataRegistry { + let fallback: MetadataProvider | undefined; + if (!IsUndefined(registrySymbol) && + typeof Reflect !== "undefined" && + !(registrySymbol in Reflect) && + typeof Reflect.defineMetadata === "function") { + // interoperate with older version of `reflect-metadata` that did not support a registry. + fallback = CreateFallbackProvider(Reflect); + } + + let first: MetadataProvider | undefined; + let second: MetadataProvider | undefined; + let rest: Set | undefined; + const targetProviderMap = new _WeakMap>(); + const registry: MetadataRegistry = { + registerProvider, + getProvider, + setProvider, + }; + return registry; + + function registerProvider(provider: MetadataProvider) { + if (!Object.isExtensible(registry)) { + throw new Error("Cannot add provider to a frozen registry."); + } + switch (true) { + case fallback === provider: break; + case IsUndefined(first): first = provider; break; + case first === provider: break; + case IsUndefined(second): second = provider; break; + case second === provider: break; + default: + if (rest === undefined) rest = new _Set(); + rest.add(provider); + break; + } + } + + function getProviderNoCache(O: object, P: string | symbol | undefined) { + if (!IsUndefined(first)) { + if (first.isProviderFor(O, P)) return first; + if (!IsUndefined(second)) { + if (second.isProviderFor(O, P)) return first; + if (!IsUndefined(rest)) { + const iterator = GetIterator(rest); + while (true) { + const next = IteratorStep(iterator); + if (!next) { + return undefined; + } + const provider = IteratorValue(next); + if (provider.isProviderFor(O, P)) { + IteratorClose(iterator); + return provider; + } + } + } + } + } + if (!IsUndefined(fallback) && fallback.isProviderFor(O, P)) { + return fallback; + } + return undefined; + } + + function getProvider(O: object, P: string | symbol | undefined) { + let providerMap = targetProviderMap.get(O); + let provider: MetadataProvider | undefined; + if (!IsUndefined(providerMap)) { + provider = providerMap.get(P); + } + if (!IsUndefined(provider)) { + return provider; + } + + provider = getProviderNoCache(O, P); + if (!IsUndefined(provider)) { + if (IsUndefined(providerMap)) { + providerMap = new _Map(); + targetProviderMap.set(O, providerMap); + } + providerMap.set(P, provider); + } + return provider; + } + + function hasProvider(provider: MetadataProvider) { + if (IsUndefined(provider)) throw new TypeError(); + return fallback === provider || first === provider || second === provider || !IsUndefined(rest) && rest.has(provider); + } + + function setProvider(O: object, P: string | symbol | undefined, provider: MetadataProvider) { + if (!hasProvider(provider)) { + throw new Error("Metadata provider not registered."); + } + const existingProvider = getProvider(O, P); + if (existingProvider !== provider) { + if (!IsUndefined(existingProvider)) { + return false; + } + let providerMap = targetProviderMap.get(O); + if (IsUndefined(providerMap)) { + providerMap = new _Map(); + targetProviderMap.set(O, providerMap); + } + providerMap.set(P, provider); + } + return true; + } +} + +/** + * Gets or creates the shared registry of metadata providers. + */ +function GetOrCreateMetadataRegistry(): MetadataRegistry { + let metadataRegistry: MetadataRegistry | undefined; + if (!IsUndefined(registrySymbol) && IsObject(Reflect) && Object.isExtensible(Reflect)) { + metadataRegistry = (Reflect as any)[registrySymbol] as MetadataRegistry | undefined; + } + if (IsUndefined(metadataRegistry)) { + metadataRegistry = CreateMetadataRegistry(); + } + if (!IsUndefined(registrySymbol) && IsObject(Reflect) && Object.isExtensible(Reflect)) { + Object.defineProperty(Reflect, registrySymbol, { + enumerable: false, + configurable: false, + writable: false, + value: metadataRegistry + }); + } + return metadataRegistry; +} + +function CreateMetadataProvider(registry: MetadataRegistry): MetadataProvider { + // [[Metadata]] internal slot + // https://rbuckton.github.io/reflect-metadata/#ordinary-object-internal-methods-and-internal-slots + const metadata = new _WeakMap>>(); + const provider: MetadataProvider = { + isProviderFor(O, P) { + const targetMetadata = metadata.get(O); + if (IsUndefined(targetMetadata)) return false; + return targetMetadata.has(P); + }, + OrdinaryDefineOwnMetadata: OrdinaryDefineOwnMetadata, + OrdinaryHasOwnMetadata: OrdinaryHasOwnMetadata, + OrdinaryGetOwnMetadata: OrdinaryGetOwnMetadata, + OrdinaryOwnMetadataKeys: OrdinaryOwnMetadataKeys, + OrdinaryDeleteMetadata: OrdinaryDeleteMetadata, + }; + metadataRegistry.registerProvider(provider); + return provider; + + // 2.1.1 GetOrCreateMetadataMap(O, P, Create) + // https://rbuckton.github.io/reflect-metadata/#getorcreatemetadatamap + function GetOrCreateMetadataMap(O: object, P: string | symbol | undefined, Create: true): Map; + function GetOrCreateMetadataMap(O: object, P: string | symbol | undefined, Create: false): Map | undefined; + function GetOrCreateMetadataMap(O: object, P: string | symbol | undefined, Create: boolean) { + let targetMetadata = metadata.get(O); + let createdTargetMetadata = false; + if (IsUndefined(targetMetadata)) { + if (!Create) return undefined; + targetMetadata = new _Map>(); + metadata.set(O, targetMetadata); + createdTargetMetadata = true; + } + let metadataMap = targetMetadata.get(P); + if (IsUndefined(metadataMap)) { + if (!Create) return undefined; + metadataMap = new _Map(); + targetMetadata.set(P, metadataMap); + if (!registry.setProvider(O, P, provider)) { + targetMetadata.delete(P); + if (createdTargetMetadata) { + metadata.delete(O); + } + throw new Error("Wrong provider for target."); + } + } + return metadataMap; + } + + // 3.1.2.1 OrdinaryHasOwnMetadata(MetadataKey, O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinaryhasownmetadata + function OrdinaryHasOwnMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): boolean { + const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); + if (IsUndefined(metadataMap)) return false; + return ToBoolean(metadataMap.has(MetadataKey)); + } + + // 3.1.4.1 OrdinaryGetOwnMetadata(MetadataKey, O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinarygetownmetadata + function OrdinaryGetOwnMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): any { + const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); + if (IsUndefined(metadataMap)) return undefined; + return metadataMap.get(MetadataKey); + } + + // 3.1.5.1 OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinarydefineownmetadata + function OrdinaryDefineOwnMetadata(MetadataKey: any, MetadataValue: any, O: object, P: string | symbol | undefined): void { + const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ true); + metadataMap.set(MetadataKey, MetadataValue); + } + + // 3.1.7.1 OrdinaryOwnMetadataKeys(O, P) + // https://rbuckton.github.io/reflect-metadata/#ordinaryownmetadatakeys + function OrdinaryOwnMetadataKeys(O: any, P: string | symbol | undefined): any[] { + const keys: any[] = []; + const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); + if (IsUndefined(metadataMap)) return keys; + const keysObj = metadataMap.keys(); + const iterator = GetIterator(keysObj); + let k = 0; + while (true) { + const next = IteratorStep(iterator); + if (!next) { + keys.length = k; + return keys; + } + const nextValue = IteratorValue(next); + try { + keys[k] = nextValue; + } + catch (e) { + try { + IteratorClose(iterator); + } + finally { + throw e; + } + } + k++; + } + } + + function OrdinaryDeleteMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): boolean { + const metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false); + if (IsUndefined(metadataMap)) return false; + if (!metadataMap.delete(MetadataKey)) return false; + if (metadataMap.size === 0) { + const targetMetadata = metadata.get(O); + if (!IsUndefined(targetMetadata)) { + targetMetadata.delete(P); + if (targetMetadata.size === 0) { + metadata.delete(targetMetadata); + } + } + } + return true; + } +} + +function CreateFallbackProvider(reflect: typeof Reflect): MetadataProvider { + const metadataOwner = new _WeakMap>(); + const provider: MetadataProvider = { + isProviderFor(O, P) { + let metadataPropertySet = metadataOwner.get(O); + if (!IsUndefined(metadataPropertySet)) { + return metadataPropertySet.has(P); + } + if (reflect.getOwnMetadataKeys(O, P!).length) { + if (IsUndefined(metadataPropertySet)) { + metadataPropertySet = new _Set(); + metadataOwner.set(O, metadataPropertySet); + } + metadataPropertySet.add(P); + return true; + } + return false; + }, + OrdinaryDefineOwnMetadata: reflect.defineMetadata, + OrdinaryHasOwnMetadata: reflect.hasOwnMetadata, + OrdinaryGetOwnMetadata: reflect.getOwnMetadata, + OrdinaryOwnMetadataKeys: reflect.getOwnMetadataKeys, + OrdinaryDeleteMetadata: reflect.deleteMetadata, + }; + return provider; +} + +function GetMetadataProvider(O: object, P: string | symbol | undefined, Create: true): MetadataProvider; +function GetMetadataProvider(O: object, P: string | symbol | undefined, Create: false): MetadataProvider | undefined; +/** + * Gets the metadata provider for an object. If the object has no metadata provider and this is for a create operation, + * then this module's metadata provider is assigned to the object. + */ +function GetMetadataProvider(O: object, P: string | symbol | undefined, Create: boolean): MetadataProvider | undefined { + const registeredProvider = metadataRegistry.getProvider(O, P); + if (!IsUndefined(registeredProvider)) { + return registeredProvider; + } + if (Create) { + if (metadataRegistry.setProvider(O, P, metadataProvider)) { + return metadataProvider; + } + throw new Error("Illegal state."); + } + return undefined; +} diff --git a/bower.json b/bower.json deleted file mode 100644 index 04a6b1b..0000000 --- a/bower.json +++ /dev/null @@ -1,36 +0,0 @@ -{ - "name": "reflect-metadata", - "version": "0.1.12", - "description": "Polyfill for Metadata Reflection API", - "homepage": "https://github.com/rbuckton/reflect-metadata", - "authors": [ - "Ron Buckton " - ], - "main": "Reflect.js", - "types": "index.d.ts", - "moduleType": [ - "globals", - "node" - ], - "keywords": [ - "decorator", - "metadata", - "javascript", - "reflect" - ], - "license": "Apache-2.0", - "ignore": [ - "**/.*", - "node_modules", - "bower_components", - "test", - "tests" - ], - "devDependencies": { - "typescript": "^2.1.4" - }, - "repository": { - "type": "git", - "url": "https://github.com/rbuckton/reflect-metadata.git" - } -} diff --git a/globals.d.ts b/globals.d.ts new file mode 100644 index 0000000..d0b17ce --- /dev/null +++ b/globals.d.ts @@ -0,0 +1,109 @@ +/*! ***************************************************************************** +Copyright (C) Microsoft. All rights reserved. +Licensed under the Apache License, Version 2.0 (the "License"); you may not use +this file except in compliance with the License. You may obtain a copy of the +License at http://www.apache.org/licenses/LICENSE-2.0 + +THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, +MERCHANTABLITY OR NON-INFRINGEMENT. + +See the Apache Version 2.0 License for specific language governing permissions +and limitations under the License. +***************************************************************************** */ + +interface SymbolConstructor { + (description?: string): symbol; + for(key: string): symbol; + readonly iterator: symbol; + readonly toPrimitive: symbol; +} + +declare var Symbol: SymbolConstructor; + +interface Iterator { + next(value?: any): __IteratorResult; + throw?(value: any): __IteratorResult; + return?(value?: T): __IteratorResult; +} + +type __IteratorResult = { value: T, done: false } | { value: never, done: true }; + +interface Iterable { + [Symbol.iterator](): Iterator; +} + +interface IterableIterator extends Iterator { + [Symbol.iterator](): IterableIterator; +} + +interface Map extends Iterable<[K, V]> { + readonly size: number; + has(key: K): boolean; + get(key: K): V; + set(key: K, value?: V): this; + delete(key: K): boolean; + clear(): void; + keys(): IterableIterator; + values(): IterableIterator; + entries(): IterableIterator<[K, V]>; +} + +interface MapConstructor { + new (): Map; + new (): Map; + readonly prototype: Map; +} + +interface Set extends Iterable { + readonly size: number; + has(value: T): boolean; + add(value: T): this; + delete(value: T): boolean; + clear(): void; + keys(): IterableIterator; + values(): IterableIterator; + entries(): IterableIterator<[T, T]>; +} + +interface SetConstructor { + new (): Set; + new (): Set; + readonly prototype: Set; +} + +interface WeakMap { + clear(): void; + delete(key: K): boolean; + get(key: K): V; + has(key: K): boolean; + set(key: K, value?: V): WeakMap; +} + +interface WeakMapConstructor { + new (): WeakMap; + new (): WeakMap; + readonly prototype: WeakMap; +} + +declare var Map: MapConstructor; +declare var Set: SetConstructor; +declare var WeakMap: WeakMapConstructor; + +// NOTE: These are not actually global, just shared between the Reflect*.ts variants + +interface MetadataRegistry { + registerProvider(provider: MetadataProvider): void; + getProvider(O: object, P: string | symbol | undefined): MetadataProvider | undefined; + setProvider(O: object, P: string | symbol | undefined, provider: MetadataProvider): boolean; +} + +interface MetadataProvider { + isProviderFor(O: object, P: string | symbol | undefined): boolean; + OrdinaryDefineOwnMetadata(MetadataKey: any, MetadataValue: any, O: object, P: string | symbol | undefined): void; + OrdinaryDeleteMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): boolean; + OrdinaryHasOwnMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): boolean; + OrdinaryGetOwnMetadata(MetadataKey: any, O: object, P: string | symbol | undefined): any; + OrdinaryOwnMetadataKeys(O: object, P: string | symbol | undefined): any[]; +} diff --git a/gulpfile.js b/gulpfile.js index 76f754f..6f9fe58 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -14,14 +14,23 @@ const tests = tsb.create("test/tsconfig.json"); let project = debugProject; gulp.task("release", () => { project = releaseProject; }); -gulp.task("clean", () => del(["Reflect.js", "Reflect.js.map", "test/**/*.js", "test/**/*.js.map"])); +gulp.task("clean", () => del([ + "Reflect.js", + "Reflect.js.map", + "ReflectLite.js", + "ReflectLite.js.map", + "ReflectNoConflict.js", + "ReflectNoConflict.js.map", + "test/**/*.js", + "test/**/*.js.map" +])); gulp.task("build:reflect", () => gulp - .src(["Reflect.ts"]) + .src(["globals.d.ts", "Reflect.ts", "ReflectLite.ts", "ReflectNoConflict.ts"]) .pipe(project()) .pipe(gulp.dest("."))); -gulp.task("build:tests", ["build:reflect"], () => gulp +gulp.task("build:tests", () => gulp .src(["test/**/*.ts"]) .pipe(tests()) .pipe(gulp.dest("test"))); @@ -36,27 +45,26 @@ gulp.task("build:spec", () => gulp })) .pipe(gulp.dest("docs"))); -gulp.task("build", ["build:tests", "build:spec"]); +gulp.task("build", ["build:reflect", "build:tests", "build:spec"]); -gulp.task("use-polyfill", () => { - process.env["REFLECT_METADATA_USE_MAP_POLYFILL"] = "true"; -}); - -gulp.task("test", ["build:tests"], () => { - console.log("Running tests w/o polyfill..."); - return gulp - .src(["test/**/*.js"], { read: false }) - .pipe(mocha({ reporter: "dot" })); -}); - -gulp.task("test:use-polyfill", ["build:tests", "use-polyfill"], () => { - console.log("Running tests w/ polyfill..."); +gulp.task("test", ["build:reflect", "build:tests"], () => { + console.log("Running tests..."); return gulp .src(["test/**/*.js"], { read: false }) .pipe(mocha({ reporter: "dot" })); }); -gulp.task("watch:reflect", () => gulp.watch(["Reflect.ts", "tsconfig.json", "test/**/*.ts", "test/**/tsconfig.json"], ["test"])); +gulp.task("watch:reflect", () => gulp.watch([ + "index.d.ts", + "no-conflict.d.ts", + "globals.d.ts", + "Reflect.ts", + "ReflectLite.ts", + "ReflectNoConflict.ts", + "tsconfig.json", + "test/**/*.ts", + "test/**/tsconfig.json" +], ["test"])); gulp.task("watch:spec", () => gulp.watch(["spec.html"], ["build:spec"])); gulp.task("watch", ["watch:reflect", "watch:spec"], () => { const server = gls.static("docs", 8080); @@ -65,7 +73,7 @@ gulp.task("watch", ["watch:reflect", "watch:spec"], () => { return promise; }); -gulp.task("prepublish", sequence("release", "clean", "test", "test:use-polyfill")); +gulp.task("prepublish", sequence("release", "clean", "test")); gulp.task("reflect", ["build:reflect"]); gulp.task("tests", ["build:tests"]); gulp.task("spec", ["build:spec"]); diff --git a/no-conflict.d.ts b/no-conflict.d.ts new file mode 100644 index 0000000..475780f --- /dev/null +++ b/no-conflict.d.ts @@ -0,0 +1,484 @@ +/*! ***************************************************************************** +Copyright (C) Microsoft. All rights reserved. +Licensed under the Apache License, Version 2.0 (the "License"); you may not use +this file except in compliance with the License. You may obtain a copy of the +License at http://www.apache.org/licenses/LICENSE-2.0 + +THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED +WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, +MERCHANTABLITY OR NON-INFRINGEMENT. + +See the Apache Version 2.0 License for specific language governing permissions +and limitations under the License. +***************************************************************************** */ + +/** + * Applies a set of decorators to a target object. + * @param decorators An array of decorators. + * @param target The target object. + * @returns The result of applying the provided decorators. + * @remarks Decorators are applied in reverse order of their positions in the array. + * @example + * + * class Example { } + * + * // constructor + * Example = Reflect.decorate(decoratorsArray, Example); + * + */ +export declare function decorate(decorators: ClassDecorator[], target: Function): Function; +/** + * Applies a set of decorators to a property of a target object. + * @param decorators An array of decorators. + * @param target The target object. + * @param propertyKey The property key to decorate. + * @param attributes A property descriptor. + * @remarks Decorators are applied in reverse order. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod() { } + * method() { } + * } + * + * // property (on constructor) + * Reflect.decorate(decoratorsArray, Example, "staticProperty"); + * + * // property (on prototype) + * Reflect.decorate(decoratorsArray, Example.prototype, "property"); + * + * // method (on constructor) + * Object.defineProperty(Example, "staticMethod", + * Reflect.decorate(decoratorsArray, Example, "staticMethod", + * Object.getOwnPropertyDescriptor(Example, "staticMethod"))); + * + * // method (on prototype) + * Object.defineProperty(Example.prototype, "method", + * Reflect.decorate(decoratorsArray, Example.prototype, "method", + * Object.getOwnPropertyDescriptor(Example.prototype, "method"))); + * + */ +export declare function decorate(decorators: (PropertyDecorator | MethodDecorator)[], target: Object, propertyKey: string | symbol, attributes?: PropertyDescriptor): PropertyDescriptor; +/** + * A default metadata decorator factory that can be used on a class, class member, or parameter. + * @param metadataKey The key for the metadata entry. + * @param metadataValue The value for the metadata entry. + * @returns A decorator function. + * @remarks + * If `metadataKey` is already defined for the target and target key, the + * metadataValue for that key will be overwritten. + * @example + * + * // constructor + * @Reflect.metadata(key, value) + * class Example { + * } + * + * // property (on constructor, TypeScript only) + * class Example { + * @Reflect.metadata(key, value) + * static staticProperty; + * } + * + * // property (on prototype, TypeScript only) + * class Example { + * @Reflect.metadata(key, value) + * property; + * } + * + * // method (on constructor) + * class Example { + * @Reflect.metadata(key, value) + * static staticMethod() { } + * } + * + * // method (on prototype) + * class Example { + * @Reflect.metadata(key, value) + * method() { } + * } + * + */ +export declare function metadata(metadataKey: any, metadataValue: any): { + (target: Function): void; + (target: Object, propertyKey: string | symbol): void; +}; +/** + * Define a unique metadata entry on the target. + * @param metadataKey A key used to store and retrieve metadata. + * @param metadataValue A value that contains attached metadata. + * @param target The target object on which to define metadata. + * @example + * + * class Example { + * } + * + * // constructor + * Reflect.defineMetadata("custom:annotation", options, Example); + * + * // decorator factory as metadata-producing annotation. + * function MyAnnotation(options): ClassDecorator { + * return target => Reflect.defineMetadata("custom:annotation", options, target); + * } + * + */ +export declare function defineMetadata(metadataKey: any, metadataValue: any, target: Object): void; +/** + * Define a unique metadata entry on the target. + * @param metadataKey A key used to store and retrieve metadata. + * @param metadataValue A value that contains attached metadata. + * @param target The target object on which to define metadata. + * @param propertyKey The property key for the target. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * Reflect.defineMetadata("custom:annotation", Number, Example, "staticProperty"); + * + * // property (on prototype) + * Reflect.defineMetadata("custom:annotation", Number, Example.prototype, "property"); + * + * // method (on constructor) + * Reflect.defineMetadata("custom:annotation", Number, Example, "staticMethod"); + * + * // method (on prototype) + * Reflect.defineMetadata("custom:annotation", Number, Example.prototype, "method"); + * + * // decorator factory as metadata-producing annotation. + * function MyAnnotation(options): PropertyDecorator { + * return (target, key) => Reflect.defineMetadata("custom:annotation", options, target, key); + * } + * + */ +export declare function defineMetadata(metadataKey: any, metadataValue: any, target: Object, propertyKey: string | symbol): void; +/** + * Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.hasMetadata("custom:annotation", Example); + * + */ +export declare function hasMetadata(metadataKey: any, target: Object): boolean; +/** + * Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.hasMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.hasMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "method"); + * + */ +export declare function hasMetadata(metadataKey: any, target: Object, propertyKey: string | symbol): boolean; +/** + * Gets a value indicating whether the target object has the provided metadata key defined. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @returns `true` if the metadata key was defined on the target object; otherwise, `false`. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.hasOwnMetadata("custom:annotation", Example); + * + */ +export declare function hasOwnMetadata(metadataKey: any, target: Object): boolean; +/** + * Gets a value indicating whether the target object has the provided metadata key defined. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns `true` if the metadata key was defined on the target object; otherwise, `false`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "method"); + * + */ +export declare function hasOwnMetadata(metadataKey: any, target: Object, propertyKey: string | symbol): boolean; +/** + * Gets the metadata value for the provided metadata key on the target object or its prototype chain. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @returns The metadata value for the metadata key if found; otherwise, `undefined`. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.getMetadata("custom:annotation", Example); + * + */ +export declare function getMetadata(metadataKey: any, target: Object): any; +/** + * Gets the metadata value for the provided metadata key on the target object or its prototype chain. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns The metadata value for the metadata key if found; otherwise, `undefined`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.getMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getMetadata("custom:annotation", Example.prototype, "method"); + * + */ +export declare function getMetadata(metadataKey: any, target: Object, propertyKey: string | symbol): any; +/** + * Gets the metadata value for the provided metadata key on the target object. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @returns The metadata value for the metadata key if found; otherwise, `undefined`. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.getOwnMetadata("custom:annotation", Example); + * + */ +export declare function getOwnMetadata(metadataKey: any, target: Object): any; +/** + * Gets the metadata value for the provided metadata key on the target object. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns The metadata value for the metadata key if found; otherwise, `undefined`. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "method"); + * + */ +export declare function getOwnMetadata(metadataKey: any, target: Object, propertyKey: string | symbol): any; +/** + * Gets the metadata keys defined on the target object or its prototype chain. + * @param target The target object on which the metadata is defined. + * @returns An array of unique metadata keys. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.getMetadataKeys(Example); + * + */ +export declare function getMetadataKeys(target: Object): any[]; +/** + * Gets the metadata keys defined on the target object or its prototype chain. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns An array of unique metadata keys. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.getMetadataKeys(Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getMetadataKeys(Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getMetadataKeys(Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getMetadataKeys(Example.prototype, "method"); + * + */ +export declare function getMetadataKeys(target: Object, propertyKey: string | symbol): any[]; +/** + * Gets the unique metadata keys defined on the target object. + * @param target The target object on which the metadata is defined. + * @returns An array of unique metadata keys. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.getOwnMetadataKeys(Example); + * + */ +export declare function getOwnMetadataKeys(target: Object): any[]; +/** + * Gets the unique metadata keys defined on the target object. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns An array of unique metadata keys. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.getOwnMetadataKeys(Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.getOwnMetadataKeys(Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.getOwnMetadataKeys(Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.getOwnMetadataKeys(Example.prototype, "method"); + * + */ +export declare function getOwnMetadataKeys(target: Object, propertyKey: string | symbol): any[]; +/** + * Deletes the metadata entry from the target object with the provided key. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @returns `true` if the metadata entry was found and deleted; otherwise, false. + * @example + * + * class Example { + * } + * + * // constructor + * result = Reflect.deleteMetadata("custom:annotation", Example); + * + */ +export declare function deleteMetadata(metadataKey: any, target: Object): boolean; +/** + * Deletes the metadata entry from the target object with the provided key. + * @param metadataKey A key used to store and retrieve metadata. + * @param target The target object on which the metadata is defined. + * @param propertyKey The property key for the target. + * @returns `true` if the metadata entry was found and deleted; otherwise, false. + * @example + * + * class Example { + * // property declarations are not part of ES6, though they are valid in TypeScript: + * // static staticProperty; + * // property; + * + * static staticMethod(p) { } + * method(p) { } + * } + * + * // property (on constructor) + * result = Reflect.deleteMetadata("custom:annotation", Example, "staticProperty"); + * + * // property (on prototype) + * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "property"); + * + * // method (on constructor) + * result = Reflect.deleteMetadata("custom:annotation", Example, "staticMethod"); + * + * // method (on prototype) + * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "method"); + * + */ +export declare function deleteMetadata(metadataKey: any, target: Object, propertyKey: string | symbol): boolean; diff --git a/package.json b/package.json index a620727..3ca7324 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,23 @@ { "name": "reflect-metadata", - "version": "0.1.14", + "version": "0.2.0-pre.0", "description": "Polyfill for Metadata Reflection API", + "type": "commonjs", "main": "Reflect.js", "types": "index.d.ts", - "typescript": { - "definition": "Reflect.d.ts" + "exports": { + ".": { + "types": "./index.d.ts", + "default": "./Reflect.js" + }, + "./lite": { + "types": "./index.d.ts", + "default": "./ReflectLite.js" + }, + "./no-conflict": { + "types": "./no-conflict.d.ts", + "default": "./ReflectNoConflict.js" + } }, "scripts": { "prepublishOnly": "gulp prepublish", @@ -37,7 +49,7 @@ "devDependencies": { "@types/chai": "^3.4.34", "@types/mocha": "^2.2.34", - "@types/node": "^6.0.52", + "@types/node": "^10.17.60", "chai": "^3.5.0", "del": "^2.2.2", "ecmarkup": "^3.9.3", diff --git a/reflect-metadata.d.ts b/reflect-metadata.d.ts deleted file mode 100644 index 2f342ce..0000000 --- a/reflect-metadata.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -/*! ***************************************************************************** -Copyright (C) Microsoft. All rights reserved. -Licensed under the Apache License, Version 2.0 (the "License"); you may not use -this file except in compliance with the License. You may obtain a copy of the -License at http://www.apache.org/licenses/LICENSE-2.0 - -THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED -WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, -MERCHANTABLITY OR NON-INFRINGEMENT. - -See the Apache Version 2.0 License for specific language governing permissions -and limitations under the License. -***************************************************************************** */ - -// NOTE: This file is obsolete and may be removed in a later release. -// For CommonJS/AMD/UMD/SystemJS declarations please use 'index.d.ts'. -// For standalone browser declarations, please use 'standalone.d.ts'. - -/// \ No newline at end of file diff --git a/test/reflect-decorate.ts b/test/reflect-decorate.ts index 4189e43..b07430b 100644 --- a/test/reflect-decorate.ts +++ b/test/reflect-decorate.ts @@ -1,210 +1,294 @@ // Reflect.decorate ( decorators, target [, propertyKey [, descriptor] ] ) -import "../Reflect"; +/// import { assert } from "chai"; +import { script } from "./vm"; +import { suites } from "./suites"; -describe("Reflect.decorate", () => { - it("ThrowsIfDecoratorsArgumentNotArrayForFunctionOverload", () => { - let target = function() { }; - assert.throws(() => Reflect.decorate(undefined, target, undefined, undefined), TypeError); +for (const { name, header, context } of suites) { + describe(name, () => { + describe("Reflect.decorate", () => { + it("ThrowsIfDecoratorsArgumentNotArrayForFunctionOverload", () => { + const { Reflect, TypeError } = script(context)` + ${header} + exports.Reflect = Reflect; + exports.TypeError = TypeError; + `; + let target = function() { }; + assert.throws(() => Reflect.decorate(undefined!, target, undefined!, undefined), TypeError); + }); + + it("ThrowsIfTargetArgumentNotFunctionForFunctionOverload", () => { + const { Reflect, TypeError } = script(context)` + ${header} + exports.Reflect = Reflect; + exports.TypeError = TypeError; + `; + let decorators: (MethodDecorator | PropertyDecorator)[] = []; + let target = {}; + assert.throws(() => Reflect.decorate(decorators, target, undefined!, undefined), TypeError); + }); + + it("ThrowsIfDecoratorsArgumentNotArrayForPropertyOverload", () => { + const { Reflect, TypeError } = script(context)` + ${header} + exports.Reflect = Reflect; + exports.TypeError = TypeError; + `; + let target = {}; + let name = "name"; + assert.throws(() => Reflect.decorate(undefined!, target, name, undefined), TypeError); + }); + + it("ThrowsIfTargetArgumentNotObjectForPropertyOverload", () => { + const { Reflect, TypeError } = script(context)` + ${header} + exports.Reflect = Reflect; + exports.TypeError = TypeError; + `; + let decorators: (MethodDecorator | PropertyDecorator)[] = []; + let target = 1; + let name = "name"; + assert.throws(() => Reflect.decorate(decorators, target, name, undefined), TypeError); + }); + + it("ThrowsIfDecoratorsArgumentNotArrayForPropertyDescriptorOverload", () => { + const { Reflect, TypeError } = script(context)` + ${header} + exports.Reflect = Reflect; + exports.TypeError = TypeError; + `; + let target = {}; + let name = "name"; + let descriptor = {}; + assert.throws(() => Reflect.decorate(undefined!, target, name, descriptor), TypeError); + }); + + it("ThrowsIfTargetArgumentNotObjectForPropertyDescriptorOverload", () => { + const { Reflect, TypeError } = script(context)` + ${header} + exports.Reflect = Reflect; + exports.TypeError = TypeError; + `; + let decorators: (MethodDecorator | PropertyDecorator)[] = []; + let target = 1; + let name = "name"; + let descriptor = {}; + assert.throws(() => Reflect.decorate(decorators, target, name, descriptor), TypeError); + }); + + it("ExecutesDecoratorsInReverseOrderForFunctionOverload", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let order: number[] = []; + let decorators = [ + (_target: Function): void => { order.push(0); }, + (_target: Function): void => { order.push(1); } + ]; + let target = function() { }; + Reflect.decorate(decorators, target); + assert.deepEqual(order, [1, 0]); + }); + + it("ExecutesDecoratorsInReverseOrderForPropertyOverload", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let order: number[] = []; + let decorators = [ + (_target: Object, _name: string | symbol): void => { order.push(0); }, + (_target: Object, _name: string | symbol): void => { order.push(1); } + ]; + let target = {}; + let name = "name"; + Reflect.decorate(decorators, target, name, undefined); + assert.deepEqual(order, [1, 0]); + }); + + it("ExecutesDecoratorsInReverseOrderForPropertyDescriptorOverload", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let order: number[] = []; + let decorators = [ + (_target: Object, _name: string | symbol): void => { order.push(0); }, + (_target: Object, _name: string | symbol): void => { order.push(1); } + ]; + let target = {}; + let name = "name"; + let descriptor = {}; + Reflect.decorate(decorators, target, name, descriptor); + assert.deepEqual(order, [1, 0]); + }); + + it("DecoratorPipelineForFunctionOverload", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let A = function A(): void { }; + let B = function B(): void { }; + let decorators = [ + (_target: Function): any => { return undefined; }, + (_target: Function): any => { return A; }, + (_target: Function): any => { return B; } + ]; + let target = function (): void { }; + let result = Reflect.decorate(decorators, target); + assert.strictEqual(result, A); + }); + + it("DecoratorPipelineForPropertyOverload", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let A = {}; + let B = {}; + let decorators = [ + (_target: Object, _name: string | symbol): any => { return undefined; }, + (_target: Object, _name: string | symbol): any => { return A; }, + (_target: Object, _name: string | symbol): any => { return B; } + ]; + let target = {}; + let result = Reflect.decorate(decorators, target, "name", undefined); + assert.strictEqual(result, A); + }); + + it("DecoratorPipelineForPropertyDescriptorOverload", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let A = {}; + let B = {}; + let C = {}; + let decorators = [ + (_target: Object, _name: string | symbol): any => { return undefined; }, + (_target: Object, _name: string | symbol): any => { return A; }, + (_target: Object, _name: string | symbol): any => { return B; } + ]; + let target = {}; + let result = Reflect.decorate(decorators, target, "name", C); + assert.strictEqual(result, A); + }); + + it("DecoratorCorrectTargetInPipelineForFunctionOverload", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let sent: Function[] = []; + let A = function A(): void { }; + let B = function B(): void { }; + let decorators = [ + (target: Function): any => { sent.push(target); return undefined; }, + (target: Function): any => { sent.push(target); return undefined; }, + (target: Function): any => { sent.push(target); return A; }, + (target: Function): any => { sent.push(target); return B; } + ]; + let target = function (): void { }; + Reflect.decorate(decorators, target); + assert.deepEqual(sent, [target, B, A, A]); + }); + + it("DecoratorCorrectTargetInPipelineForPropertyOverload", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let sent: Object[] = []; + let decorators = [ + (target: Object, _name: string | symbol): any => { sent.push(target); }, + (target: Object, _name: string | symbol): any => { sent.push(target); }, + (target: Object, _name: string | symbol): any => { sent.push(target); }, + (target: Object, _name: string | symbol): any => { sent.push(target); } + ]; + let target = { }; + Reflect.decorate(decorators, target, "name"); + assert.deepEqual(sent, [target, target, target, target]); + }); + + it("DecoratorCorrectNameInPipelineForPropertyOverload", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let sent: (symbol | string)[] = []; + let decorators = [ + (_target: Object, name: string | symbol): any => { sent.push(name); }, + (_target: Object, name: string | symbol): any => { sent.push(name); }, + (_target: Object, name: string | symbol): any => { sent.push(name); }, + (_target: Object, name: string | symbol): any => { sent.push(name); } + ]; + let target = { }; + Reflect.decorate(decorators, target, "name"); + assert.deepEqual(sent, ["name", "name", "name", "name"]); + }); + + it("DecoratorCorrectTargetInPipelineForPropertyDescriptorOverload", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let sent: Object[] = []; + let A = { }; + let B = { }; + let C = { }; + let decorators = [ + (target: Object, _name: string | symbol): any => { sent.push(target); return undefined; }, + (target: Object, _name: string | symbol): any => { sent.push(target); return undefined; }, + (target: Object, _name: string | symbol): any => { sent.push(target); return A; }, + (target: Object, _name: string | symbol): any => { sent.push(target); return B; } + ]; + let target = { }; + Reflect.decorate(decorators, target, "name", C); + assert.deepEqual(sent, [target, target, target, target]); + }); + + it("DecoratorCorrectNameInPipelineForPropertyDescriptorOverload", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let sent: (symbol | string)[] = []; + let A = { }; + let B = { }; + let C = { }; + let decorators = [ + (_target: Object, name: string | symbol): any => { sent.push(name); return undefined; }, + (_target: Object, name: string | symbol): any => { sent.push(name); return undefined; }, + (_target: Object, name: string | symbol): any => { sent.push(name); return A; }, + (_target: Object, name: string | symbol): any => { sent.push(name); return B; } + ]; + let target = { }; + Reflect.decorate(decorators, target, "name", C); + assert.deepEqual(sent, ["name", "name", "name", "name"]); + }); + + it("DecoratorCorrectDescriptorInPipelineForPropertyDescriptorOverload", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let sent: PropertyDescriptor[] = []; + let A = { }; + let B = { }; + let C = { }; + let decorators = [ + (_target: Object, _name: string | symbol, descriptor: PropertyDescriptor): any => { sent.push(descriptor); return undefined; }, + (_target: Object, _name: string | symbol, descriptor: PropertyDescriptor): any => { sent.push(descriptor); return undefined; }, + (_target: Object, _name: string | symbol, descriptor: PropertyDescriptor): any => { sent.push(descriptor); return A; }, + (_target: Object, _name: string | symbol, descriptor: PropertyDescriptor): any => { sent.push(descriptor); return B; } + ]; + let target = { }; + Reflect.decorate(decorators, target, "name", C); + assert.deepEqual(sent, [C, B, A, A]); + }); + }); }); - - it("ThrowsIfTargetArgumentNotFunctionForFunctionOverload", () => { - let decorators: (ClassDecorator | MethodDecorator | PropertyDecorator)[] = []; - let target = {}; - assert.throws(() => Reflect.decorate(decorators, target, undefined, undefined), TypeError); - }); - - it("ThrowsIfDecoratorsArgumentNotArrayForPropertyOverload", () => { - let target = {}; - let name = "name"; - assert.throws(() => Reflect.decorate(undefined, target, name, undefined), TypeError); - }); - - it("ThrowsIfTargetArgumentNotObjectForPropertyOverload", () => { - let decorators: (ClassDecorator | MethodDecorator | PropertyDecorator)[] = []; - let target = 1; - let name = "name"; - assert.throws(() => Reflect.decorate(decorators, target, name, undefined), TypeError); - }); - - it("ThrowsIfDecoratorsArgumentNotArrayForPropertyDescriptorOverload", () => { - let target = {}; - let name = "name"; - let descriptor = {}; - assert.throws(() => Reflect.decorate(undefined, target, name, descriptor), TypeError); - }); - - it("ThrowsIfTargetArgumentNotObjectForPropertyDescriptorOverload", () => { - let decorators: (ClassDecorator | MethodDecorator | PropertyDecorator)[] = []; - let target = 1; - let name = "name"; - let descriptor = {}; - assert.throws(() => Reflect.decorate(decorators, target, name, descriptor), TypeError); - }); - - it("ExecutesDecoratorsInReverseOrderForFunctionOverload", () => { - let order: number[] = []; - let decorators = [ - (target: Function): void => { order.push(0); }, - (target: Function): void => { order.push(1); } - ]; - let target = function() { }; - Reflect.decorate(decorators, target); - assert.deepEqual(order, [1, 0]); - }); - - it("ExecutesDecoratorsInReverseOrderForPropertyOverload", () => { - let order: number[] = []; - let decorators = [ - (target: Object, name: string | symbol): void => { order.push(0); }, - (target: Object, name: string | symbol): void => { order.push(1); } - ]; - let target = {}; - let name = "name"; - Reflect.decorate(decorators, target, name, undefined); - assert.deepEqual(order, [1, 0]); - }); - - it("ExecutesDecoratorsInReverseOrderForPropertyDescriptorOverload", () => { - let order: number[] = []; - let decorators = [ - (target: Object, name: string | symbol): void => { order.push(0); }, - (target: Object, name: string | symbol): void => { order.push(1); } - ]; - let target = {}; - let name = "name"; - let descriptor = {}; - Reflect.decorate(decorators, target, name, descriptor); - assert.deepEqual(order, [1, 0]); - }); - - it("DecoratorPipelineForFunctionOverload", () => { - let A = function A(): void { }; - let B = function B(): void { }; - let decorators = [ - (target: Function): any => { return undefined; }, - (target: Function): any => { return A; }, - (target: Function): any => { return B; } - ]; - let target = function (): void { }; - let result = Reflect.decorate(decorators, target); - assert.strictEqual(result, A); - }); - - it("DecoratorPipelineForPropertyOverload", () => { - let A = {}; - let B = {}; - let decorators = [ - (target: Object, name: string | symbol): any => { return undefined; }, - (target: Object, name: string | symbol): any => { return A; }, - (target: Object, name: string | symbol): any => { return B; } - ]; - let target = {}; - let result = Reflect.decorate(decorators, target, "name", undefined); - assert.strictEqual(result, A); - }); - - it("DecoratorPipelineForPropertyDescriptorOverload", () => { - let A = {}; - let B = {}; - let C = {}; - let decorators = [ - (target: Object, name: string | symbol): any => { return undefined; }, - (target: Object, name: string | symbol): any => { return A; }, - (target: Object, name: string | symbol): any => { return B; } - ]; - let target = {}; - let result = Reflect.decorate(decorators, target, "name", C); - assert.strictEqual(result, A); - }); - - it("DecoratorCorrectTargetInPipelineForFunctionOverload", () => { - let sent: Function[] = []; - let A = function A(): void { }; - let B = function B(): void { }; - let decorators = [ - (target: Function): any => { sent.push(target); return undefined; }, - (target: Function): any => { sent.push(target); return undefined; }, - (target: Function): any => { sent.push(target); return A; }, - (target: Function): any => { sent.push(target); return B; } - ]; - let target = function (): void { }; - Reflect.decorate(decorators, target); - assert.deepEqual(sent, [target, B, A, A]); - }); - - it("DecoratorCorrectTargetInPipelineForPropertyOverload", () => { - let sent: Object[] = []; - let decorators = [ - (target: Object, name: string | symbol): any => { sent.push(target); }, - (target: Object, name: string | symbol): any => { sent.push(target); }, - (target: Object, name: string | symbol): any => { sent.push(target); }, - (target: Object, name: string | symbol): any => { sent.push(target); } - ]; - let target = { }; - Reflect.decorate(decorators, target, "name"); - assert.deepEqual(sent, [target, target, target, target]); - }); - - it("DecoratorCorrectNameInPipelineForPropertyOverload", () => { - let sent: (symbol | string)[] = []; - let decorators = [ - (target: Object, name: string | symbol): any => { sent.push(name); }, - (target: Object, name: string | symbol): any => { sent.push(name); }, - (target: Object, name: string | symbol): any => { sent.push(name); }, - (target: Object, name: string | symbol): any => { sent.push(name); } - ]; - let target = { }; - Reflect.decorate(decorators, target, "name"); - assert.deepEqual(sent, ["name", "name", "name", "name"]); - }); - - it("DecoratorCorrectTargetInPipelineForPropertyDescriptorOverload", () => { - let sent: Object[] = []; - let A = { }; - let B = { }; - let C = { }; - let decorators = [ - (target: Object, name: string | symbol): any => { sent.push(target); return undefined; }, - (target: Object, name: string | symbol): any => { sent.push(target); return undefined; }, - (target: Object, name: string | symbol): any => { sent.push(target); return A; }, - (target: Object, name: string | symbol): any => { sent.push(target); return B; } - ]; - let target = { }; - Reflect.decorate(decorators, target, "name", C); - assert.deepEqual(sent, [target, target, target, target]); - }); - - it("DecoratorCorrectNameInPipelineForPropertyDescriptorOverload", () => { - let sent: (symbol | string)[] = []; - let A = { }; - let B = { }; - let C = { }; - let decorators = [ - (target: Object, name: string | symbol): any => { sent.push(name); return undefined; }, - (target: Object, name: string | symbol): any => { sent.push(name); return undefined; }, - (target: Object, name: string | symbol): any => { sent.push(name); return A; }, - (target: Object, name: string | symbol): any => { sent.push(name); return B; } - ]; - let target = { }; - Reflect.decorate(decorators, target, "name", C); - assert.deepEqual(sent, ["name", "name", "name", "name"]); - }); - - it("DecoratorCorrectDescriptorInPipelineForPropertyDescriptorOverload", () => { - let sent: PropertyDescriptor[] = []; - let A = { }; - let B = { }; - let C = { }; - let decorators = [ - (target: Object, name: string | symbol, descriptor: PropertyDescriptor): any => { sent.push(descriptor); return undefined; }, - (target: Object, name: string | symbol, descriptor: PropertyDescriptor): any => { sent.push(descriptor); return undefined; }, - (target: Object, name: string | symbol, descriptor: PropertyDescriptor): any => { sent.push(descriptor); return A; }, - (target: Object, name: string | symbol, descriptor: PropertyDescriptor): any => { sent.push(descriptor); return B; } - ]; - let target = { }; - Reflect.decorate(decorators, target, "name", C); - assert.deepEqual(sent, [C, B, A, A]); - }); -}); +} diff --git a/test/reflect-definemetadata.ts b/test/reflect-definemetadata.ts index 320b1f6..733e0d7 100644 --- a/test/reflect-definemetadata.ts +++ b/test/reflect-definemetadata.ts @@ -1,19 +1,39 @@ // 4.1.2 Reflect.defineMetadata ( metadataKey, metadataValue, target, propertyKey ) // https://rbuckton.github.io/reflect-metadata/#reflect.definemetadata -import "../Reflect"; +/// import { assert } from "chai"; +import { script } from "./vm"; +import { suites } from "./suites"; -describe("Reflect.defineMetadata", () => { - it("InvalidTarget", () => { - assert.throws(() => Reflect.defineMetadata("key", "value", undefined, undefined), TypeError); - }); +for (const { name, header, context } of suites) { + describe(name, () => { + describe("Reflect.defineMetadata", () => { + it("InvalidTarget", () => { + debugger; + const { Reflect, TypeError } = script(context)` + ${header} + exports.Reflect = Reflect; + exports.TypeError = TypeError; + `; + assert.throws(() => Reflect.defineMetadata("key", "value", undefined, undefined!), TypeError); + }); - it("ValidTargetWithoutTargetKey", () => { - assert.doesNotThrow(() => Reflect.defineMetadata("key", "value", { }, undefined)); - }); + it("ValidTargetWithoutTargetKey", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + assert.doesNotThrow(() => Reflect.defineMetadata("key", "value", { }, undefined!)); + }); - it("ValidTargetWithTargetKey", () => { - assert.doesNotThrow(() => Reflect.defineMetadata("key", "value", { }, "name")); + it("ValidTargetWithTargetKey", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + assert.doesNotThrow(() => Reflect.defineMetadata("key", "value", { }, "name")); + }); + }); }); -}); \ No newline at end of file +} \ No newline at end of file diff --git a/test/reflect-deletemetadata.ts b/test/reflect-deletemetadata.ts index 3612e51..e5613d2 100644 --- a/test/reflect-deletemetadata.ts +++ b/test/reflect-deletemetadata.ts @@ -1,40 +1,67 @@ // 4.1.10 Reflect.deleteMetadata ( metadataKey, target [, propertyKey] ) // https://rbuckton.github.io/reflect-metadata/#reflect.deletemetadata -import "../Reflect"; +/// import { assert } from "chai"; +import { script } from "./vm"; +import { suites } from "./suites"; -describe("Reflect.deleteMetadata", () => { - it("InvalidTarget", () => { - assert.throws(() => Reflect.deleteMetadata("key", undefined, undefined), TypeError); - }); +for (const { name, header, context } of suites) { + describe(name, () => { + describe("Reflect.deleteMetadata", () => { + it("InvalidTarget", () => { + const { Reflect, TypeError } = script(context)` + ${header} + exports.Reflect = Reflect; + exports.TypeError = TypeError; + `; + assert.throws(() => Reflect.deleteMetadata("key", undefined, undefined!), TypeError); + }); - it("WhenNotDefinedWithoutTargetKey", () => { - let obj = {}; - let result = Reflect.deleteMetadata("key", obj, undefined); - assert.equal(result, false); - }); + it("WhenNotDefinedWithoutTargetKey", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + let result = Reflect.deleteMetadata("key", obj, undefined!); + assert.equal(result, false); + }); - it("WhenDefinedWithoutTargetKey", () => { - let obj = {}; - Reflect.defineMetadata("key", "value", obj, undefined); - let result = Reflect.deleteMetadata("key", obj, undefined); - assert.equal(result, true); - }); + it("WhenDefinedWithoutTargetKey", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key", "value", obj, undefined!); + let result = Reflect.deleteMetadata("key", obj, undefined!); + assert.equal(result, true); + }); - it("WhenDefinedOnPrototypeWithoutTargetKey", () => { - let prototype = {}; - Reflect.defineMetadata("key", "value", prototype, undefined); - let obj = Object.create(prototype); - let result = Reflect.deleteMetadata("key", obj, undefined); - assert.equal(result, false); - }); + it("WhenDefinedOnPrototypeWithoutTargetKey", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let prototype = {}; + Reflect.defineMetadata("key", "value", prototype, undefined!); + let obj = Object.create(prototype); + let result = Reflect.deleteMetadata("key", obj, undefined!); + assert.equal(result, false); + }); - it("AfterDeleteMetadata", () => { - let obj = {}; - Reflect.defineMetadata("key", "value", obj, undefined); - Reflect.deleteMetadata("key", obj, undefined); - let result = Reflect.hasOwnMetadata("key", obj, undefined); - assert.equal(result, false); + it("AfterDeleteMetadata", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key", "value", obj, undefined!); + Reflect.deleteMetadata("key", obj, undefined!); + let result = Reflect.hasOwnMetadata("key", obj, undefined!); + assert.equal(result, false); + }); + }); }); -}); \ No newline at end of file +} \ No newline at end of file diff --git a/test/reflect-getmetadata.ts b/test/reflect-getmetadata.ts index 5b85635..2cdf48a 100644 --- a/test/reflect-getmetadata.ts +++ b/test/reflect-getmetadata.ts @@ -1,60 +1,88 @@ // 4.1.5 Reflect.getMetadata ( metadataKey, target [, propertyKey] ) // https://rbuckton.github.io/reflect-metadata/#reflect.getmetadata -import "../Reflect"; +/// import { assert } from "chai"; +import { script } from "./vm"; +import { suites } from "./suites"; -describe("Reflect.getMetadata", () => { - it("InvalidTarget", () => { - assert.throws(() => Reflect.getMetadata("key", undefined, undefined), TypeError); - }); - - it("WithoutTargetKeyWhenNotDefined", () => { - let obj = {}; - let result = Reflect.getMetadata("key", obj, undefined); - assert.equal(result, undefined); - }); +for (const { name, header, context } of suites) { + describe(name, () => { + describe("Reflect.getMetadata", () => { + it("InvalidTarget", () => { + const { Reflect, TypeError } = script(context)` + ${header} + exports.Reflect = Reflect; + exports.TypeError = TypeError; + `; + assert.throws(() => Reflect.getMetadata("key", undefined, undefined!), TypeError); + }); - it("WithoutTargetKeyWhenDefined", () => { - let obj = {}; - Reflect.defineMetadata("key", "value", obj, undefined); - let result = Reflect.getMetadata("key", obj, undefined); - assert.equal(result, "value"); - }); + it("WithoutTargetKeyWhenNotDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + let result = Reflect.getMetadata("key", obj, undefined!); + assert.equal(result, undefined); + }); - it("WithoutTargetKeyWhenDefinedOnPrototype", () => { - let prototype = {}; - let obj = Object.create(prototype); - Reflect.defineMetadata("key", "value", prototype, undefined); - let result = Reflect.getMetadata("key", obj, undefined); - assert.equal(result, "value"); - }); + it("WithoutTargetKeyWhenDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key", "value", obj, undefined!); + let result = Reflect.getMetadata("key", obj, undefined!); + assert.equal(result, "value"); + }); - it("WithTargetKeyWhenNotDefined", () => { - let obj = {}; - let result = Reflect.getMetadata("key", obj, "name"); - assert.equal(result, undefined); - }); + it("WithoutTargetKeyWhenDefinedOnPrototype", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let prototype = {}; + let obj = Object.create(prototype); + Reflect.defineMetadata("key", "value", prototype, undefined!); + let result = Reflect.getMetadata("key", obj, undefined!); + assert.equal(result, "value"); + }); - it("WithTargetKeyWhenDefined", () => { - let obj = {}; - Reflect.defineMetadata("key", "value", obj, "name"); - let result = Reflect.getMetadata("key", obj, "name"); - assert.equal(result, "value"); - }); + it("WithTargetKeyWhenNotDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + let result = Reflect.getMetadata("key", obj, "name"); + assert.equal(result, undefined); + }); - it("WhenKeyIsNaN", () => { - let obj = {}; - Reflect.defineMetadata(NaN, "value", obj, "name"); - let result = Reflect.getMetadata(NaN, obj, "name"); - assert.equal(result, "value"); - }); + it("WithTargetKeyWhenDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key", "value", obj, "name"); + let result = Reflect.getMetadata("key", obj, "name"); + assert.equal(result, "value"); + }); - it("WithTargetKeyWhenDefinedOnPrototype", () => { - let prototype = {}; - let obj = Object.create(prototype); - Reflect.defineMetadata("key", "value", prototype, "name"); - let result = Reflect.getMetadata("key", obj, "name"); - assert.equal(result, "value"); + it("WithTargetKeyWhenDefinedOnPrototype", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let prototype = {}; + let obj = Object.create(prototype); + Reflect.defineMetadata("key", "value", prototype, "name"); + let result = Reflect.getMetadata("key", obj, "name"); + assert.equal(result, "value"); + }); + }); }); -}); \ No newline at end of file +} \ No newline at end of file diff --git a/test/reflect-getmetadatakeys.ts b/test/reflect-getmetadatakeys.ts index 7a8bb30..477eda8 100644 --- a/test/reflect-getmetadatakeys.ts +++ b/test/reflect-getmetadatakeys.ts @@ -1,100 +1,155 @@ // 4.1.8 Reflect.getMetadataKeys ( target [, propertyKey] ) // https://rbuckton.github.io/reflect-metadata/#reflect.getmetadatakeys -import "../Reflect"; +/// import { assert } from "chai"; +import { script } from "./vm"; +import { suites } from "./suites"; -describe("Reflect.getMetadataKeys", () => { - it("KeysInvalidTarget", () => { - // 1. If Type(target) is not Object, throw a TypeError exception. - assert.throws(() => Reflect.getMetadataKeys(undefined, undefined), TypeError); - }); +for (const { name, header, context } of suites) { + describe(name, () => { + describe("Reflect.getMetadataKeys", () => { + it("KeysInvalidTarget", () => { + const { Reflect, TypeError } = script(context)` + ${header} + exports.Reflect = Reflect; + exports.TypeError = TypeError; + `; + // 1. If Type(target) is not Object, throw a TypeError exception. + assert.throws(() => Reflect.getMetadataKeys(undefined, undefined!), TypeError); + }); - it("KeysWithoutTargetKeyWhenNotDefined", () => { - let obj = {}; - let result = Reflect.getMetadataKeys(obj, undefined); - assert.deepEqual(result, []); - }); + it("KeysWithoutTargetKeyWhenNotDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + let result = Reflect.getMetadataKeys(obj, undefined!); + assert.deepEqual(result, []); + }); - it("KeysWithoutTargetKeyWhenDefined", () => { - let obj = {}; - Reflect.defineMetadata("key", "value", obj, undefined); - let result = Reflect.getMetadataKeys(obj, undefined); - assert.deepEqual(result, ["key"]); - }); + it("KeysWithoutTargetKeyWhenDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key", "value", obj, undefined!); + let result = Reflect.getMetadataKeys(obj, undefined!); + assert.deepEqual(result, ["key"]); + }); - it("KeysWithoutTargetKeyWhenDefinedOnPrototype", () => { - let prototype = {}; - let obj = Object.create(prototype); - Reflect.defineMetadata("key", "value", prototype, undefined); - let result = Reflect.getMetadataKeys(obj, undefined); - assert.deepEqual(result, ["key"]); - }); + it("KeysWithoutTargetKeyWhenDefinedOnPrototype", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let prototype = {}; + let obj = Object.create(prototype); + Reflect.defineMetadata("key", "value", prototype, undefined!); + let result = Reflect.getMetadataKeys(obj, undefined!); + assert.deepEqual(result, ["key"]); + }); - it("KeysOrderWithoutTargetKey", () => { - let obj = {}; - Reflect.defineMetadata("key1", "value", obj, undefined); - Reflect.defineMetadata("key0", "value", obj, undefined); - let result = Reflect.getMetadataKeys(obj, undefined); - assert.deepEqual(result, ["key1", "key0"]); - }); + it("KeysOrderWithoutTargetKey", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key1", "value", obj, undefined!); + Reflect.defineMetadata("key0", "value", obj, undefined!); + let result = Reflect.getMetadataKeys(obj, undefined!); + assert.deepEqual(result, ["key1", "key0"]); + }); - it("KeysOrderAfterRedefineWithoutTargetKey", () => { - let obj = {}; - Reflect.defineMetadata("key1", "value", obj, undefined); - Reflect.defineMetadata("key0", "value", obj, undefined); - Reflect.defineMetadata("key1", "value", obj, undefined); - let result = Reflect.getMetadataKeys(obj, undefined); - assert.deepEqual(result, ["key1", "key0"]); - }); + it("KeysOrderAfterRedefineWithoutTargetKey", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key1", "value", obj, undefined!); + Reflect.defineMetadata("key0", "value", obj, undefined!); + Reflect.defineMetadata("key1", "value", obj, undefined!); + let result = Reflect.getMetadataKeys(obj, undefined!); + assert.deepEqual(result, ["key1", "key0"]); + }); - it("KeysOrderWithoutTargetKeyWhenDefinedOnPrototype", () => { - let prototype = {}; - Reflect.defineMetadata("key2", "value", prototype, undefined); - let obj = Object.create(prototype); - Reflect.defineMetadata("key1", "value", obj, undefined); - Reflect.defineMetadata("key0", "value", obj, undefined); - let result = Reflect.getMetadataKeys(obj, undefined); - assert.deepEqual(result, ["key1", "key0", "key2"]); - }); + it("KeysOrderWithoutTargetKeyWhenDefinedOnPrototype", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let prototype = {}; + Reflect.defineMetadata("key2", "value", prototype, undefined!); + let obj = Object.create(prototype); + Reflect.defineMetadata("key1", "value", obj, undefined!); + Reflect.defineMetadata("key0", "value", obj, undefined!); + let result = Reflect.getMetadataKeys(obj, undefined!); + assert.deepEqual(result, ["key1", "key0", "key2"]); + }); - it("KeysWithTargetKeyWhenNotDefined", () => { - let obj = {}; - let result = Reflect.getMetadataKeys(obj, "name"); - assert.deepEqual(result, []); - }); + it("KeysWithTargetKeyWhenNotDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + let result = Reflect.getMetadataKeys(obj, "name"); + assert.deepEqual(result, []); + }); - it("KeysWithTargetKeyWhenDefined", () => { - let obj = {}; - Reflect.defineMetadata("key", "value", obj, "name"); - let result = Reflect.getMetadataKeys(obj, "name"); - assert.deepEqual(result, ["key"]); - }); + it("KeysWithTargetKeyWhenDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key", "value", obj, "name"); + let result = Reflect.getMetadataKeys(obj, "name"); + assert.deepEqual(result, ["key"]); + }); - it("KeysWithTargetKeyWhenDefinedOnPrototype", () => { - let prototype = {}; - let obj = Object.create(prototype); - Reflect.defineMetadata("key", "value", prototype, "name"); - let result = Reflect.getMetadataKeys(obj, "name"); - assert.deepEqual(result, ["key"]); - }); + it("KeysWithTargetKeyWhenDefinedOnPrototype", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let prototype = {}; + let obj = Object.create(prototype); + Reflect.defineMetadata("key", "value", prototype, "name"); + let result = Reflect.getMetadataKeys(obj, "name"); + assert.deepEqual(result, ["key"]); + }); - it("KeysOrderAfterRedefineWithTargetKey", () => { - let obj = {}; - Reflect.defineMetadata("key1", "value", obj, "name"); - Reflect.defineMetadata("key0", "value", obj, "name"); - Reflect.defineMetadata("key1", "value", obj, "name"); - let result = Reflect.getMetadataKeys(obj, "name"); - assert.deepEqual(result, ["key1", "key0"]); - }); + it("KeysOrderAfterRedefineWithTargetKey", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key1", "value", obj, "name"); + Reflect.defineMetadata("key0", "value", obj, "name"); + Reflect.defineMetadata("key1", "value", obj, "name"); + let result = Reflect.getMetadataKeys(obj, "name"); + assert.deepEqual(result, ["key1", "key0"]); + }); - it("KeysOrderWithTargetKeyWhenDefinedOnPrototype", () => { - let prototype = {}; - Reflect.defineMetadata("key2", "value", prototype, "name"); - let obj = Object.create(prototype); - Reflect.defineMetadata("key1", "value", obj, "name"); - Reflect.defineMetadata("key0", "value", obj, "name"); - let result = Reflect.getMetadataKeys(obj, "name"); - assert.deepEqual(result, ["key1", "key0", "key2"]); + it("KeysOrderWithTargetKeyWhenDefinedOnPrototype", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let prototype = {}; + Reflect.defineMetadata("key2", "value", prototype, "name"); + let obj = Object.create(prototype); + Reflect.defineMetadata("key1", "value", obj, "name"); + Reflect.defineMetadata("key0", "value", obj, "name"); + let result = Reflect.getMetadataKeys(obj, "name"); + assert.deepEqual(result, ["key1", "key0", "key2"]); + }); + }); }); -}); \ No newline at end of file +} \ No newline at end of file diff --git a/test/reflect-getownmetadata.ts b/test/reflect-getownmetadata.ts index 856f2fb..2a3041c 100644 --- a/test/reflect-getownmetadata.ts +++ b/test/reflect-getownmetadata.ts @@ -1,53 +1,88 @@ // 4.1.7 Reflect.getOwnMetadata ( metadataKey, target [, propertyKey] ) // https://rbuckton.github.io/reflect-metadata/#reflect.getownmetadata -import "../Reflect"; +/// import { assert } from "chai"; +import { script } from "./vm"; +import { suites } from "./suites"; -describe("Reflect.getOwnMetadata", () => { - it("InvalidTarget", () => { - assert.throws(() => Reflect.getOwnMetadata("key", undefined, undefined), TypeError); - }); +for (const { name, header, context } of suites) { + describe(name, () => { + describe("Reflect.getOwnMetadata", () => { + it("InvalidTarget", () => { + const { Reflect, TypeError } = script(context)` + ${header} + exports.Reflect = Reflect; + exports.TypeError = TypeError; + `; + assert.throws(() => Reflect.getOwnMetadata("key", undefined, undefined!), TypeError); + }); - it("WithoutTargetKeyWhenNotDefined", () => { - let obj = {}; - let result = Reflect.getOwnMetadata("key", obj, undefined); - assert.equal(result, undefined); - }); + it("WithoutTargetKeyWhenNotDefined", () => {0 + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + let result = Reflect.getOwnMetadata("key", obj, undefined!); + assert.equal(result, undefined); + }); - it("WithoutTargetKeyWhenDefined", () => { - let obj = {}; - Reflect.defineMetadata("key", "value", obj, undefined); - let result = Reflect.getOwnMetadata("key", obj, undefined); - assert.equal(result, "value"); - }); + it("WithoutTargetKeyWhenDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key", "value", obj, undefined!); + let result = Reflect.getOwnMetadata("key", obj, undefined!); + assert.equal(result, "value"); + }); - it("WithoutTargetKeyWhenDefinedOnPrototype", () => { - let prototype = {}; - let obj = Object.create(prototype); - Reflect.defineMetadata("key", "value", prototype, undefined); - let result = Reflect.getOwnMetadata("key", obj, undefined); - assert.equal(result, undefined); - }); + it("WithoutTargetKeyWhenDefinedOnPrototype", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let prototype = {}; + let obj = Object.create(prototype); + Reflect.defineMetadata("key", "value", prototype, undefined!); + let result = Reflect.getOwnMetadata("key", obj, undefined!); + assert.equal(result, undefined); + }); - it("WithTargetKeyWhenNotDefined", () => { - let obj = {}; - let result = Reflect.getOwnMetadata("key", obj, "name"); - assert.equal(result, undefined); - }); + it("WithTargetKeyWhenNotDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + let result = Reflect.getOwnMetadata("key", obj, "name"); + assert.equal(result, undefined); + }); - it("WithTargetKeyWhenDefined", () => { - let obj = {}; - Reflect.defineMetadata("key", "value", obj, "name"); - let result = Reflect.getOwnMetadata("key", obj, "name"); - assert.equal(result, "value"); - }); + it("WithTargetKeyWhenDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key", "value", obj, "name"); + let result = Reflect.getOwnMetadata("key", obj, "name"); + assert.equal(result, "value"); + }); - it("WithTargetKeyWhenDefinedOnPrototype", () => { - let prototype = {}; - let obj = Object.create(prototype); - Reflect.defineMetadata("key", "value", prototype, "name"); - let result = Reflect.getOwnMetadata("key", obj, "name"); - assert.equal(result, undefined); + it("WithTargetKeyWhenDefinedOnPrototype", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let prototype = {}; + let obj = Object.create(prototype); + Reflect.defineMetadata("key", "value", prototype, "name"); + let result = Reflect.getOwnMetadata("key", obj, "name"); + assert.equal(result, undefined); + }); + }); }); -}); \ No newline at end of file +} \ No newline at end of file diff --git a/test/reflect-getownmetadatakeys.ts b/test/reflect-getownmetadatakeys.ts index 16eeb86..018aaab 100644 --- a/test/reflect-getownmetadatakeys.ts +++ b/test/reflect-getownmetadatakeys.ts @@ -1,80 +1,127 @@ // 4.1.9 Reflect.getOwnMetadataKeysKeys ( target [, propertyKey] ) // https://rbuckton.github.io/reflect-metadata/#reflect.getownmetadatakeys -import "../Reflect"; +/// import { assert } from "chai"; +import { script } from "./vm"; +import { suites } from "./suites"; -describe("Reflect.deleteMetadata", () => { - it("KeysKeysInvalidTarget", () => { - // 1. If Type(target) is not Object, throw a TypeError exception. - assert.throws(() => Reflect.getOwnMetadataKeys(undefined, undefined), TypeError); - }); +for (const { name, header, context } of suites) { + describe(name, () => { + describe("Reflect.getOwnMetadataKeys", () => { + it("KeysKeysInvalidTarget", () => { + const { Reflect, TypeError } = script(context)` + ${header} + exports.Reflect = Reflect; + exports.TypeError = TypeError; + `; + // 1. If Type(target) is not Object, throw a TypeError exception. + assert.throws(() => Reflect.getOwnMetadataKeys(undefined, undefined!), TypeError); + }); - it("KeysWithoutTargetKeyWhenNotDefined", () => { - let obj = {}; - let result = Reflect.getOwnMetadataKeys(obj, undefined); - assert.deepEqual(result, []); - }); + it("KeysWithoutTargetKeyWhenNotDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + let result = Reflect.getOwnMetadataKeys(obj, undefined!); + assert.deepEqual(result, []); + }); - it("KeysWithoutTargetKeyWhenDefined", () => { - let obj = {}; - Reflect.defineMetadata("key", "value", obj, undefined); - let result = Reflect.getOwnMetadataKeys(obj, undefined); - assert.deepEqual(result, ["key"]); - }); + it("KeysWithoutTargetKeyWhenDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key", "value", obj, undefined!); + let result = Reflect.getOwnMetadataKeys(obj, undefined!); + assert.deepEqual(result, ["key"]); + }); - it("KeysWithoutTargetKeyWhenDefinedOnPrototype", () => { - let prototype = {}; - let obj = Object.create(prototype); - Reflect.defineMetadata("key", "value", prototype, undefined); - let result = Reflect.getOwnMetadataKeys(obj, undefined); - assert.deepEqual(result, []); - }); + it("KeysWithoutTargetKeyWhenDefinedOnPrototype", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let prototype = {}; + let obj = Object.create(prototype); + Reflect.defineMetadata("key", "value", prototype, undefined!); + let result = Reflect.getOwnMetadataKeys(obj, undefined!); + assert.deepEqual(result, []); + }); - it("KeysOrderWithoutTargetKey", () => { - let obj = {}; - Reflect.defineMetadata("key1", "value", obj, undefined); - Reflect.defineMetadata("key0", "value", obj, undefined); - let result = Reflect.getOwnMetadataKeys(obj, undefined); - assert.deepEqual(result, ["key1", "key0"]); - }); + it("KeysOrderWithoutTargetKey", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key1", "value", obj, undefined!); + Reflect.defineMetadata("key0", "value", obj, undefined!); + let result = Reflect.getOwnMetadataKeys(obj, undefined!); + assert.deepEqual(result, ["key1", "key0"]); + }); - it("KeysOrderAfterRedefineWithoutTargetKey", () => { - let obj = {}; - Reflect.defineMetadata("key1", "value", obj, undefined); - Reflect.defineMetadata("key0", "value", obj, undefined); - Reflect.defineMetadata("key1", "value", obj, undefined); - let result = Reflect.getOwnMetadataKeys(obj, undefined); - assert.deepEqual(result, ["key1", "key0"]); - }); + it("KeysOrderAfterRedefineWithoutTargetKey", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key1", "value", obj, undefined!); + Reflect.defineMetadata("key0", "value", obj, undefined!); + Reflect.defineMetadata("key1", "value", obj, undefined!); + let result = Reflect.getOwnMetadataKeys(obj, undefined!); + assert.deepEqual(result, ["key1", "key0"]); + }); - it("KeysWithTargetKeyWhenNotDefined", () => { - let obj = {}; - let result = Reflect.getOwnMetadataKeys(obj, "name"); - assert.deepEqual(result, []); - }); + it("KeysWithTargetKeyWhenNotDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + let result = Reflect.getOwnMetadataKeys(obj, "name"); + assert.deepEqual(result, []); + }); - it("KeysWithTargetKeyWhenDefined", () => { - let obj = {}; - Reflect.defineMetadata("key", "value", obj, "name"); - let result = Reflect.getOwnMetadataKeys(obj, "name"); - assert.deepEqual(result, ["key"]); - }); + it("KeysWithTargetKeyWhenDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key", "value", obj, "name"); + let result = Reflect.getOwnMetadataKeys(obj, "name"); + assert.deepEqual(result, ["key"]); + }); - it("KeysWithTargetKeyWhenDefinedOnPrototype", () => { - let prototype = {}; - let obj = Object.create(prototype); - Reflect.defineMetadata("key", "value", prototype, "name"); - let result = Reflect.getOwnMetadataKeys(obj, "name"); - assert.deepEqual(result, []); - }); + it("KeysWithTargetKeyWhenDefinedOnPrototype", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let prototype = {}; + let obj = Object.create(prototype); + Reflect.defineMetadata("key", "value", prototype, "name"); + let result = Reflect.getOwnMetadataKeys(obj, "name"); + assert.deepEqual(result, []); + }); - it("KeysOrderAfterRedefineWithTargetKey", () => { - let obj = {}; - Reflect.defineMetadata("key1", "value", obj, "name"); - Reflect.defineMetadata("key0", "value", obj, "name"); - Reflect.defineMetadata("key1", "value", obj, "name"); - let result = Reflect.getOwnMetadataKeys(obj, "name"); - assert.deepEqual(result, ["key1", "key0"]); + it("KeysOrderAfterRedefineWithTargetKey", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key1", "value", obj, "name"); + Reflect.defineMetadata("key0", "value", obj, "name"); + Reflect.defineMetadata("key1", "value", obj, "name"); + let result = Reflect.getOwnMetadataKeys(obj, "name"); + assert.deepEqual(result, ["key1", "key0"]); + }); + }); }); -}); \ No newline at end of file +} \ No newline at end of file diff --git a/test/reflect-hasmetadata.ts b/test/reflect-hasmetadata.ts index 8a9825c..1be3ea2 100644 --- a/test/reflect-hasmetadata.ts +++ b/test/reflect-hasmetadata.ts @@ -1,53 +1,88 @@ // 4.1.4 Reflect.hasMetadata ( metadataKey, target [, propertyKey] ) // https://rbuckton.github.io/reflect-metadata/#reflect.hasmetadata -import "../Reflect"; +/// import { assert } from "chai"; +import { script } from "./vm"; +import { suites } from "./suites"; -describe("Reflect.hasMetadata", () => { - it("InvalidTarget", () => { - assert.throws(() => Reflect.hasMetadata("key", undefined, undefined), TypeError); - }); +for (const { name, header, context } of suites) { + describe(name, () => { + describe("Reflect.hasMetadata", () => { + it("InvalidTarget", () => { + const { Reflect, TypeError } = script(context)` + ${header} + exports.Reflect = Reflect; + exports.TypeError = TypeError; + `; + assert.throws(() => Reflect.hasMetadata("key", undefined, undefined!), TypeError); + }); - it("WithoutTargetKeyWhenNotDefined", () => { - let obj = {}; - let result = Reflect.hasMetadata("key", obj, undefined); - assert.equal(result, false); - }); + it("WithoutTargetKeyWhenNotDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + let result = Reflect.hasMetadata("key", obj, undefined!); + assert.equal(result, false); + }); - it("WithoutTargetKeyWhenDefined", () => { - let obj = {}; - Reflect.defineMetadata("key", "value", obj, undefined); - let result = Reflect.hasMetadata("key", obj, undefined); - assert.equal(result, true); - }); + it("WithoutTargetKeyWhenDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key", "value", obj, undefined!); + let result = Reflect.hasMetadata("key", obj, undefined!); + assert.equal(result, true); + }); - it("WithoutTargetKeyWhenDefinedOnPrototype", () => { - let prototype = {}; - let obj = Object.create(prototype); - Reflect.defineMetadata("key", "value", prototype, undefined); - let result = Reflect.hasMetadata("key", obj, undefined); - assert.equal(result, true); - }); + it("WithoutTargetKeyWhenDefinedOnPrototype", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let prototype = {}; + let obj = Object.create(prototype); + Reflect.defineMetadata("key", "value", prototype, undefined!); + let result = Reflect.hasMetadata("key", obj, undefined!); + assert.equal(result, true); + }); - it("WithTargetKeyWhenNotDefined", () => { - let obj = {}; - let result = Reflect.hasMetadata("key", obj, "name"); - assert.equal(result, false); - }); + it("WithTargetKeyWhenNotDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + let result = Reflect.hasMetadata("key", obj, "name"); + assert.equal(result, false); + }); - it("WithTargetKeyWhenDefined", () => { - let obj = {}; - Reflect.defineMetadata("key", "value", obj, "name"); - let result = Reflect.hasMetadata("key", obj, "name"); - assert.equal(result, true); - }); + it("WithTargetKeyWhenDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key", "value", obj, "name"); + let result = Reflect.hasMetadata("key", obj, "name"); + assert.equal(result, true); + }); - it("WithTargetKeyWhenDefinedOnPrototype", () => { - let prototype = {}; - let obj = Object.create(prototype); - Reflect.defineMetadata("key", "value", prototype, "name"); - let result = Reflect.hasMetadata("key", obj, "name"); - assert.equal(result, true); + it("WithTargetKeyWhenDefinedOnPrototype", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let prototype = {}; + let obj = Object.create(prototype); + Reflect.defineMetadata("key", "value", prototype, "name"); + let result = Reflect.hasMetadata("key", obj, "name"); + assert.equal(result, true); + }); + }); }); -}); \ No newline at end of file +} \ No newline at end of file diff --git a/test/reflect-hasownmetadata.ts b/test/reflect-hasownmetadata.ts index 6ce4954..5cbd4cb 100644 --- a/test/reflect-hasownmetadata.ts +++ b/test/reflect-hasownmetadata.ts @@ -1,53 +1,88 @@ // 4.1.5 Reflect.hasOwnMetadata ( metadataKey, target [, propertyKey] ) // https://rbuckton.github.io/reflect-metadata/#reflect.hasownmetadata -import "../Reflect"; +/// import { assert } from "chai"; +import { script } from "./vm"; +import { suites } from "./suites"; -describe("Reflect.hasOwnMetadata", () => { - it("InvalidTarget", () => { - assert.throws(() => Reflect.hasOwnMetadata("key", undefined, undefined), TypeError); - }); +for (const { name, header, context } of suites) { + describe(name, () => { + describe("Reflect.hasOwnMetadata", () => { + it("InvalidTarget", () => { + const { Reflect, TypeError } = script(context)` + ${header} + exports.Reflect = Reflect; + exports.TypeError = TypeError; + `; + assert.throws(() => Reflect.hasOwnMetadata("key", undefined, undefined!), TypeError); + }); - it("WithoutTargetKeyWhenNotDefined", () => { - let obj = {}; - let result = Reflect.hasOwnMetadata("key", obj, undefined); - assert.equal(result, false); - }); + it("WithoutTargetKeyWhenNotDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + let result = Reflect.hasOwnMetadata("key", obj, undefined!); + assert.equal(result, false); + }); - it("WithoutTargetKeyWhenDefined", () => { - let obj = {}; - Reflect.defineMetadata("key", "value", obj, undefined); - let result = Reflect.hasOwnMetadata("key", obj, undefined); - assert.equal(result, true); - }); + it("WithoutTargetKeyWhenDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key", "value", obj, undefined!); + let result = Reflect.hasOwnMetadata("key", obj, undefined!); + assert.equal(result, true); + }); - it("WithoutTargetKeyWhenDefinedOnPrototype", () => { - let prototype = {}; - let obj = Object.create(prototype); - Reflect.defineMetadata("key", "value", prototype, undefined); - let result = Reflect.hasOwnMetadata("key", obj, undefined); - assert.equal(result, false); - }); + it("WithoutTargetKeyWhenDefinedOnPrototype", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let prototype = {}; + let obj = Object.create(prototype); + Reflect.defineMetadata("key", "value", prototype, undefined!); + let result = Reflect.hasOwnMetadata("key", obj, undefined!); + assert.equal(result, false); + }); - it("WithTargetKeyWhenNotDefined", () => { - let obj = {}; - let result = Reflect.hasOwnMetadata("key", obj, "name"); - assert.equal(result, false); - }); + it("WithTargetKeyWhenNotDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + let result = Reflect.hasOwnMetadata("key", obj, "name"); + assert.equal(result, false); + }); - it("WithTargetKeyWhenDefined", () => { - let obj = {}; - Reflect.defineMetadata("key", "value", obj, "name"); - let result = Reflect.hasOwnMetadata("key", obj, "name"); - assert.equal(result, true); - }); + it("WithTargetKeyWhenDefined", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let obj = {}; + Reflect.defineMetadata("key", "value", obj, "name"); + let result = Reflect.hasOwnMetadata("key", obj, "name"); + assert.equal(result, true); + }); - it("WithTargetKeyWhenDefinedOnPrototype", () => { - let prototype = {}; - let obj = Object.create(prototype); - Reflect.defineMetadata("key", "value", prototype, "name"); - let result = Reflect.hasOwnMetadata("key", obj, "name"); - assert.equal(result, false); + it("WithTargetKeyWhenDefinedOnPrototype", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let prototype = {}; + let obj = Object.create(prototype); + Reflect.defineMetadata("key", "value", prototype, "name"); + let result = Reflect.hasOwnMetadata("key", obj, "name"); + assert.equal(result, false); + }); + }); }); -}); \ No newline at end of file +} \ No newline at end of file diff --git a/test/reflect-metadata.ts b/test/reflect-metadata.ts index 27dc9dd..8d99cc4 100644 --- a/test/reflect-metadata.ts +++ b/test/reflect-metadata.ts @@ -1,40 +1,68 @@ // 4.1.2 Reflect.metadata ( metadataKey, metadataValue ) // https://rbuckton.github.io/reflect-metadata/#reflect.metadata -import "../Reflect"; +/// import { assert } from "chai"; +import { script } from "./vm"; +import { suites } from "./suites"; -describe("Reflect.metadata", () => { - it("ReturnsDecoratorFunction", () => { - let result = Reflect.metadata("key", "value"); - assert.equal(typeof result, "function"); - }); +for (const { name, header, context } of suites) { + describe(name, () => { + describe("Reflect.metadata", () => { + it("ReturnsDecoratorFunction", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let result = Reflect.metadata("key", "value"); + assert.equal(typeof result, "function"); + }); - it("DecoratorThrowsWithInvalidTargetWithTargetKey", () => { - let decorator = Reflect.metadata("key", "value"); - assert.throws(() => decorator(undefined, "name"), TypeError); - }); + it("DecoratorThrowsWithInvalidTargetWithTargetKey", () => { + const { Reflect, TypeError } = script(context)` + ${header} + exports.Reflect = Reflect; + exports.TypeError = TypeError; + `; + let decorator = Reflect.metadata("key", "value"); + assert.throws(() => decorator(undefined!, "name"), TypeError); + }); - it("DecoratorThrowsWithInvalidTargetKey", () => { - let decorator = Reflect.metadata("key", "value"); - assert.throws(() => decorator({}, {}), TypeError); - }); + it("DecoratorThrowsWithInvalidTargetKey", () => { + const { Reflect, TypeError } = script(context)` + ${header} + exports.Reflect = Reflect; + exports.TypeError = TypeError; + `; + let decorator = Reflect.metadata("key", "value"); + assert.throws(() => decorator({}, {}), TypeError); + }); - it("OnTargetWithoutTargetKey", () => { - let decorator = Reflect.metadata("key", "value"); - let target = function () {} - decorator(target); + it("OnTargetWithoutTargetKey", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let decorator = Reflect.metadata("key", "value"); + let target = function () {} + decorator(target); - let result = Reflect.hasOwnMetadata("key", target, undefined); - assert.equal(result, true); - }); + let result = Reflect.hasOwnMetadata("key", target, undefined!); + assert.equal(result, true); + }); - it("OnTargetWithTargetKey", () => { - let decorator = Reflect.metadata("key", "value"); - let target = {} - decorator(target, "name"); + it("OnTargetWithTargetKey", () => { + const { Reflect } = script(context)` + ${header} + exports.Reflect = Reflect; + `; + let decorator = Reflect.metadata("key", "value"); + let target = {} + decorator(target, "name"); - let result = Reflect.hasOwnMetadata("key", target, "name"); - assert.equal(result, true); + let result = Reflect.hasOwnMetadata("key", target, "name"); + assert.equal(result, true); + }); + }); }); -}); \ No newline at end of file +} \ No newline at end of file diff --git a/test/reflect-other.ts b/test/reflect-other.ts index 059ca47..83007b1 100644 --- a/test/reflect-other.ts +++ b/test/reflect-other.ts @@ -1,15 +1,29 @@ -import * as fs from "fs"; import { assert } from "chai"; +import { script } from "./vm"; +import { suites } from "./suites"; -describe("Reflect", () => { - it("does not overwrite existing implementation", () => { - const defineMetadata = Reflect.defineMetadata; +for (const { name, header, context } of suites.filter(s => s.global)) { + describe(name, () => { + describe("Reflect", () => { + it("does not clobber existing implementation", () => { + const { Reflect, defineMetadata, obj } = script(context)` + const fs = require("fs"); + ${header} - const reflectPath = require.resolve("../Reflect.js"); - const reflectContent = fs.readFileSync(reflectPath, "utf8"); - const reflectFunction = Function(reflectContent); - reflectFunction(); + exports.Reflect = Reflect; + exports.defineMetadata = Reflect.defineMetadata; + exports.obj = {}; + Reflect.defineMetadata("key", "value", exports.obj); - assert.strictEqual(Reflect.defineMetadata, defineMetadata); + const reflectPath = require.resolve("../Reflect.js"); + const reflectContent = fs.readFileSync(reflectPath, "utf8"); + const reflectFunction = Function(reflectContent); + reflectFunction(); + `; + + assert.notStrictEqual(Reflect.defineMetadata, defineMetadata); + assert.strictEqual(Reflect.getOwnMetadata("key", obj), "value"); + }); + }); }); -}); \ No newline at end of file +} \ No newline at end of file diff --git a/test/registry.ts b/test/registry.ts new file mode 100644 index 0000000..c1f4086 --- /dev/null +++ b/test/registry.ts @@ -0,0 +1,59 @@ +/// +/// +import { assert } from "chai"; +import { script } from "./vm"; +import { suites } from "./suites"; + +for (const { name, header, context } of suites.filter(s => s.global && !s.polyfill)) { + describe(`ReflectNoConflict + ${name}`, () => { + describe("MetadataRegistry", () => { + it("defines registry", () => { + const { registry } = script(context)` + const ReflectNoConflict = require("../ReflectNoConflict"); + ${header} + + const registrySymbol = Symbol.for("@reflect-metadata:registry"); + exports.registry = Reflect[registrySymbol]; + `; + assert.isDefined(registry); + }); + it("two registries", () => { + const { provider1, provider2 } = script(context)` + const ReflectNoConflict = require("../ReflectNoConflict"); + ${header} + + const registrySymbol = Symbol.for("@reflect-metadata:registry"); + const registry = Reflect[registrySymbol]; + + const obj1 = {}; + ReflectNoConflict.defineMetadata("key", "value", obj1); + + const obj2 = {}; + Reflect.defineMetadata("key", "value", obj2); + + const provider1 = registry.getProvider(obj1, undefined); + const provider2 = registry.getProvider(obj2, undefined); + + exports.provider1 = provider1; + exports.provider2 = provider2; + exports.registry = registry; + `; + assert.isDefined(provider1); + assert.isDefined(provider2); + assert.notStrictEqual(provider1, provider2); + }); + it("registries are shared", () => { + const { ReflectNoConflict, Reflect } = script(context)` + const ReflectNoConflict = require("../ReflectNoConflict"); + ${header} + + exports.ReflectNoConflict = ReflectNoConflict; + exports.Reflect = Reflect; + `; + const obj = {}; + ReflectNoConflict.defineMetadata("key", "value", obj); + assert.isTrue(Reflect.hasOwnMetadata("key", obj)); + }); + }); + }); +} \ No newline at end of file diff --git a/test/suites.ts b/test/suites.ts new file mode 100644 index 0000000..7fc2216 --- /dev/null +++ b/test/suites.ts @@ -0,0 +1,35 @@ +interface TestSuite { + name: string; + header: string; + global?: boolean; + polyfill?: boolean; + context?: any; +} + +export const suites: TestSuite[] = [ + { + name: "Reflect.js", + header: `require("../Reflect");`, + global: true, + }, + { + name: "Reflect.js (w/polyfill)", + header: `require("../Reflect");`, + global: true, + polyfill: true, + context: { + Map: {}, + Set: {}, + WeakMap: {}, + } + }, + { + name: "ReflectLite.js", + header: `require("../ReflectLite");`, + global: true, + }, + { + name: "ReflectNoConflict.js", + header: `const Reflect = require("../ReflectNoConflict");`, + }, +]; \ No newline at end of file diff --git a/test/tsconfig.json b/test/tsconfig.json index 53e30f3..609ebe8 100644 --- a/test/tsconfig.json +++ b/test/tsconfig.json @@ -5,8 +5,7 @@ "sourceMap": true, "module": "commonjs", "types": ["node", "mocha"], - "typeRoots": ["../../node_modules/@types"], - "rootDir": "." + "typeRoots": ["../node_modules/@types"], }, "include": [ "**/*.ts" diff --git a/test/vm.ts b/test/vm.ts new file mode 100644 index 0000000..3da0677 --- /dev/null +++ b/test/vm.ts @@ -0,0 +1,61 @@ +/// +import vm = require("vm"); +import mod = require("module"); +import fs = require("fs"); +import path = require("path"); + +class Loader { + cache = new Map(); + context: vm.Context; + constructor(context: vm.Context = { }) { + this.context = vm.createContext(context); + } + load(id: string, filename = id, code?: string): any { + let module = this.cache.get(id); + if (module) return module.exports; + module = { + id, + filename, + exports: {}, + require: undefined, + loaded: false, + paths: undefined!, + children: undefined!, + parent: undefined! + }; + this.cache.set(id, module); + if (mod.builtinModules.indexOf(id) >= 0) { + module.exports = require(id); + module.loaded = true; + } + else { + module.require = this.createRequire(filename); + if (code === undefined) { + code = fs.readFileSync(filename, { encoding: "utf8" }); + } + code = `(function(module, exports, require, __filename, __dirname) {${code}\n})`; + const func = vm.runInContext(code, this.context, { filename }); + func(module, module.exports, module.require, filename, path.dirname(filename)); + module.loaded = true; + } + return module.exports; + } + createRequire(filename: string) { + const req = mod.createRequireFromPath(filename) as NodeRequire; + const require = ((id: string) => this.load(mod.builtinModules.indexOf(id) >= 0 ? id : req.resolve(id))) as NodeRequire; + require.resolve = req.resolve; + return require; + } +} + +export function script(context: vm.Context = {}): (array: TemplateStringsArray, ...args: any[]) => any{ + return (array, ...args) => { + let code = array[0]; + for (let i = 1; i < array.length; i++) { + code += args[i - 1]; + code += array[i]; + } + const loader = new Loader(context); + return loader.load("test.js", __filename, code); + }; +} diff --git a/tsconfig-release.json b/tsconfig-release.json index 733808d..a99678e 100644 --- a/tsconfig-release.json +++ b/tsconfig-release.json @@ -1,17 +1,20 @@ { - "compilerOptions": { - "target": "es5", - "module": "commonjs", - "newLine": "LF", - "sourceMap": false, - "strictNullChecks": true, - "noImplicitAny": true, - "noUnusedLocals": true, - "noUnusedParameters": true, - "noImplicitReturns": true, - "noImplicitThis": true - }, - "files": [ - "Reflect.ts" - ] - } \ No newline at end of file + "compilerOptions": { + "target": "es5", + "module": "commonjs", + "newLine": "LF", + "sourceMap": false, + "strictNullChecks": true, + "noImplicitAny": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "noImplicitThis": true + }, + "files": [ + "globals.d.ts", + "Reflect.ts", + "ReflectLite.ts", + "ReflectNoConflict.ts", + ] +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 30ef258..b636e9b 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,6 +12,9 @@ "noImplicitThis": true }, "files": [ - "Reflect.ts" + "globals.d.ts", + "Reflect.ts", + "ReflectLite.ts", + "ReflectNoConflict.ts", ] } \ No newline at end of file diff --git a/typings.d.ts b/typings.d.ts deleted file mode 100644 index d19ee2c..0000000 --- a/typings.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -/*! ***************************************************************************** -Copyright (C) Microsoft. All rights reserved. -Licensed under the Apache License, Version 2.0 (the "License"); you may not use -this file except in compliance with the License. You may obtain a copy of the -License at http://www.apache.org/licenses/LICENSE-2.0 - -THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED -WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, -MERCHANTABLITY OR NON-INFRINGEMENT. - -See the Apache Version 2.0 License for specific language governing permissions -and limitations under the License. -***************************************************************************** */ - -// NOTE: This file is obsolete and may be removed in a later release. -// For CommonJS/AMD/UMD/SystemJS declarations please use 'index.d.ts'. -// For standalone browser declarations, please use 'standalone.d.ts'. - -import "./reflect-metadata"; \ No newline at end of file