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

Method constructor

gi/wrapperutils.h:541–588  ·  view source on GitHub ↗

* GIWrapperBase::constructor: * * C++ implementation of the JS constructor passed to JS_InitClass(). Only * called on instances, never on prototypes. This method contains the * functionality common to all GI wrapper classes. There must be a * corresponding Instance::constructor_impl method containing the rest of * the functionality. */

Source from the content-addressed store, hash-verified

539 * the functionality.
540 */
541 GJS_JSAPI_RETURN_CONVENTION
542 static bool constructor(JSContext* cx, unsigned argc, JS::Value* vp) {
543 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
544
545 if (!args.isConstructing()) {
546 gjs_throw_constructor_error(cx);
547 return false;
548 }
549 JS::RootedObject obj(
550 cx, JS_NewObjectForConstructor(cx, &Base::klass, args));
551 if (!obj)
552 return false;
553
554 JS::RootedObject proto(cx);
555 if (!JS_GetPrototype(cx, obj, &proto))
556 return false;
557
558 Prototype* prototype = resolve_prototype(cx, proto);
559 if (!prototype)
560 return false;
561
562 args.rval().setUndefined();
563
564 Instance* priv = Instance::new_for_js_object(prototype, obj);
565
566 {
567 std::string full_name{
568 GJS_PROFILER_DYNAMIC_STRING(cx, priv->format_name())};
569 AutoProfilerLabel label{cx, "constructor", full_name};
570
571 if (!priv->constructor_impl(cx, obj, args))
572 return false;
573 }
574
575 priv->debug_lifecycle("Adding associated memory (GIWrapperBase constructor)");
576 priv->add_associated_memory(obj);
577
578 static_cast<GIWrapperBase*>(priv)->debug_lifecycle(obj,
579 "JSObject created");
580 gjs_debug_lifecycle(Base::DEBUG_TOPIC, "m_proto is %p",
581 priv->get_prototype());
582
583 // We may need to return a value different from obj (for example because
584 // we delegate to another constructor)
585 if (args.rval().isUndefined())
586 args.rval().setObject(*obj);
587 return true;
588 }
589
590 /**
591 * GIWrapperBase::to_string:

Callers

nothing calls this directly

Calls 6

get_prototypeMethod · 0.80
format_nameMethod · 0.45
constructor_implMethod · 0.45
debug_lifecycleMethod · 0.45
add_associated_memoryMethod · 0.45

Tested by

no test coverage detected