[gjs/wip/ptomato/mozjs31: 23/25] js: Adapt to new JS_DefineProperty API



commit 178a5ede2a8d2b15e29157fcbbb24aa90915010e
Author: Philip Chimento <philip endlessm com>
Date:   Fri Nov 4 17:29:21 2016 -0700

    js: Adapt to new JS_DefineProperty API
    
    The setter and getter parameters now come after the flags, and passing
    NULL for them is no longer required, since they have default values in
    mozjs31. I believe that NULL (use the object's class's default setters
    and getters) is different from JSPropertyStub and JSStrictPropertyStub,
    so I left those in where they occurred.
    
    In mozjs31 a bunch of overloads were defined, where you can pass
    JS::HandleObject, JS::HandleString, int32_t, uint32_t, and double
    directly to JS_DefineProperty() instead of only JS::HandleValue. This
    allows us to get rid of some roots in our code where we created a
    JS::RootedValue for the sole purpose of passing some value to
    JS_DefineProperty().

 gi/arg.cpp                  |    3 +--
 gi/boxed.cpp                |   12 ++++++------
 gi/enumeration.cpp          |   15 +++++----------
 gi/function.cpp             |    5 +----
 gi/fundamental.cpp          |    7 +++----
 gi/interface.cpp            |    7 +++----
 gi/object.cpp               |    8 ++++----
 gi/param.cpp                |    7 +++----
 gi/repo.cpp                 |    5 +----
 gi/union.cpp                |    7 +++----
 gjs/context.cpp             |    5 +----
 gjs/importer.cpp            |   29 +++++++++--------------------
 gjs/jsapi-dynamic-class.cpp |   14 ++++++++------
 gjs/jsapi-util.cpp          |   11 +++++------
 gjs/jsapi-util.h            |   11 +++++------
 modules/system.cpp          |   11 +++++------
 16 files changed, 63 insertions(+), 94 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index 9239bda..00b9a49 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -2567,8 +2567,7 @@ gjs_object_from_g_hash (JSContext             *context,
                                        true))
             goto out;
 
-        if (!JS_DefineProperty(context, obj, keyutf8, valjs,
-                               NULL, NULL, JSPROP_ENUMERATE))
+        if (!JS_DefineProperty(context, obj, keyutf8, valjs, JSPROP_ENUMERATE))
             goto out;
 
         g_free(keyutf8);
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index 97dcbc2..86fd610 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -862,8 +862,8 @@ define_boxed_class_fields (JSContext       *context,
         bool result;
 
         result = JS_DefineProperty(context, proto, field_name, JS::NullHandleValue,
-                                   boxed_field_getter, boxed_field_setter,
-                                   JSPROP_PERMANENT | JSPROP_SHARED);
+                                   JSPROP_PERMANENT | JSPROP_SHARED,
+                                   boxed_field_getter, boxed_field_setter);
 
         g_base_info_unref ((GIBaseInfo *)field);
 
@@ -1172,10 +1172,10 @@ gjs_define_boxed_class(JSContext       *context,
     define_boxed_class_fields (context, priv, prototype);
     gjs_define_static_methods (context, constructor, priv->gtype, priv->info);
 
-    JS::RootedValue value(context,
-        JS::ObjectOrNullValue(gjs_gtype_create_gtype_wrapper(context, priv->gtype)));
-    JS_DefineProperty(context, constructor, "$gtype", value,
-                      NULL, NULL, JSPROP_PERMANENT);
+    JS::RootedObject gtype_obj(context,
+        gjs_gtype_create_gtype_wrapper(context, priv->gtype));
+    JS_DefineProperty(context, constructor, "$gtype", gtype_obj,
+                      JSPROP_PERMANENT);
 }
 
 JSObject*
diff --git a/gi/enumeration.cpp b/gi/enumeration.cpp
index f4b901e..d73622e 100644
--- a/gi/enumeration.cpp
+++ b/gi/enumeration.cpp
@@ -65,10 +65,8 @@ gjs_define_enum_value(JSContext       *context,
               "Defining enum value %s (fixed from %s) %" G_GINT64_MODIFIER "d",
               fixed_name, value_name, value_val);
 
