applyDecs2311.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = applyDecs2311;
  6. var _checkInRHS = require("./checkInRHS.js");
  7. var _setFunctionName = require("./setFunctionName.js");
  8. var _toPropertyKey = require("./toPropertyKey.js");
  9. function applyDecs2311(targetClass, classDecs, memberDecs, classDecsHaveThis, instanceBrand, parentClass) {
  10. var symbolMetadata = Symbol.metadata || Symbol["for"]("Symbol.metadata");
  11. var defineProperty = Object.defineProperty;
  12. var create = Object.create;
  13. var metadata;
  14. var existingNonFields = [create(null), create(null)];
  15. var hasClassDecs = classDecs.length;
  16. var _;
  17. function createRunInitializers(initializers, useStaticThis, hasValue) {
  18. return function (thisArg, value) {
  19. if (useStaticThis) {
  20. value = thisArg;
  21. thisArg = targetClass;
  22. }
  23. for (var i = 0; i < initializers.length; i++) {
  24. value = initializers[i].apply(thisArg, hasValue ? [value] : []);
  25. }
  26. return hasValue ? value : thisArg;
  27. };
  28. }
  29. function assertCallable(fn, hint1, hint2, throwUndefined) {
  30. if (typeof fn !== "function") {
  31. if (throwUndefined || fn !== void 0) {
  32. throw new TypeError(hint1 + " must " + (hint2 || "be") + " a function" + (throwUndefined ? "" : " or undefined"));
  33. }
  34. }
  35. return fn;
  36. }
  37. function applyDec(Class, decInfo, decoratorsHaveThis, name, kind, initializers, ret, isStatic, isPrivate, isField, hasPrivateBrand) {
  38. function assertInstanceIfPrivate(target) {
  39. if (!hasPrivateBrand(target)) {
  40. throw new TypeError("Attempted to access private element on non-instance");
  41. }
  42. }
  43. var decs = [].concat(decInfo[0]),
  44. decVal = decInfo[3],
  45. isClass = !ret;
  46. var isAccessor = kind === 1;
  47. var isGetter = kind === 3;
  48. var isSetter = kind === 4;
  49. var isMethod = kind === 2;
  50. function _bindPropCall(name, useStaticThis, before) {
  51. return function (_this, value) {
  52. if (useStaticThis) {
  53. value = _this;
  54. _this = Class;
  55. }
  56. if (before) {
  57. before(_this);
  58. }
  59. return desc[name].call(_this, value);
  60. };
  61. }
  62. if (!isClass) {
  63. var desc = {},
  64. init = [],
  65. key = isGetter ? "get" : isSetter || isAccessor ? "set" : "value";
  66. if (isPrivate) {
  67. if (isField || isAccessor) {
  68. desc = {
  69. get: (0, _setFunctionName.default)(function () {
  70. return decVal(this);
  71. }, name, "get"),
  72. set: function (value) {
  73. decInfo[4](this, value);
  74. }
  75. };
  76. } else {
  77. desc[key] = decVal;
  78. }
  79. if (!isField) {
  80. (0, _setFunctionName.default)(desc[key], name, isMethod ? "" : key);
  81. }
  82. } else if (!isField) {
  83. desc = Object.getOwnPropertyDescriptor(Class, name);
  84. }
  85. if (!isField && !isPrivate) {
  86. _ = existingNonFields[+isStatic][name];
  87. if (_ && (_ ^ kind) !== 7) {
  88. throw new Error("Decorating two elements with the same name (" + desc[key].name + ") is not supported yet");
  89. }
  90. existingNonFields[+isStatic][name] = kind < 3 ? 1 : kind;
  91. }
  92. }
  93. var newValue = Class;
  94. for (var i = decs.length - 1; i >= 0; i -= decoratorsHaveThis ? 2 : 1) {
  95. var dec = assertCallable(decs[i], "A decorator", "be", true),
  96. decThis = decoratorsHaveThis ? decs[i - 1] : void 0;
  97. var decoratorFinishedRef = {};
  98. var ctx = {
  99. kind: ["field", "accessor", "method", "getter", "setter", "class"][kind],
  100. name: name,
  101. metadata: metadata,
  102. addInitializer: function (decoratorFinishedRef, initializer) {
  103. if (decoratorFinishedRef.v) {
  104. throw new TypeError("attempted to call addInitializer after decoration was finished");
  105. }
  106. assertCallable(initializer, "An initializer", "be", true);
  107. initializers.push(initializer);
  108. }.bind(null, decoratorFinishedRef)
  109. };
  110. if (isClass) {
  111. _ = dec.call(decThis, newValue, ctx);
  112. decoratorFinishedRef.v = 1;
  113. if (assertCallable(_, "class decorators", "return")) {
  114. newValue = _;
  115. }
  116. } else {
  117. ctx["static"] = isStatic;
  118. ctx["private"] = isPrivate;
  119. _ = ctx.access = {
  120. has: isPrivate ? hasPrivateBrand.bind() : function (target) {
  121. return name in target;
  122. }
  123. };
  124. if (!isSetter) {
  125. _.get = isPrivate ? isMethod ? function (_this) {
  126. assertInstanceIfPrivate(_this);
  127. return desc.value;
  128. } : _bindPropCall("get", 0, assertInstanceIfPrivate) : function (target) {
  129. return target[name];
  130. };
  131. }
  132. if (!isMethod && !isGetter) {
  133. _.set = isPrivate ? _bindPropCall("set", 0, assertInstanceIfPrivate) : function (target, v) {
  134. target[name] = v;
  135. };
  136. }
  137. newValue = dec.call(decThis, isAccessor ? {
  138. get: desc.get,
  139. set: desc.set
  140. } : desc[key], ctx);
  141. decoratorFinishedRef.v = 1;
  142. if (isAccessor) {
  143. if (typeof newValue === "object" && newValue) {
  144. if (_ = assertCallable(newValue.get, "accessor.get")) {
  145. desc.get = _;
  146. }
  147. if (_ = assertCallable(newValue.set, "accessor.set")) {
  148. desc.set = _;
  149. }
  150. if (_ = assertCallable(newValue.init, "accessor.init")) {
  151. init.unshift(_);
  152. }
  153. } else if (newValue !== void 0) {
  154. throw new TypeError("accessor decorators must return an object with get, set, or init properties or undefined");
  155. }
  156. } else if (assertCallable(newValue, (isField ? "field" : "method") + " decorators", "return")) {
  157. if (isField) {
  158. init.unshift(newValue);
  159. } else {
  160. desc[key] = newValue;
  161. }
  162. }
  163. }
  164. }
  165. if (kind < 2) {
  166. ret.push(createRunInitializers(init, isStatic, 1), createRunInitializers(initializers, isStatic, 0));
  167. }
  168. if (!isField && !isClass) {
  169. if (isPrivate) {
  170. if (isAccessor) {
  171. ret.splice(-1, 0, _bindPropCall("get", isStatic), _bindPropCall("set", isStatic));
  172. } else {
  173. ret.push(isMethod ? desc[key] : assertCallable.call.bind(desc[key]));
  174. }
  175. } else {
  176. defineProperty(Class, name, desc);
  177. }
  178. }
  179. return newValue;
  180. }
  181. function applyMemberDecs() {
  182. var ret = [];
  183. var protoInitializers;
  184. var staticInitializers;
  185. var pushInitializers = function (initializers) {
  186. if (initializers) {
  187. ret.push(createRunInitializers(initializers));
  188. }
  189. };
  190. var applyMemberDecsOfKind = function (isStatic, isField) {
  191. for (var i = 0; i < memberDecs.length; i++) {
  192. var decInfo = memberDecs[i];
  193. var kind = decInfo[1];
  194. var kindOnly = kind & 7;
  195. if ((kind & 8) == isStatic && !kindOnly == isField) {
  196. var name = decInfo[2];
  197. var isPrivate = !!decInfo[3];
  198. var decoratorsHaveThis = kind & 16;
  199. applyDec(isStatic ? targetClass : targetClass.prototype, decInfo, decoratorsHaveThis, isPrivate ? "#" + name : (0, _toPropertyKey.default)(name), kindOnly, kindOnly < 2 ? [] : isStatic ? staticInitializers = staticInitializers || [] : protoInitializers = protoInitializers || [], ret, !!isStatic, isPrivate, isField, isStatic && isPrivate ? function (_) {
  200. return (0, _checkInRHS.default)(_) === targetClass;
  201. } : instanceBrand);
  202. }
  203. }
  204. };
  205. applyMemberDecsOfKind(8, 0);
  206. applyMemberDecsOfKind(0, 0);
  207. applyMemberDecsOfKind(8, 1);
  208. applyMemberDecsOfKind(0, 1);
  209. pushInitializers(protoInitializers);
  210. pushInitializers(staticInitializers);
  211. return ret;
  212. }
  213. function defineMetadata(Class) {
  214. return defineProperty(Class, symbolMetadata, {
  215. configurable: true,
  216. enumerable: true,
  217. value: metadata
  218. });
  219. }
  220. if (parentClass !== undefined) {
  221. metadata = parentClass[symbolMetadata];
  222. }
  223. metadata = create(metadata == null ? null : metadata);
  224. _ = applyMemberDecs();
  225. if (!hasClassDecs) defineMetadata(targetClass);
  226. return {
  227. e: _,
  228. get c() {
  229. var initializers = [];
  230. return hasClassDecs && [defineMetadata(targetClass = applyDec(targetClass, [classDecs], classDecsHaveThis, targetClass.name, 5, initializers)), createRunInitializers(initializers, 1)];
  231. }
  232. };
  233. }
  234. //# sourceMappingURL=applyDecs2311.js.map