applyDecs2305.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. var _typeof = require("./typeof.js")["default"];
  2. var checkInRHS = require("./checkInRHS.js");
  3. function createAddInitializerMethod(initializers, decoratorFinishedRef) {
  4. return function (initializer) {
  5. assertNotFinished(decoratorFinishedRef, "addInitializer"), assertCallable(initializer, "An initializer"), initializers.push(initializer);
  6. };
  7. }
  8. function assertInstanceIfPrivate(has, target) {
  9. if (!has(target)) throw new TypeError("Attempted to access private element on non-instance");
  10. }
  11. function memberDec(dec, thisArg, name, desc, initializers, kind, isStatic, isPrivate, value, hasPrivateBrand) {
  12. var kindStr;
  13. switch (kind) {
  14. case 1:
  15. kindStr = "accessor";
  16. break;
  17. case 2:
  18. kindStr = "method";
  19. break;
  20. case 3:
  21. kindStr = "getter";
  22. break;
  23. case 4:
  24. kindStr = "setter";
  25. break;
  26. default:
  27. kindStr = "field";
  28. }
  29. var get,
  30. set,
  31. ctx = {
  32. kind: kindStr,
  33. name: isPrivate ? "#" + name : name,
  34. "static": isStatic,
  35. "private": isPrivate
  36. },
  37. decoratorFinishedRef = {
  38. v: !1
  39. };
  40. if (0 !== kind && (ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef)), isPrivate || 0 !== kind && 2 !== kind) {
  41. if (2 === kind) get = function get(target) {
  42. return assertInstanceIfPrivate(hasPrivateBrand, target), desc.value;
  43. };else {
  44. var t = 0 === kind || 1 === kind;
  45. (t || 3 === kind) && (get = isPrivate ? function (target) {
  46. return assertInstanceIfPrivate(hasPrivateBrand, target), desc.get.call(target);
  47. } : function (target) {
  48. return desc.get.call(target);
  49. }), (t || 4 === kind) && (set = isPrivate ? function (target, value) {
  50. assertInstanceIfPrivate(hasPrivateBrand, target), desc.set.call(target, value);
  51. } : function (target, value) {
  52. desc.set.call(target, value);
  53. });
  54. }
  55. } else get = function get(target) {
  56. return target[name];
  57. }, 0 === kind && (set = function set(target, v) {
  58. target[name] = v;
  59. });
  60. var has = isPrivate ? hasPrivateBrand.bind() : function (target) {
  61. return name in target;
  62. };
  63. ctx.access = get && set ? {
  64. get: get,
  65. set: set,
  66. has: has
  67. } : get ? {
  68. get: get,
  69. has: has
  70. } : {
  71. set: set,
  72. has: has
  73. };
  74. try {
  75. return dec.call(thisArg, value, ctx);
  76. } finally {
  77. decoratorFinishedRef.v = !0;
  78. }
  79. }
  80. function assertNotFinished(decoratorFinishedRef, fnName) {
  81. if (decoratorFinishedRef.v) throw new Error("attempted to call " + fnName + " after decoration was finished");
  82. }
  83. function assertCallable(fn, hint) {
  84. if ("function" != typeof fn) throw new TypeError(hint + " must be a function");
  85. }
  86. function assertValidReturnValue(kind, value) {
  87. var type = _typeof(value);
  88. if (1 === kind) {
  89. if ("object" !== type || null === value) throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
  90. void 0 !== value.get && assertCallable(value.get, "accessor.get"), void 0 !== value.set && assertCallable(value.set, "accessor.set"), void 0 !== value.init && assertCallable(value.init, "accessor.init");
  91. } else if ("function" !== type) {
  92. var hint;
  93. throw hint = 0 === kind ? "field" : 5 === kind ? "class" : "method", new TypeError(hint + " decorators must return a function or void 0");
  94. }
  95. }
  96. function curryThis1(fn) {
  97. return function () {
  98. return fn(this);
  99. };
  100. }
  101. function curryThis2(fn) {
  102. return function (value) {
  103. fn(this, value);
  104. };
  105. }
  106. function applyMemberDec(ret, base, decInfo, decoratorsHaveThis, name, kind, isStatic, isPrivate, initializers, hasPrivateBrand) {
  107. var desc,
  108. init,
  109. value,
  110. newValue,
  111. get,
  112. set,
  113. decs = decInfo[0];
  114. decoratorsHaveThis || Array.isArray(decs) || (decs = [decs]), isPrivate ? desc = 0 === kind || 1 === kind ? {
  115. get: curryThis1(decInfo[3]),
  116. set: curryThis2(decInfo[4])
  117. } : 3 === kind ? {
  118. get: decInfo[3]
  119. } : 4 === kind ? {
  120. set: decInfo[3]
  121. } : {
  122. value: decInfo[3]
  123. } : 0 !== kind && (desc = Object.getOwnPropertyDescriptor(base, name)), 1 === kind ? value = {
  124. get: desc.get,
  125. set: desc.set
  126. } : 2 === kind ? value = desc.value : 3 === kind ? value = desc.get : 4 === kind && (value = desc.set);
  127. for (var inc = decoratorsHaveThis ? 2 : 1, i = decs.length - 1; i >= 0; i -= inc) {
  128. var newInit;
  129. if (void 0 !== (newValue = memberDec(decs[i], decoratorsHaveThis ? decs[i - 1] : void 0, name, desc, initializers, kind, isStatic, isPrivate, value, hasPrivateBrand))) assertValidReturnValue(kind, newValue), 0 === kind ? newInit = newValue : 1 === kind ? (newInit = newValue.init, get = newValue.get || value.get, set = newValue.set || value.set, value = {
  130. get: get,
  131. set: set
  132. }) : value = newValue, void 0 !== newInit && (void 0 === init ? init = newInit : "function" == typeof init ? init = [init, newInit] : init.push(newInit));
  133. }
  134. if (0 === kind || 1 === kind) {
  135. if (void 0 === init) init = function init(instance, _init) {
  136. return _init;
  137. };else if ("function" != typeof init) {
  138. var ownInitializers = init;
  139. init = function init(instance, _init2) {
  140. for (var value = _init2, i = ownInitializers.length - 1; i >= 0; i--) value = ownInitializers[i].call(instance, value);
  141. return value;
  142. };
  143. } else {
  144. var originalInitializer = init;
  145. init = function init(instance, _init3) {
  146. return originalInitializer.call(instance, _init3);
  147. };
  148. }
  149. ret.push(init);
  150. }
  151. 0 !== kind && (1 === kind ? (desc.get = value.get, desc.set = value.set) : 2 === kind ? desc.value = value : 3 === kind ? desc.get = value : 4 === kind && (desc.set = value), isPrivate ? 1 === kind ? (ret.push(function (instance, args) {
  152. return value.get.call(instance, args);
  153. }), ret.push(function (instance, args) {
  154. return value.set.call(instance, args);
  155. })) : 2 === kind ? ret.push(value) : ret.push(function (instance, args) {
  156. return value.call(instance, args);
  157. }) : Object.defineProperty(base, name, desc));
  158. }
  159. function applyMemberDecs(Class, decInfos, instanceBrand) {
  160. for (var protoInitializers, staticInitializers, staticBrand, ret = [], existingProtoNonFields = new Map(), existingStaticNonFields = new Map(), i = 0; i < decInfos.length; i++) {
  161. var decInfo = decInfos[i];
  162. if (Array.isArray(decInfo)) {
  163. var base,
  164. initializers,
  165. kind = decInfo[1],
  166. name = decInfo[2],
  167. isPrivate = decInfo.length > 3,
  168. decoratorsHaveThis = 16 & kind,
  169. isStatic = !!(8 & kind),
  170. hasPrivateBrand = instanceBrand;
  171. if (kind &= 7, isStatic ? (base = Class, 0 !== kind && (initializers = staticInitializers = staticInitializers || []), isPrivate && !staticBrand && (staticBrand = function staticBrand(_) {
  172. return checkInRHS(_) === Class;
  173. }), hasPrivateBrand = staticBrand) : (base = Class.prototype, 0 !== kind && (initializers = protoInitializers = protoInitializers || [])), 0 !== kind && !isPrivate) {
  174. var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields,
  175. existingKind = existingNonFields.get(name) || 0;
  176. if (!0 === existingKind || 3 === existingKind && 4 !== kind || 4 === existingKind && 3 !== kind) 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);
  177. existingNonFields.set(name, !(!existingKind && kind > 2) || kind);
  178. }
  179. applyMemberDec(ret, base, decInfo, decoratorsHaveThis, name, kind, isStatic, isPrivate, initializers, hasPrivateBrand);
  180. }
  181. }
  182. return pushInitializers(ret, protoInitializers), pushInitializers(ret, staticInitializers), ret;
  183. }
  184. function pushInitializers(ret, initializers) {
  185. initializers && ret.push(function (instance) {
  186. for (var i = 0; i < initializers.length; i++) initializers[i].call(instance);
  187. return instance;
  188. });
  189. }
  190. function applyClassDecs(targetClass, classDecs, decoratorsHaveThis) {
  191. if (classDecs.length) {
  192. for (var initializers = [], newClass = targetClass, name = targetClass.name, inc = decoratorsHaveThis ? 2 : 1, i = classDecs.length - 1; i >= 0; i -= inc) {
  193. var decoratorFinishedRef = {
  194. v: !1
  195. };
  196. try {
  197. var nextNewClass = classDecs[i].call(decoratorsHaveThis ? classDecs[i - 1] : void 0, newClass, {
  198. kind: "class",
  199. name: name,
  200. addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef)
  201. });
  202. } finally {
  203. decoratorFinishedRef.v = !0;
  204. }
  205. void 0 !== nextNewClass && (assertValidReturnValue(5, nextNewClass), newClass = nextNewClass);
  206. }
  207. return [newClass, function () {
  208. for (var i = 0; i < initializers.length; i++) initializers[i].call(newClass);
  209. }];
  210. }
  211. }
  212. function applyDecs2305(targetClass, memberDecs, classDecs, classDecsHaveThis, instanceBrand) {
  213. return {
  214. e: applyMemberDecs(targetClass, memberDecs, instanceBrand),
  215. get c() {
  216. return applyClassDecs(targetClass, classDecs, classDecsHaveThis);
  217. }
  218. };
  219. }
  220. module.exports = applyDecs2305, module.exports.__esModule = true, module.exports["default"] = module.exports;