| 2607 | } |
| 2608 | |
| 2609 | bool ObjectInstance::init_impl(JSContext* cx, const JS::CallArgs& args, |
| 2610 | JS::HandleObject object) { |
| 2611 | g_assert(gtype() != G_TYPE_NONE); |
| 2612 | |
| 2613 | if (args.length() > 1 && |
| 2614 | !JS::WarnUTF8(cx, |
| 2615 | "Too many arguments to the constructor of %s: expected " |
| 2616 | "1, got %u", |
| 2617 | name(), args.length())) |
| 2618 | return false; |
| 2619 | |
| 2620 | std::vector<const char*> names; |
| 2621 | AutoGValueVector values; |
| 2622 | |
| 2623 | if (args.length() > 0 && !args[0].isUndefined()) { |
| 2624 | if (!args[0].isObject()) { |
| 2625 | gjs_throw(cx, |
| 2626 | "Argument to the constructor of %s should be a plain JS " |
| 2627 | "object with properties to set", |
| 2628 | name()); |
| 2629 | return false; |
| 2630 | } |
| 2631 | |
| 2632 | JS::RootedObject props{cx, &args[0].toObject()}; |
| 2633 | if (ObjectBase::for_js(cx, props)) { |
| 2634 | gjs_throw(cx, |
| 2635 | "Argument to the constructor of %s should be a plain JS " |
| 2636 | "object with properties to set", |
| 2637 | name()); |
| 2638 | return false; |
| 2639 | } |
| 2640 | Gjs::AutoTypeClass<GObjectClass> object_class{gtype()}; |
| 2641 | if (!m_proto->props_to_g_parameters(cx, object_class, props, &names, |
| 2642 | &values)) |
| 2643 | return false; |
| 2644 | } |
| 2645 | |
| 2646 | if (G_TYPE_IS_ABSTRACT(gtype())) { |
| 2647 | gjs_throw(cx, "Cannot instantiate abstract type %s", |
| 2648 | g_type_name(gtype())); |
| 2649 | return false; |
| 2650 | } |
| 2651 | |
| 2652 | // Mark this object in the construction stack, it will be popped in |
| 2653 | // gjs_object_custom_init() in gi/gobject.cpp. |
| 2654 | if (is_custom_js_class()) { |
| 2655 | GjsContextPrivate* gjs = GjsContextPrivate::from_cx(cx); |
| 2656 | if (!gjs->object_init_list().append(object)) { |
| 2657 | JS_ReportOutOfMemory(cx); |
| 2658 | return false; |
| 2659 | } |
| 2660 | } |
| 2661 | |
| 2662 | g_assert(names.size() == values.size()); |
| 2663 | GObject* gobj = g_object_new_with_properties(gtype(), values.size(), |
| 2664 | names.data(), values.data()); |
| 2665 | |
| 2666 | ObjectInstance* other_priv = ObjectInstance::for_gobject(gobj); |
no test coverage detected