[gjs/wip/ptomato/mozjs52: 2/9] js: New JSClass struct layout



commit bd8e043470c0a3e4e3c163eef7bd9f03e14fed42
Author: Philip Chimento <philip chimento gmail com>
Date:   Mon May 1 22:44:34 2017 -0700

    js: New JSClass struct layout
    
    Instead of the various operation hooks being members of JSClass directly,
    now JSClass contains a pointer to a JSClassOps struct. The JSClassOps
    instead contains the function pointers to the operation hooks.
    
    For importer.cpp, we still use the internal js::Class instead of JSClass.
    This also contains a pointer to another struct, JSObjectOps, which we still
    need for our internal lazy enumerate hook.

 gi/boxed.cpp                |   21 ++++++++++--------
 gi/function.cpp             |   11 ++++++---
 gi/fundamental.cpp          |   15 ++++++-------
 gi/gerror.cpp               |   11 ++++++---
 gi/interface.cpp            |   11 ++++++---
 gi/ns.cpp                   |   10 ++++++--
 gi/object.cpp               |   11 ++++++---
 gi/param.cpp                |   11 ++++++---
 gi/repo.cpp                 |   10 ++++++--
 gi/union.cpp                |   10 ++++++--
 gjs/byteArray.cpp           |   11 ++++++---
 gjs/coverage.cpp            |   10 ++++++--
 gjs/importer.cpp            |   49 ++++++++++++++++++++++---------------------
 gjs/jsapi-class.h           |    9 +++++--
 gjs/jsapi-dynamic-class.cpp |    4 +-
 gjs/jsapi-util.cpp          |   10 ++++++--
 test/gjs-test-rooting.cpp   |   10 ++++++--
 17 files changed, 136 insertions(+), 88 deletions(-)
