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