@vicin/sigil 1.2.6 → 1.3.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.
@@ -133,8 +133,7 @@
133
133
  * @param newRegistry - New Set<string> instance to use as the active registry, or `null` to disable checks.
134
134
  */
135
135
  replaceRegistry(newRegistry) {
136
- if (newRegistry)
137
- updateOptions({ registry: new _SigilRegistry(newRegistry) });
136
+ if (newRegistry) updateOptions({ registry: new _SigilRegistry(newRegistry) });
138
137
  else updateOptions({ registry: newRegistry });
139
138
  }
140
139
  /**
@@ -191,9 +190,7 @@
191
190
  * @param thisArg - Optional `this` context for the callback.
192
191
  */
193
192
  forEach(callback, thisArg) {
194
- this._registry.forEach(
195
- (ctor, label) => callback.call(thisArg, ctor, label)
196
- );
193
+ this._registry.forEach((ctor, label) => callback.call(thisArg, ctor, label));
197
194
  }
198
195
  /**
199
196
  * Get the size (number of entries) of the active registry.
@@ -254,9 +251,7 @@
254
251
  var __SIGIL__ = /* @__PURE__ */ Symbol.for("@Sigil.__SIGIL__");
255
252
  var __SIGIL_BASE__ = /* @__PURE__ */ Symbol.for("@Sigil.__SIGIL_BASE__");
256
253
  var __DECORATED__ = /* @__PURE__ */ Symbol.for("@Sigil.__DECORATED__");
257
- var __INHERITANCE_CHECKED__ = /* @__PURE__ */ Symbol.for(
258
- "@Sigil.__INHERITANCE_CHECKED__"
259
- );
254
+ var __INHERITANCE_CHECKED__ = /* @__PURE__ */ Symbol.for("@Sigil.__INHERITANCE_CHECKED__");
260
255
  var __LABEL__ = /* @__PURE__ */ Symbol.for("@Sigil.__LABEL__");
261
256
  var __TYPE__ = /* @__PURE__ */ Symbol.for("@Sigil.__TYPE__");
262
257
  var __TYPE_LINEAGE__ = /* @__PURE__ */ Symbol.for("@Sigil.__TYPE_LINEAGE__");
@@ -422,8 +417,7 @@
422
417
 
423
418
  // src/core/mixin.ts
