applyDecs2305.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = applyDecs2305;
  6. var _checkInRHS = require("checkInRHS");
  7. function createAddInitializerMethod(initializers, decoratorFinishedRef) {
  8. return function addInitializer(initializer) {
  9. assertNotFinished(decoratorFinishedRef, "addInitializer");
  10. assertCallable(initializer, "An initializer");
  11. initializers.push(initializer);
  12. };
  13. }
  14. function assertInstanceIfPrivate(has, target) {
  15. if (!has(target)) {
  16. throw new TypeError("Attempted to access private element on non-instance");
  17. }
  18. }
  19. function memberDec(dec, thisArg, name, desc, initializers, kind, isStatic, isPrivate, value, hasPrivateBrand, metadata) {
  20. var kindStr;
  21. switch (kind) {
  22. case 1:
  23. kindStr = "accessor";
  24. break;
  25. case 2:
  26. kindStr = "method";
  27. break;
  28. case 3:
  29. kindStr = "getter";
  30. break;
  31. case 4:
  32. kindStr = "setter";
  33. break;
  34. default:
  35. kindStr = "field";
  36. }
  37. var ctx = {
  38. kind: kindStr,
  39. name: isPrivate ? "#" + name : name,
  40. static: isStatic,
  41. private: isPrivate,
  42. metadata: metadata
  43. };
  44. var decoratorFinishedRef = {
  45. v: false
  46. };
  47. if (kind !== 0) {
  48. ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);
  49. }
  50. var get, set;
  51. if (!isPrivate && (kind === 0 || kind === 2)) {
  52. get = function (target) {
  53. return target[name];
  54. };
  55. if (kind === 0) {
  56. set = function (target, v) {
  57. target[name] = v;
  58. };
  59. }
  60. } else if (kind === 2) {
  61. get = function (target) {
  62. assertInstanceIfPrivate(hasPrivateBrand, target);
  63. return desc.value;
  64. };
  65. } else {
  66. var t = kind === 0 || kind === 1;
  67. if (t || kind === 3) {
  68. if (isPrivate) {
  69. get = function (target) {
  70. assertInstanceIfPrivate(hasPrivateBrand, target);
  71. return desc.get.call(target);
  72. };
  73. } else {
  74. get = function (target) {
  75. return desc.get.call(target);
  76. };
  77. }
  78. }
  79. if (t || kind === 4) {
  80. if (isPrivate) {
  81. set = function (target, value) {
  82. assertInstanceIfPrivate(hasPrivateBrand, target);
  83. desc.set.call(target, value);
  84. };
  85. } else {
  86. set = function (target, value) {
  87. desc.set.call(target, value);
  88. };
  89. }
  90. }
  91. }
  92. var has = isPrivate ? hasPrivateBrand.bind() : function (target) {
  93. return name in target;
  94. };
  95. ctx.access = get && set ? {
  96. get: get,
  97. set: set,
  98. has: has
  99. } : get ? {
  100. get: get,
  101. has: has
  102. } : {
  103. set: set,
  104. has: has
  105. };
  106. try {
  107. return dec.call(thisArg, value, ctx);
  108. } finally {
  109. decoratorFinishedRef.v = true;
  110. }
  111. }
  112. function assertNotFinished(decoratorFinishedRef, fnName) {
  113. if (decoratorFinishedRef.v) {
  114. throw new Error("attempted to call " + fnName + " after decoration was finished");
  115. }
  116. }
  117. function assertCallable(fn, hint) {
  118. if (typeof fn !== "function") {
  119. throw new TypeError(hint + " must be a function");
  120. }
  121. }
  122. function assertValidReturnValue(kind, value) {
  123. var type = typeof value;
  124. if (kind === 1) {
  125. if (type !== "object" || value === null) {
  126. throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
  127. }
  128. if (value.get !== undefined) {
  129. assertCallable(value.get, "accessor.get");
  130. }
  131. if (value.set !== undefined) {
  132. assertCallable(value.set, "accessor.set");
  133. }
  134. if (value.init !== undefined) {
  135. assertCallable(value.init, "accessor.init");
  136. }
  137. } else if (type !== "function") {
  138. var hint;
  139. if (kind === 0) {
  140. hint = "field";
  141. } else if (kind === 5) {
  142. hint = "class";
  143. } else {
  144. hint = "method";
  145. }
  146. throw new TypeError(hint + " decorators must return a function or void 0");
  147. }
  148. }
  149. function curryThis1(fn) {
  150. return function () {
  151. return fn(this);
  152. };
  153. }
  154. function curryThis2(fn) {
  155. return function (value) {
  156. fn(this, value);
  157. };
  158. }
  159. function applyMemberDec(ret, base, decInfo, decoratorsHaveThis, name, kind, isStatic, isPrivate, initializers, hasPrivateBrand, metadata) {
  160. var decs = decInfo[0];
  161. if (!decoratorsHaveThis && !Array.isArray(decs)) {
  162. decs = [decs];
  163. }
  164. var desc, init, value;
  165. if (isPrivate) {
  166. if (kind === 0 || kind === 1) {
  167. desc = {
  168. get: curryThis1(decInfo[3]),
  169. set: curryThis2(decInfo[4])
  170. };
  171. } else {
  172. if (kind === 3) {
  173. desc = {
  174. get: decInfo[3]
  175. };
  176. } else if (kind === 4) {
  177. desc = {
  178. set: decInfo[3]
  179. };
  180. } else {
  181. desc = {
  182. value: decInfo[3]
  183. };
  184. }
  185. }
  186. } else if (kind !== 0) {
  187. desc = Object.getOwnPropertyDescriptor(base, name);
  188. }
  189. if (kind === 1) {
  190. value = {
  191. get: desc.get,
  192. set: desc.set
  193. };
  194. } else if (kind === 2) {
  195. value = desc.value;
  196. } else if (kind === 3) {
  197. value = desc.get;
  198. } else if (kind === 4) {
  199. value = desc.set;
  200. }
  201. var newValue, get, set;
  202. var inc = decoratorsHaveThis ? 2 : 1;
  203. for (var i = decs.length - 1; i >= 0; i -= inc) {
  204. var dec = decs[i];
  205. newValue = memberDec(dec, decoratorsHaveThis ? decs[i - 1] : undefined, name, desc, initializers, kind, isStatic, isPrivate, value, hasPrivateBrand, metadata);
  206. if (newValue !== void 0) {
  207. assertValidReturnValue(kind, newValue);
  208. var newInit;
  209. if (kind === 0) {
  210. newInit = newValue;
  211. } else if (kind === 1) {
  212. newInit = newValue.init;
  213. get = newValue.get || value.get;
  214. set = newValue.set || value.set;
  215. value = {
  216. get: get,
  217. set: set
  218. };
  219. } else {
  220. value = newValue;
  221. }
  222. if (newInit !== void 0) {
  223. if (init === void 0) {
  224. init = newInit;
  225. } else if (typeof init === "function") {
  226. init = [init, newInit];
  227. } else {
  228. init.push(newInit);
  229. }
  230. }
  231. }
  232. }
  233. if (kind === 0 || kind === 1) {
  234. if (init === void 0) {
  235. init = function (instance, init) {
  236. return init;
  237. };
  238. } else if (typeof init !== "function") {
  239. var ownInitializers = init;
  240. init = function (instance, init) {
  241. var value = init;
  242. for (var i = ownInitializers.length - 1; i >= 0; i--) {
  243. value = ownInitializers[i].call(instance, value);
  244. }
  245. return value;
  246. };
  247. } else {
  248. var originalInitializer = init;
  249. init = function (instance, init) {
  250. return originalInitializer.call(instance, init);
  251. };
  252. }
  253. ret.push(init);
  254. }
  255. if (kind !== 0) {
  256. if (kind === 1) {
  257. desc.get = value.get;
  258. desc.set = value.set;
  259. } else if (kind === 2) {
  260. desc.value = value;
  261. } else if (kind === 3) {
  262. desc.get = value;
  263. } else if (kind === 4) {
  264. desc.set = value;
  265. }
  266. if (isPrivate) {
  267. if (kind === 1) {
  268. ret.push(function (instance, args) {
  269. return value.get.call(instance, args);
  270. });
  271. ret.push(function (instance, args) {
  272. return value.set.call(instance, args);
  273. });
  274. } else if (kind === 2) {
  275. ret.push(value);
  276. } else {
  277. ret.push(function (instance, args) {
  278. return value.call(instance, args);
  279. });
  280. }
  281. } else {
  282. Object.defineProperty(base, name, desc);
  283. }
  284. }
  285. }
  286. function applyMemberDecs(Class, decInfos, instanceBrand, metadata) {
  287. var ret = [];
  288. var protoInitializers;
  289. var staticInitializers;
  290. var staticBrand;
  291. var existingProtoNonFields = new Map();
  292. var existingStaticNonFields = new Map();
  293. for (var i = 0; i < decInfos.length; i++) {
  294. var decInfo = decInfos[i];
  295. if (!Array.isArray(decInfo)) continue;
  296. var kind = decInfo[1];
  297. var name = decInfo[2];
  298. var isPrivate = decInfo.length > 3;
  299. var decoratorsHaveThis = kind & 16;
  300. var isStatic = !!(kind & 8);
  301. var base;
  302. var initializers;
  303. var hasPrivateBrand = instanceBrand;
  304. kind &= 7;
  305. if (isStatic) {
  306. base = Class;
  307. if (kind !== 0) {
  308. staticInitializers = staticInitializers || [];
  309. initializers = staticInitializers;
  310. }
  311. if (isPrivate && !staticBrand) {
  312. staticBrand = function (_) {
  313. return _checkInRHS(_) === Class;
  314. };
  315. }
  316. hasPrivateBrand = staticBrand;
  317. } else {
  318. base = Class.prototype;
  319. if (kind !== 0) {
  320. protoInitializers = protoInitializers || [];
  321. initializers = protoInitializers;
  322. }
  323. }
  324. if (kind !== 0 && !isPrivate) {
  325. var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields;
  326. var existingKind = existingNonFields.get(name) || 0;
  327. if (existingKind === true || existingKind === 3 && kind !== 4 || existingKind === 4 && kind !== 3) {
  328. throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + name);
  329. }
  330. existingNonFields.set(name, !existingKind && kind > 2 ? kind : true);
  331. }
  332. applyMemberDec(ret, base, decInfo, decoratorsHaveThis, name, kind, isStatic, isPrivate, initializers, hasPrivateBrand, metadata);
  333. }
  334. pushInitializers(ret, protoInitializers);
  335. pushInitializers(ret, staticInitializers);
  336. return ret;
  337. }
  338. function pushInitializers(ret, initializers) {
  339. if (initializers) {
  340. ret.push(function (instance) {
  341. for (var i = 0; i < initializers.length; i++) {
  342. initializers[i].call(instance);
  343. }
  344. return instance;
  345. });
  346. }
  347. }
  348. function applyClassDecs(targetClass, classDecs, decoratorsHaveThis, metadata) {
  349. if (classDecs.length) {
  350. var initializers = [];
  351. var newClass = targetClass;
  352. var name = targetClass.name;
  353. var inc = decoratorsHaveThis ? 2 : 1;
  354. for (var i = classDecs.length - 1; i >= 0; i -= inc) {
  355. var decoratorFinishedRef = {
  356. v: false
  357. };
  358. try {
  359. var nextNewClass = classDecs[i].call(decoratorsHaveThis ? classDecs[i - 1] : undefined, newClass, {
  360. kind: "class",
  361. name: name,
  362. addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef),
  363. metadata
  364. });
  365. } finally {
  366. decoratorFinishedRef.v = true;
  367. }
  368. if (nextNewClass !== undefined) {
  369. assertValidReturnValue(5, nextNewClass);
  370. newClass = nextNewClass;
  371. }
  372. }
  373. return [defineMetadata(newClass, metadata), function () {
  374. for (var i = 0; i < initializers.length; i++) {
  375. initializers[i].call(newClass);
  376. }
  377. }];
  378. }
  379. }
  380. function defineMetadata(Class, metadata) {
  381. return Object.defineProperty(Class, Symbol.metadata || Symbol.for("Symbol.metadata"), {
  382. configurable: true,
  383. enumerable: true,
  384. value: metadata
  385. });
  386. }
  387. function applyDecs2305(targetClass, memberDecs, classDecs, classDecsHaveThis, instanceBrand, parentClass) {
  388. if (arguments.length >= 6) {
  389. var parentMetadata = parentClass[Symbol.metadata || Symbol.for("Symbol.metadata")];
  390. }
  391. var metadata = Object.create(parentMetadata === void 0 ? null : parentMetadata);
  392. var e = applyMemberDecs(targetClass, memberDecs, instanceBrand, metadata);
  393. if (!classDecs.length) defineMetadata(targetClass, metadata);
  394. return {
  395. e: e,
  396. get c() {
  397. return applyClassDecs(targetClass, classDecs, classDecsHaveThis, metadata);
  398. }
  399. };
  400. }
  401. //# sourceMappingURL=applyDecs2305.js.map