[gjs/ewlsh/interface-resolution: 5/6] (review and squash) Store the interface accessor's ID in the function's ID slot




commit 31d62efc786beb0e3f26647dde96add69a7a9e37
Author: Philip Chimento <philip chimento gmail com>
Date:   Fri Oct 15 14:54:04 2021 -0700

    (review and squash) Store the interface accessor's ID in the function's ID slot

 gi/object.cpp | 59 ++++++++++++++++-------------------------------------------
 1 file changed, 16 insertions(+), 43 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index 4ee45384..85cc9a60 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -645,28 +645,15 @@ bool ObjectPrototype::lazy_define_gobject_property(JSContext* cx,
     return true;
 }
 
-enum class InterfaceAccessorPrivateSlot : size_t {
-    // The identifier to get on the interface' prototype.
-    IDENTIFIER = 0,
-    // An object shared by the getter and setter to store the interface'
-    // prototype and
-    // overrides.
-    ACCESSOR = 1,
-};
+// An object shared by the getter and setter to store the interface' prototype
+// and overrides.
+static constexpr size_t ACCESSOR_SLOT = 0;
 
 static bool interface_getter(JSContext* cx, unsigned argc, JS::Value* vp) {
     JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
-    JS::RootedValue v_method_id(
-        cx, js::GetFunctionNativeReserved(
-                &args.callee(),
-                static_cast<size_t>(InterfaceAccessorPrivateSlot::IDENTIFIER)));
-    JS::RootedId id(cx);
-    if (!JS_ValueToId(cx, v_method_id, &id))
-        return false;
+
     JS::RootedValue v_accessor(
-        cx, js::GetFunctionNativeReserved(
-                &args.callee(),
-                static_cast<size_t>(InterfaceAccessorPrivateSlot::ACCESSOR)));
+        cx, js::GetFunctionNativeReserved(&args.callee(), ACCESSOR_SLOT));
     g_assert(v_accessor.isObject() && "accessor must be an object");
     JS::RootedObject accessor(cx, &v_accessor.toObject());
 
@@ -683,15 +670,15 @@ static bool interface_getter(JSContext* cx, unsigned argc, JS::Value* vp) {
     g_assert(v_prototype.isObject() && "prototype must be an object");
 
     JS::RootedObject prototype(cx, &v_prototype.toObject());
+    JS::RootedId id(cx, JS::PropertyKey::fromNonIntAtom(JS_GetFunctionId(
+                            JS_GetObjectFunction(&args.callee()))));
     return JS_GetPropertyById(cx, prototype, id, args.rval());
 }
 
 static bool interface_setter(JSContext* cx, unsigned argc, JS::Value* vp) {
     JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
     JS::RootedValue v_accessor(
-        cx, js::GetFunctionNativeReserved(
-                &args.callee(),
-                static_cast<size_t>(InterfaceAccessorPrivateSlot::ACCESSOR)));
+        cx, js::GetFunctionNativeReserved(&args.callee(), ACCESSOR_SLOT));
     JS::RootedObject accessor(cx, &v_accessor.toObject());
 
     const GjsAtoms& atoms = GjsContextPrivate::atoms(cx);
@@ -721,39 +708,25 @@ static bool resolve_on_interface_prototype(JSContext* cx,
 
     JS::Rooted<JS::PropertyDescriptor> target_desc(cx, desc);
     JS::RootedObject getter(
-        cx, JS_GetFunctionObject(js::NewFunctionWithReserved(
-                cx, interface_getter, 0, 0, "interface_prototype_getter")));
+        cx, JS_GetFunctionObject(js::NewFunctionByIdWithReserved(
+                cx, interface_getter, 0, 0, identifier)));
     if (!getter)
         return false;
 
     JS::RootedObject setter(
-        cx, JS_GetFunctionObject(js::NewFunctionWithReserved(
-                cx, interface_setter, 1, 0, "interface_prototype_setter")));
+        cx, JS_GetFunctionObject(js::NewFunctionByIdWithReserved(
+                cx, interface_setter, 1, 0, identifier)));
     if (!setter)
         return false;
 
-    JS::RootedValue v_id(cx);
-    if (!JS_IdToValue(cx, identifier, &v_id))
-        return false;
-
-    // Store the property identifier...
-    js::SetFunctionNativeReserved(
-        setter, static_cast<size_t>(InterfaceAccessorPrivateSlot::IDENTIFIER),
-        v_id);
-    js::SetFunctionNativeReserved(
-        getter, static_cast<size_t>(InterfaceAccessorPrivateSlot::IDENTIFIER),
-        v_id);
-
     JS::RootedObject accessor(cx, JS_NewPlainObject(cx));
     if (!accessor)
         return false;
 
-    js::SetFunctionNativeReserved(
-        setter, static_cast<size_t>(InterfaceAccessorPrivateSlot::ACCESSOR),
-        JS::ObjectValue(*accessor));
-    js::SetFunctionNativeReserved(
-        getter, static_cast<size_t>(InterfaceAccessorPrivateSlot::ACCESSOR),
-        JS::ObjectValue(*accessor));
+    js::SetFunctionNativeReserved(setter, ACCESSOR_SLOT,
+                                  JS::ObjectValue(*accessor));
+    js::SetFunctionNativeReserved(getter, ACCESSOR_SLOT,
+                                  JS::ObjectValue(*accessor));
 
     const GjsAtoms& atoms = GjsContextPrivate::atoms(cx);
     JS::RootedValue v_prototype(cx, JS::ObjectValue(*prototype));


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]