@geastack/compiler 1.0.31 → 1.0.33

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.
@@ -622,9 +622,997 @@ bool nativeDocumentEnumerable(const Ref<T>& source, const PropertyKey& key) {
622
622
  return false;
623
623
  }
624
624
 
625
+ /**
626
+ * The document protocol of an `ArrayObject<E>`, written once for every `E`.
627
+ *
628
+ * An array's own properties are its elements, `length` and its expando, and
629
+ * its element access is already type-erased behind `NativeArrayOps`; the
630
+ * element type changes nothing else. Instantiating the generic
631
+ * `nativeDocument*` templates per element type compiled ~20 KB of identical
632
+ * code into the binary for every array type a program boxes. What really
633
+ * depends on the type is the handful of guards below, which are small.
634
+ *
635
+ * `payload` is the handle's address, the convention every erased table
636
+ * already takes from `Value::payload()`: a `Ref<void>` word read as the
637
+ * `Ref<ArrayObject<E>>` it holds. `source` is the same handle. Each function
638
+ * is the generic template's array path, unchanged.
639
+ */
640
+ struct NativeArrayDocumentBridge {
641
+ const NativeArrayOps* (*elements)();
642
+ bool (*fieldsWritable)(const void* payload);
643
+ bool (*extensible)(const void* payload);
644
+ NativeCallReceiver (*receiver)(const void* payload);
645
+ void (*requireLane)(const void* payload, const NativeDescriptorData& value);
646
+ };
647
+
648
+ inline void noteArrayExpandoKeyCreated(const Ref<void>& source, const PropertyKey& key) {
649
+ noteNativeOwnKeyCreated(source, key, [](std::vector<PropertyKey>&) {});
650
+ }
651
+
652
+ [[gnu::noinline]] inline bool arrayDocumentHas(const Ref<void>& source, const void* payload, const NativeArrayDocumentBridge& bridge,
653
+ const PropertyKey& key, bool own) {
654
+ const auto* elements = bridge.elements();
655
+ if (const auto* origin = record::nativeViewRoute(source); origin && origin->hasOwn)
656
+ return own ? origin->hasOwn(origin->immediate, key) : origin->hasProperty && origin->hasProperty(origin->immediate, key);
657
+ if (!key.isSymbol() && key.text() == "length") return true;
658
+ std::size_t index = 0;
659
+ if (arrayIndexOfKey(key, index)) return index < elements->length(payload) && elements->has(payload, index);
660
+ const auto overlay = expandoFor(elements->owner(payload), false);
661
+ if (overlay && (own ? overlay->ownProperty(key) != nullptr : overlay->hasProperty(key))) return true;
662
+ if (own) return false;
663
+ return dynamicArrayPrototypeHas(key) || ordinaryObjectPrototypeHas(key);
664
+ }
665
+
666
+ [[gnu::noinline]] inline bool arrayDocumentRead(const Ref<void>&, const void* payload, const NativeArrayDocumentBridge& bridge,
667
+ const PropertyKey& key, Value& out) {
668
+ const auto* elements = bridge.elements();
669
+ if (!key.isSymbol() && key.text() == "length") {
670
+ out = Value::box(Value::Tag::Number, static_cast<double>(elements->length(payload)));
671
+ return true;
672
+ }
673
+ std::size_t index = 0;
674
+ if (arrayIndexOfKey(key, index)) {
675
+ elements->element(payload, index, out);
676
+ return true;
677
+ }
678
+ const auto sidecar = expandoFor(elements->owner(payload), false);
679
+ if (sidecar && sidecar->hasProperty(key)) {
680
+ out = sidecar->getWithNativeReceiver(key, bridge.receiver(payload));
681
+ return true;
682
+ }
683
+ out = dynamicArrayPrototypeGet(key);
684
+ return out.tag() != Value::Tag::Undefined;
685
+ }
686
+
687
+ [[gnu::noinline]] inline bool arrayDocumentDescriptor(const Ref<void>& source, const void* payload, const NativeArrayDocumentBridge& bridge,
688
+ const PropertyKey& key, PropertyDescriptor& out) {
689
+ const auto* elements = bridge.elements();
690
+ const bool frozen = elements->frozen(payload) || !bridge.fieldsWritable(payload);
691
+ if (!key.isSymbol() && key.text() == "length") {
692
+ out = PropertyDescriptor::assignment(Value::box(Value::Tag::Number, static_cast<double>(elements->length(payload))));
693
+ out.enumerable = out.configurable = false;
694
+ out.writable = !frozen && (elements->writableLength == nullptr || elements->writableLength(payload));
695
+ return true;
696
+ }
697
+ std::size_t index = 0;
698
+ if (arrayIndexOfKey(key, index)) return elements->ownDescriptor(payload, index, out);
699
+ if (!source) return false;
700
+ Optional<PropertyDescriptor> original;
701
+ if (record::ownNativeViewDescriptor(source, key, original)) {
702
+ if (!original.has_value()) return false;
703
+ out = *original;
704
+ return true;
705
+ }
706
+ const Ref<DynamicObject> expando = expandoFor(elements->owner(payload), false);
707
+ const PropertyDescriptor* descriptor = expando ? expando->ownProperty(key) : nullptr;
708
+ if (descriptor == nullptr) return false;
709
+ out = *descriptor;
710
+ return true;
711
+ }
712
+
713
+ [[gnu::noinline]] inline std::vector<PropertyKey> arrayDocumentKeys(const Ref<void>& source, const void* payload,
714
+ const NativeArrayDocumentBridge& bridge) {
715
+ const auto* elements = bridge.elements();
716
+ std::vector<PropertyKey> properties;
717
+ if (source) {
718
+ for (std::size_t index = 0; index < elements->length(payload); ++index)
719
+ if (elements->has(payload, index)) properties.push_back(arrayIndexedKey(index));
720
+ properties.push_back(PropertyKey::string("length"));
721
+ if (const auto expando = expandoFor(elements->owner(payload), false))
722
+ for (const auto& key : expando->ownKeys())
723
+ if (std::find(properties.begin(), properties.end(), key) == properties.end()) properties.push_back(key);
724
+ }
725
+ std::vector<PropertyKey> keys;
726
+ for (std::size_t index = 0; index < elements->length(payload); ++index) {
727
+ const auto key = PropertyKey::string(std::to_string(index));
728
+ if (!arrayDocumentHas(source, payload, bridge, key, true)) continue;
729
+ if (std::find(keys.begin(), keys.end(), key) == keys.end()) keys.push_back(key);
730
+ }
731
+ const auto length = PropertyKey::string("length");
732
+ if (std::find(keys.begin(), keys.end(), length) == keys.end()) keys.push_back(length);
733
+ for (auto& key : properties)
734
+ if (std::find(keys.begin(), keys.end(), key) == keys.end()) keys.push_back(std::move(key));
735
+ return ordinaryOwnPropertyKeyOrder(std::move(keys));
736
+ }
737
+
738
+ [[gnu::noinline]] inline bool arrayDocumentSet(const Ref<void>& source, void* payload, const NativeArrayDocumentBridge& bridge,
739
+ const PropertyKey& key, const Value& value) {
740
+ const auto* elements = bridge.elements();
741
+ if (elements->frozen(payload) || !bridge.fieldsWritable(payload)) return false;
742
+ std::size_t index = 0;
743
+ if (arrayIndexOfKey(key, index)) return elements->setElement(payload, index, value);
744
+ if (!key.isSymbol() && key.text() == "length") {
745
+ const double length = dynamicToNumber(value);
746
+ if (!std::isfinite(length) || length < 0 || length > 4294967295.0 || length != std::floor(length))
747
+ host::throwRuntimeError("RangeError", "Invalid array length");
748
+ elements->resize(payload, length);
749
+ return true;
750
+ }
751
+ const auto receiver = bridge.receiver(payload);
752
+ const auto anchor = elements->owner(payload);
753
+ const auto existing = expandoFor(anchor, false);
754
+ if (existing && existing->ownProperty(key) != nullptr) return existing->setWithNativeReceiver(key, value, receiver);
755
+ if (!bridge.extensible(payload)) return false;
756
+ if (!expandoFor(anchor, true)->setWithNativeReceiver(key, value, receiver)) return false;
757
+ noteArrayExpandoKeyCreated(source, key);
758
+ return true;
759
+ }
760
+
761
+ [[gnu::noinline]] inline bool arrayDocumentDefine(const Ref<void>& source, void* payload, const NativeArrayDocumentBridge& bridge,
762
+ const PropertyKey& key, const PropertyDescriptor& descriptor) {
763
+ const auto* elements = bridge.elements();
764
+ if (!key.isSymbol() && key.text() == "length") return elements->defineLength(payload, descriptor);
765
+ std::size_t index = 0;
766
+ if (arrayIndexOfKey(key, index)) return elements->define(payload, index, descriptor);
767
+ if (!source) return false;
768
+ if (record::immutableDescriptorSnapshotView(source))
769
+ host::throwRuntimeError("TypeError", "a native descriptor snapshot definition has no certified source protocol");
770
+ if (const auto defined = record::defineDocumentView(source, key, descriptor)) return *defined;
771
+ const auto anchor = elements->owner(payload);
772
+ const Ref<DynamicObject> existing = expandoFor(anchor, false);
773
+ if (existing && existing->ownProperty(key) != nullptr) return existing->defineOwnProperty(key, descriptor);
774
+ if (!bridge.extensible(payload)) return false;
775
+ if (!expandoFor(anchor, true)->defineOwnProperty(key, descriptor)) return false;
776
+ noteArrayExpandoKeyCreated(source, key);
777
+ return true;
778
+ }
779
+
780
+ [[gnu::noinline]] inline bool arrayDocumentDelete(const Ref<void>& source, void* payload, const NativeArrayDocumentBridge& bridge,
781
+ const PropertyKey& key) {
782
+ const auto* elements = bridge.elements();
783
+ if (!key.isSymbol() && key.text() == "length") return false;
784
+ std::size_t index = 0;
785
+ if (arrayIndexOfKey(key, index)) return elements->remove(payload, index);
786
+ if (!source) return true;
787
+ if (record::immutableDescriptorSnapshotView(source))
788
+ host::throwRuntimeError("TypeError", "a native descriptor snapshot deletion has no certified source protocol");
789
+ if (const auto* document = record::documentViewRoute(source)) return document->document->remove(document->held, key);
790
+ const Ref<DynamicObject> expando = expandoFor(elements->owner(payload), false);
791
+ return expando ? expando->deleteOwnProperty(key) : true;
792
+ }
793
+
794
+ [[gnu::noinline]] inline bool arrayDocumentEnumerable(const Ref<void>& source, const void* payload, const NativeArrayDocumentBridge& bridge,
795
+ const PropertyKey& key) {
796
+ const auto* elements = bridge.elements();
797
+ if (!key.isSymbol() && key.text() == "length") return false;
798
+ std::size_t index = 0;
799
+ if (arrayIndexOfKey(key, index)) {
800
+ PropertyDescriptor descriptor;
801
+ return elements->ownDescriptor(payload, index, descriptor) && descriptor.enumerable;
802
+ }
803
+ const auto overlay = expandoFor(elements->owner(payload), false);
804
+ const auto* descriptor = overlay ? overlay->ownProperty(key) : nullptr;
805
+ if (descriptor) return descriptor->enumerable;
806
+ if (arrayDocumentHas(source, payload, bridge, key, true))
807
+ host::throwRuntimeError("TypeError", "a native Document field has no enumerability-only protocol");
808
+ return false;
809
+ }
810
+
811
+ [[gnu::noinline]] inline std::optional<bool> arrayDocumentReadNative(const Ref<void>& source, const void* payload,
812
+ const NativeArrayDocumentBridge& bridge, const PropertyKey& key,
813
+ const NativeFieldRead& read) {
814
+ const auto* elements = bridge.elements();
815
+ if (record::hasLiveFieldView(source)) return record::readFieldView(source, key, read);
816
+ std::size_t index = 0;
817
+ if (arrayIndexOfKey(key, index)) {
818
+ PropertyDescriptor descriptor;
819
+ if (!elements->ownDescriptor(payload, index, descriptor)) return std::nullopt;
820
+ if (!descriptor.isAccessor())
821
+ return descriptor.nativeValue ? std::optional<bool>(descriptor.nativeValue.read(read)) : std::nullopt;
822
+ if (descriptor.nativeGet && descriptor.nativeGet.read != nullptr) return descriptor.nativeGet.readNative(bridge.receiver(payload), read);
823
+ return std::nullopt;
824
+ }
825
+ const auto sidecar = expandoFor(elements->owner(payload), false);
826
+ return sidecar ? readDocumentNativeObject(sidecar, key, bridge.receiver(payload), read) : std::nullopt;
827
+ }
828
+
829
+ [[gnu::noinline]] inline bool arrayDocumentSetNative(const Ref<void>& source, void* payload, const NativeArrayDocumentBridge& bridge,
830
+ const PropertyKey& key, const NativeDescriptorData& value,
831
+ const NativeFieldWrite& written) {
832
+ const auto* elements = bridge.elements();
833
+ if (record::hasLiveFieldView(source)) return record::writeFieldView(source, key, written);
834
+ std::size_t index = 0;
835
+ if (arrayIndexOfKey(key, index)) {
836
+ if (elements->frozen(payload) || !bridge.fieldsWritable(payload)) return false;
837
+ PropertyDescriptor current;
838
+ const bool present = elements->ownDescriptor(payload, index, current);
839
+ if (present && current.isAccessor()) {
840
+ if (!current.hasSet) return false;
841
+ const auto receiver = bridge.receiver(payload);
842
+ if (current.nativeSet && current.nativeSet.write) return current.nativeSet.writeNative(receiver, written);
843
+ if (current.nativeSet && current.nativeSet.dynamicWrite) {
844
+ current.nativeSet.observeWrite(receiver, value.materialize());
845
+ return true;
846
+ }
847
+ if (!current.set) return false;
848
+ current.set(receiver.dynamicValue(), value.materialize());
849
+ return true;
850
+ }
851
+ if (present && !current.writable) return false;
852
+ bridge.requireLane(payload, value);
853
+ auto descriptor = present ? PropertyDescriptor{} : PropertyDescriptor::assignment(Value{});
854
+ descriptor.hasValue = true;
855
+ descriptor.nativeValue = value;
856
+ return elements->define(payload, index, descriptor);
857
+ }
858
+ if (!key.isSymbol() && key.text() == "length")
859
+ host::throwRuntimeError("TypeError", "a native Document length assignment needs a numeric entry writer");
860
+ const auto receiver = bridge.receiver(payload);
861
+ const auto anchor = elements->owner(payload);
862
+ const auto existing = expandoFor(anchor, false);
863
+ if (existing && existing->ownProperty(key)) return existing->setNativeDescriptorWithNativeReceiver(key, value, written, receiver);
864
+ if (!bridge.extensible(payload)) return false;
865
+ const auto overlay = existing ? existing : expandoFor(anchor, true);
866
+ if (!overlay->setNativeDescriptorWithNativeReceiver(key, value, written, receiver)) return false;
867
+ if (overlay->ownProperty(key)) noteArrayExpandoKeyCreated(source, key);
868
+ return true;
869
+ }
870
+
871
+ template <typename T>
872
+ constexpr bool sharedArrayDocument() {
873
+ // The shared functions transcribe the generic templates' path for a type
874
+ // with none of these member protocols; a type that gains one must not be
875
+ // routed through them.
876
+ static_assert(!NativeFieldTable<T> && !TypedStringDictionaryTable<T> && !NativePrototypeTable<T> && !NativeOwnFieldPredicate<T> &&
877
+ !NativeOwnFieldPresenceTable<T> && !NativeOwnIndexPresenceTable<T> && !NativeOwnFieldEnumerableTable<T> &&
878
+ !NativeOwnIndexEnumerableTable<T> && !NativeOwnKeysTable<T> && !IsMapPayload<Ref<T>>::value,
879
+ "an array type with its own field protocol needs the generic document templates");
880
+ return true;
881
+ }
882
+
883
+ template <typename T>
884
+ inline constexpr NativeArrayDocumentBridge arrayDocumentBridge{
885
+ &nativeArrayOpsFor<Ref<T>>,
886
+ [](const void* payload) { return nativeOwnFieldsWritable(*static_cast<const Ref<T>*>(payload)); },
887
+ [](const void* payload) { return nativeIsExtensible(*static_cast<const Ref<T>*>(payload)); },
888
+ [](const void* payload) { return NativeCallReceiver::object(*static_cast<const Ref<T>*>(payload)); },
889
+ [](const void* payload, const NativeDescriptorData& value) { requireNativeEntryFitsArrayLane(**static_cast<const Ref<T>*>(payload), value); }};
890
+
891
+ template <typename T>
892
+ const NativeDocumentOps* nativeArrayDocumentOpsFor() {
893
+ static_assert(sharedArrayDocument<T>());
894
+ static const NativeDocumentOps operations{
895
+ +[](const Ref<void>& source, const PropertyKey& key, Value& out) { return arrayDocumentRead(source, &source, arrayDocumentBridge<T>, key, out); },
896
+ +[](const Ref<void>& source, const PropertyKey& key, const Value& value) {
897
+ return arrayDocumentSet(source, const_cast<Ref<void>*>(&source), arrayDocumentBridge<T>, key, value);
898
+ },
899
+ +[](const Ref<void>& source, const PropertyKey& key, PropertyDescriptor& out) {
900
+ return arrayDocumentDescriptor(source, &source, arrayDocumentBridge<T>, key, out);
901
+ },
902
+ +[](const Ref<void>& source, const PropertyKey& key, const PropertyDescriptor& descriptor) {
903
+ return arrayDocumentDefine(source, const_cast<Ref<void>*>(&source), arrayDocumentBridge<T>, key, descriptor);
904
+ },
905
+ +[](const Ref<void>& source, const PropertyKey& key) {
906
+ return arrayDocumentDelete(source, const_cast<Ref<void>*>(&source), arrayDocumentBridge<T>, key);
907
+ },
908
+ +[](const Ref<void>& source, const PropertyKey& key, bool own) { return arrayDocumentHas(source, &source, arrayDocumentBridge<T>, key, own); },
909
+ +[](const Ref<void>& source) { return arrayDocumentKeys(source, &source, arrayDocumentBridge<T>); },
910
+ +[](const Ref<void>& source, const PropertyKey& key) { return arrayDocumentEnumerable(source, &source, arrayDocumentBridge<T>, key); },
911
+ +[](const Ref<void>& source) { return Value::box(Value::Tag::Object, source.template staticCast<T>()); },
912
+ payloadTypeTagFor<Ref<T>>(), IsArrayPayload<Ref<T>>::value, false, nullptr,
913
+ +[](const Ref<void>& source, const PropertyKey& key, const NativeFieldRead& read) {
914
+ return arrayDocumentReadNative(source, &source, arrayDocumentBridge<T>, key, read);
915
+ },
916
+ +[](const Ref<void>& source, const PropertyKey& key, const NativeDescriptorData& value, const NativeFieldWrite& written) {
917
+ return arrayDocumentSetNative(source, const_cast<Ref<void>*>(&source), arrayDocumentBridge<T>, key, value, written);
918
+ }};
919
+ return &operations;
920
+ }
921
+
922
+ /**
923
+ * The document protocol of every other native object -- a generated record or
924
+ * class, or a runtime type such as `Error` or `TypedArray` -- written once.
925
+ *
926
+ * The generic `nativeDocument*` templates ask each type at compile time which
927
+ * member hooks it states (`gea_ownFieldPresent`, `gea_readPrototypeProperty`,
928
+ * ...) and were instantiated per type: 10-22 KB of mostly identical key
929
+ * checks, expando and view routing, prototype fallbacks and refusals for
930
+ * every class or record a program reads dynamically. Here each hook is a
931
+ * pointer, null when the type does not state it, so `if constexpr (hook)`
932
+ * becomes `if (bridge.hook)` and only the hook calls stay per type.
933
+ *
934
+ * Hooks take the erased handle and read it as the `Ref<T>` it holds -- the
935
+ * convention every erased table already takes from `Value::payload()`.
936
+ */
937
+ struct NativeRecordDocumentBridge {
938
+ bool (*fieldsWritable)(const Ref<void>& source);
939
+ bool (*extensible)(const Ref<void>& source);
940
+ NativeCallReceiver (*receiver)(const Ref<void>& source);
941
+ void (*keyCreated)(const Ref<void>& source, const PropertyKey& key);
942
+ const NativeFieldOps* (*fields)();
943
+ // A positional tuple is an Array exotic object as well: its element table,
944
+ // consulted first exactly where the generic templates consult it. Null for
945
+ // every other type.
946
+ const NativeArrayOps* (*elements)();
947
+ bool fieldTable;
948
+ bool indexFieldTable;
949
+ bool wholeOwnFieldProtocol;
950
+ bool prototypeTable;
951
+ bool ownFieldPredicate;
952
+ bool (*ownFieldPresent)(const Ref<void>&, const PropertyKey&, bool&);
953
+ bool (*ownIndexPresent)(const Ref<void>&, const PropertyKey&, bool&);
954
+ bool (*ownFieldEnumerable)(const Ref<void>&, const PropertyKey&, bool&);
955
+ bool (*ownIndexEnumerable)(const Ref<void>&, const PropertyKey&, bool&);
956
+ bool (*matchesOwnField)(const Ref<void>&, const PropertyKey&);
957
+ bool (*matchesOwnIndex)(const Ref<void>&, const PropertyKey&);
958
+ bool (*readOwnField)(const Ref<void>&, const PropertyKey&, Value&);
959
+ bool (*readOwnIndex)(const Ref<void>&, const PropertyKey&, Value&);
960
+ bool (*ownFieldDescriptor)(const Ref<void>&, const PropertyKey&, PropertyDescriptor&);
961
+ bool (*ownIndexDescriptor)(const Ref<void>&, const PropertyKey&, PropertyDescriptor&);
962
+ void (*ownFieldKeys)(const Ref<void>&, std::vector<PropertyKey>&);
963
+ bool (*defineOwnField)(const Ref<void>&, const PropertyKey&, const PropertyDescriptor&, bool extensible);
964
+ bool (*defineOwnIndex)(const Ref<void>&, const PropertyKey&, const PropertyDescriptor&, bool extensible);
965
+ bool (*deleteOwnField)(const Ref<void>&, const PropertyKey&);
966
+ bool (*deleteOwnIndex)(const Ref<void>&, const PropertyKey&);
967
+ // Each native field hook is stated only together with its predicate.
968
+ bool (*readOwnFieldNative)(const Ref<void>&, const PropertyKey&, NativeFieldRead&);
969
+ bool (*readOwnIndexNative)(const Ref<void>&, const PropertyKey&, NativeFieldRead&);
970
+ bool (*writeOwnFieldNative)(const Ref<void>&, const PropertyKey&, const NativeFieldWrite&, bool extensible);
971
+ bool (*writeOwnIndexNative)(const Ref<void>&, const PropertyKey&, const NativeFieldWrite&, bool extensible);
972
+ bool (*readPrototypeProperty)(const Ref<void>&, const PropertyKey&, Value&);
973
+ bool (*hasPrototypeProperty)(const Ref<void>&, const PropertyKey&);
974
+ NativePrototypeOps::SetResult (*setPrototypePropertyNative)(const Ref<void>&, const PropertyKey&, const Value&, const NativeCallReceiver&);
975
+ NativePrototypeOps::SetResult (*setPrototypePropertyFieldNative)(const Ref<void>&, const PropertyKey&, const NativeFieldWrite&,
976
+ const NativeCallReceiver&);
977
+ };
978
+
979
+ [[gnu::noinline]] inline bool recordDocumentHas(const Ref<void>& source, const NativeRecordDocumentBridge& bridge, const PropertyKey& key,
980
+ bool own) {
981
+ if (const auto* origin = record::nativeViewRoute(source); origin && origin->hasOwn)
982
+ return own ? origin->hasOwn(origin->immediate, key) : origin->hasProperty && origin->hasProperty(origin->immediate, key);
983
+ if (bridge.elements && !key.isSymbol() && key.text() == "length") return true;
984
+ if (bridge.ownFieldPresent) {
985
+ bool present = false;
986
+ if (bridge.ownFieldPresent(source, key, present) && present) return true;
987
+ } else if (bridge.fieldTable && bridge.ownFieldPredicate) {
988
+ if (bridge.matchesOwnField(source, key)) host::throwRuntimeError("TypeError", "a native Document field has no presence-only protocol");
989
+ }
990
+ if (bridge.ownIndexPresent) {
991
+ bool present = false;
992
+ if (bridge.ownIndexPresent(source, key, present) && present) return true;
993
+ }
994
+ const auto overlay = expandoFor(source, false);
995
+ if (overlay && (own ? overlay->ownProperty(key) != nullptr : overlay->hasProperty(key))) return true;
996
+ if (own) return false;
997
+ if (bridge.hasPrototypeProperty && bridge.hasPrototypeProperty(source, key)) return true;
998
+ if (bridge.elements) return dynamicArrayPrototypeHas(key) || ordinaryObjectPrototypeHas(key);
999
+ return ordinaryObjectPrototypeHas(key);
1000
+ }
1001
+
1002
+ [[gnu::noinline]] inline bool recordDocumentRead(const Ref<void>& source, const NativeRecordDocumentBridge& bridge, const PropertyKey& key,
1003
+ Value& out) {
1004
+ const auto* elements = bridge.elements ? bridge.elements() : nullptr;
1005
+ if (elements != nullptr) {
1006
+ if (!key.isSymbol() && key.text() == "length") {
1007
+ out = Value::box(Value::Tag::Number, static_cast<double>(elements->length(&source)));
1008
+ return true;
1009
+ }
1010
+ std::size_t index = 0;
1011
+ if (arrayIndexOfKey(key, index)) {
1012
+ elements->element(&source, index, out);
1013
+ return true;
1014
+ }
1015
+ }
1016
+ if (const auto* fields = bridge.fields()) {
1017
+ if (fields->read(&source, key, out) || fields->readIndex(&source, key, out)) return true;
1018
+ if (fields->matchesField(&source, key) || fields->matchesIndex(&source, key)) return false;
1019
+ }
1020
+ if (bridge.ownFieldPresent) {
1021
+ bool present = false;
1022
+ if (bridge.ownFieldPresent(source, key, present) && present)
1023
+ host::throwRuntimeError("TypeError", "a native Document field has no published value protocol");
1024
+ }
1025
+ const auto sidecar = expandoFor(source, false);
1026
+ if (sidecar && sidecar->hasProperty(key)) {
1027
+ out = sidecar->getWithNativeReceiver(key, bridge.receiver(source));
1028
+ return true;
1029
+ }
1030
+ if (elements != nullptr) {
1031
+ out = dynamicArrayPrototypeGet(key);
1032
+ return out.tag() != Value::Tag::Undefined;
1033
+ }
1034
+ if (bridge.readPrototypeProperty && bridge.readPrototypeProperty(source, key, out)) return true;
1035
+ if (bridge.hasPrototypeProperty && bridge.hasPrototypeProperty(source, key))
1036
+ host::throwRuntimeError("TypeError", "a native Document prototype property has no published read protocol");
1037
+ return false;
1038
+ }
1039
+
1040
+ [[gnu::noinline]] inline bool recordDocumentDescriptor(const Ref<void>& source, const NativeRecordDocumentBridge& bridge,
1041
+ const PropertyKey& key, PropertyDescriptor& out) {
1042
+ if (const auto* elements = bridge.elements ? bridge.elements() : nullptr) {
1043
+ const bool frozen = elements->frozen(&source) || !bridge.fieldsWritable(source);
1044
+ if (!key.isSymbol() && key.text() == "length") {
1045
+ out = PropertyDescriptor::assignment(Value::box(Value::Tag::Number, static_cast<double>(elements->length(&source))));
1046
+ out.enumerable = out.configurable = false;
1047
+ out.writable = !frozen && (elements->writableLength == nullptr || elements->writableLength(&source));
1048
+ return true;
1049
+ }
1050
+ std::size_t index = 0;
1051
+ if (arrayIndexOfKey(key, index) && elements->ownDescriptor != nullptr) return elements->ownDescriptor(&source, index, out);
1052
+ }
1053
+ return recordOwnDescriptor(source, bridge, key, out);
1054
+ }
1055
+
1056
+ /** `nativeOwnPropertyDescriptor` of a shared-record type. */
1057
+ [[gnu::noinline]] inline bool recordOwnDescriptor(const Ref<void>& source, const NativeRecordDocumentBridge& bridge, const PropertyKey& key,
1058
+ PropertyDescriptor& out) {
1059
+ if (!source) return false;
1060
+ Optional<PropertyDescriptor> original;
1061
+ if (record::ownNativeViewDescriptor(source, key, original)) {
1062
+ if (!original.has_value()) return false;
1063
+ out = *original;
1064
+ return true;
1065
+ }
1066
+ if (bridge.ownFieldDescriptor) {
1067
+ PropertyDescriptor descriptor;
1068
+ if (bridge.ownFieldDescriptor(source, key, descriptor)) {
1069
+ if (!bridge.fieldsWritable(source)) descriptor.writable = false;
1070
+ out = descriptor;
1071
+ return true;
1072
+ }
1073
+ }
1074
+ if (bridge.wholeOwnFieldProtocol && bridge.matchesOwnField(source, key)) return false;
1075
+ if (bridge.fieldTable) {
1076
+ Value found;
1077
+ if (bridge.readOwnField(source, key, found)) {
1078
+ out = PropertyDescriptor::assignment(found);
1079
+ out.configurable = false;
1080
+ out.writable = bridge.fieldsWritable(source);
1081
+ return true;
1082
+ }
1083
+ if (bridge.indexFieldTable) {
1084
+ PropertyDescriptor descriptor;
1085
+ if (bridge.ownIndexDescriptor(source, key, descriptor)) {
1086
+ out = descriptor;
1087
+ return true;
1088
+ }
1089
+ if (bridge.matchesOwnIndex(source, key)) return false;
1090
+ }
1091
+ }
1092
+ const Ref<DynamicObject> expando = expandoFor(source, false);
1093
+ const PropertyDescriptor* descriptor = expando ? expando->ownProperty(key) : nullptr;
1094
+ if (descriptor == nullptr) return false;
1095
+ out = *descriptor;
1096
+ return true;
1097
+ }
1098
+
1099
+ [[gnu::noinline]] inline std::vector<PropertyKey> recordDocumentOwnKeys(const Ref<void>& source, const NativeRecordDocumentBridge& bridge) {
1100
+ std::vector<PropertyKey> keys;
1101
+ if (!source) return keys;
1102
+ if (record::ownNativeViewKeys(source, keys, false)) return keys;
1103
+ keys.reserve(16);
1104
+ if (bridge.ownFieldKeys) bridge.ownFieldKeys(source, keys);
1105
+ if (const Ref<DynamicObject> expando = expandoFor(source, false)) {
1106
+ const std::size_t layout = keys.size();
1107
+ for (const PropertyKey& key : expando->ownKeys()) {
1108
+ if (std::find(keys.begin(), keys.begin() + static_cast<std::ptrdiff_t>(layout), key) == keys.begin() + static_cast<std::ptrdiff_t>(layout))
1109
+ keys.push_back(key);
1110
+ }
1111
+ }
1112
+ return nativeOwnKeysInCreationOrder(source.get(), std::move(keys));
1113
+ }
1114
+
1115
+ [[gnu::noinline]] inline std::vector<PropertyKey> recordDocumentKeys(const Ref<void>& source, const NativeRecordDocumentBridge& bridge) {
1116
+ auto keys = recordDocumentOwnKeys(source, bridge);
1117
+ if (const auto* elements = bridge.elements ? bridge.elements() : nullptr) {
1118
+ auto properties = std::move(keys);
1119
+ keys.clear();
1120
+ for (std::size_t index = 0; index < elements->length(&source); ++index) {
1121
+ if (!recordDocumentHas(source, bridge, PropertyKey::string(std::to_string(index)), true)) continue;
1122
+ const auto key = PropertyKey::string(std::to_string(index));
1123
+ if (std::find(keys.begin(), keys.end(), key) == keys.end()) keys.push_back(key);
1124
+ }
1125
+ const auto length = PropertyKey::string("length");
1126
+ if (std::find(keys.begin(), keys.end(), length) == keys.end()) keys.push_back(length);
1127
+ for (auto& key : properties)
1128
+ if (std::find(keys.begin(), keys.end(), key) == keys.end()) keys.push_back(std::move(key));
1129
+ }
1130
+ return ordinaryOwnPropertyKeyOrder(std::move(keys));
1131
+ }
1132
+
1133
+ [[gnu::noinline]] inline bool recordDocumentSet(const Ref<void>& source, const NativeRecordDocumentBridge& bridge, const PropertyKey& key,
1134
+ const Value& value) {
1135
+ if (const auto* elements = bridge.elements ? bridge.elements() : nullptr) {
1136
+ auto mutableSource = source;
1137
+ if (elements->frozen(&source) || !bridge.fieldsWritable(source)) return false;
1138
+ std::size_t index = 0;
1139
+ if (arrayIndexOfKey(key, index)) return elements->setElement(&mutableSource, index, value);
1140
+ if (!key.isSymbol() && key.text() == "length") {
1141
+ const double length = dynamicToNumber(value);
1142
+ if (!std::isfinite(length) || length < 0 || length > 4294967295.0 || length != std::floor(length))
1143
+ host::throwRuntimeError("RangeError", "Invalid array length");
1144
+ elements->resize(&mutableSource, length);
1145
+ return true;
1146
+ }
1147
+ }
1148
+ if (const auto* fields = bridge.fields()) {
1149
+ auto* payload = const_cast<Ref<void>*>(&source);
1150
+ if (fields->matchesField(&source, key))
1151
+ return bridge.fieldsWritable(source) && fields->write(payload, key, value, bridge.extensible(source));
1152
+ if (fields->matchesIndex(&source, key)) return fields->writeIndex(payload, key, value, bridge.extensible(source));
1153
+ }
1154
+ const auto receiver = bridge.receiver(source);
1155
+ const auto existing = expandoFor(source, false);
1156
+ if (existing && existing->ownProperty(key) != nullptr) return existing->setWithNativeReceiver(key, value, receiver);
1157
+ if (bridge.setPrototypePropertyNative) {
1158
+ const auto accepted = bridge.setPrototypePropertyNative(source, key, value, receiver);
1159
+ if (accepted != NativePrototypeOps::SetResult::Absent) return accepted == NativePrototypeOps::SetResult::Accepted;
1160
+ } else if (bridge.prototypeTable) {
1161
+ if (bridge.hasPrototypeProperty(source, key))
1162
+ host::throwRuntimeError("TypeError", "a native Document prototype setter has no native receiver protocol");
1163
+ }
1164
+ if (!bridge.extensible(source)) return false;
1165
+ if (!expandoFor(source, true)->setWithNativeReceiver(key, value, receiver)) return false;
1166
+ bridge.keyCreated(source, key);
1167
+ return true;
1168
+ }
1169
+
1170
+ [[gnu::noinline]] inline bool recordDocumentDefine(const Ref<void>& source, const NativeRecordDocumentBridge& bridge, const PropertyKey& key,
1171
+ const PropertyDescriptor& descriptor) {
1172
+ if (const auto* elements = bridge.elements ? bridge.elements() : nullptr) {
1173
+ auto mutableSource = source;
1174
+ if (!key.isSymbol() && key.text() == "length")
1175
+ return elements->defineLength != nullptr && elements->defineLength(&mutableSource, descriptor);
1176
+ std::size_t index = 0;
1177
+ if (arrayIndexOfKey(key, index)) return elements->define != nullptr && elements->define(&mutableSource, index, descriptor);
1178
+ }
1179
+ return recordDynamicDefine(source, bridge, key, descriptor);
1180
+ }
1181
+
1182
+ /** `nativeDynamicDefineProperty` of a shared-record type. */
1183
+ [[gnu::noinline]] inline bool recordDynamicDefine(const Ref<void>& source, const NativeRecordDocumentBridge& bridge, const PropertyKey& key,
1184
+ const PropertyDescriptor& descriptor) {
1185
+ if (!source) return false;
1186
+ if (record::immutableDescriptorSnapshotView(source))
1187
+ host::throwRuntimeError("TypeError", "a native descriptor snapshot definition has no certified source protocol");
1188
+ if (const auto defined = record::defineDocumentView(source, key, descriptor)) return *defined;
1189
+ if (bridge.fieldTable) {
1190
+ if (bridge.wholeOwnFieldProtocol) {
1191
+ if (bridge.matchesOwnField(source, key)) return bridge.defineOwnField(source, key, descriptor, bridge.extensible(source));
1192
+ } else {
1193
+ Value fixed;
1194
+ if (bridge.readOwnField(source, key, fixed)) return false;
1195
+ }
1196
+ if (bridge.indexFieldTable && bridge.matchesOwnIndex(source, key))
1197
+ return bridge.defineOwnIndex(source, key, descriptor, bridge.extensible(source));
1198
+ }
1199
+ const Ref<DynamicObject> existing = expandoFor(source, false);
1200
+ if (existing && existing->ownProperty(key) != nullptr) return existing->defineOwnProperty(key, descriptor);
1201
+ if (!bridge.extensible(source)) return false;
1202
+ if (!expandoFor(source, true)->defineOwnProperty(key, descriptor)) return false;
1203
+ bridge.keyCreated(source, key);
1204
+ return true;
1205
+ }
1206
+
1207
+ [[gnu::noinline]] inline bool recordDocumentDelete(const Ref<void>& source, const NativeRecordDocumentBridge& bridge, const PropertyKey& key) {
1208
+ if (!source) return true;
1209
+ if (record::immutableDescriptorSnapshotView(source))
1210
+ host::throwRuntimeError("TypeError", "a native descriptor snapshot deletion has no certified source protocol");
1211
+ if (const auto* document = record::documentViewRoute(source)) return document->document->remove(document->held, key);
1212
+ if (bridge.deleteOwnField) {
1213
+ if (bridge.matchesOwnField(source, key)) {
1214
+ if (!bridge.deleteOwnField(source, key)) return false;
1215
+ forgetNativeOwnKey(source.get(), key);
1216
+ return true;
1217
+ }
1218
+ } else if (bridge.fieldTable) {
1219
+ Value probe;
1220
+ if (bridge.readOwnField(source, key, probe)) return false;
1221
+ }
1222
+ if (bridge.deleteOwnIndex && bridge.matchesOwnIndex(source, key)) return bridge.deleteOwnIndex(source, key);
1223
+ const Ref<DynamicObject> expando = expandoFor(source, false);
1224
+ return expando ? expando->deleteOwnProperty(key) : true;
1225
+ }
1226
+
1227
+ [[gnu::noinline]] inline bool recordDocumentEnumerable(const Ref<void>& source, const NativeRecordDocumentBridge& bridge,
1228
+ const PropertyKey& key) {
1229
+ if (bridge.elements) {
1230
+ if (!key.isSymbol() && key.text() == "length") return false;
1231
+ std::size_t index = 0;
1232
+ if (arrayIndexOfKey(key, index)) {
1233
+ PropertyDescriptor descriptor;
1234
+ const auto* elements = bridge.elements();
1235
+ return elements && elements->ownDescriptor && elements->ownDescriptor(&source, index, descriptor) && descriptor.enumerable;
1236
+ }
1237
+ }
1238
+ if (bridge.ownFieldEnumerable) {
1239
+ bool enumerable = false;
1240
+ if (bridge.ownFieldEnumerable(source, key, enumerable)) return enumerable;
1241
+ }
1242
+ if (bridge.ownIndexEnumerable) {
1243
+ bool enumerable = false;
1244
+ if (bridge.ownIndexEnumerable(source, key, enumerable)) return enumerable;
1245
+ }
1246
+ const auto overlay = expandoFor(source, false);
1247
+ const auto* descriptor = overlay ? overlay->ownProperty(key) : nullptr;
1248
+ if (descriptor) return descriptor->enumerable;
1249
+ if (recordDocumentHas(source, bridge, key, true))
1250
+ host::throwRuntimeError("TypeError", "a native Document field has no enumerability-only protocol");
1251
+ return false;
1252
+ }
1253
+
1254
+ [[gnu::noinline]] inline std::optional<bool> recordDocumentReadNative(const Ref<void>& source, const NativeRecordDocumentBridge& bridge,
1255
+ const PropertyKey& key, const NativeFieldRead& read) {
1256
+ if (record::hasLiveFieldView(source)) return record::readFieldView(source, key, read);
1257
+ if (const auto* elements = bridge.elements ? bridge.elements() : nullptr) {
1258
+ std::size_t index = 0;
1259
+ if (arrayIndexOfKey(key, index)) {
1260
+ PropertyDescriptor descriptor;
1261
+ if (elements->ownDescriptor == nullptr || !elements->ownDescriptor(&source, index, descriptor)) return std::nullopt;
1262
+ if (!descriptor.isAccessor()) return descriptor.nativeValue ? std::optional<bool>(descriptor.nativeValue.read(read)) : std::nullopt;
1263
+ if (descriptor.nativeGet && descriptor.nativeGet.read != nullptr) return descriptor.nativeGet.readNative(bridge.receiver(source), read);
1264
+ return std::nullopt;
1265
+ }
1266
+ }
1267
+ auto mutableRead = read;
1268
+ if (bridge.readOwnFieldNative && bridge.matchesOwnField(source, key)) {
1269
+ bool present = true;
1270
+ if (bridge.ownFieldPresent && bridge.ownFieldPresent(source, key, present) && !present) return std::nullopt;
1271
+ return bridge.readOwnFieldNative(source, key, mutableRead);
1272
+ }
1273
+ if (bridge.readOwnIndexNative && bridge.matchesOwnIndex(source, key)) {
1274
+ bool present = true;
1275
+ if (bridge.ownIndexPresent && bridge.ownIndexPresent(source, key, present) && !present) return std::nullopt;
1276
+ return bridge.readOwnIndexNative(source, key, mutableRead);
1277
+ }
1278
+ const auto sidecar = expandoFor(source, false);
1279
+ return sidecar ? readDocumentNativeObject(sidecar, key, bridge.receiver(source), read) : std::nullopt;
1280
+ }
1281
+
1282
+ [[gnu::noinline]] inline bool recordDocumentSetNative(const Ref<void>& source, const NativeRecordDocumentBridge& bridge, const PropertyKey& key,
1283
+ const NativeDescriptorData& value, const NativeFieldWrite& written) {
1284
+ if (record::hasLiveFieldView(source)) return record::writeFieldView(source, key, written);
1285
+ if (const auto* elements = bridge.elements ? bridge.elements() : nullptr) {
1286
+ std::size_t index = 0;
1287
+ if (arrayIndexOfKey(key, index)) {
1288
+ if (elements->frozen(&source) || !bridge.fieldsWritable(source)) return false;
1289
+ PropertyDescriptor current;
1290
+ const bool present = elements->ownDescriptor && elements->ownDescriptor(&source, index, current);
1291
+ if (present && current.isAccessor()) {
1292
+ if (!current.hasSet) return false;
1293
+ const auto receiver = bridge.receiver(source);
1294
+ if (current.nativeSet && current.nativeSet.write) return current.nativeSet.writeNative(receiver, written);
1295
+ if (current.nativeSet && current.nativeSet.dynamicWrite) {
1296
+ current.nativeSet.observeWrite(receiver, value.materialize());
1297
+ return true;
1298
+ }
1299
+ if (!current.set) return false;
1300
+ current.set(receiver.dynamicValue(), value.materialize());
1301
+ return true;
1302
+ }
1303
+ if (present && !current.writable) return false;
1304
+ // A tuple's lane is its declared slot: `requireNativeEntryFitsArrayLane`
1305
+ // checks only an `ArrayObject`'s element lane.
1306
+ auto descriptor = present ? PropertyDescriptor{} : PropertyDescriptor::assignment(Value{});
1307
+ descriptor.hasValue = true;
1308
+ descriptor.nativeValue = value;
1309
+ auto mutableSource = source;
1310
+ return elements->define && elements->define(&mutableSource, index, descriptor);
1311
+ }
1312
+ if (!key.isSymbol() && key.text() == "length")
1313
+ host::throwRuntimeError("TypeError", "a native Document length assignment needs a numeric entry writer");
1314
+ }
1315
+ if (bridge.writeOwnFieldNative && bridge.matchesOwnField(source, key))
1316
+ return bridge.fieldsWritable(source) && bridge.writeOwnFieldNative(source, key, written, bridge.extensible(source));
1317
+ if (bridge.writeOwnIndexNative && bridge.matchesOwnIndex(source, key))
1318
+ return bridge.writeOwnIndexNative(source, key, written, bridge.extensible(source));
1319
+ const auto* fields = bridge.fields();
1320
+ if (fields && (fields->matchesField(&source, key) || fields->matchesIndex(&source, key)))
1321
+ host::throwRuntimeError("TypeError", "a Document native entry has no selected physical field writer");
1322
+ const auto receiver = bridge.receiver(source);
1323
+ const auto existing = expandoFor(source, false);
1324
+ if (existing && existing->ownProperty(key)) return existing->setNativeDescriptorWithNativeReceiver(key, value, written, receiver);
1325
+ if (bridge.setPrototypePropertyFieldNative) {
1326
+ const auto accepted = bridge.setPrototypePropertyFieldNative(source, key, written, receiver);
1327
+ if (accepted != NativePrototypeOps::SetResult::Absent) return accepted == NativePrototypeOps::SetResult::Accepted;
1328
+ } else if (bridge.prototypeTable) {
1329
+ if (bridge.hasPrototypeProperty(source, key)) host::throwRuntimeError("TypeError", "a Document native entry has no selected prototype setter");
1330
+ }
1331
+ if (!bridge.extensible(source)) return false;
1332
+ const auto overlay = existing ? existing : expandoFor(source, true);
1333
+ if (!overlay->setNativeDescriptorWithNativeReceiver(key, value, written, receiver)) return false;
1334
+ if (overlay->ownProperty(key)) bridge.keyCreated(source, key);
1335
+ return true;
1336
+ }
1337
+
1338
+ /** `nativeDynamicHas` of a shared-record type. */
1339
+ [[gnu::noinline]] inline bool recordDynamicHas(const Ref<void>& source, const NativeRecordDocumentBridge& bridge, const PropertyKey& key) {
1340
+ if (!source) return false;
1341
+ if (record::hasLiveFieldView(source)) return record::hasOwnFieldView(source, key);
1342
+ if (bridge.ownFieldPresent) {
1343
+ bool present = false;
1344
+ if (bridge.ownFieldPresent(source, key, present)) return present;
1345
+ }
1346
+ if (bridge.ownIndexPresent) {
1347
+ bool present = false;
1348
+ if (bridge.ownIndexPresent(source, key, present)) return present;
1349
+ }
1350
+ Value probe;
1351
+ if (bridge.fieldTable) {
1352
+ if (bridge.readOwnField(source, key, probe)) return true;
1353
+ if (bridge.wholeOwnFieldProtocol && bridge.matchesOwnField(source, key)) return false;
1354
+ if (bridge.indexFieldTable) {
1355
+ if (bridge.readOwnIndex(source, key, probe)) return true;
1356
+ if (bridge.matchesOwnIndex(source, key)) return false;
1357
+ }
1358
+ }
1359
+ const Ref<DynamicObject> expando = expandoFor(source, false);
1360
+ return expando ? expando->hasProperty(key) : false;
1361
+ }
1362
+
1363
+ /** `nativeEnumerableStringKeys` of a shared-record type. */
1364
+ [[gnu::noinline]] inline std::vector<PropertyKey> recordEnumerableStringKeys(const Ref<void>& source, const NativeRecordDocumentBridge& bridge) {
1365
+ std::vector<PropertyKey> original;
1366
+ if (record::ownNativeViewKeys(source, original, true)) return original;
1367
+ std::vector<PropertyKey> keys = recordDocumentOwnKeys(source, bridge);
1368
+ std::size_t kept = 0;
1369
+ for (std::size_t at = 0; at < keys.size(); ++at) {
1370
+ const PropertyKey& key = keys[at];
1371
+ if (key.isSymbol()) continue;
1372
+ bool enumerable = false;
1373
+ bool known = false;
1374
+ if (bridge.ownFieldEnumerable) known = bridge.ownFieldEnumerable(source, key, enumerable);
1375
+ if (!known && bridge.ownIndexEnumerable) known = bridge.ownIndexEnumerable(source, key, enumerable);
1376
+ if (!known) {
1377
+ PropertyDescriptor descriptor;
1378
+ enumerable = recordOwnDescriptor(source, bridge, key, descriptor) && descriptor.enumerable;
1379
+ }
1380
+ if (!enumerable) continue;
1381
+ if (kept != at) keys[kept] = std::move(keys[at]);
1382
+ ++kept;
1383
+ }
1384
+ keys.erase(keys.begin() + static_cast<std::ptrdiff_t>(kept), keys.end());
1385
+ return keys;
1386
+ }
1387
+
1388
+ template <typename T>
1389
+ const Ref<T>& recordDocumentHandle(const Ref<void>& source) {
1390
+ return *static_cast<const Ref<T>*>(static_cast<const void*>(&source));
1391
+ }
1392
+
1393
+ template <typename T>
1394
+ inline constexpr NativeRecordDocumentBridge recordDocumentBridge{
1395
+ [](const Ref<void>& source) { return nativeOwnFieldsWritable(recordDocumentHandle<T>(source)); },
1396
+ [](const Ref<void>& source) { return nativeIsExtensible(recordDocumentHandle<T>(source)); },
1397
+ [](const Ref<void>& source) { return NativeCallReceiver::object(recordDocumentHandle<T>(source)); },
1398
+ [](const Ref<void>& source, const PropertyKey& key) { noteNativeExpandoKeyCreated(recordDocumentHandle<T>(source), key); },
1399
+ &nativeFieldOpsFor<Ref<T>>,
1400
+ IsArrayPayload<Ref<T>>::value ? &nativeArrayOpsFor<Ref<T>> : nullptr,
1401
+ NativeFieldTable<T>,
1402
+ NativeFieldTable<T> && NativeIndexFieldTable<T>,
1403
+ wholeNativeOwnFieldProtocol<T>(),
1404
+ NativePrototypeTable<T>,
1405
+ NativeOwnFieldPredicate<T>,
1406
+ [] {
1407
+ if constexpr (NativeOwnFieldPresenceTable<T>)
1408
+ return +[](const Ref<void>& source, const PropertyKey& key, bool& present) {
1409
+ return recordDocumentHandle<T>(source)->gea_ownFieldPresent(key, present);
1410
+ };
1411
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&, bool&)>(nullptr);
1412
+ }(),
1413
+ [] {
1414
+ if constexpr (NativeOwnIndexPresenceTable<T>)
1415
+ return +[](const Ref<void>& source, const PropertyKey& key, bool& present) {
1416
+ return recordDocumentHandle<T>(source)->gea_ownIndexPresent(key, present);
1417
+ };
1418
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&, bool&)>(nullptr);
1419
+ }(),
1420
+ [] {
1421
+ if constexpr (NativeOwnFieldEnumerableTable<T>)
1422
+ return +[](const Ref<void>& source, const PropertyKey& key, bool& enumerable) {
1423
+ return recordDocumentHandle<T>(source)->gea_ownFieldEnumerable(key, enumerable);
1424
+ };
1425
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&, bool&)>(nullptr);
1426
+ }(),
1427
+ [] {
1428
+ if constexpr (NativeOwnIndexEnumerableTable<T>)
1429
+ return +[](const Ref<void>& source, const PropertyKey& key, bool& enumerable) {
1430
+ return recordDocumentHandle<T>(source)->gea_ownIndexEnumerable(key, enumerable);
1431
+ };
1432
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&, bool&)>(nullptr);
1433
+ }(),
1434
+ [] {
1435
+ if constexpr (requires(T& reader, const PropertyKey& key) { reader.gea_matchesOwnField(key); })
1436
+ return +[](const Ref<void>& source, const PropertyKey& key) { return recordDocumentHandle<T>(source)->gea_matchesOwnField(key); };
1437
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&)>(nullptr);
1438
+ }(),
1439
+ [] {
1440
+ if constexpr (requires(T& reader, const PropertyKey& key) { reader.gea_matchesOwnIndex(key); })
1441
+ return +[](const Ref<void>& source, const PropertyKey& key) { return recordDocumentHandle<T>(source)->gea_matchesOwnIndex(key); };
1442
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&)>(nullptr);
1443
+ }(),
1444
+ [] {
1445
+ if constexpr (NativeFieldTable<T>)
1446
+ return +[](const Ref<void>& source, const PropertyKey& key, Value& out) {
1447
+ return recordDocumentHandle<T>(source)->gea_readOwnField(key, out);
1448
+ };
1449
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&, Value&)>(nullptr);
1450
+ }(),
1451
+ [] {
1452
+ if constexpr (NativeFieldTable<T> && NativeIndexFieldTable<T>)
1453
+ return +[](const Ref<void>& source, const PropertyKey& key, Value& out) {
1454
+ return recordDocumentHandle<T>(source)->gea_readOwnIndex(key, out);
1455
+ };
1456
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&, Value&)>(nullptr);
1457
+ }(),
1458
+ [] {
1459
+ if constexpr (requires(T& reader, const PropertyKey& key, PropertyDescriptor& descriptor) {
1460
+ reader.gea_ownFieldDescriptor(key, descriptor);
1461
+ })
1462
+ return +[](const Ref<void>& source, const PropertyKey& key, PropertyDescriptor& out) {
1463
+ return recordDocumentHandle<T>(source)->gea_ownFieldDescriptor(key, out);
1464
+ };
1465
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&, PropertyDescriptor&)>(nullptr);
1466
+ }(),
1467
+ [] {
1468
+ if constexpr (NativeFieldTable<T> && NativeIndexFieldTable<T>)
1469
+ return +[](const Ref<void>& source, const PropertyKey& key, PropertyDescriptor& out) {
1470
+ return recordDocumentHandle<T>(source)->gea_ownIndexDescriptor(key, out);
1471
+ };
1472
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&, PropertyDescriptor&)>(nullptr);
1473
+ }(),
1474
+ [] {
1475
+ if constexpr (NativeOwnKeysTable<T>)
1476
+ return +[](const Ref<void>& source, std::vector<PropertyKey>& keys) { recordDocumentHandle<T>(source)->gea_ownFieldKeys(keys); };
1477
+ else return static_cast<void (*)(const Ref<void>&, std::vector<PropertyKey>&)>(nullptr);
1478
+ }(),
1479
+ [] {
1480
+ if constexpr (NativeFieldTable<T> && wholeNativeOwnFieldProtocol<T>())
1481
+ return +[](const Ref<void>& source, const PropertyKey& key, const PropertyDescriptor& descriptor, bool extensible) {
1482
+ return nativeDefineOwnField(*recordDocumentHandle<T>(source), key, descriptor, extensible, source);
1483
+ };
1484
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&, const PropertyDescriptor&, bool)>(nullptr);
1485
+ }(),
1486
+ [] {
1487
+ if constexpr (NativeFieldTable<T> && NativeIndexFieldTable<T>)
1488
+ return +[](const Ref<void>& source, const PropertyKey& key, const PropertyDescriptor& descriptor, bool extensible) {
1489
+ return recordDocumentHandle<T>(source)->gea_defineOwnIndex(key, descriptor, extensible);
1490
+ };
1491
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&, const PropertyDescriptor&, bool)>(nullptr);
1492
+ }(),
1493
+ [] {
1494
+ if constexpr (NativeOwnFieldPredicate<T> && NativeOwnFieldDeletion<T>)
1495
+ return +[](const Ref<void>& source, const PropertyKey& key) { return recordDocumentHandle<T>(source)->gea_deleteOwnField(key); };
1496
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&)>(nullptr);
1497
+ }(),
1498
+ [] {
1499
+ if constexpr (requires(T& writer, const PropertyKey& key) {
1500
+ writer.gea_matchesOwnIndex(key);
1501
+ writer.gea_deleteOwnIndex(key);
1502
+ })
1503
+ return +[](const Ref<void>& source, const PropertyKey& key) { return recordDocumentHandle<T>(source)->gea_deleteOwnIndex(key); };
1504
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&)>(nullptr);
1505
+ }(),
1506
+ [] {
1507
+ if constexpr (requires(T& reader, const PropertyKey& key, NativeFieldRead& read) {
1508
+ reader.gea_matchesOwnField(key);
1509
+ reader.gea_readOwnFieldNative(key, read);
1510
+ })
1511
+ return +[](const Ref<void>& source, const PropertyKey& key, NativeFieldRead& read) {
1512
+ return recordDocumentHandle<T>(source)->gea_readOwnFieldNative(key, read);
1513
+ };
1514
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&, NativeFieldRead&)>(nullptr);
1515
+ }(),
1516
+ [] {
1517
+ if constexpr (requires(T& reader, const PropertyKey& key, NativeFieldRead& read) {
1518
+ reader.gea_matchesOwnIndex(key);
1519
+ reader.gea_readOwnIndexNative(key, read);
1520
+ })
1521
+ return +[](const Ref<void>& source, const PropertyKey& key, NativeFieldRead& read) {
1522
+ return recordDocumentHandle<T>(source)->gea_readOwnIndexNative(key, read);
1523
+ };
1524
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&, NativeFieldRead&)>(nullptr);
1525
+ }(),
1526
+ [] {
1527
+ if constexpr (requires(T& writer, const PropertyKey& key, const NativeFieldWrite& written) {
1528
+ writer.gea_matchesOwnField(key);
1529
+ writer.gea_writeOwnFieldNative(key, written, true);
1530
+ })
1531
+ return +[](const Ref<void>& source, const PropertyKey& key, const NativeFieldWrite& written, bool extensible) {
1532
+ return recordDocumentHandle<T>(source)->gea_writeOwnFieldNative(key, written, extensible);
1533
+ };
1534
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&, const NativeFieldWrite&, bool)>(nullptr);
1535
+ }(),
1536
+ [] {
1537
+ if constexpr (requires(T& writer, const PropertyKey& key, const NativeFieldWrite& written) {
1538
+ writer.gea_matchesOwnIndex(key);
1539
+ writer.gea_writeOwnIndexNative(key, written, true);
1540
+ })
1541
+ return +[](const Ref<void>& source, const PropertyKey& key, const NativeFieldWrite& written, bool extensible) {
1542
+ return recordDocumentHandle<T>(source)->gea_writeOwnIndexNative(key, written, extensible);
1543
+ };
1544
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&, const NativeFieldWrite&, bool)>(nullptr);
1545
+ }(),
1546
+ [] {
1547
+ if constexpr (requires(T& reader, const PropertyKey& key, Value& out) { reader.gea_readPrototypeProperty(key, out); })
1548
+ return +[](const Ref<void>& source, const PropertyKey& key, Value& out) {
1549
+ return recordDocumentHandle<T>(source)->gea_readPrototypeProperty(key, out);
1550
+ };
1551
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&, Value&)>(nullptr);
1552
+ }(),
1553
+ [] {
1554
+ if constexpr (requires(T& reader, const PropertyKey& key) { reader.gea_hasPrototypeProperty(key); })
1555
+ return +[](const Ref<void>& source, const PropertyKey& key) { return recordDocumentHandle<T>(source)->gea_hasPrototypeProperty(key); };
1556
+ else return static_cast<bool (*)(const Ref<void>&, const PropertyKey&)>(nullptr);
1557
+ }(),
1558
+ [] {
1559
+ if constexpr (requires(T& writer, const PropertyKey& key, const Value& value, const NativeCallReceiver& receiver) {
1560
+ writer.gea_setPrototypePropertyNative(key, value, receiver);
1561
+ })
1562
+ return +[](const Ref<void>& source, const PropertyKey& key, const Value& value, const NativeCallReceiver& receiver) {
1563
+ return recordDocumentHandle<T>(source)->gea_setPrototypePropertyNative(key, value, receiver);
1564
+ };
1565
+ else return static_cast<NativePrototypeOps::SetResult (*)(const Ref<void>&, const PropertyKey&, const Value&, const NativeCallReceiver&)>(nullptr);
1566
+ }(),
1567
+ [] {
1568
+ if constexpr (requires(T& writer, const PropertyKey& key, const NativeFieldWrite& written, const NativeCallReceiver& receiver) {
1569
+ writer.gea_setPrototypePropertyFieldNative(key, written, receiver);
1570
+ })
1571
+ return +[](const Ref<void>& source, const PropertyKey& key, const NativeFieldWrite& written, const NativeCallReceiver& receiver) {
1572
+ return recordDocumentHandle<T>(source)->gea_setPrototypePropertyFieldNative(key, written, receiver);
1573
+ };
1574
+ else return static_cast<NativePrototypeOps::SetResult (*)(const Ref<void>&, const PropertyKey&, const NativeFieldWrite&,
1575
+ const NativeCallReceiver&)>(nullptr);
1576
+ }()};
1577
+
1578
+ template <typename T>
1579
+ const NativeRecordDocumentBridge& recordDocumentBridgeOf() {
1580
+ return recordDocumentBridge<T>;
1581
+ }
1582
+
1583
+ template <typename T>
1584
+ const NativeDocumentOps* nativeRecordDocumentOpsFor() {
1585
+ static const NativeDocumentOps operations{
1586
+ +[](const Ref<void>& source, const PropertyKey& key, Value& out) { return recordDocumentRead(source, recordDocumentBridge<T>, key, out); },
1587
+ +[](const Ref<void>& source, const PropertyKey& key, const Value& value) {
1588
+ return recordDocumentSet(source, recordDocumentBridge<T>, key, value);
1589
+ },
1590
+ +[](const Ref<void>& source, const PropertyKey& key, PropertyDescriptor& out) {
1591
+ return recordDocumentDescriptor(source, recordDocumentBridge<T>, key, out);
1592
+ },
1593
+ +[](const Ref<void>& source, const PropertyKey& key, const PropertyDescriptor& descriptor) {
1594
+ return recordDocumentDefine(source, recordDocumentBridge<T>, key, descriptor);
1595
+ },
1596
+ +[](const Ref<void>& source, const PropertyKey& key) { return recordDocumentDelete(source, recordDocumentBridge<T>, key); },
1597
+ +[](const Ref<void>& source, const PropertyKey& key, bool own) { return recordDocumentHas(source, recordDocumentBridge<T>, key, own); },
1598
+ +[](const Ref<void>& source) { return recordDocumentKeys(source, recordDocumentBridge<T>); },
1599
+ +[](const Ref<void>& source, const PropertyKey& key) { return recordDocumentEnumerable(source, recordDocumentBridge<T>, key); },
1600
+ +[](const Ref<void>& source) { return Value::box(Value::Tag::Object, source.template staticCast<T>()); },
1601
+ payloadTypeTagFor<Ref<T>>(), IsArrayPayload<Ref<T>>::value, false, nullptr,
1602
+ +[](const Ref<void>& source, const PropertyKey& key, const NativeFieldRead& read) {
1603
+ return recordDocumentReadNative(source, recordDocumentBridge<T>, key, read);
1604
+ },
1605
+ +[](const Ref<void>& source, const PropertyKey& key, const NativeDescriptorData& value, const NativeFieldWrite& written) {
1606
+ return recordDocumentSetNative(source, recordDocumentBridge<T>, key, value, written);
1607
+ }};
1608
+ return &operations;
1609
+ }
1610
+
625
1611
  template <typename T>
626
1612
  const NativeDocumentOps* nativeDocumentOpsFor() {
627
1613
  if constexpr (std::is_void_v<T> || refStandalone<T>) return nullptr;
1614
+ else if constexpr (IsArrayObjectStruct<T>::value) return nativeArrayDocumentOpsFor<T>();
1615
+ else if constexpr (sharedRecordDocument<T>) return nativeRecordDocumentOpsFor<T>();
628
1616
  else {
629
1617
  Ref<Map<Value, Value>> (*mapView)(const Ref<void>&) = nullptr;
630
1618
  if constexpr (IsMapPayload<Ref<T>>::value) {