-    JS::RootedValue value_js(context, JS::NumberValue(value_val));
     if (!JS_DefineProperty(context, in_object,
-                           fixed_name, value_js,
-                           NULL, NULL,
+                           fixed_name, (double) value_val,
                            GJS_MODULE_PROP_FLAGS)) {
         gjs_throw(context, "Unable to define enumeration value %s %" G_GINT64_FORMAT " (no memory most 
likely)",
                   fixed_name, value_val);
@@ -106,10 +104,9 @@ gjs_define_enum_values(JSContext       *context,
     }
 
     gtype = g_registered_type_info_get_g_type((GIRegisteredTypeInfo*)info);
-    JS::RootedValue value(context,
-        JS::ObjectOrNullValue(gjs_gtype_create_gtype_wrapper(context, gtype)));
-    JS_DefineProperty(context, in_object, "$gtype", value,
-                      NULL, NULL, JSPROP_PERMANENT);
+    JS::RootedObject gtype_obj(context,
+        gjs_gtype_create_gtype_wrapper(context, gtype));
+    JS_DefineProperty(context, in_object, "$gtype", gtype_obj, JSPROP_PERMANENT);
 
     return true;
 }
@@ -189,9 +186,7 @@ gjs_define_enumeration(JSContext       *context,
               g_base_info_get_namespace( (GIBaseInfo*) info),
               enum_name, enum_obj.get());
 