---
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index f5fd4bc..b1eb969 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -924,16 +924,8 @@ boxed_trace(JSTracer *tracer,
 /* 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.
- *
- * We allocate 1 reserved slot; this is typically unused, but if the
- * boxed is for a nested structure inside a parent structure, the
- * reserved slot is used to hold onto the parent Javascript object and
- * make sure it doesn't get freed.
  */
-struct JSClass gjs_boxed_class = {
-    "GObject_Boxed",
-    JSCLASS_HAS_PRIVATE |
-    JSCLASS_HAS_RESERVED_SLOTS(1),
+static const struct JSClassOps gjs_boxed_class_ops = {
     NULL,  /* addProperty */
     NULL,  /* deleteProperty */
     NULL,  /* getProperty */
@@ -948,6 +940,17 @@ struct JSClass gjs_boxed_class = {
     boxed_trace
 };
 
+/* We allocate 1 reserved slot; this is typically unused, but if the
+ * boxed is for a nested structure inside a parent structure, the
+ * reserved slot is used to hold onto the parent Javascript object and
+ * make sure it doesn't get freed.
+ */
+struct JSClass gjs_boxed_class = {
+    "GObject_Boxed",
+    JSCLASS_HAS_PRIVATE | JSCLASS_HAS_RESERVED_SLOTS(1),
+    &gjs_boxed_class_ops
+};
+
 JSPropertySpec gjs_boxed_proto_props[] = {
     JS_PS_END
 };
diff --git a/gi/function.cpp b/gi/function.cpp
index 074904d..70306cd 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -1483,10 +1483,7 @@ function_to_string (JSContext *context,
  * instances of the object, and to the prototype that instances of the
  * class have.
  */
-struct JSClass gjs_function_class = {
-    "GIRepositoryFunction", /* means "new GIRepositoryFunction()" works */
-    JSCLASS_HAS_PRIVATE |
-    JSCLASS_BACKGROUND_FINALIZE,
+static const struct JSClassOps gjs_function_class_ops = {
     NULL,  /* addProperty */
     NULL,  /* deleteProperty */
     NULL,  /* getProperty */
@@ -1498,6 +1495,12 @@ struct JSClass gjs_function_class = {
     function_call
 };
 
+struct JSClass gjs_function_class = {
+    "GIRepositoryFunction", /* means "new GIRepositoryFunction()" works */
+    JSCLASS_HAS_PRIVATE | JSCLASS_BACKGROUND_FINALIZE,
+    &gjs_function_class_ops
+};
+
 static JSPropertySpec gjs_function_proto_props[] = {
     JS_PSG("length", get_num_arguments, JSPROP_PERMANENT),
     JS_PS_END
diff --git a/gi/fundamental.cpp b/gi/fundamental.cpp
index 677fd65..ae8e3f8 100644
--- a/gi/fundamental.cpp
+++ b/gi/fundamental.cpp
@@ -549,15 +549,8 @@ fundamental_trace(JSTracer *tracer,
  * tell, it would only be used if no constructor were provided to
  * JS_InitClass. The constructor from JS_InitClass is not applied to
  * the prototype unless JSCLASS_CONSTRUCT_PROTOTYPE is in flags.
- *
- * We allocate 1 reserved slot; this is typically unused, but if the
- * fundamental is for a nested structure inside a parent structure, the
- * reserved slot is used to hold onto the parent Javascript object and
- * make sure it doesn't get freed.
  */
-struct JSClass gjs_fundamental_instance_class = {
-    "GFundamental_Object",
-    JSCLASS_HAS_PRIVATE,
+static const struct JSClassOps gjs_fundamental_class_ops = {
     NULL,  /* addProperty */
     NULL,  /* deleteProperty */
     NULL,  /* getProperty */
@@ -572,6 +565,12 @@ struct JSClass gjs_fundamental_instance_class = {
     fundamental_trace
 };
 
+struct JSClass gjs_fundamental_instance_class = {
+    "GFundamental_Object",
+    JSCLASS_HAS_PRIVATE,
+    &gjs_fundamental_class_ops
+};
+
 static JSPropertySpec gjs_fundamental_instance_proto_props[] = {
     JS_PS_END
 };
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index 00cc3cf..d6111f3 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -269,10 +269,7 @@ error_constructor_value_of(JSContext *context,
  * instances of the object, and to the prototype that instances of the
  * class have.
  */
-struct JSClass gjs_error_class = {
-    "GLib_Error",
-    JSCLASS_HAS_PRIVATE |
-    JSCLASS_BACKGROUND_FINALIZE,
+static const struct JSClassOps gjs_error_class_ops = {
     NULL,  /* addProperty */
     NULL,  /* deleteProperty */
     NULL,  /* getProperty */
@@ -283,6 +280,12 @@ struct JSClass gjs_error_class = {
     error_finalize
 };
 
+struct JSClass gjs_error_class = {
+    "GLib_Error",
+    JSCLASS_HAS_PRIVATE | JSCLASS_BACKGROUND_FINALIZE,
+    &gjs_error_class_ops
+};
+
 /* We need to shadow all fields of GError, to prevent calling the getter from GBoxed
    (which would trash memory accessing the instance private data) */
 JSPropertySpec gjs_error_proto_props[] = {
diff --git a/gi/interface.cpp b/gi/interface.cpp
index b1f9647..1da35b8 100644
--- a/gi/interface.cpp
+++ b/gi/interface.cpp
@@ -162,10 +162,7 @@ interface_resolve(JSContext       *context,
     return true;
 }
 
-struct JSClass gjs_interface_class = {
-    "GObject_Interface",
-    JSCLASS_HAS_PRIVATE |
-    JSCLASS_BACKGROUND_FINALIZE,
+static const struct JSClassOps gjs_interface_class_ops = {
     NULL,  /* addProperty */
     NULL,  /* deleteProperty */
     NULL,  /* getProperty */
@@ -176,6 +173,12 @@ struct JSClass gjs_interface_class = {
     interface_finalize
 };
 
+struct JSClass gjs_interface_class = {
+    "GObject_Interface",
+    JSCLASS_HAS_PRIVATE | JSCLASS_BACKGROUND_FINALIZE,
+    &gjs_interface_class_ops
+};
+
 JSPropertySpec gjs_interface_proto_props[] = {
     JS_PS_END
 };
diff --git a/gi/ns.cpp b/gi/ns.cpp
index 7c5fe2b..f16ab24 100644
--- a/gi/ns.cpp
+++ b/gi/ns.cpp
@@ -156,9 +156,7 @@ ns_finalize(JSFreeOp *fop,
  * instances of the object, and to the prototype that instances of the
  * class have.
  */
-struct JSClass gjs_ns_class = {
-    "GIRepositoryNamespace",
-    JSCLASS_HAS_PRIVATE,
+static const struct JSClassOps gjs_ns_class_ops = {
     NULL,  /* addProperty */
     NULL,  /* deleteProperty */
     NULL,  /* getProperty */
@@ -169,6 +167,12 @@ struct JSClass gjs_ns_class = {
     ns_finalize
 };
 
+struct JSClass gjs_ns_class = {
+    "GIRepositoryNamespace",
+    JSCLASS_HAS_PRIVATE,
+    &gjs_ns_class_ops
+};
+
 static JSPropertySpec gjs_ns_proto_props[] = {
     JS_PSG("__name__", get_name, GJS_MODULE_PROP_FLAGS),
     JS_PS_END
diff --git a/gi/object.cpp b/gi/object.cpp
index ddcf1d7..aa43a1c 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1806,9 +1806,7 @@ to_string_func(JSContext *context,
                                      priv->gobj, rec.rval());
 }
 
-struct JSClass gjs_object_instance_class = {
-    "GObject_Object",
-    JSCLASS_HAS_PRIVATE,
+static const struct JSClassOps gjs_object_class_ops = {
     NULL,  /* addProperty */
     NULL,  /* deleteProperty */
     object_instance_get_prop,
@@ -1821,7 +1819,12 @@ struct JSClass gjs_object_instance_class = {
     NULL,
     NULL,
     object_instance_trace,
-    
+};
+
+struct JSClass gjs_object_instance_class = {
+    "GObject_Object",
+    JSCLASS_HAS_PRIVATE,
+    &gjs_object_class_ops
 };
 
 static bool
diff --git a/gi/param.cpp b/gi/param.cpp
index d2e6e5a..230f5c1 100644
--- a/gi/param.cpp
+++ b/gi/param.cpp
@@ -144,10 +144,7 @@ param_finalize(JSFreeOp *fop,
  * instances of the object, and to the prototype that instances of the
  * class have.
  */
-struct JSClass gjs_param_class = {
-    "GObject_ParamSpec",
-    JSCLASS_HAS_PRIVATE |
-    JSCLASS_BACKGROUND_FINALIZE,
+static const struct JSClassOps gjs_param_class_ops = {
     NULL,  /* addProperty */
     NULL,  /* deleteProperty */
     NULL,  /* getProperty */
@@ -158,6 +155,12 @@ struct JSClass gjs_param_class = {
     param_finalize
 };
 
+struct JSClass gjs_param_class = {
+    "GObject_ParamSpec",
+    JSCLASS_HAS_PRIVATE | JSCLASS_BACKGROUND_FINALIZE,
+    &gjs_param_class_ops
+};
+
 JSPropertySpec gjs_param_proto_props[] = {
     JS_PS_END
 };
diff --git a/gi/repo.cpp b/gi/repo.cpp
index cdc5064..b30f90a 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -225,9 +225,7 @@ repo_finalize(JSFreeOp *fop,
  * instances of the object, and to the prototype that instances of the
  * class have.
  */
-struct JSClass gjs_repo_class = {
-    "GIRepository", /* means "new GIRepository()" works */
-    JSCLASS_HAS_PRIVATE,
+static const struct JSClassOps gjs_repo_class_ops = {
     NULL,  /* addProperty */
     NULL,  /* deleteProperty */
     NULL,  /* getProperty */
@@ -238,6 +236,12 @@ struct JSClass gjs_repo_class = {
     repo_finalize
 };
 
+struct JSClass gjs_repo_class = {
+    "GIRepository", /* means "new GIRepository()" works */
+    JSCLASS_HAS_PRIVATE,
+    &gjs_repo_class_ops,
+};
+
 static JSPropertySpec *gjs_repo_proto_props = nullptr;
 static JSFunctionSpec *gjs_repo_proto_funcs = nullptr;
 static JSFunctionSpec *gjs_repo_static_funcs = nullptr;
diff --git a/gi/union.cpp b/gi/union.cpp
index 8823b91..02c759d 100644
--- a/gi/union.cpp
+++ b/gi/union.cpp
@@ -292,9 +292,7 @@ to_string_func(JSContext *context,
  * instances of the object, and to the prototype that instances of the
  * class have.
  */
-struct JSClass gjs_union_class = {
-    "GObject_Union",
-    JSCLASS_HAS_PRIVATE,
+static const struct JSClassOps gjs_union_class_ops = {
     NULL,  /* addProperty */
     NULL,  /* deleteProperty */
     NULL,  /* getProperty */
@@ -305,6 +303,12 @@ struct JSClass gjs_union_class = {
     union_finalize
 };
 
+struct JSClass gjs_union_class = {
+    "GObject_Union",
+    JSCLASS_HAS_PRIVATE,
+    &gjs_union_class_ops
+};
+
 JSPropertySpec gjs_union_proto_props[] = {
     JS_PS_END
 };
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 9395a74..06121ee 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -55,10 +55,7 @@ static void   byte_array_finalize      (JSFreeOp     *fop,
 
 static JSObject *gjs_byte_array_get_proto(JSContext *);
 
-struct JSClass gjs_byte_array_class = {
-    "ByteArray",
-    JSCLASS_HAS_PRIVATE |
-    JSCLASS_BACKGROUND_FINALIZE,
+static const struct JSClassOps gjs_byte_array_class_ops = {
     NULL,  /* addProperty */
     NULL,  /* deleteProperty */
     byte_array_get_prop,
@@ -69,6 +66,12 @@ struct JSClass gjs_byte_array_class = {
     byte_array_finalize
 };
 
+struct JSClass gjs_byte_array_class = {
+    "ByteArray",
+    JSCLASS_HAS_PRIVATE | JSCLASS_BACKGROUND_FINALIZE,
+    &gjs_byte_array_class_ops
+};
+
 bool
 gjs_typecheck_bytearray(JSContext       *context,
                         JS::HandleObject object,
diff --git a/gjs/coverage.cpp b/gjs/coverage.cpp
index 42e44c7..ccfd6c8 100644
--- a/gjs/coverage.cpp
+++ b/gjs/coverage.cpp
@@ -1259,9 +1259,7 @@ gjs_coverage_init(GjsCoverage *self)
 {
 }
 
-static JSClass coverage_global_class = {
-    "GjsCoverageGlobal",
-    JSCLASS_GLOBAL_FLAGS_WITH_SLOTS(GJS_GLOBAL_SLOT_LAST),
+static const struct JSClassOps gjs_coverage_global_class_ops = {
     NULL,  /* addProperty */
     NULL,  /* deleteProperty */
     NULL,  /* getProperty */
@@ -1276,6 +1274,12 @@ static JSClass coverage_global_class = {
     JS_GlobalObjectTraceHook
 };
 
+static JSClass coverage_global_class = {
+    "GjsCoverageGlobal",
+    JSCLASS_GLOBAL_FLAGS_WITH_SLOTS(GJS_GLOBAL_SLOT_LAST),
+    &gjs_coverage_global_class_ops
+};
+
 static bool
 gjs_context_eval_file_in_compartment(GjsContext      *context,
                                      const char      *filename,
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index 505f717..5eb4bcf 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -860,9 +860,7 @@ importer_finalize(js::FreeOp *fop,
  * instances of the object, and to the prototype that instances of the
  * class have.
  */
-const js::Class gjs_importer_real_class = {
-    "GjsFileImporter",
-    JSCLASS_HAS_PRIVATE,
+static const js::ClassOps gjs_importer_class_ops = {
     NULL,  /* addProperty */
     NULL,  /* deleteProperty */
     NULL,  /* getProperty */
@@ -870,27 +868,30 @@ const js::Class gjs_importer_real_class = {
     NULL,  /* enumerate (see below) */
     importer_resolve,
     nullptr,  /* mayResolve */
-    importer_finalize,
-    NULL,  /* call */
-    NULL,  /* hasInstance */
-    NULL,  /* construct */
-    NULL,  /* trace */
-    JS_NULL_CLASS_SPEC,
-    JS_NULL_CLASS_EXT,
-    {
-        NULL,  /* lookupProperty */
-        NULL,  /* defineProperty */
-        NULL,  /* hasProperty */
-        NULL,  /* getProperty */
-        NULL,  /* setProperty */
-        NULL,  /* getOwnPropertyDescriptor */
-        NULL,  /* deleteProperty */
-        NULL,  /* watch */
-        NULL,  /* unwatch */
-        NULL,  /* getElements */
-        importer_enumerate,
-        NULL,  /* thisObject */
-    }
+    importer_finalize
+};
+
+static const js::ObjectOps gjs_importer_object_ops = {
+    NULL,  /* lookupProperty */
+    NULL,  /* defineProperty */
+    NULL,  /* hasProperty */
+    NULL,  /* getProperty */
+    NULL,  /* setProperty */
+    NULL,  /* getOwnPropertyDescriptor */
+    NULL,  /* deleteProperty */
+    NULL,  /* watch */
+    NULL,  /* unwatch */
+    NULL,  /* getElements */
+    importer_enumerate
+};
+
+const js::Class gjs_importer_real_class = {
+    "GjsFileImporter",
+    JSCLASS_HAS_PRIVATE,
+    &gjs_importer_class_ops,
+    nullptr,
+    nullptr,
+    &gjs_importer_object_ops
 };
 
 static JSPropertySpec *gjs_importer_proto_props = nullptr;
diff --git a/gjs/jsapi-class.h b/gjs/jsapi-class.h
index 47696e0..729b790 100644
--- a/gjs/jsapi-class.h
+++ b/gjs/jsapi-class.h
@@ -170,9 +170,7 @@ extern JSPropertySpec gjs_##cname##_proto_props[];                           \
 extern JSFunctionSpec gjs_##cname##_proto_funcs[];                           \
 extern JSFunctionSpec gjs_##cname##_static_funcs[];                          \
 static void gjs_##cname##_finalize(JSFreeOp *fop, JSObject *obj);            \
-static struct JSClass gjs_##cname##_class = {                                \
-    type_name,                                                               \
-    JSCLASS_HAS_PRIVATE | jsclass_flags,                                     \
+static const struct JSClassOps gjs_##cname##_class_ops = {                   \
     nullptr,  /* addProperty */                                              \
     nullptr,  /* deleteProperty */                                           \
     nullptr,  /* getProperty */                                              \
@@ -182,6 +180,11 @@ static struct JSClass gjs_##cname##_class = {                                \
     nullptr,  /* mayResolve */                                               \
     gjs_##cname##_finalize                                                   \
 };                                                                           \
+static struct JSClass gjs_##cname##_class = {                                \
+    type_name,                                                               \
+    JSCLASS_HAS_PRIVATE | jsclass_flags,                                     \
+    &gjs_##cname##_class_ops                                                 \
+};                                                                           \
 _GJS_DEFINE_GET_PROTO(cname)                                                 \
 _GJS_DEFINE_DEFINE_PROTO(cname, parent_cname, ctor, gtype)
 
diff --git a/gjs/jsapi-dynamic-class.cpp b/gjs/jsapi-dynamic-class.cpp
index d0e947b..a660c19 100644
--- a/gjs/jsapi-dynamic-class.cpp
+++ b/gjs/jsapi-dynamic-class.cpp
@@ -88,7 +88,7 @@ gjs_init_class_dynamic(JSContext              *context,
         goto out;
 
     /* Bypass resolve hooks when defining the initial properties */
-    if (clasp->resolve) {
+    if (clasp->cOps->resolve) {
         JSPropertySpec *ps_iter;
         JSFunctionSpec *fs_iter;
         for (ps_iter = proto_ps; ps_iter && ps_iter->name; ps_iter++)
@@ -115,7 +115,7 @@ gjs_init_class_dynamic(JSContext              *context,
     if (static_fs && !JS_DefineFunctions(context, constructor, static_fs))
         goto out;
 
-    if (!clasp->resolve) {
+    if (!clasp->cOps->resolve) {
         if (!JS_LinkConstructorAndPrototype(context, constructor, prototype))
             goto out;
     } else {
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 9e1692e..a8461a0 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -45,9 +45,7 @@ gjs_util_error_quark (void)
     return g_quark_from_static_string ("gjs-util-error-quark");
 }
 
-static JSClass global_class = {
-    "GjsGlobal",
-    JSCLASS_GLOBAL_FLAGS_WITH_SLOTS(GJS_GLOBAL_SLOT_LAST),
+static const JSClassOps global_class_ops = {
     NULL,  /* addProperty */
     NULL,  /* deleteProperty */
     NULL,  /* getProperty */
@@ -62,6 +60,12 @@ static JSClass global_class = {
     JS_GlobalObjectTraceHook
 };
 
+static JSClass global_class = {
+    "GjsGlobal",
+    JSCLASS_GLOBAL_FLAGS_WITH_SLOTS(GJS_GLOBAL_SLOT_LAST),
+    &global_class_ops
+};
+
 /**
  * gjs_init_context_standard:
  * @context: a #JSContext
diff --git a/test/gjs-test-rooting.cpp b/test/gjs-test-rooting.cpp
index 99bb0bc..3b90b52 100644
--- a/test/gjs-test-rooting.cpp
+++ b/test/gjs-test-rooting.cpp
@@ -24,9 +24,7 @@ test_obj_finalize(JSFreeOp *fop,
     *finalized_p = true;
 }
 
-static JSClass test_obj_class = {
-    "TestObj",
-    JSCLASS_HAS_PRIVATE,
+static const JSClassOps test_obj_class_ops = {
     NULL,  /* addProperty */
     NULL,  /* deleteProperty */
     NULL,  /* getProperty */
@@ -37,6 +35,12 @@ static JSClass test_obj_class = {
     test_obj_finalize
 };
 
+static JSClass test_obj_class = {
+    "TestObj",
+    JSCLASS_HAS_PRIVATE,
+    &test_obj_class_ops
+};
+
 static JSObject *
 test_obj_new(GjsRootingFixture *fx)
 {


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