| 657 | template <class Base, class Prototype, class Instance> |
| 658 | template <class FieldBase> |
| 659 | bool 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); |
nothing calls this directly
no test coverage detected