ReflectNoConflict.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. "use strict";
  2. /*! *****************************************************************************
  3. Copyright (C) Microsoft. All rights reserved.
  4. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  5. this file except in compliance with the License. You may obtain a copy of the
  6. License at http://www.apache.org/licenses/LICENSE-2.0
  7. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  8. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  9. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  10. MERCHANTABLITY OR NON-INFRINGEMENT.
  11. See the Apache Version 2.0 License for specific language governing permissions
  12. and limitations under the License.
  13. ***************************************************************************** */
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. // feature test for Symbol support
  16. var supportsSymbol = typeof Symbol === "function";
  17. var toPrimitiveSymbol = supportsSymbol && typeof Symbol.toPrimitive !== "undefined" ? Symbol.toPrimitive : fail("Symbol.toPrimitive not found.");
  18. var iteratorSymbol = supportsSymbol && typeof Symbol.iterator !== "undefined" ? Symbol.iterator : fail("Symbol.iterator not found.");
  19. // Load global or shim versions of Map, Set, and WeakMap
  20. var functionPrototype = Object.getPrototypeOf(Function);
  21. var _Map = typeof Map === "function" && typeof Map.prototype.entries === "function" ? Map : fail("A valid Map constructor could not be found.");
  22. var _Set = typeof Set === "function" && typeof Set.prototype.entries === "function" ? Set : fail("A valid Set constructor could not be found.");
  23. var _WeakMap = typeof WeakMap === "function" ? WeakMap : fail("A valid WeakMap constructor could not be found.");
  24. var registrySymbol = supportsSymbol ? Symbol.for("@reflect-metadata:registry") : undefined;
  25. var metadataRegistry = GetOrCreateMetadataRegistry();
  26. var metadataProvider = CreateMetadataProvider(metadataRegistry);
  27. /**
  28. * Applies a set of decorators to a property of a target object.
  29. * @param decorators An array of decorators.
  30. * @param target The target object.
  31. * @param propertyKey (Optional) The property key to decorate.
  32. * @param attributes (Optional) The property descriptor for the target key.
  33. * @remarks Decorators are applied in reverse order.
  34. * @example
  35. *
  36. * class Example {
  37. * // property declarations are not part of ES6, though they are valid in TypeScript:
  38. * // static staticProperty;
  39. * // property;
  40. *
  41. * constructor(p) { }
  42. * static staticMethod(p) { }
  43. * method(p) { }
  44. * }
  45. *
  46. * // constructor
  47. * Example = Reflect.decorate(decoratorsArray, Example);
  48. *
  49. * // property (on constructor)
  50. * Reflect.decorate(decoratorsArray, Example, "staticProperty");
  51. *
  52. * // property (on prototype)
  53. * Reflect.decorate(decoratorsArray, Example.prototype, "property");
  54. *
  55. * // method (on constructor)
  56. * Object.defineProperty(Example, "staticMethod",
  57. * Reflect.decorate(decoratorsArray, Example, "staticMethod",
  58. * Object.getOwnPropertyDescriptor(Example, "staticMethod")));
  59. *
  60. * // method (on prototype)
  61. * Object.defineProperty(Example.prototype, "method",
  62. * Reflect.decorate(decoratorsArray, Example.prototype, "method",
  63. * Object.getOwnPropertyDescriptor(Example.prototype, "method")));
  64. *
  65. */
  66. function decorate(decorators, target, propertyKey, attributes) {
  67. if (!IsUndefined(propertyKey)) {
  68. if (!IsArray(decorators))
  69. throw new TypeError();
  70. if (!IsObject(target))
  71. throw new TypeError();
  72. if (!IsObject(attributes) && !IsUndefined(attributes) && !IsNull(attributes))
  73. throw new TypeError();
  74. if (IsNull(attributes))
  75. attributes = undefined;
  76. propertyKey = ToPropertyKey(propertyKey);
  77. return DecorateProperty(decorators, target, propertyKey, attributes);
  78. }
  79. else {
  80. if (!IsArray(decorators))
  81. throw new TypeError();
  82. if (!IsConstructor(target))
  83. throw new TypeError();
  84. return DecorateConstructor(decorators, target);
  85. }
  86. }
  87. exports.decorate = decorate;
  88. // 4.1.2 Reflect.metadata(metadataKey, metadataValue)
  89. // https://rbuckton.github.io/reflect-metadata/#reflect.metadata
  90. /**
  91. * A default metadata decorator factory that can be used on a class, class member, or parameter.
  92. * @param metadataKey The key for the metadata entry.
  93. * @param metadataValue The value for the metadata entry.
  94. * @returns A decorator function.
  95. * @remarks
  96. * If `metadataKey` is already defined for the target and target key, the
  97. * metadataValue for that key will be overwritten.
  98. * @example
  99. *
  100. * // constructor
  101. * @Reflect.metadata(key, value)
  102. * class Example {
  103. * }
  104. *
  105. * // property (on constructor, TypeScript only)
  106. * class Example {
  107. * @Reflect.metadata(key, value)
  108. * static staticProperty;
  109. * }
  110. *
  111. * // property (on prototype, TypeScript only)
  112. * class Example {
  113. * @Reflect.metadata(key, value)
  114. * property;
  115. * }
  116. *
  117. * // method (on constructor)
  118. * class Example {
  119. * @Reflect.metadata(key, value)
  120. * static staticMethod() { }
  121. * }
  122. *
  123. * // method (on prototype)
  124. * class Example {
  125. * @Reflect.metadata(key, value)
  126. * method() { }
  127. * }
  128. *
  129. */
  130. function metadata(metadataKey, metadataValue) {
  131. if (typeof Reflect !== "undefined" && typeof Reflect.metadata === "function" && Reflect.metadata !== metadata) {
  132. return Reflect.metadata(metadataKey, metadataValue);
  133. }
  134. function decorator(target, propertyKey) {
  135. if (!IsObject(target))
  136. throw new TypeError();
  137. if (!IsUndefined(propertyKey) && !IsPropertyKey(propertyKey))
  138. throw new TypeError();
  139. OrdinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey);
  140. }
  141. return decorator;
  142. }
  143. exports.metadata = metadata;
  144. /**
  145. * Define a unique metadata entry on the target.
  146. * @param metadataKey A key used to store and retrieve metadata.
  147. * @param metadataValue A value that contains attached metadata.
  148. * @param target The target object on which to define metadata.
  149. * @param propertyKey (Optional) The property key for the target.
  150. * @example
  151. *
  152. * class Example {
  153. * // property declarations are not part of ES6, though they are valid in TypeScript:
  154. * // static staticProperty;
  155. * // property;
  156. *
  157. * constructor(p) { }
  158. * static staticMethod(p) { }
  159. * method(p) { }
  160. * }
  161. *
  162. * // constructor
  163. * Reflect.defineMetadata("custom:annotation", options, Example);
  164. *
  165. * // property (on constructor)
  166. * Reflect.defineMetadata("custom:annotation", options, Example, "staticProperty");
  167. *
  168. * // property (on prototype)
  169. * Reflect.defineMetadata("custom:annotation", options, Example.prototype, "property");
  170. *
  171. * // method (on constructor)
  172. * Reflect.defineMetadata("custom:annotation", options, Example, "staticMethod");
  173. *
  174. * // method (on prototype)
  175. * Reflect.defineMetadata("custom:annotation", options, Example.prototype, "method");
  176. *
  177. * // decorator factory as metadata-producing annotation.
  178. * function MyAnnotation(options): Decorator {
  179. * return (target, key?) => Reflect.defineMetadata("custom:annotation", options, target, key);
  180. * }
  181. *
  182. */
  183. function defineMetadata(metadataKey, metadataValue, target, propertyKey) {
  184. if (!IsObject(target))
  185. throw new TypeError();
  186. if (!IsUndefined(propertyKey))
  187. propertyKey = ToPropertyKey(propertyKey);
  188. return OrdinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey);
  189. }
  190. exports.defineMetadata = defineMetadata;
  191. /**
  192. * Gets a value indicating whether the target object or its prototype chain has the provided metadata key defined.
  193. * @param metadataKey A key used to store and retrieve metadata.
  194. * @param target The target object on which the metadata is defined.
  195. * @param propertyKey (Optional) The property key for the target.
  196. * @returns `true` if the metadata key was defined on the target object or its prototype chain; otherwise, `false`.
  197. * @example
  198. *
  199. * class Example {
  200. * // property declarations are not part of ES6, though they are valid in TypeScript:
  201. * // static staticProperty;
  202. * // property;
  203. *
  204. * constructor(p) { }
  205. * static staticMethod(p) { }
  206. * method(p) { }
  207. * }
  208. *
  209. * // constructor
  210. * result = Reflect.hasMetadata("custom:annotation", Example);
  211. *
  212. * // property (on constructor)
  213. * result = Reflect.hasMetadata("custom:annotation", Example, "staticProperty");
  214. *
  215. * // property (on prototype)
  216. * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "property");
  217. *
  218. * // method (on constructor)
  219. * result = Reflect.hasMetadata("custom:annotation", Example, "staticMethod");
  220. *
  221. * // method (on prototype)
  222. * result = Reflect.hasMetadata("custom:annotation", Example.prototype, "method");
  223. *
  224. */
  225. function hasMetadata(metadataKey, target, propertyKey) {
  226. if (!IsObject(target))
  227. throw new TypeError();
  228. if (!IsUndefined(propertyKey))
  229. propertyKey = ToPropertyKey(propertyKey);
  230. return OrdinaryHasMetadata(metadataKey, target, propertyKey);
  231. }
  232. exports.hasMetadata = hasMetadata;
  233. /**
  234. * Gets a value indicating whether the target object has the provided metadata key defined.
  235. * @param metadataKey A key used to store and retrieve metadata.
  236. * @param target The target object on which the metadata is defined.
  237. * @param propertyKey (Optional) The property key for the target.
  238. * @returns `true` if the metadata key was defined on the target object; otherwise, `false`.
  239. * @example
  240. *
  241. * class Example {
  242. * // property declarations are not part of ES6, though they are valid in TypeScript:
  243. * // static staticProperty;
  244. * // property;
  245. *
  246. * constructor(p) { }
  247. * static staticMethod(p) { }
  248. * method(p) { }
  249. * }
  250. *
  251. * // constructor
  252. * result = Reflect.hasOwnMetadata("custom:annotation", Example);
  253. *
  254. * // property (on constructor)
  255. * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticProperty");
  256. *
  257. * // property (on prototype)
  258. * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "property");
  259. *
  260. * // method (on constructor)
  261. * result = Reflect.hasOwnMetadata("custom:annotation", Example, "staticMethod");
  262. *
  263. * // method (on prototype)
  264. * result = Reflect.hasOwnMetadata("custom:annotation", Example.prototype, "method");
  265. *
  266. */
  267. function hasOwnMetadata(metadataKey, target, propertyKey) {
  268. if (!IsObject(target))
  269. throw new TypeError();
  270. if (!IsUndefined(propertyKey))
  271. propertyKey = ToPropertyKey(propertyKey);
  272. return OrdinaryHasOwnMetadata(metadataKey, target, propertyKey);
  273. }
  274. exports.hasOwnMetadata = hasOwnMetadata;
  275. /**
  276. * Gets the metadata value for the provided metadata key on the target object or its prototype chain.
  277. * @param metadataKey A key used to store and retrieve metadata.
  278. * @param target The target object on which the metadata is defined.
  279. * @param propertyKey (Optional) The property key for the target.
  280. * @returns The metadata value for the metadata key if found; otherwise, `undefined`.
  281. * @example
  282. *
  283. * class Example {
  284. * // property declarations are not part of ES6, though they are valid in TypeScript:
  285. * // static staticProperty;
  286. * // property;
  287. *
  288. * constructor(p) { }
  289. * static staticMethod(p) { }
  290. * method(p) { }
  291. * }
  292. *
  293. * // constructor
  294. * result = Reflect.getMetadata("custom:annotation", Example);
  295. *
  296. * // property (on constructor)
  297. * result = Reflect.getMetadata("custom:annotation", Example, "staticProperty");
  298. *
  299. * // property (on prototype)
  300. * result = Reflect.getMetadata("custom:annotation", Example.prototype, "property");
  301. *
  302. * // method (on constructor)
  303. * result = Reflect.getMetadata("custom:annotation", Example, "staticMethod");
  304. *
  305. * // method (on prototype)
  306. * result = Reflect.getMetadata("custom:annotation", Example.prototype, "method");
  307. *
  308. */
  309. function getMetadata(metadataKey, target, propertyKey) {
  310. if (!IsObject(target))
  311. throw new TypeError();
  312. if (!IsUndefined(propertyKey))
  313. propertyKey = ToPropertyKey(propertyKey);
  314. return OrdinaryGetMetadata(metadataKey, target, propertyKey);
  315. }
  316. exports.getMetadata = getMetadata;
  317. /**
  318. * Gets the metadata value for the provided metadata key on the target object.
  319. * @param metadataKey A key used to store and retrieve metadata.
  320. * @param target The target object on which the metadata is defined.
  321. * @param propertyKey (Optional) The property key for the target.
  322. * @returns The metadata value for the metadata key if found; otherwise, `undefined`.
  323. * @example
  324. *
  325. * class Example {
  326. * // property declarations are not part of ES6, though they are valid in TypeScript:
  327. * // static staticProperty;
  328. * // property;
  329. *
  330. * constructor(p) { }
  331. * static staticMethod(p) { }
  332. * method(p) { }
  333. * }
  334. *
  335. * // constructor
  336. * result = Reflect.getOwnMetadata("custom:annotation", Example);
  337. *
  338. * // property (on constructor)
  339. * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticProperty");
  340. *
  341. * // property (on prototype)
  342. * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "property");
  343. *
  344. * // method (on constructor)
  345. * result = Reflect.getOwnMetadata("custom:annotation", Example, "staticMethod");
  346. *
  347. * // method (on prototype)
  348. * result = Reflect.getOwnMetadata("custom:annotation", Example.prototype, "method");
  349. *
  350. */
  351. function getOwnMetadata(metadataKey, target, propertyKey) {
  352. if (!IsObject(target))
  353. throw new TypeError();
  354. if (!IsUndefined(propertyKey))
  355. propertyKey = ToPropertyKey(propertyKey);
  356. return OrdinaryGetOwnMetadata(metadataKey, target, propertyKey);
  357. }
  358. exports.getOwnMetadata = getOwnMetadata;
  359. /**
  360. * Gets the metadata keys defined on the target object or its prototype chain.
  361. * @param target The target object on which the metadata is defined.
  362. * @param propertyKey (Optional) The property key for the target.
  363. * @returns An array of unique metadata keys.
  364. * @example
  365. *
  366. * class Example {
  367. * // property declarations are not part of ES6, though they are valid in TypeScript:
  368. * // static staticProperty;
  369. * // property;
  370. *
  371. * constructor(p) { }
  372. * static staticMethod(p) { }
  373. * method(p) { }
  374. * }
  375. *
  376. * // constructor
  377. * result = Reflect.getMetadataKeys(Example);
  378. *
  379. * // property (on constructor)
  380. * result = Reflect.getMetadataKeys(Example, "staticProperty");
  381. *
  382. * // property (on prototype)
  383. * result = Reflect.getMetadataKeys(Example.prototype, "property");
  384. *
  385. * // method (on constructor)
  386. * result = Reflect.getMetadataKeys(Example, "staticMethod");
  387. *
  388. * // method (on prototype)
  389. * result = Reflect.getMetadataKeys(Example.prototype, "method");
  390. *
  391. */
  392. function getMetadataKeys(target, propertyKey) {
  393. if (!IsObject(target))
  394. throw new TypeError();
  395. if (!IsUndefined(propertyKey))
  396. propertyKey = ToPropertyKey(propertyKey);
  397. return OrdinaryMetadataKeys(target, propertyKey);
  398. }
  399. exports.getMetadataKeys = getMetadataKeys;
  400. /**
  401. * Gets the unique metadata keys defined on the target object.
  402. * @param target The target object on which the metadata is defined.
  403. * @param propertyKey (Optional) The property key for the target.
  404. * @returns An array of unique metadata keys.
  405. * @example
  406. *
  407. * class Example {
  408. * // property declarations are not part of ES6, though they are valid in TypeScript:
  409. * // static staticProperty;
  410. * // property;
  411. *
  412. * constructor(p) { }
  413. * static staticMethod(p) { }
  414. * method(p) { }
  415. * }
  416. *
  417. * // constructor
  418. * result = Reflect.getOwnMetadataKeys(Example);
  419. *
  420. * // property (on constructor)
  421. * result = Reflect.getOwnMetadataKeys(Example, "staticProperty");
  422. *
  423. * // property (on prototype)
  424. * result = Reflect.getOwnMetadataKeys(Example.prototype, "property");
  425. *
  426. * // method (on constructor)
  427. * result = Reflect.getOwnMetadataKeys(Example, "staticMethod");
  428. *
  429. * // method (on prototype)
  430. * result = Reflect.getOwnMetadataKeys(Example.prototype, "method");
  431. *
  432. */
  433. function getOwnMetadataKeys(target, propertyKey) {
  434. if (!IsObject(target))
  435. throw new TypeError();
  436. if (!IsUndefined(propertyKey))
  437. propertyKey = ToPropertyKey(propertyKey);
  438. return OrdinaryOwnMetadataKeys(target, propertyKey);
  439. }
  440. exports.getOwnMetadataKeys = getOwnMetadataKeys;
  441. /**
  442. * Deletes the metadata entry from the target object with the provided key.
  443. * @param metadataKey A key used to store and retrieve metadata.
  444. * @param target The target object on which the metadata is defined.
  445. * @param propertyKey (Optional) The property key for the target.
  446. * @returns `true` if the metadata entry was found and deleted; otherwise, false.
  447. * @example
  448. *
  449. * class Example {
  450. * // property declarations are not part of ES6, though they are valid in TypeScript:
  451. * // static staticProperty;
  452. * // property;
  453. *
  454. * constructor(p) { }
  455. * static staticMethod(p) { }
  456. * method(p) { }
  457. * }
  458. *
  459. * // constructor
  460. * result = Reflect.deleteMetadata("custom:annotation", Example);
  461. *
  462. * // property (on constructor)
  463. * result = Reflect.deleteMetadata("custom:annotation", Example, "staticProperty");
  464. *
  465. * // property (on prototype)
  466. * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "property");
  467. *
  468. * // method (on constructor)
  469. * result = Reflect.deleteMetadata("custom:annotation", Example, "staticMethod");
  470. *
  471. * // method (on prototype)
  472. * result = Reflect.deleteMetadata("custom:annotation", Example.prototype, "method");
  473. *
  474. */
  475. function deleteMetadata(metadataKey, target, propertyKey) {
  476. if (!IsObject(target))
  477. throw new TypeError();
  478. if (!IsUndefined(propertyKey))
  479. propertyKey = ToPropertyKey(propertyKey);
  480. var provider = GetMetadataProvider(target, propertyKey, /*Create*/ false);
  481. if (IsUndefined(provider))
  482. return false;
  483. return provider.OrdinaryDeleteMetadata(metadataKey, target, propertyKey);
  484. }
  485. exports.deleteMetadata = deleteMetadata;
  486. function DecorateConstructor(decorators, target) {
  487. for (var i = decorators.length - 1; i >= 0; --i) {
  488. var decorator = decorators[i];
  489. var decorated = decorator(target);
  490. if (!IsUndefined(decorated) && !IsNull(decorated)) {
  491. if (!IsConstructor(decorated))
  492. throw new TypeError();
  493. target = decorated;
  494. }
  495. }
  496. return target;
  497. }
  498. function DecorateProperty(decorators, target, propertyKey, descriptor) {
  499. for (var i = decorators.length - 1; i >= 0; --i) {
  500. var decorator = decorators[i];
  501. var decorated = decorator(target, propertyKey, descriptor);
  502. if (!IsUndefined(decorated) && !IsNull(decorated)) {
  503. if (!IsObject(decorated))
  504. throw new TypeError();
  505. descriptor = decorated;
  506. }
  507. }
  508. return descriptor;
  509. }
  510. // 3.1.1.1 OrdinaryHasMetadata(MetadataKey, O, P)
  511. // https://rbuckton.github.io/reflect-metadata/#ordinaryhasmetadata
  512. function OrdinaryHasMetadata(MetadataKey, O, P) {
  513. var hasOwn = OrdinaryHasOwnMetadata(MetadataKey, O, P);
  514. if (hasOwn)
  515. return true;
  516. var parent = OrdinaryGetPrototypeOf(O);
  517. if (!IsNull(parent))
  518. return OrdinaryHasMetadata(MetadataKey, parent, P);
  519. return false;
  520. }
  521. // 3.1.2.1 OrdinaryHasOwnMetadata(MetadataKey, O, P)
  522. // https://rbuckton.github.io/reflect-metadata/#ordinaryhasownmetadata
  523. function OrdinaryHasOwnMetadata(MetadataKey, O, P) {
  524. var provider = GetMetadataProvider(O, P, /*Create*/ false);
  525. if (IsUndefined(provider))
  526. return false;
  527. return ToBoolean(provider.OrdinaryHasOwnMetadata(MetadataKey, O, P));
  528. }
  529. // 3.1.3.1 OrdinaryGetMetadata(MetadataKey, O, P)
  530. // https://rbuckton.github.io/reflect-metadata/#ordinarygetmetadata
  531. function OrdinaryGetMetadata(MetadataKey, O, P) {
  532. var hasOwn = OrdinaryHasOwnMetadata(MetadataKey, O, P);
  533. if (hasOwn)
  534. return OrdinaryGetOwnMetadata(MetadataKey, O, P);
  535. var parent = OrdinaryGetPrototypeOf(O);
  536. if (!IsNull(parent))
  537. return OrdinaryGetMetadata(MetadataKey, parent, P);
  538. return undefined;
  539. }
  540. // 3.1.4.1 OrdinaryGetOwnMetadata(MetadataKey, O, P)
  541. // https://rbuckton.github.io/reflect-metadata/#ordinarygetownmetadata
  542. function OrdinaryGetOwnMetadata(MetadataKey, O, P) {
  543. var provider = GetMetadataProvider(O, P, /*Create*/ false);
  544. if (IsUndefined(provider))
  545. return;
  546. return provider.OrdinaryGetOwnMetadata(MetadataKey, O, P);
  547. }
  548. // 3.1.5.1 OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P)
  549. // https://rbuckton.github.io/reflect-metadata/#ordinarydefineownmetadata
  550. function OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P) {
  551. var provider = GetMetadataProvider(O, P, /*Create*/ true);
  552. provider.OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P);
  553. }
  554. // 3.1.6.1 OrdinaryMetadataKeys(O, P)
  555. // https://rbuckton.github.io/reflect-metadata/#ordinarymetadatakeys
  556. function OrdinaryMetadataKeys(O, P) {
  557. var ownKeys = OrdinaryOwnMetadataKeys(O, P);
  558. var parent = OrdinaryGetPrototypeOf(O);
  559. if (parent === null)
  560. return ownKeys;
  561. var parentKeys = OrdinaryMetadataKeys(parent, P);
  562. if (parentKeys.length <= 0)
  563. return ownKeys;
  564. if (ownKeys.length <= 0)
  565. return parentKeys;
  566. var set = new _Set();
  567. var keys = [];
  568. for (var _i = 0, ownKeys_1 = ownKeys; _i < ownKeys_1.length; _i++) {
  569. var key = ownKeys_1[_i];
  570. var hasKey = set.has(key);
  571. if (!hasKey) {
  572. set.add(key);
  573. keys.push(key);
  574. }
  575. }
  576. for (var _a = 0, parentKeys_1 = parentKeys; _a < parentKeys_1.length; _a++) {
  577. var key = parentKeys_1[_a];
  578. var hasKey = set.has(key);
  579. if (!hasKey) {
  580. set.add(key);
  581. keys.push(key);
  582. }
  583. }
  584. return keys;
  585. }
  586. // 3.1.7.1 OrdinaryOwnMetadataKeys(O, P)
  587. // https://rbuckton.github.io/reflect-metadata/#ordinaryownmetadatakeys
  588. function OrdinaryOwnMetadataKeys(O, P) {
  589. var provider = GetMetadataProvider(O, P, /*create*/ false);
  590. if (!provider) {
  591. return [];
  592. }
  593. return provider.OrdinaryOwnMetadataKeys(O, P);
  594. }
  595. // 6 ECMAScript Data Types and Values
  596. // https://tc39.github.io/ecma262/#sec-ecmascript-data-types-and-values
  597. function Type(x) {
  598. if (x === null)
  599. return 1 /* Null */;
  600. switch (typeof x) {
  601. case "undefined": return 0 /* Undefined */;
  602. case "boolean": return 2 /* Boolean */;
  603. case "string": return 3 /* String */;
  604. case "symbol": return 4 /* Symbol */;
  605. case "number": return 5 /* Number */;
  606. case "object": return x === null ? 1 /* Null */ : 6 /* Object */;
  607. default: return 6 /* Object */;
  608. }
  609. }
  610. // 6.1.1 The Undefined Type
  611. // https://tc39.github.io/ecma262/#sec-ecmascript-language-types-undefined-type
  612. function IsUndefined(x) {
  613. return x === undefined;
  614. }
  615. // 6.1.2 The Null Type
  616. // https://tc39.github.io/ecma262/#sec-ecmascript-language-types-null-type
  617. function IsNull(x) {
  618. return x === null;
  619. }
  620. // 6.1.5 The Symbol Type
  621. // https://tc39.github.io/ecma262/#sec-ecmascript-language-types-symbol-type
  622. function IsSymbol(x) {
  623. return typeof x === "symbol";
  624. }
  625. // 6.1.7 The Object Type
  626. // https://tc39.github.io/ecma262/#sec-object-type
  627. function IsObject(x) {
  628. return typeof x === "object" ? x !== null : typeof x === "function";
  629. }
  630. // 7.1 Type Conversion
  631. // https://tc39.github.io/ecma262/#sec-type-conversion
  632. // 7.1.1 ToPrimitive(input [, PreferredType])
  633. // https://tc39.github.io/ecma262/#sec-toprimitive
  634. function ToPrimitive(input, PreferredType) {
  635. switch (Type(input)) {
  636. case 0 /* Undefined */: return input;
  637. case 1 /* Null */: return input;
  638. case 2 /* Boolean */: return input;
  639. case 3 /* String */: return input;
  640. case 4 /* Symbol */: return input;
  641. case 5 /* Number */: return input;
  642. }
  643. var hint = PreferredType === 3 /* String */ ? "string" : PreferredType === 5 /* Number */ ? "number" : "default";
  644. var exoticToPrim = GetMethod(input, toPrimitiveSymbol);
  645. if (exoticToPrim !== undefined) {
  646. var result = exoticToPrim.call(input, hint);
  647. if (IsObject(result))
  648. throw new TypeError();
  649. return result;
  650. }
  651. return OrdinaryToPrimitive(input, hint === "default" ? "number" : hint);
  652. }
  653. // 7.1.1.1 OrdinaryToPrimitive(O, hint)
  654. // https://tc39.github.io/ecma262/#sec-ordinarytoprimitive
  655. function OrdinaryToPrimitive(O, hint) {
  656. if (hint === "string") {
  657. var toString_1 = O.toString;
  658. if (IsCallable(toString_1)) {
  659. var result = toString_1.call(O);
  660. if (!IsObject(result))
  661. return result;
  662. }
  663. var valueOf = O.valueOf;
  664. if (IsCallable(valueOf)) {
  665. var result = valueOf.call(O);
  666. if (!IsObject(result))
  667. return result;
  668. }
  669. }
  670. else {
  671. var valueOf = O.valueOf;
  672. if (IsCallable(valueOf)) {
  673. var result = valueOf.call(O);
  674. if (!IsObject(result))
  675. return result;
  676. }
  677. var toString_2 = O.toString;
  678. if (IsCallable(toString_2)) {
  679. var result = toString_2.call(O);
  680. if (!IsObject(result))
  681. return result;
  682. }
  683. }
  684. throw new TypeError();
  685. }
  686. // 7.1.2 ToBoolean(argument)
  687. // https://tc39.github.io/ecma262/2016/#sec-toboolean
  688. function ToBoolean(argument) {
  689. return !!argument;
  690. }
  691. // 7.1.12 ToString(argument)
  692. // https://tc39.github.io/ecma262/#sec-tostring
  693. function ToString(argument) {
  694. return "" + argument;
  695. }
  696. // 7.1.14 ToPropertyKey(argument)
  697. // https://tc39.github.io/ecma262/#sec-topropertykey
  698. function ToPropertyKey(argument) {
  699. var key = ToPrimitive(argument, 3 /* String */);
  700. if (IsSymbol(key))
  701. return key;
  702. return ToString(key);
  703. }
  704. // 7.2 Testing and Comparison Operations
  705. // https://tc39.github.io/ecma262/#sec-testing-and-comparison-operations
  706. // 7.2.2 IsArray(argument)
  707. // https://tc39.github.io/ecma262/#sec-isarray
  708. function IsArray(argument) {
  709. return Array.isArray
  710. ? Array.isArray(argument)
  711. : argument instanceof Object
  712. ? argument instanceof Array
  713. : Object.prototype.toString.call(argument) === "[object Array]";
  714. }
  715. // 7.2.3 IsCallable(argument)
  716. // https://tc39.github.io/ecma262/#sec-iscallable
  717. function IsCallable(argument) {
  718. // NOTE: This is an approximation as we cannot check for [[Call]] internal method.
  719. return typeof argument === "function";
  720. }
  721. // 7.2.4 IsConstructor(argument)
  722. // https://tc39.github.io/ecma262/#sec-isconstructor
  723. function IsConstructor(argument) {
  724. // NOTE: This is an approximation as we cannot check for [[Construct]] internal method.
  725. return typeof argument === "function";
  726. }
  727. // 7.2.7 IsPropertyKey(argument)
  728. // https://tc39.github.io/ecma262/#sec-ispropertykey
  729. function IsPropertyKey(argument) {
  730. switch (Type(argument)) {
  731. case 3 /* String */: return true;
  732. case 4 /* Symbol */: return true;
  733. default: return false;
  734. }
  735. }
  736. // 7.3 Operations on Objects
  737. // https://tc39.github.io/ecma262/#sec-operations-on-objects
  738. // 7.3.9 GetMethod(V, P)
  739. // https://tc39.github.io/ecma262/#sec-getmethod
  740. function GetMethod(V, P) {
  741. var func = V[P];
  742. if (func === undefined || func === null)
  743. return undefined;
  744. if (!IsCallable(func))
  745. throw new TypeError();
  746. return func;
  747. }
  748. // 7.4 Operations on Iterator Objects
  749. // https://tc39.github.io/ecma262/#sec-operations-on-iterator-objects
  750. function GetIterator(obj) {
  751. var method = GetMethod(obj, iteratorSymbol);
  752. if (!IsCallable(method))
  753. throw new TypeError(); // from Call
  754. var iterator = method.call(obj);
  755. if (!IsObject(iterator))
  756. throw new TypeError();
  757. return iterator;
  758. }
  759. // 7.4.4 IteratorValue(iterResult)
  760. // https://tc39.github.io/ecma262/2016/#sec-iteratorvalue
  761. function IteratorValue(iterResult) {
  762. return iterResult.value;
  763. }
  764. // 7.4.5 IteratorStep(iterator)
  765. // https://tc39.github.io/ecma262/#sec-iteratorstep
  766. function IteratorStep(iterator) {
  767. var result = iterator.next();
  768. return result.done ? false : result;
  769. }
  770. // 7.4.6 IteratorClose(iterator, completion)
  771. // https://tc39.github.io/ecma262/#sec-iteratorclose
  772. function IteratorClose(iterator) {
  773. var f = iterator["return"];
  774. if (f)
  775. f.call(iterator);
  776. }
  777. // 9.1 Ordinary Object Internal Methods and Internal Slots
  778. // https://tc39.github.io/ecma262/#sec-ordinary-object-internal-methods-and-internal-slots
  779. // 9.1.1.1 OrdinaryGetPrototypeOf(O)
  780. // https://tc39.github.io/ecma262/#sec-ordinarygetprototypeof
  781. function OrdinaryGetPrototypeOf(O) {
  782. var proto = Object.getPrototypeOf(O);
  783. if (typeof O !== "function" || O === functionPrototype)
  784. return proto;
  785. // TypeScript doesn't set __proto__ in ES5, as it's non-standard.
  786. // Try to determine the superclass constructor. Compatible implementations
  787. // must either set __proto__ on a subclass constructor to the superclass constructor,
  788. // or ensure each class has a valid `constructor` property on its prototype that
  789. // points back to the constructor.
  790. // If this is not the same as Function.[[Prototype]], then this is definately inherited.
  791. // This is the case when in ES6 or when using __proto__ in a compatible browser.
  792. if (proto !== functionPrototype)
  793. return proto;
  794. // If the super prototype is Object.prototype, null, or undefined, then we cannot determine the heritage.
  795. var prototype = O.prototype;
  796. var prototypeProto = prototype && Object.getPrototypeOf(prototype);
  797. if (prototypeProto == null || prototypeProto === Object.prototype)
  798. return proto;
  799. // If the constructor was not a function, then we cannot determine the heritage.
  800. var constructor = prototypeProto.constructor;
  801. if (typeof constructor !== "function")
  802. return proto;
  803. // If we have some kind of self-reference, then we cannot determine the heritage.
  804. if (constructor === O)
  805. return proto;
  806. // we have a pretty good guess at the heritage.
  807. return constructor;
  808. }
  809. function fail(e) {
  810. throw e;
  811. }
  812. // Global metadata registry
  813. // - Allows `import "reflect-metadata"` and `import "reflect-metadata/no-conflict"` to interoperate.
  814. // - Uses isolated metadata if `Reflect` is frozen before the registry can be installed.
  815. /**
  816. * Creates a registry used to allow multiple `reflect-metadata` providers.
  817. */
  818. function CreateMetadataRegistry() {
  819. var fallback;
  820. if (!IsUndefined(registrySymbol) &&
  821. typeof Reflect !== "undefined" &&
  822. !(registrySymbol in Reflect) &&
  823. typeof Reflect.defineMetadata === "function") {
  824. // interoperate with older version of `reflect-metadata` that did not support a registry.
  825. fallback = CreateFallbackProvider(Reflect);
  826. }
  827. var first;
  828. var second;
  829. var rest;
  830. var targetProviderMap = new _WeakMap();
  831. var registry = {
  832. registerProvider: registerProvider,
  833. getProvider: getProvider,
  834. setProvider: setProvider,
  835. };
  836. return registry;
  837. function registerProvider(provider) {
  838. if (!Object.isExtensible(registry)) {
  839. throw new Error("Cannot add provider to a frozen registry.");
  840. }
  841. switch (true) {
  842. case fallback === provider: break;
  843. case IsUndefined(first):
  844. first = provider;
  845. break;
  846. case first === provider: break;
  847. case IsUndefined(second):
  848. second = provider;
  849. break;
  850. case second === provider: break;
  851. default:
  852. if (rest === undefined)
  853. rest = new _Set();
  854. rest.add(provider);
  855. break;
  856. }
  857. }
  858. function getProviderNoCache(O, P) {
  859. if (!IsUndefined(first)) {
  860. if (first.isProviderFor(O, P))
  861. return first;
  862. if (!IsUndefined(second)) {
  863. if (second.isProviderFor(O, P))
  864. return first;
  865. if (!IsUndefined(rest)) {
  866. var iterator = GetIterator(rest);
  867. while (true) {
  868. var next = IteratorStep(iterator);
  869. if (!next) {
  870. return undefined;
  871. }
  872. var provider = IteratorValue(next);
  873. if (provider.isProviderFor(O, P)) {
  874. IteratorClose(iterator);
  875. return provider;
  876. }
  877. }
  878. }
  879. }
  880. }
  881. if (!IsUndefined(fallback) && fallback.isProviderFor(O, P)) {
  882. return fallback;
  883. }
  884. return undefined;
  885. }
  886. function getProvider(O, P) {
  887. var providerMap = targetProviderMap.get(O);
  888. var provider;
  889. if (!IsUndefined(providerMap)) {
  890. provider = providerMap.get(P);
  891. }
  892. if (!IsUndefined(provider)) {
  893. return provider;
  894. }
  895. provider = getProviderNoCache(O, P);
  896. if (!IsUndefined(provider)) {
  897. if (IsUndefined(providerMap)) {
  898. providerMap = new _Map();
  899. targetProviderMap.set(O, providerMap);
  900. }
  901. providerMap.set(P, provider);
  902. }
  903. return provider;
  904. }
  905. function hasProvider(provider) {
  906. if (IsUndefined(provider))
  907. throw new TypeError();
  908. return fallback === provider || first === provider || second === provider || !IsUndefined(rest) && rest.has(provider);
  909. }
  910. function setProvider(O, P, provider) {
  911. if (!hasProvider(provider)) {
  912. throw new Error("Metadata provider not registered.");
  913. }
  914. var existingProvider = getProvider(O, P);
  915. if (existingProvider !== provider) {
  916. if (!IsUndefined(existingProvider)) {
  917. return false;
  918. }
  919. var providerMap = targetProviderMap.get(O);
  920. if (IsUndefined(providerMap)) {
  921. providerMap = new _Map();
  922. targetProviderMap.set(O, providerMap);
  923. }
  924. providerMap.set(P, provider);
  925. }
  926. return true;
  927. }
  928. }
  929. /**
  930. * Gets or creates the shared registry of metadata providers.
  931. */
  932. function GetOrCreateMetadataRegistry() {
  933. var metadataRegistry;
  934. if (!IsUndefined(registrySymbol) && IsObject(Reflect) && Object.isExtensible(Reflect)) {
  935. metadataRegistry = Reflect[registrySymbol];
  936. }
  937. if (IsUndefined(metadataRegistry)) {
  938. metadataRegistry = CreateMetadataRegistry();
  939. }
  940. if (!IsUndefined(registrySymbol) && IsObject(Reflect) && Object.isExtensible(Reflect)) {
  941. Object.defineProperty(Reflect, registrySymbol, {
  942. enumerable: false,
  943. configurable: false,
  944. writable: false,
  945. value: metadataRegistry
  946. });
  947. }
  948. return metadataRegistry;
  949. }
  950. function CreateMetadataProvider(registry) {
  951. // [[Metadata]] internal slot
  952. // https://rbuckton.github.io/reflect-metadata/#ordinary-object-internal-methods-and-internal-slots
  953. var metadata = new _WeakMap();
  954. var provider = {
  955. isProviderFor: function (O, P) {
  956. var targetMetadata = metadata.get(O);
  957. if (IsUndefined(targetMetadata))
  958. return false;
  959. return targetMetadata.has(P);
  960. },
  961. OrdinaryDefineOwnMetadata: OrdinaryDefineOwnMetadata,
  962. OrdinaryHasOwnMetadata: OrdinaryHasOwnMetadata,
  963. OrdinaryGetOwnMetadata: OrdinaryGetOwnMetadata,
  964. OrdinaryOwnMetadataKeys: OrdinaryOwnMetadataKeys,
  965. OrdinaryDeleteMetadata: OrdinaryDeleteMetadata,
  966. };
  967. metadataRegistry.registerProvider(provider);
  968. return provider;
  969. function GetOrCreateMetadataMap(O, P, Create) {
  970. var targetMetadata = metadata.get(O);
  971. var createdTargetMetadata = false;
  972. if (IsUndefined(targetMetadata)) {
  973. if (!Create)
  974. return undefined;
  975. targetMetadata = new _Map();
  976. metadata.set(O, targetMetadata);
  977. createdTargetMetadata = true;
  978. }
  979. var metadataMap = targetMetadata.get(P);
  980. if (IsUndefined(metadataMap)) {
  981. if (!Create)
  982. return undefined;
  983. metadataMap = new _Map();
  984. targetMetadata.set(P, metadataMap);
  985. if (!registry.setProvider(O, P, provider)) {
  986. targetMetadata.delete(P);
  987. if (createdTargetMetadata) {
  988. metadata.delete(O);
  989. }
  990. throw new Error("Wrong provider for target.");
  991. }
  992. }
  993. return metadataMap;
  994. }
  995. // 3.1.2.1 OrdinaryHasOwnMetadata(MetadataKey, O, P)
  996. // https://rbuckton.github.io/reflect-metadata/#ordinaryhasownmetadata
  997. function OrdinaryHasOwnMetadata(MetadataKey, O, P) {
  998. var metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false);
  999. if (IsUndefined(metadataMap))
  1000. return false;
  1001. return ToBoolean(metadataMap.has(MetadataKey));
  1002. }
  1003. // 3.1.4.1 OrdinaryGetOwnMetadata(MetadataKey, O, P)
  1004. // https://rbuckton.github.io/reflect-metadata/#ordinarygetownmetadata
  1005. function OrdinaryGetOwnMetadata(MetadataKey, O, P) {
  1006. var metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false);
  1007. if (IsUndefined(metadataMap))
  1008. return undefined;
  1009. return metadataMap.get(MetadataKey);
  1010. }
  1011. // 3.1.5.1 OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P)
  1012. // https://rbuckton.github.io/reflect-metadata/#ordinarydefineownmetadata
  1013. function OrdinaryDefineOwnMetadata(MetadataKey, MetadataValue, O, P) {
  1014. var metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ true);
  1015. metadataMap.set(MetadataKey, MetadataValue);
  1016. }
  1017. // 3.1.7.1 OrdinaryOwnMetadataKeys(O, P)
  1018. // https://rbuckton.github.io/reflect-metadata/#ordinaryownmetadatakeys
  1019. function OrdinaryOwnMetadataKeys(O, P) {
  1020. var keys = [];
  1021. var metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false);
  1022. if (IsUndefined(metadataMap))
  1023. return keys;
  1024. var keysObj = metadataMap.keys();
  1025. var iterator = GetIterator(keysObj);
  1026. var k = 0;
  1027. while (true) {
  1028. var next = IteratorStep(iterator);
  1029. if (!next) {
  1030. keys.length = k;
  1031. return keys;
  1032. }
  1033. var nextValue = IteratorValue(next);
  1034. try {
  1035. keys[k] = nextValue;
  1036. }
  1037. catch (e) {
  1038. try {
  1039. IteratorClose(iterator);
  1040. }
  1041. finally {
  1042. throw e;
  1043. }
  1044. }
  1045. k++;
  1046. }
  1047. }
  1048. function OrdinaryDeleteMetadata(MetadataKey, O, P) {
  1049. var metadataMap = GetOrCreateMetadataMap(O, P, /*Create*/ false);
  1050. if (IsUndefined(metadataMap))
  1051. return false;
  1052. if (!metadataMap.delete(MetadataKey))
  1053. return false;
  1054. if (metadataMap.size === 0) {
  1055. var targetMetadata = metadata.get(O);
  1056. if (!IsUndefined(targetMetadata)) {
  1057. targetMetadata.delete(P);
  1058. if (targetMetadata.size === 0) {
  1059. metadata.delete(targetMetadata);
  1060. }
  1061. }
  1062. }
  1063. return true;
  1064. }
  1065. }
  1066. function CreateFallbackProvider(reflect) {
  1067. var defineMetadata = reflect.defineMetadata, hasOwnMetadata = reflect.hasOwnMetadata, getOwnMetadata = reflect.getOwnMetadata, getOwnMetadataKeys = reflect.getOwnMetadataKeys, deleteMetadata = reflect.deleteMetadata;
  1068. var metadataOwner = new _WeakMap();
  1069. var provider = {
  1070. isProviderFor: function (O, P) {
  1071. var metadataPropertySet = metadataOwner.get(O);
  1072. if (!IsUndefined(metadataPropertySet) && metadataPropertySet.has(P)) {
  1073. return true;
  1074. }
  1075. if (getOwnMetadataKeys(O, P).length) {
  1076. if (IsUndefined(metadataPropertySet)) {
  1077. metadataPropertySet = new _Set();
  1078. metadataOwner.set(O, metadataPropertySet);
  1079. }
  1080. metadataPropertySet.add(P);
  1081. return true;
  1082. }
  1083. return false;
  1084. },
  1085. OrdinaryDefineOwnMetadata: defineMetadata,
  1086. OrdinaryHasOwnMetadata: hasOwnMetadata,
  1087. OrdinaryGetOwnMetadata: getOwnMetadata,
  1088. OrdinaryOwnMetadataKeys: getOwnMetadataKeys,
  1089. OrdinaryDeleteMetadata: deleteMetadata,
  1090. };
  1091. return provider;
  1092. }
  1093. /**
  1094. * Gets the metadata provider for an object. If the object has no metadata provider and this is for a create operation,
  1095. * then this module's metadata provider is assigned to the object.
  1096. */
  1097. function GetMetadataProvider(O, P, Create) {
  1098. var registeredProvider = metadataRegistry.getProvider(O, P);
  1099. if (!IsUndefined(registeredProvider)) {
  1100. return registeredProvider;
  1101. }
  1102. if (Create) {
  1103. if (metadataRegistry.setProvider(O, P, metadataProvider)) {
  1104. return metadataProvider;
  1105. }
  1106. throw new Error("Illegal state.");
  1107. }
  1108. return undefined;
  1109. }