424
419
  function Sigilify(Base, label, opts) {
425
- if (isSigilCtor(Base))
426
- throw new Error(`[Sigil Error] 'Sigilify(${label})' already siglified.`);
420
+ if (isSigilCtor(Base)) throw new Error(`[Sigil Error] 'Sigilify(${label})' already siglified.`);
427
421
  let l;
428
422
  if (label) {
429
423
  verifyLabel(label, opts);
@@ -476,9 +470,208 @@
476
470
  const ctor = getConstructor(this);
477
471
  if (!ctor) {
478
472
  if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
479
- throw new Error(
480
- `[Sigil Error] 'Sigilify(${label})' instance without constructor`
481
- );
473
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
474
+ return;
475
+ }
476
+ checkInheritance(ctor);
477
+ }
478
+ /**
479
+ * Runtime predicate indicating whether `obj` is an instance produced by a sigil class.
480
+ *
481
+ * @param obj - The value to test.
482
+ * @returns `true` if `obj` is a sigil instance.
483
+ */
484
+ static isSigilified(obj) {
485
+ return isSigilInstance(obj);
486
+ }
487
+ /**
488
+ * Check whether `other` is (or inherits from) the type represented by the calling constructor.
489
+ *
490
+ * This replaces `instanceof` so that checks remain valid across bundles/realms
491
+ * and when subclassing.
492
+ *
493
+ * @typeParam T - The calling constructor type (narrowing the returned instance type).
494
+ * @param this - The constructor performing the check.
495
+ * @param other - The object to test.
496
+ * @returns `true` if `other` is an instance of this type or a subtype.
497
+ */
498
+ static isOfType(other) {
499
+ var _a;
500
+ if (!isSigilInstance(other)) return false;
501
+ const otherSet = (_a = getConstructor(other)) == null ? void 0 : _a[__TYPE_SET__];
502
+ const thisType = this[__TYPE__];
503
+ return !!otherSet && otherSet.has(thisType);
504
+ }
505
+ /**
506
+ * Strict lineage check: compares the type symbol lineage arrays element-by-element.
507
+ *
508
+ * @typeParam T - The calling constructor type.
509
+ * @param this - The constructor performing the check.
510
+ * @param other - The object to test.
511
+ * @returns `true` if `other` has an identical lineage up to the length of this constructor's lineage.
512
+ */
513
+ static isOfTypeStrict(other) {
514
+ var _a;
515
+ if (!isSigilInstance(other)) return false;
516
+ const otherLineage = (_a = getConstructor(other)) == null ? void 0 : _a[__TYPE_LINEAGE__];
517
+ const thisLineage = this[__TYPE_LINEAGE__];
518
+ return !!otherLineage && thisLineage.every((s, i) => s === otherLineage[i]);
519
+ }
520
+ /**
521
+ * Check whether `other` is (or inherits from) the type instance.
522
+ *
523
+ * Allows 'instanceof' like checks but in instances.
524
+ *
525
+ * @typeParam T - The instance type.
526
+ * @param this - The instance performing the check.
527
+ * @param other - The object to test.
528
+ * @returns `true` if `other` is the same instance of this type or a subtype.
529
+ */
530
+ isOfType(other) {
531
+ var _a;
532
+ if (!isSigilInstance(other)) return false;
533
+ const otherSet = (_a = getConstructor(other)) == null ? void 0 : _a[__TYPE_SET__];
534
+ const thisType = getConstructor(this)[__TYPE__];
535
+ return !!otherSet && otherSet.has(thisType);
536
+ }
537
+ /**
538
+ * Strict lineage check: compares the type symbol lineage arrays element-by-element.
539
+ *
540
+ * Allows 'instanceof' like checks but in instances.
541
+ *
542
+ * @typeParam T - The instance type.
543
+ * @param this - The instance performing the check.
544
+ * @param other - The object to test.
545
+ * @returns `true` if `other` has an identical lineage up to the length of this instance's lineage.
546
+ */
547
+ isOfTypeStrict(other) {
548
+ var _a, _b;
549
+ if (!isSigilInstance(other)) return false;
550
+ const otherLineage = (_a = getConstructor(other)) == null ? void 0 : _a[__TYPE_LINEAGE__];
551
+ const thisLineage = (_b = getConstructor(this)) == null ? void 0 : _b[__TYPE_LINEAGE__];
552
+ return !!otherLineage && thisLineage.every((s, i) => s === otherLineage[i]);
553
+ }
554
+ /**
555
+ * Returns the human-readable sigil label of this instance's constructor.
556
+ *
557
+ * @returns The label string (e.g. '@scope/pkg.ClassName') or '@Sigil.unknown' in DEV when constructor is missing.
558
+ */
559
+ getSigilLabel() {
560
+ var _a;
561
+ const ctor = getConstructor(this);
562
+ if (!ctor) {
563
+ if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
564
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
565
+ return "@Sigil.unknown";
566
+ }
567
+ return ctor.SigilLabel;
568
+ }
569
+ /**
570
+ * Returns the runtime sigil type symbol of this instance's constructor.
571
+ *
572
+ * @returns The symbol that identifies this type at runtime.
573
+ */
574
+ getSigilType() {
575
+ var _a;
576
+ const ctor = getConstructor(this);
577
+ if (!ctor) {
578
+ if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
579
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
580
+ return /* @__PURE__ */ Symbol.for("@Sigil.unknown");
581
+ }
582
+ return ctor.SigilType;
583
+ }
584
+ /**
585
+ * Returns a copy of the sigil type symbol lineage for this instance's constructor.
586
+ *
587
+ * @returns readonly array of symbols representing the type lineage.
588
+ */
589
+ getSigilTypeLineage() {
590
+ var _a;
591
+ const ctor = getConstructor(this);
592
+ if (!ctor) {
593
+ if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
594
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
595
+ return [/* @__PURE__ */ Symbol.for("@Sigil.unknown")];
596
+ }
597
+ return ctor.SigilTypeLineage;
598
+ }
599
+ /**
600
+ * Returns a readonly copy of the sigil type symbol set for this instance's constructor.
601
+ *
602
+ * @returns A Readonly Set of symbols representing the type lineage for O(1) membership tests.
603
+ */
604
+ getSigilTypeSet() {
605
+ var _a;
606
+ const ctor = getConstructor(this);
607
+ if (!ctor) {
608
+ if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
609
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
610
+ return /* @__PURE__ */ new Set([/* @__PURE__ */ Symbol.for("@Sigil.unknown")]);
611
+ }
612
+ return ctor.SigilTypeSet;
613
+ }
614
+ }
615
+ decorateCtor(Sigilified, l, opts, true);
616
+ markSigil(Sigilified);
617
+ markSigilBase(Sigilified);
618
+ return Sigilified;
619
+ }
620
+ function SigilifyAbstract(Base, label, opts) {
621
+ if (isSigilCtor(Base)) throw new Error(`[Sigil Error] 'Sigilify(${label})' already siglified.`);
622
+ let l;
623
+ if (label) {
624
+ verifyLabel(label, opts);
625
+ l = label;
626
+ } else l = generateRandomLabel();
627
+ class Sigilified extends Base {
628
+ /**
629
+ * Class-level human-readable label constant for this sigil constructor.
630
+ */
631
+ static get SigilLabel() {
632
+ return this[__LABEL__];
633
+ }
634
+ /**
635
+ * Class-level unique runtime symbol used as the type identifier.
636
+ *
637
+ * This symbol is created with `Symbol.for(label)` during decoration so it is
638
+ * stable across realms that share the same global symbol registry.
639
+ */
640
+ static get SigilType() {
641
+ return this[__TYPE__];
642
+ }
643
+ /**
644
+ * Copy of the linearized sigil type symbol chain for the current constructor.
645
+ *
646
+ * Useful for debugging and performing strict lineage comparisons.
647
+ *
648
+ * @returns An array of symbols representing parent → child type symbols.
649
+ */
650
+ static get SigilTypeLineage() {
651
+ var _a;
652
+ return [...(_a = this[__TYPE_LINEAGE__]) != null ? _a : []];
653
+ }
654
+ /**
655
+ * Copy of the sigil type symbol set for the current constructor.
656
+ *
657
+ * Useful for quick membership checks (O(1) lookups) and debugging.
658
+ *
659
+ * @returns A Readonly Set of symbols that represent the type lineage.
660
+ */
661
+ static get SigilTypeSet() {
662
+ const set = /* @__PURE__ */ new Set();
663
+ for (const s of this[__TYPE_SET__]) set.add(s);
664
+ return set;
665
+ }
666
+ constructor(...args) {
667
+ var _a;
668
+ super(...args);
669
+ if (Object.getPrototypeOf(this) !== new.target.prototype)
670
+ Object.setPrototypeOf(this, new.target.prototype);
671
+ const ctor = getConstructor(this);
672
+ if (!ctor) {
673
+ if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
674
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
482
675
  return;
483
676
  }
484
677
  checkInheritance(ctor);
@@ -508,11 +701,11 @@
508
701
  * @returns `true` if `other` is an instance of this type or a subtype.
509
702
  */
510
703
  static isOfType(other) {
511
- if (!isSigilInstance(other)) return false;
512
- const otherCtor = getConstructor(other);
513
- if (!otherCtor) return false;
514
- const otherSet = otherCtor[__TYPE_SET__];
515
- return !!otherSet && otherSet.has(this.SigilType);
704
+ var _a;
705
+ if (!isSigilInstance(other) || !isSigilCtor(this)) return false;
706
+ const otherSet = (_a = getConstructor(other)) == null ? void 0 : _a[__TYPE_SET__];
707
+ const thisType = this[__TYPE__];
708
+ return !!otherSet && otherSet.has(thisType);
516
709
  }
517
710
  /**
518
711
  * Strict lineage check: compares the type symbol lineage arrays element-by-element.
@@ -527,13 +720,46 @@
527
720
  * @returns `true` if `other` has an identical lineage up to the length of this constructor's lineage.
528
721
  */
529
722
  static isOfTypeStrict(other) {
530
- if (!isSigilInstance(other)) return false;
531
- const otherCtor = getConstructor(other);
532
- if (!otherCtor) return false;
533
- const otherLineage = otherCtor[__TYPE_LINEAGE__];
723
+ var _a;
724
+ if (!isSigilInstance(other) || !isSigilCtor(this)) return false;
725
+ const otherLineage = (_a = getConstructor(other)) == null ? void 0 : _a[__TYPE_LINEAGE__];
534
726
  const thisLineage = this[__TYPE_LINEAGE__];
535
727
  return !!otherLineage && thisLineage.every((s, i) => s === otherLineage[i]);
536
728
  }
729
+ /**
730
+ * Check whether `other` is (or inherits from) the type instance.
731
+ *
732
+ * Allows 'instanceof' like checks but in instances.
733
+ *
734
+ * @typeParam T - The instance type.
735
+ * @param this - The instance performing the check.
736
+ * @param other - The object to test.
737
+ * @returns `true` if `other` is the same instance of this type or a subtype.
738
+ */
739
+ isOfType(other) {
740
+ var _a;
741
+ if (!isSigilInstance(other) || !isSigilInstance(this)) return false;
742
+ const otherSet = (_a = getConstructor(other)) == null ? void 0 : _a[__TYPE_SET__];
743
+ const thisType = getConstructor(this)[__TYPE__];
744
+ return !!otherSet && otherSet.has(thisType);
745
+ }
746
+ /**
747
+ * Strict lineage check: compares the type symbol lineage arrays element-by-element.
748
+ *
749
+ * Allows 'instanceof' like checks but in instances.
750
+ *
751
+ * @typeParam T - The instance type.
752
+ * @param this - The instance performing the check.
753
+ * @param other - The object to test.
754
+ * @returns `true` if `other` has an identical lineage up to the length of this instance's lineage.
755
+ */
756
+ isOfTypeStrict(other) {
757
+ var _a, _b;
758
+ if (!isSigilInstance(other) || !isSigilInstance(this)) return false;
759
+ const otherLineage = (_a = getConstructor(other)) == null ? void 0 : _a[__TYPE_LINEAGE__];
760
+ const thisLineage = (_b = getConstructor(this)) == null ? void 0 : _b[__TYPE_LINEAGE__];
761
+ return !!otherLineage && thisLineage.every((s, i) => s === otherLineage[i]);
762
+ }
537
763
  /**
538
764
  * Returns the human-readable sigil label of this instance's constructor.
539
765
  *
@@ -544,9 +770,7 @@
544
770
  const ctor = getConstructor(this);
545
771
  if (!ctor) {
546
772
  if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
547
- throw new Error(
548
- `[Sigil Error] 'Sigilify(${label})' instance without constructor`
549
- );
773
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
550
774
  return "@Sigil.unknown";
551
775
  }
552
776
  return ctor.SigilLabel;
@@ -561,9 +785,7 @@
561
785
  const ctor = getConstructor(this);
562
786
  if (!ctor) {
563
787
  if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
564
- throw new Error(
565
- `[Sigil Error] 'Sigilify(${label})' instance without constructor`
566
- );
788
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
567
789
  return /* @__PURE__ */ Symbol.for("@Sigil.unknown");
568
790
  }
569
791
  return ctor.SigilType;
@@ -578,9 +800,7 @@
578
800
  const ctor = getConstructor(this);
579
801
  if (!ctor) {
580
802
  if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
581
- throw new Error(
582
- `[Sigil Error] 'Sigilify(${label})' instance without constructor`
583
- );
803
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
584
804
  return [/* @__PURE__ */ Symbol.for("@Sigil.unknown")];
585
805
  }
586
806
  return ctor.SigilTypeLineage;
@@ -595,9 +815,7 @@
595
815
  const ctor = getConstructor(this);
596
816
  if (!ctor) {
597
817
  if ((_a = opts == null ? void 0 : opts.devMarker) != null ? _a : OPTIONS.devMarker)
598
- throw new Error(
599
- `[Sigil Error] 'Sigilify(${label})' instance without constructor`
600
- );
818
+ throw new Error(`[Sigil Error] 'Sigilify(${label})' instance without constructor`);
601
819
  return /* @__PURE__ */ new Set([/* @__PURE__ */ Symbol.for("@Sigil.unknown")]);
602
820
  }
603
821
  return ctor.SigilTypeSet;
@@ -687,6 +905,7 @@
687
905
  exports.SigilError = SigilError;
688
906
  exports.SigilRegistry = SigilRegistry;
689
907
  exports.Sigilify = Sigilify;
908
+ exports.SigilifyAbstract = SigilifyAbstract;
690
909
  exports.WithSigil = WithSigil;
691
910
  exports.getActiveRegistry = getActiveRegistry;
692
911
  exports.isDecorated = isDecorated;