-    JS::RootedValue v_enum(context, JS::ObjectValue(*enum_obj));
-    if (!JS_DefineProperty(context, in_object, enum_name, v_enum,
-                           NULL, NULL,
+    if (!JS_DefineProperty(context, in_object, enum_name, enum_obj,
                            GJS_MODULE_PROP_FLAGS)) {
         gjs_throw(context, "Unable to define enumeration property (no memory most likely)");
         return false;
diff --git a/gi/function.cpp b/gi/function.cpp
index 8cb1ce2..53618c2 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -1743,10 +1743,7 @@ gjs_define_function(JSContext       *context,
         g_assert_not_reached ();
     }
 
-    /* COMPAT: JS_DefineProperty is overloaded to take JS::HandleObject in 31 */
-    JS::RootedValue v_function(context, JS::ObjectValue(*function));
-    if (!JS_DefineProperty(context, in_object, name, v_function,
-                           NULL, NULL,
+    if (!JS_DefineProperty(context, in_object, name, function,
                            GJS_MODULE_PROP_FLAGS)) {
         gjs_debug(GJS_DEBUG_GFUNCTION, "Failed to define function");
         function = NULL;
diff --git a/gi/fundamental.cpp b/gi/fundamental.cpp
index 197709a..eab9cc4 100644
--- a/gi/fundamental.cpp
+++ b/gi/fundamental.cpp
@@ -730,10 +730,9 @@ gjs_define_fundamental_class(JSContext              *context,
 
     gjs_object_define_static_methods(context, constructor, gtype, info);
 
-    JS::RootedValue value(context,
-        JS::ObjectOrNullValue(gjs_gtype_create_gtype_wrapper(context, gtype)));
-    JS_DefineProperty(context, constructor, "$gtype", value,
-                      NULL, NULL, JSPROP_PERMANENT);
+    JS::RootedObject gtype_obj(context,
+        gjs_gtype_create_gtype_wrapper(context, gtype));
+    JS_DefineProperty(context, constructor, "$gtype", gtype_obj, JSPROP_PERMANENT);
 
     return true;
 }
diff --git a/gi/interface.cpp b/gi/interface.cpp
index 631eef0..f4558eb 100644
--- a/gi/interface.cpp
+++ b/gi/interface.cpp
@@ -225,10 +225,9 @@ gjs_define_interface_class(JSContext              *context,
     if (priv->info)
         gjs_define_static_methods(context, constructor, priv->gtype, priv->info);
 
-    JS::RootedValue value(context,
-        JS::ObjectOrNullValue(gjs_gtype_create_gtype_wrapper(context, priv->gtype)));
-    JS_DefineProperty(context, constructor, "$gtype", value,
-                      NULL, NULL, JSPROP_PERMANENT);
+    JS::RootedObject gtype_obj(context,
+        gjs_gtype_create_gtype_wrapper(context, priv->gtype));
+    JS_DefineProperty(context, constructor, "$gtype", gtype_obj, JSPROP_PERMANENT);
 
     return true;
 }
diff --git a/gi/object.cpp b/gi/object.cpp
index 21f5217..11fff54 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1962,10 +1962,10 @@ gjs_define_object_class(JSContext              *context,
     if (info)
         gjs_object_define_static_methods(context, constructor, gtype, info);
 
-    JS::RootedValue value(context,
-        JS::ObjectValue(*gjs_gtype_create_gtype_wrapper(context, gtype)));
-    JS_DefineProperty(context, constructor, "$gtype", value,
-                      NULL, NULL, JSPROP_PERMANENT);
+    JS::RootedObject gtype_obj(context,
+        gjs_gtype_create_gtype_wrapper(context, gtype));
+    JS_DefineProperty(context, constructor, "$gtype", gtype_obj,
+                      JSPROP_PERMANENT);
 }
 
 static void
diff --git a/gi/param.cpp b/gi/param.cpp
index baa7960..bc23f31 100644
--- a/gi/param.cpp
+++ b/gi/param.cpp
@@ -237,10 +237,9 @@ gjs_define_param_class(JSContext       *context,
         g_error("Can't init class %s", constructor_name);
     }
 
-    JS::RootedValue value(context,
-        JS::ObjectOrNullValue(gjs_gtype_create_gtype_wrapper(context, G_TYPE_PARAM)));
-    JS_DefineProperty(context, constructor, "$gtype", value,
-                      NULL, NULL, JSPROP_PERMANENT);
+    JS::RootedObject gtype_obj(context,
+        gjs_gtype_create_gtype_wrapper(context, G_TYPE_PARAM));
+    JS_DefineProperty(context, constructor, "$gtype", gtype_obj, JSPROP_PERMANENT);
 
     info = (GIObjectInfo*)g_irepository_find_by_gtype(g_irepository_get_default(), G_TYPE_PARAM);
     gjs_object_define_static_methods(context, constructor, G_TYPE_PARAM, info);
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 4ffce4e..0397958 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -120,9 +120,7 @@ resolve_namespace_object(JSContext       *context,
 
     /* Define the property early, to avoid reentrancy issues if
        the override module looks for namespaces that import this */
-    JS::RootedValue v_namespace(context, JS::ObjectValue(*gi_namespace));
-    if (!JS_DefineProperty(context, repo_obj, ns_name, v_namespace,
-                           NULL, NULL,
+    if (!JS_DefineProperty(context, repo_obj, ns_name, gi_namespace,
                            GJS_MODULE_PROP_FLAGS))
         g_error("no memory to define ns property");
 
@@ -354,7 +352,6 @@ gjs_define_constant(JSContext       *context,
 
     if (JS_DefineProperty(context, in_object,
                           name, value,
-                          NULL, NULL,
                           GJS_MODULE_PROP_FLAGS))
         ret = true;
 
diff --git a/gi/union.cpp b/gi/union.cpp
index bdedaa7..7484f51 100644
--- a/gi/union.cpp
+++ b/gi/union.cpp
@@ -374,10 +374,9 @@ gjs_define_union_class(JSContext       *context,
               constructor_name, prototype.get(), JS_GetClass(prototype),
               in_object.get());
 
-    JS::RootedValue value(context,
-        JS::ObjectOrNullValue(gjs_gtype_create_gtype_wrapper(context, gtype)));
-    JS_DefineProperty(context, constructor, "$gtype", value,
-                      NULL, NULL, JSPROP_PERMANENT);
+    JS::RootedObject gtype_obj(context,
+        gjs_gtype_create_gtype_wrapper(context, gtype));
+    JS_DefineProperty(context, constructor, "$gtype", gtype_obj, JSPROP_PERMANENT);
 
     return true;
 }
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 1c4e034..7f8a537 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -437,10 +437,7 @@ gjs_context_constructed(GObject *object)
 
     JSAutoCompartment ac(js_context->context, global);
 
-    JS::RootedValue v_global(js_context->context, JS::ObjectValue(*global));
-    if (!JS_DefineProperty(js_context->context, global,
-                           "window", v_global,
-                           NULL, NULL,
+    if (!JS_DefineProperty(js_context->context, global, "window", global,
                            JSPROP_READONLY | JSPROP_PERMANENT))
         g_error("No memory to export global object as 'window'");
 
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index b14ecbe..1aa6c97 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -71,10 +71,8 @@ define_meta_properties(JSContext       *context,
               module_name ? module_name : "<root>", parent_is_module);
 
     if (full_path != NULL) {
-        JS::RootedValue file_val(context,
-            JS::StringValue(JS_NewStringCopyZ(context, full_path)));
-        if (!JS_DefineProperty(context, module_obj, "__file__", file_val,
-                               NULL, NULL,
+        JS::RootedString file(context, JS_NewStringCopyZ(context, full_path));
+        if (!JS_DefineProperty(context, module_obj, "__file__", file,
                                /* don't set ENUMERATE since we wouldn't want to copy
                                 * this symbol to any other object for example.
                                 */
@@ -91,7 +89,6 @@ define_meta_properties(JSContext       *context,
 
     if (!JS_DefineProperty(context, module_obj,
                            "__moduleName__", module_name_val,
-                           NULL, NULL,
                            /* don't set ENUMERATE since we wouldn't want to copy
                             * this symbol to any other object for example.
                             */
@@ -100,7 +97,6 @@ define_meta_properties(JSContext       *context,
 
     if (!JS_DefineProperty(context, module_obj,
                            "__parentModule__", parent_module_val,
-                           NULL, NULL,
                            /* don't set ENUMERATE since we wouldn't want to copy
                             * this symbol to any other object for example.
                             */
@@ -136,9 +132,7 @@ define_import(JSContext       *context,
               JS::HandleObject module_obj,
               const char      *name)
 {
-    JS::RootedValue module_val(context, JS::ObjectValue(*module_obj));
-    if (!JS_DefineProperty(context, obj, name, module_val,
-                           NULL, NULL,
+    if (!JS_DefineProperty(context, obj, name, module_obj,
                            GJS_MODULE_PROP_FLAGS & ~JSPROP_PERMANENT)) {
         gjs_debug(GJS_DEBUG_IMPORTER,
                   "Failed to define '%s' in importer",
@@ -493,9 +487,7 @@ do_import(JSContext       *context,
             JS::RootedValue obj_val(context);
             if (JS_GetProperty(context, module_obj, name, &obj_val)) {
                 if (!obj_val.isUndefined() &&
-                    JS_DefineProperty(context, obj,
-                                      name, obj_val,
-                                      NULL, NULL,
+                    JS_DefineProperty(context, obj, name, obj_val,
                                       GJS_MODULE_PROP_FLAGS & ~JSPROP_PERMANENT)) {
                     result = true;
                     goto out;
@@ -1068,19 +1060,16 @@ gjs_define_importer(JSContext       *context,
                     bool             add_standard_search_path)
 
 {
-    JSObject *importer;
-
-    importer = gjs_create_importer(context, importer_name, initial_search_path,
-                                   add_standard_search_path, false, in_object);
+    JS::RootedObject importer(context,
+        gjs_create_importer(context, importer_name, initial_search_path,
+                            add_standard_search_path, false, in_object));
 
-    JS::RootedValue v_importer(context, JS::ObjectValue(*importer));
-    if (!JS_DefineProperty(context, in_object, importer_name, v_importer,
-                           NULL, NULL,
+    if (!JS_DefineProperty(context, in_object, importer_name, importer,
                            GJS_MODULE_PROP_FLAGS))
         g_error("no memory to define importer property");
 
     gjs_debug(GJS_DEBUG_IMPORTER,
-              "Defined importer '%s' %p in %p", importer_name, importer,
+              "Defined importer '%s' %p in %p", importer_name, importer.get(),
               in_object.get());
 
     return importer;
diff --git a/gjs/jsapi-dynamic-class.cpp b/gjs/jsapi-dynamic-class.cpp
index 559cfec..9db3833 100644
--- a/gjs/jsapi-dynamic-class.cpp
+++ b/gjs/jsapi-dynamic-class.cpp
@@ -106,17 +106,19 @@ gjs_init_class_dynamic(JSContext              *context,
     if (static_fs && !JS_DefineFunctions(context, constructor, static_fs))
         goto out;
 
-    if (!JS_DefineProperty(context, constructor, "prototype", JS::ObjectValue(*prototype),
-                           JS_PropertyStub, JS_StrictPropertyStub, JSPROP_PERMANENT | JSPROP_READONLY))
+    if (!JS_DefineProperty(context, constructor, "prototype", prototype,
+                           JSPROP_PERMANENT | JSPROP_READONLY,
+                           JS_PropertyStub, JS_StrictPropertyStub))
         goto out;
-    if (!JS_DefineProperty(context, prototype, "constructor", JS::ObjectValue(*constructor),
-                           JS_PropertyStub, JS_StrictPropertyStub, 0))
+    if (!JS_DefineProperty(context, prototype, "constructor", constructor,
+                           0, JS_PropertyStub, JS_StrictPropertyStub))
         goto out;
 
     /* The constructor defined by JS_InitClass has no property attributes, but this
        is a more useful default for gjs */
-    if (!JS_DefineProperty(context, in_object, class_name, JS::ObjectValue(*constructor),
-                           JS_PropertyStub, JS_StrictPropertyStub, GJS_MODULE_PROP_FLAGS))
+    if (!JS_DefineProperty(context, in_object, class_name, constructor,
+                           GJS_MODULE_PROP_FLAGS,
+                           JS_PropertyStub, JS_StrictPropertyStub))
         goto out;
 
     res = true;
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 6212ed1..8130e26 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -333,12 +333,11 @@ gjs_define_string_array(JSContext       *context,
     JS::RootedObject array(context,
         gjs_build_string_array(context, array_length, (char **) array_values));
 
-    if (array != NULL) {
-        JS::RootedValue v_array(context, JS::ObjectValue(*array));
-        if (!JS_DefineProperty(context, in_object, array_name, v_array,
-                               NULL, NULL, attrs))
-            array = NULL;
-    }
+    if (array == NULL)
+        return NULL;
+
+    if (!JS_DefineProperty(context, in_object, array_name, array, attrs))
+        return NULL;
 
     return array;
 }
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 9c5e5a9..4692162 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -224,15 +224,14 @@ gjs_##cname##_create_proto(JSContext *context,                                 \
             return JS::NullValue(); \
         } \
         if (!JS_DefineProperty(context, module, proto_name, \
-                               rval, NULL, NULL, GJS_MODULE_PROP_FLAGS)) \
+                               rval, GJS_MODULE_PROP_FLAGS))                   \
             return JS::NullValue(); \
         if (gtype != G_TYPE_NONE) { \
             JS::RootedObject rval_obj(context, &rval.toObject());              \
-            JS::RootedValue value(context,                                     \
-                JS::ObjectOrNullValue(gjs_gtype_create_gtype_wrapper(context,  \
-                                                                     gtype))); \
-            JS_DefineProperty(context, rval_obj, "$gtype", value,              \
-                              NULL, NULL, JSPROP_PERMANENT);            \
+            JS::RootedObject gtype_obj(context,                                \
+                gjs_gtype_create_gtype_wrapper(context, gtype));               \
+            JS_DefineProperty(context, rval_obj, "$gtype", gtype_obj,          \
+                              JSPROP_PERMANENT);                               \
         } \
     } \
     return rval; \
diff --git a/modules/system.cpp b/modules/system.cpp
index 462bfdd..4c49799 100644
--- a/modules/system.cpp
+++ b/modules/system.cpp
@@ -170,7 +170,6 @@ gjs_js_define_system_stuff(JSContext              *context,
                  NULL);
 
     JS::RootedValue value(context);
-    JS::RootedValue gjs_version(context, JS::Int32Value(GJS_VERSION));
     if (!gjs_string_from_utf8(context, program_name,
                               -1, &value))
         goto out;
@@ -180,16 +179,16 @@ gjs_js_define_system_stuff(JSContext              *context,
     if (!JS_DefineProperty(context, module,
                            "programInvocationName",
                            value,
+                           GJS_MODULE_PROP_FLAGS | JSPROP_READONLY,
                            JS_PropertyStub,
-                           JS_StrictPropertyStub,
-                           GJS_MODULE_PROP_FLAGS | JSPROP_READONLY))
+                           JS_StrictPropertyStub))
         goto out;
 
     if (!JS_DefineProperty(context, module,
-                           "version", gjs_version,
+                           "version", GJS_VERSION,
+                           GJS_MODULE_PROP_FLAGS | JSPROP_READONLY,
                            JS_PropertyStub,
-                           JS_StrictPropertyStub,
-                           GJS_MODULE_PROP_FLAGS | JSPROP_READONLY))
+                           JS_StrictPropertyStub))
         goto out;
 
     retval = true;


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