[gjs: 1/2] repo: Remove JSClass macros




commit bbf6ef7326f60c36d3ccb3877a842b9aed9e77a1
Author: Philip Chimento <philip chimento gmail com>
Date:   Mon Apr 6 14:08:01 2020 -0700

    repo: Remove JSClass macros
    
    The macros in jsapi-class.h are not needed here. Instead of using
    JS_InitClass() which requires a constructor and prototype object, we can
    just use JS_NewObject() which will use a plain object as the prototype,
    and we don't need to store private data in the instances since there is
    none.

 gi/repo.cpp       | 80 ++++++-------------------------------------------------
 gjs/global.h      |  1 -
 gjs/mem-private.h |  1 -
 3 files changed, 8 insertions(+), 74 deletions(-)
---
diff --git a/gi/repo.cpp b/gi/repo.cpp
index d34e3b16..1f76d847 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -39,17 +39,9 @@
 #include "gjs/atoms.h"
 #include "gjs/context-private.h"
 #include "gjs/global.h"
-#include "gjs/jsapi-class.h"
 #include "gjs/jsapi-util.h"
-#include "gjs/mem-private.h"
 #include "util/log.h"
 
-extern struct JSClass gjs_repo_class;
-
-struct Repo {};
-
-GJS_DEFINE_PRIV_FROM_JS(Repo, gjs_repo_class)
-
 GJS_JSAPI_RETURN_CONVENTION
 static bool lookup_override_function(JSContext *, JS::HandleId,
                                      JS::MutableHandleValue);
@@ -160,8 +152,6 @@ repo_resolve(JSContext       *context,
              JS::HandleId     id,
              bool            *resolved)
 {
-    Repo *priv;
-
     if (!JSID_IS_STRING(id)) {
         *resolved = false;
         return true; /* not resolved, but no error */
@@ -174,20 +164,8 @@ repo_resolve(JSContext       *context,
         return true;
     }
 
-    priv = priv_from_js(context, obj);
-    gjs_debug_jsprop(GJS_DEBUG_GREPO, "Resolve prop '%s' hook, obj %s, priv %p",
-                     gjs_debug_id(id).c_str(), gjs_debug_object(obj).c_str(), priv);
-
-    if (priv == NULL) {
-        /* we are the prototype, or have the wrong class */
-        *resolved = false;
-        return true;
-    }
-
-    if (!JSID_IS_STRING(id)) {
-        *resolved = false;
-        return true;
-    }
+    gjs_debug_jsprop(GJS_DEBUG_GREPO, "Resolve prop '%s' hook, obj %s",
+                     gjs_debug_id(id).c_str(), gjs_debug_object(obj).c_str());
 
     if (!resolve_namespace_object(context, obj, id))
         return false;
@@ -196,72 +174,30 @@ repo_resolve(JSContext       *context,
     return true;
 }
 
-GJS_NATIVE_CONSTRUCTOR_DEFINE_ABSTRACT(repo)
-
-static void repo_finalize(JSFreeOp*, JSObject* obj) {
-    Repo *priv;
-
-    priv = (Repo*) JS_GetPrivate(obj);
-    gjs_debug_lifecycle(GJS_DEBUG_GREPO,
-                        "finalize, obj %p priv %p", obj, priv);
-    if (priv == NULL)
-        return; /* we are the prototype, not a real instance */
-
-    GJS_DEC_COUNTER(repo);
-    delete priv;
-}
-
-/* The bizarre thing about this vtable is that it applies to both
- * instances of the object, and to the prototype that instances of the
- * class have.
- */
 static const struct JSClassOps gjs_repo_class_ops = {
     nullptr,  // addProperty
     nullptr,  // deleteProperty
     nullptr,  // enumerate
     nullptr,  // newEnumerate
     repo_resolve,
-    nullptr,  // mayResolve
-    repo_finalize};
+};
 
 struct JSClass gjs_repo_class = {
-    "GIRepository", /* means "new GIRepository()" works */
-    JSCLASS_HAS_PRIVATE | JSCLASS_FOREGROUND_FINALIZE,
+    "GIRepository",
+    0,
     &gjs_repo_class_ops,
 };
 
-// clang-format off
-static const JSPropertySpec gjs_repo_proto_props[] = {
-    JS_STRING_SYM_PS(toStringTag, "GIRepository", JSPROP_READONLY),
-    JS_PS_END};
-// clang-format on
-
-static JSFunctionSpec *gjs_repo_proto_funcs = nullptr;
-static JSFunctionSpec *gjs_repo_static_funcs = nullptr;
-
-GJS_DEFINE_PROTO_FUNCS(repo)
-
 GJS_JSAPI_RETURN_CONVENTION
 static JSObject*
 repo_new(JSContext *context)
 {
-    JS::RootedObject proto(context);
-    if (!gjs_repo_define_proto(context, nullptr, &proto))
-        return nullptr;
-
-    JS::RootedObject repo(context,
-        JS_NewObjectWithGivenProto(context, &gjs_repo_class, proto));
+    JS::RootedObject repo(context, JS_NewObject(context, &gjs_repo_class));
     if (repo == nullptr)
         return nullptr;
 
-    auto* priv = new Repo();
-    GJS_INC_COUNTER(repo);
-
-    g_assert(priv_from_js(context, repo) == NULL);
-    JS_SetPrivate(repo, priv);
-
-    gjs_debug_lifecycle(GJS_DEBUG_GREPO,
-                        "repo constructor, obj %p priv %p", repo.get(), priv);
+    gjs_debug_lifecycle(GJS_DEBUG_GREPO, "repo constructor, obj %p",
+                        repo.get());
 
     const GjsAtoms& atoms = GjsContextPrivate::atoms(context);
     JS::RootedObject versions(context, JS_NewPlainObject(context));
diff --git a/gjs/global.h b/gjs/global.h
index 0ac56c51..bdbc18e2 100644
--- a/gjs/global.h
+++ b/gjs/global.h
@@ -36,7 +36,6 @@ enum class GjsGlobalSlot : uint32_t {
     PROTOTYPE_importer,
     PROTOTYPE_function,
     PROTOTYPE_ns,
-    PROTOTYPE_repo,
     PROTOTYPE_cairo_context,
     PROTOTYPE_cairo_gradient,
     PROTOTYPE_cairo_image_surface,
diff --git a/gjs/mem-private.h b/gjs/mem-private.h
index 739fad0b..22c513e2 100644
--- a/gjs/mem-private.h
+++ b/gjs/mem-private.h
@@ -29,7 +29,6 @@ typedef struct {
     macro(object_instance)          \
     macro(object_prototype)         \
     macro(param)                    \
-    macro(repo)                     \
     macro(union_instance)           \
     macro(union_prototype)
 // clang-format on


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