* ObjectInstance::new_for_gobject: * * Creates a new JSObject wrapper for the GObject pointer @gobj, and an * ObjectInstance private structure to go along with it. */
| 3568 | * ObjectInstance private structure to go along with it. |
| 3569 | */ |
| 3570 | ObjectInstance* ObjectInstance::new_for_gobject(JSContext* cx, GObject* gobj) { |
| 3571 | g_assert(gobj && "Cannot create JSObject for null GObject pointer"); |
| 3572 | |
| 3573 | GType gtype = G_TYPE_FROM_INSTANCE(gobj); |
| 3574 | |
| 3575 | gjs_debug_marshal(GJS_DEBUG_GOBJECT, "Wrapping %s %p with JSObject", |
| 3576 | g_type_name(gtype), gobj); |
| 3577 | |
| 3578 | JS::RootedObject proto(cx, gjs_lookup_object_prototype(cx, gtype)); |
| 3579 | if (!proto) |
| 3580 | return nullptr; |
| 3581 | |
| 3582 | JS::RootedObject obj( |
| 3583 | cx, JS_NewObjectWithGivenProto(cx, &ObjectBase::klass, proto)); |
| 3584 | if (!obj) |
| 3585 | return nullptr; |
| 3586 | |
| 3587 | ObjectPrototype* prototype = resolve_prototype(cx, proto); |
| 3588 | if (!prototype) |
| 3589 | return nullptr; |
| 3590 | |
| 3591 | auto* priv = new ObjectInstance(prototype, obj); |
| 3592 | |
| 3593 | ObjectBase::init_private(obj, priv); |
| 3594 | |
| 3595 | g_object_ref_sink(gobj); |
| 3596 | priv->associate_js_gobject(cx, obj, gobj); |
| 3597 | |
| 3598 | g_assert(priv->wrapper() == obj.get()); |
| 3599 | |
| 3600 | GTypeQuery query; |
| 3601 | g_type_query(gtype, &query); |
| 3602 | g_assert(query.type); |
| 3603 | if (strcmp(query.type_name, "GdkPixbuf") == 0) { |
| 3604 | int32_t total_memory = estimate_size_of_gdkpixbuf(gobj); |
| 3605 | JS::AddAssociatedMemory(obj, total_memory, MemoryUse::GdkPixbuf); |
| 3606 | JS::SetReservedSlot(obj, MEMORY_SIZE, JS::Int32Value(total_memory)); |
| 3607 | } |
| 3608 | return priv; |
| 3609 | } |
| 3610 | |
| 3611 | /** |
| 3612 | * ObjectInstance::wrapper_from_gobject: |
nothing calls this directly
no test coverage detected