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

Method new_for_gobject

gi/object.cpp:3570–3609  ·  view source on GitHub ↗

* ObjectInstance::new_for_gobject: * * Creates a new JSObject wrapper for the GObject pointer @gobj, and an * ObjectInstance private structure to go along with it. */

Source from the content-addressed store, hash-verified

3568 * ObjectInstance private structure to go along with it.
3569 */
3570ObjectInstance* 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:

Callers

nothing calls this directly

Calls 6

strcmpFunction · 0.85
associate_js_gobjectMethod · 0.80
wrapperMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected