@jsenv/core 30.4.1 → 31.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,575 @@
1
+ /* @minVersion 7.21.0 */
2
+
3
+ import checkInRHS from "../checkInRHS/checkInRHS.js";
4
+
5
+ /**
6
+ Enums are used in this file, but not assigned to vars to avoid non-hoistable values
7
+
8
+ CONSTRUCTOR = 0;
9
+ PUBLIC = 1;
10
+ PRIVATE = 2;
11
+
12
+ FIELD = 0;
13
+ ACCESSOR = 1;
14
+ METHOD = 2;
15
+ GETTER = 3;
16
+ SETTER = 4;
17
+
18
+ STATIC = 5;
19
+
20
+ CLASS = 10; // only used in assertValidReturnValue
21
+ */
22
+
23
+ function createAddInitializerMethod(initializers, decoratorFinishedRef) {
24
+ return function addInitializer(initializer) {
25
+ assertNotFinished(decoratorFinishedRef, "addInitializer");
26
+ assertCallable(initializer, "An initializer");
27
+ initializers.push(initializer);
28
+ };
29
+ }
30
+ function assertInstanceIfPrivate(has, target) {
31
+ if (!has(target)) {
32
+ throw new TypeError("Attempted to access private element on non-instance");
33
+ }
34
+ }
35
+ function memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, value, hasPrivateBrand) {
36
+ var kindStr;
37
+ switch (kind) {
38
+ case 1 /* ACCESSOR */:
39
+ kindStr = "accessor";
40
+ break;
41
+ case 2 /* METHOD */:
42
+ kindStr = "method";
43
+ break;
44
+ case 3 /* GETTER */:
45
+ kindStr = "getter";
46
+ break;
47
+ case 4 /* SETTER */:
48
+ kindStr = "setter";
49
+ break;
50
+ default:
51
+ kindStr = "field";
52
+ }
53
+ var ctx = {
54
+ kind: kindStr,
55
+ name: isPrivate ? "#" + name : name,
56
+ static: isStatic,
57
+ private: isPrivate
58
+ };
59
+ var decoratorFinishedRef = {
60
+ v: false
61
+ };
62
+ if (kind !== 0 /* FIELD */) {
63
+ ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);
64
+ }
65
+ var get, set;
66
+ if (!isPrivate && (kind === 0 /* FIELD */ || kind === 2) /* METHOD */) {
67
+ get = function (target) {
68
+ return target[name];
69
+ };
70
+ if (kind === 0 /* FIELD */) {
71
+ set = function (target, v) {
72
+ target[name] = v;
73
+ };
74
+ }
75
+ } else if (kind === 2 /* METHOD */) {
76
+ // Assert: isPrivate is true.
77
+ get = function (target) {
78
+ assertInstanceIfPrivate(hasPrivateBrand, target);
79
+ return desc.value;
80
+ };
81
+ } else {
82
+ // Assert: If kind === 0, then isPrivate is true.
83
+ var t = kind === 0 /* FIELD */ || kind === 1; /* ACCESSOR */
84
+ if (t || kind === 3 /* GETTER */) {
85
+ if (isPrivate) {
86
+ get = function (target) {
87
+ assertInstanceIfPrivate(hasPrivateBrand, target);
88
+ return desc.get.call(target);
89
+ };
90
+ } else {
91
+ get = function (target) {
92
+ return desc.get.call(target);
93
+ };
94
+ }
95
+ }
96
+ if (t || kind === 4 /* SETTER */) {
97
+ if (isPrivate) {
98
+ set = function (target, value) {
99
+ assertInstanceIfPrivate(hasPrivateBrand, target);
100
+ desc.set.call(target, value);
101
+ };
102
+ } else {
103
+ set = function (target, value) {
104
+ desc.set.call(target, value);
105
+ };
106
+ }
107
+ }
108
+ }
109
+ var has = isPrivate ? hasPrivateBrand.bind() : function (target) {
110
+ return name in target;
111
+ };
112
+ ctx.access = get && set ? {
113
+ get: get,
114
+ set: set,
115
+ has: has
116
+ } : get ? {
117
+ get: get,
118
+ has: has
119
+ } : {
120
+ set: set,
121
+ has: has
122
+ };
123
+ try {
124
+ return dec(value, ctx);
125
+ } finally {
126
+ decoratorFinishedRef.v = true;
127
+ }
128
+ }
129
+ function assertNotFinished(decoratorFinishedRef, fnName) {
130
+ if (decoratorFinishedRef.v) {
131
+ throw new Error("attempted to call " + fnName + " after decoration was finished");
132
+ }
133
+ }
134
+ function assertCallable(fn, hint) {
135
+ if (typeof fn !== "function") {
136
+ throw new TypeError(hint + " must be a function");
137
+ }
138
+ }
139
+ function assertValidReturnValue(kind, value) {
140
+ var type = typeof value;
141
+ if (kind === 1 /* ACCESSOR */) {
142
+ if (type !== "object" || value === null) {
143
+ throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
144
+ }
145
+ if (value.get !== undefined) {
146
+ assertCallable(value.get, "accessor.get");
147
+ }
148
+ if (value.set !== undefined) {
149
+ assertCallable(value.set, "accessor.set");
150
+ }
151
+ if (value.init !== undefined) {
152
+ assertCallable(value.init, "accessor.init");
153
+ }
154
+ } else if (type !== "function") {
155
+ var hint;
156
+ if (kind === 0 /* FIELD */) {
157
+ hint = "field";
158
+ } else if (kind === 10 /* CLASS */) {
159
+ hint = "class";
160
+ } else {
161
+ hint = "method";
162
+ }
163
+ throw new TypeError(hint + " decorators must return a function or void 0");
164
+ }
165
+ }
166
+ function curryThis1(fn) {
167
+ return function () {
168
+ return fn(this);
169
+ };
170
+ }
171
+ function curryThis2(fn) {
172
+ return function (value) {
173
+ fn(this, value);
174
+ };
175
+ }
176
+ function applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, hasPrivateBrand) {
177
+ var decs = decInfo[0];
178
+ var desc, init, value;
179
+ if (isPrivate) {
180
+ if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) {
181
+ desc = {
182
+ get: curryThis1(decInfo[3]),
183
+ set: curryThis2(decInfo[4])
184
+ };
185
+ } else {
186
+ if (kind === 3 /* GETTER */) {
187
+ desc = {
188
+ get: decInfo[3]
189
+ };
190
+ } else if (kind === 4 /* SETTER */) {
191
+ desc = {
192
+ set: decInfo[3]
193
+ };
194
+ } else {
195
+ desc = {
196
+ value: decInfo[3]
197
+ };
198
+ }
199
+ }
200
+ } else if (kind !== 0 /* FIELD */) {
201
+ desc = Object.getOwnPropertyDescriptor(base, name);
202
+ }
203
+ if (kind === 1 /* ACCESSOR */) {
204
+ value = {
205
+ get: desc.get,
206
+ set: desc.set
207
+ };
208
+ } else if (kind === 2 /* METHOD */) {
209
+ value = desc.value;
210
+ } else if (kind === 3 /* GETTER */) {
211
+ value = desc.get;
212
+ } else if (kind === 4 /* SETTER */) {
213
+ value = desc.set;
214
+ }
215
+ var newValue, get, set;
216
+ if (typeof decs === "function") {
217
+ newValue = memberDec(decs, name, desc, initializers, kind, isStatic, isPrivate, value, hasPrivateBrand);
218
+ if (newValue !== void 0) {
219
+ assertValidReturnValue(kind, newValue);
220
+ if (kind === 0 /* FIELD */) {
221
+ init = newValue;
222
+ } else if (kind === 1 /* ACCESSOR */) {
223
+ init = newValue.init;
224
+ get = newValue.get || value.get;
225
+ set = newValue.set || value.set;
226
+ value = {
227
+ get: get,
228
+ set: set
229
+ };
230
+ } else {
231
+ value = newValue;
232
+ }
233
+ }
234
+ } else {
235
+ for (var i = decs.length - 1; i >= 0; i--) {
236
+ var dec = decs[i];
237
+ newValue = memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, value, hasPrivateBrand);
238
+ if (newValue !== void 0) {
239
+ assertValidReturnValue(kind, newValue);
240
+ var newInit;
241
+ if (kind === 0 /* FIELD */) {
242
+ newInit = newValue;
243
+ } else if (kind === 1 /* ACCESSOR */) {
244
+ newInit = newValue.init;
245
+ get = newValue.get || value.get;
246
+ set = newValue.set || value.set;
247
+ value = {
248
+ get: get,
249
+ set: set
250
+ };
251
+ } else {
252
+ value = newValue;
253
+ }
254
+ if (newInit !== void 0) {
255
+ if (init === void 0) {
256
+ init = newInit;
257
+ } else if (typeof init === "function") {
258
+ init = [init, newInit];
259
+ } else {
260
+ init.push(newInit);
261
+ }
262
+ }
263
+ }
264
+ }
265
+ }
266
+ if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) {
267
+ if (init === void 0) {
268
+ // If the initializer was void 0, sub in a dummy initializer
269
+ init = function (instance, init) {
270
+ return init;
271
+ };
272
+ } else if (typeof init !== "function") {
273
+ var ownInitializers = init;
274
+ init = function (instance, init) {
275
+ var value = init;
276
+ for (var i = 0; i < ownInitializers.length; i++) {
277
+ value = ownInitializers[i].call(instance, value);
278
+ }
279
+ return value;
280
+ };
281
+ } else {
282
+ var originalInitializer = init;
283
+ init = function (instance, init) {
284
+ return originalInitializer.call(instance, init);
285
+ };
286
+ }
287
+ ret.push(init);
288
+ }
289
+ if (kind !== 0 /* FIELD */) {
290
+ if (kind === 1 /* ACCESSOR */) {
291
+ desc.get = value.get;
292
+ desc.set = value.set;
293
+ } else if (kind === 2 /* METHOD */) {
294
+ desc.value = value;
295
+ } else if (kind === 3 /* GETTER */) {
296
+ desc.get = value;
297
+ } else if (kind === 4 /* SETTER */) {
298
+ desc.set = value;
299
+ }
300
+ if (isPrivate) {
301
+ if (kind === 1 /* ACCESSOR */) {
302
+ ret.push(function (instance, args) {
303
+ return value.get.call(instance, args);
304
+ });
305
+ ret.push(function (instance, args) {
306
+ return value.set.call(instance, args);
307
+ });
308
+ } else if (kind === 2 /* METHOD */) {
309
+ ret.push(value);
310
+ } else {
311
+ ret.push(function (instance, args) {
312
+ return value.call(instance, args);
313
+ });
314
+ }
315
+ } else {
316
+ Object.defineProperty(base, name, desc);
317
+ }
318
+ }
319
+ }
320
+ function applyMemberDecs(Class, decInfos, instanceBrand) {
321
+ var ret = [];
322
+ var protoInitializers;
323
+ var staticInitializers;
324
+ var staticBrand;
325
+ var existingProtoNonFields = new Map();
326
+ var existingStaticNonFields = new Map();
327
+ for (var i = 0; i < decInfos.length; i++) {
328
+ var decInfo = decInfos[i];
329
+
330
+ // skip computed property names
331
+ if (!Array.isArray(decInfo)) continue;
332
+ var kind = decInfo[1];
333
+ var name = decInfo[2];
334
+ var isPrivate = decInfo.length > 3;
335
+ var isStatic = kind >= 5; /* STATIC */
336
+ var base;
337
+ var initializers;
338
+ var hasPrivateBrand = instanceBrand;
339
+ if (isStatic) {
340
+ base = Class;
341
+ kind = kind - 5 /* STATIC */;
342
+ // initialize staticInitializers when we see a non-field static member
343
+ if (kind !== 0 /* FIELD */) {
344
+ staticInitializers = staticInitializers || [];
345
+ initializers = staticInitializers;
346
+ }
347
+ if (isPrivate && !staticBrand) {
348
+ staticBrand = function (_) {
349
+ return checkInRHS(_) === Class;
350
+ };
351
+ }
352
+ hasPrivateBrand = staticBrand;
353
+ } else {
354
+ base = Class.prototype;
355
+ // initialize protoInitializers when we see a non-field member
356
+ if (kind !== 0 /* FIELD */) {
357
+ protoInitializers = protoInitializers || [];
358
+ initializers = protoInitializers;
359
+ }
360
+ }
361
+ if (kind !== 0 /* FIELD */ && !isPrivate) {
362
+ var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields;
363
+ var existingKind = existingNonFields.get(name) || 0;
364
+ if (existingKind === true || existingKind === 3 /* GETTER */ && kind !== 4 /* SETTER */ || existingKind === 4 /* SETTER */ && kind !== 3 /* GETTER */) {
365
+ 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);
366
+ } else if (!existingKind && kind > 2 /* METHOD */) {
367
+ existingNonFields.set(name, kind);
368
+ } else {
369
+ existingNonFields.set(name, true);
370
+ }
371
+ }
372
+ applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, hasPrivateBrand);
373
+ }
374
+ pushInitializers(ret, protoInitializers);
375
+ pushInitializers(ret, staticInitializers);
376
+ return ret;
377
+ }
378
+ function pushInitializers(ret, initializers) {
379
+ if (initializers) {
380
+ ret.push(function (instance) {
381
+ for (var i = 0; i < initializers.length; i++) {
382
+ initializers[i].call(instance);
383
+ }
384
+ return instance;
385
+ });
386
+ }
387
+ }
388
+ function applyClassDecs(targetClass, classDecs) {
389
+ if (classDecs.length > 0) {
390
+ var initializers = [];
391
+ var newClass = targetClass;
392
+ var name = targetClass.name;
393
+ for (var i = classDecs.length - 1; i >= 0; i--) {
394
+ var decoratorFinishedRef = {
395
+ v: false
396
+ };
397
+ try {
398
+ var nextNewClass = classDecs[i](newClass, {
399
+ kind: "class",
400
+ name: name,
401
+ addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef)
402
+ });
403
+ } finally {
404
+ decoratorFinishedRef.v = true;
405
+ }
406
+ if (nextNewClass !== undefined) {
407
+ assertValidReturnValue(10 /* CLASS */, nextNewClass);
408
+ newClass = nextNewClass;
409
+ }
410
+ }
411
+ return [newClass, function () {
412
+ for (var i = 0; i < initializers.length; i++) {
413
+ initializers[i].call(newClass);
414
+ }
415
+ }];
416
+ }
417
+ // The transformer will not emit assignment when there are no class decorators,
418
+ // so we don't have to return an empty array here.
419
+ }
420
+
421
+ /**
422
+ Basic usage:
423
+
424
+ applyDecs(
425
+ Class,
426
+ [
427
+ // member decorators
428
+ [
429
+ dec, // dec or array of decs
430
+ 0, // kind of value being decorated
431
+ 'prop', // name of public prop on class containing the value being decorated,
432
+ '#p', // the name of the private property (if is private, void 0 otherwise),
433
+ ]
434
+ ],
435
+ [
436
+ // class decorators
437
+ dec1, dec2
438
+ ]
439
+ )
440
+ ```
441
+
442
+ Fully transpiled example:
443
+
444
+ ```js
445
+ @dec
446
+ class Class {
447
+ @dec
448
+ a = 123;
449
+
450
+ @dec
451
+ #a = 123;
452
+
453
+ @dec
454
+ @dec2
455
+ accessor b = 123;
456
+
457
+ @dec
458
+ accessor #b = 123;
459
+
460
+ @dec
461
+ c() { console.log('c'); }
462
+
463
+ @dec
464
+ #c() { console.log('privC'); }
465
+
466
+ @dec
467
+ get d() { console.log('d'); }
468
+
469
+ @dec
470
+ get #d() { console.log('privD'); }
471
+
472
+ @dec
473
+ set e(v) { console.log('e'); }
474
+
475
+ @dec
476
+ set #e(v) { console.log('privE'); }
477
+ }
478
+
479
+
480
+ // becomes
481
+ let initializeInstance;
482
+ let initializeClass;
483
+
484
+ let initA;
485
+ let initPrivA;
486
+
487
+ let initB;
488
+ let initPrivB, getPrivB, setPrivB;
489
+
490
+ let privC;
491
+ let privD;
492
+ let privE;
493
+
494
+ let Class;
495
+ class _Class {
496
+ static {
497
+ let ret = applyDecs(
498
+ this,
499
+ [
500
+ [dec, 0, 'a'],
501
+ [dec, 0, 'a', (i) => i.#a, (i, v) => i.#a = v],
502
+ [[dec, dec2], 1, 'b'],
503
+ [dec, 1, 'b', (i) => i.#privBData, (i, v) => i.#privBData = v],
504
+ [dec, 2, 'c'],
505
+ [dec, 2, 'c', () => console.log('privC')],
506
+ [dec, 3, 'd'],
507
+ [dec, 3, 'd', () => console.log('privD')],
508
+ [dec, 4, 'e'],
509
+ [dec, 4, 'e', () => console.log('privE')],
510
+ ],
511
+ [
512
+ dec
513
+ ]
514
+ )
515
+
516
+ initA = ret[0];
517
+
518
+ initPrivA = ret[1];
519
+
520
+ initB = ret[2];
521
+
522
+ initPrivB = ret[3];
523
+ getPrivB = ret[4];
524
+ setPrivB = ret[5];
525
+
526
+ privC = ret[6];
527
+
528
+ privD = ret[7];
529
+
530
+ privE = ret[8];
531
+
532
+ initializeInstance = ret[9];
533
+
534
+ Class = ret[10]
535
+
536
+ initializeClass = ret[11];
537
+ }
538
+
539
+ a = (initializeInstance(this), initA(this, 123));
540
+
541
+ #a = initPrivA(this, 123);
542
+
543
+ #bData = initB(this, 123);
544
+ get b() { return this.#bData }
545
+ set b(v) { this.#bData = v }
546
+
547
+ #privBData = initPrivB(this, 123);
548
+ get #b() { return getPrivB(this); }
549
+ set #b(v) { setPrivB(this, v); }
550
+
551
+ c() { console.log('c'); }
552
+
553
+ #c(...args) { return privC(this, ...args) }
554
+
555
+ get d() { console.log('d'); }
556
+
557
+ get #d() { return privD(this); }
558
+
559
+ set e(v) { console.log('e'); }
560
+
561
+ set #e(v) { privE(this, v); }
562
+ }
563
+
564
+ initializeClass(Class);
565
+ */
566
+
567
+ export default function applyDecs2301(targetClass, memberDecs, classDecs, instanceBrand) {
568
+ return {
569
+ e: applyMemberDecs(targetClass, memberDecs, instanceBrand),
570
+ // Lazily apply class decorations so that member init locals can be properly bound.
571
+ get c() {
572
+ return applyClassDecs(targetClass, classDecs);
573
+ }
574
+ };
575
+ }