[gjs/wip/ptomato/mozjs38: 9/22] js: Adapt to new JS_DefinePropertyById() API



commit da281d46587d1390c666cb50622d08c43f5dd892
Author: Philip Chimento <philip chimento gmail com>
Date:   Wed Jan 11 22:51:59 2017 -0800

    js: Adapt to new JS_DefinePropertyById() API
    
    Like JS_DefineProperty() could already, JS_DefinePropertyById() is now
    overloaded to take several types as the property value. This allows us to
    pass rooted objects directly in, in order to avoid creating temporary
    JS::Values.

 gi/repo.cpp        |   12 ++++--------
 gjs/importer.cpp   |    7 ++-----
 gjs/jsapi-util.cpp |   31 +++++++++++++++++++++++--------
 gjs/jsapi-util.h   |   24 ++++++++++++++++--------
 4 files changed, 45 insertions(+), 29 deletions(-)
---
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 62af9de..133ac07 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -234,8 +234,6 @@ static JSObject*
 repo_new(JSContext *context)
 {
     Repo *priv;
-    JSObject *versions;
-    JSObject *private_ns;
     bool found;
 
     JS::RootedObject global(context, gjs_get_import_global(context));
@@ -290,15 +288,13 @@ repo_new(JSContext *context)
     gjs_debug_lifecycle(GJS_DEBUG_GREPO,
                         "repo constructor, obj %p priv %p", repo.get(), priv);
 
-    versions = JS_NewObject(context, NULL, global);
-    JS::RootedValue v_versions(context, JS::ObjectValue(*versions));
+    JS::RootedObject versions(context, JS_NewObject(context, NULL, global));
     gjs_object_define_property(context, repo, GJS_STRING_GI_VERSIONS,
-                               v_versions, JSPROP_PERMANENT);
+                               versions, JSPROP_PERMANENT);
 
-    private_ns = JS_NewObject(context, NULL, global);
-    JS::RootedValue v_ns(context, JS::ObjectValue(*private_ns));
+    JS::RootedObject private_ns(context, JS_NewObject(context, NULL, global));
     gjs_object_define_property(context, repo,
-                               GJS_STRING_PRIVATE_NS_MARKER, v_ns,
+                               GJS_STRING_PRIVATE_NS_MARKER, private_ns,
                                JSPROP_PERMANENT);
 
     /* FIXME - hack to make namespaces load, since
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index 2297eab..f4630cd 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -383,9 +383,8 @@ load_module_init(JSContext       *context,
     if (!import_file (context, "__init__", file, module_obj))
         return module_obj;
 
-    JS::RootedValue v_module(context, JS::ObjectValue(*module_obj));
     gjs_object_define_property(context, in_object,
-                               GJS_STRING_MODULE_INIT, v_module,
+                               GJS_STRING_MODULE_INIT, module_obj,
                                GJS_MODULE_PROP_FLAGS & ~JSPROP_PERMANENT);
 
     return module_obj;
@@ -1194,10 +1193,8 @@ gjs_define_root_importer_object(JSContext        *context,
 {
     JSAutoRequest ar(context);
 
-    JS::RootedValue importer (JS_GetRuntime(context),
-                              JS::ObjectValue(*root_importer));
     if (!gjs_object_define_property(context, in_object,
-                                    GJS_STRING_IMPORTS, importer,
+                                    GJS_STRING_IMPORTS, root_importer,
                                     GJS_MODULE_PROP_FLAGS)) {
         gjs_debug(GJS_DEBUG_IMPORTER, "DefineProperty imports on %p failed",
                   in_object.get());
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index cbd7c02..0bfc1f1 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -175,17 +175,32 @@ gjs_object_has_property(JSContext       *cx,
                               found);
 }
 
-bool gjs_object_define_property(JSContext         *cx,
-                                JS::HandleObject   obj,
-                                GjsConstString     property_name,
-                                JS::HandleValue    value,
-                                unsigned           flags,
-                                JSPropertyOp       getter,
-                                JSStrictPropertyOp setter)
+bool
+gjs_object_define_property(JSContext       *cx,
+                           JS::HandleObject obj,
+                           GjsConstString   property_name,
+                           JS::HandleValue  value,
+                           unsigned         flags,
+                           JSNative         getter,
+                           JSNative         setter)
+{
+    return JS_DefinePropertyById(cx, obj,
+                                 gjs_context_get_const_string(cx, property_name),
+                                 value, flags, getter, setter);
+}
+
+bool
+gjs_object_define_property(JSContext       *cx,
+                           JS::HandleObject obj,
+                           GjsConstString   property_name,
+                           JS::HandleObject value,
+                           unsigned         flags,
+                           JSNative         getter,
+                           JSNative         setter)
 {
     return JS_DefinePropertyById(cx, obj,
                                  gjs_context_get_const_string(cx, property_name),
-                                 value, getter, setter, flags);
+                                 value, flags, getter, setter);
 }
 
 static void
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index d41f45d..3afcf02 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -481,16 +481,24 @@ bool gjs_object_has_property(JSContext       *cx,
                              GjsConstString   property_name,
                              bool            *found);
 
-bool gjs_object_define_property(JSContext         *cx,
-                                JS::HandleObject   obj,
-                                GjsConstString     property_name,
-                                JS::HandleValue    value,
-                                unsigned           flags,
-                                JSPropertyOp       getter = nullptr,
-                                JSStrictPropertyOp setter = nullptr);
-
 G_END_DECLS
 
+bool gjs_object_define_property(JSContext       *cx,
+                                JS::HandleObject obj,
+                                GjsConstString   property_name,
+                                JS::HandleValue  value,
+                                unsigned         flags,
+                                JSNative         getter = nullptr,
+                                JSNative         setter = nullptr);
+
+bool gjs_object_define_property(JSContext       *cx,
+                                JS::HandleObject obj,
+                                GjsConstString   property_name,
+                                JS::HandleObject value,
+                                unsigned         flags,
+                                JSNative         getter = nullptr,
+                                JSNative         setter = nullptr);
+
 JS::HandleId gjs_context_get_const_string(JSContext     *cx,
                                           GjsConstString string);
 


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