applyDecs2301.js 12 KB

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