MCPcopy Create free account
hub / github.com/GNOME/gjs / set_nested_interface_object

Method set_nested_interface_object

gi/boxed.cpp:659–724  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

657template <class Base, class Prototype, class Instance>
658template <class FieldBase>
659bool BoxedInstance<Base, Prototype, Instance>::set_nested_interface_object(
660 JSContext* cx, const GI::FieldInfo& field_info,
661 const GI::UnownedInfo<FieldBase::TAG>& boxed_info, JS::HandleValue value) {
662 if (!GI::struct_is_simple(boxed_info)) {
663 gjs_throw(cx, "Writing field %s.%s is not supported",
664 format_name().c_str(), field_info.name());
665
666 return false;
667 }
668
669 /* If we can't directly copy from the source object we need to construct a
670 * new temporary object. */
671 FieldBase* source_priv = nullptr;
672 JS::RootedObject source_object{cx};
673 bool field_has_same_info = false;
674
675 if constexpr (std::is_same_v<FieldBase, Base>) {
676 field_has_same_info = info() == boxed_info;
677 if (field_has_same_info) {
678 source_object = &value.toObject();
679 source_priv = FieldBase::for_js(cx, source_object);
680 }
681 }
682
683 if (!source_priv && !field_has_same_info) {
684 source_object = &value.toObject();
685 source_priv = FieldBase::for_js(cx, source_object);
686
687 if (source_priv && source_priv->info() != boxed_info) {
688 std::string source_name{source_priv->format_name()};
689 gjs_throw(cx, "Impossible to associate a %s to a %s.%s field",
690 source_name.c_str(), name(), field_info.name());
691 return false;
692 }
693 }
694
695 if (!source_priv) {
696 JS::RootedObject proto{cx,
697 gjs_lookup_generic_prototype(cx, boxed_info)};
698 if (!proto)
699 return false;
700
701 JS::RootedValueArray<1> args{cx};
702 args[0].set(value);
703 source_object = gjs_construct_object_dynamic(cx, proto, args);
704 if (!source_object ||
705 !FieldBase::for_js_typecheck(cx, source_object, &source_priv))
706 return false;
707 }
708
709 if (!source_priv->check_is_instance(cx, "copy"))
710 return false;
711
712 // Any pointers in the copied memory are still owned by the source struct.
713 // So we need to tie the lifetime of the source JS object to this one.
714 if (GI::simple_struct_has_pointers(boxed_info) &&
715 !m_nested_objects.put(field_info.name(), source_object)) {
716 JS_ReportOutOfMemory(cx);

Callers

nothing calls this directly

Calls 15

struct_is_simpleFunction · 0.85
gjs_throwFunction · 0.85
infoFunction · 0.85
setMethod · 0.80
check_is_instanceMethod · 0.80
offsetMethod · 0.80
to_instanceMethod · 0.80
infoMethod · 0.65
nameMethod · 0.45

Tested by

no test coverage detected