[gjs/wip/ptomato/mozjs52: 3/37] js: Use autoptr in gjs_object_require_property()



commit f5a9288b05ad3c39b44d7cf079f756bff3e2b68e
Author: Philip Chimento <philip chimento gmail com>
Date:   Sat Jun 24 22:02:54 2017 -0700

    js: Use autoptr in gjs_object_require_property()
    
    We now use GjsAutoJSChar for the overload of
    gjs_object_require_property() that deals with string properties. This
    converts existing callers to use GjsAutoJSChar, and in fact fixes a few
    instances where the string was freed with the wrong function.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=781429

 gi/gerror.cpp      |    4 +---
 gi/repo.cpp        |   11 +++--------
 gjs/jsapi-util.cpp |    7 ++-----
 gjs/jsapi-util.h   |    2 +-
 4 files changed, 7 insertions(+), 17 deletions(-)
---
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index 9d4977f..dae60df 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -54,7 +54,6 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(error)
     GJS_NATIVE_CONSTRUCTOR_VARIABLES(error)
     Error *priv;
     Error *proto_priv;
-    gchar *message;
     int32_t code;
 
     /* Check early to avoid allocating memory for nothing */
@@ -97,6 +96,7 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(error)
     priv->domain = proto_priv->domain;
 
     JS::RootedObject params_obj(context, &argv[0].toObject());
+    GjsAutoJSChar message(context);
     if (!gjs_object_require_property(context, params_obj,
                                      "GError constructor",
                                      GJS_STRING_MESSAGE, &message))
@@ -109,8 +109,6 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(error)
 
     priv->gerror = g_error_new_literal(priv->domain, code, message);
 
-    JS_free(context, message);
-
     /* We assume this error will be thrown in the same line as the constructor */
     define_error_properties(context, object);
 
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 6102c58..1c0636a 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -62,7 +62,7 @@ static bool
 get_version_for_ns (JSContext       *context,
                     JS::HandleObject repo_obj,
                     JS::HandleId     ns_id,
-                    char           **version)
+                    GjsAutoJSChar   *version)
 {
     JS::RootedObject versions(context);
     bool found;
@@ -75,10 +75,8 @@ get_version_for_ns (JSContext       *context,
     if (!JS_AlreadyHasOwnPropertyById(context, versions, ns_id, &found))
         return false;
 
-    if (!found) {
-        *version = NULL;
+    if (!found)
         return true;
-    }
 
     return gjs_object_require_property(context, versions, NULL, ns_id, version);
 }
@@ -91,10 +89,10 @@ resolve_namespace_object(JSContext       *context,
 {
     GIRepository *repo;
     GError *error;
-    char *version;
 
     JSAutoRequest ar(context);
 
+    GjsAutoJSChar version(context);
     if (!get_version_for_ns(context, repo_obj, ns_id, &version))
         return false;
 
@@ -119,12 +117,9 @@ resolve_namespace_object(JSContext       *context,
                   ns_name, version?version:"none", error->message);
 
         g_error_free(error);
-        g_free(version);
         return false;
     }
 
-    g_free(version);
-
     /* Defines a property on "obj" (the javascript repo object)
      * with the given namespace name, pointing to that namespace
      * in the repo.
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 7c7de73..0caa514 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -297,15 +297,12 @@ gjs_object_require_property(JSContext       *cx,
                             JS::HandleObject obj,
                             const char      *description,
                             JS::HandleId     property_name,
-                            char           **value)
+                            GjsAutoJSChar   *value)
 {
     JS::RootedValue prop_value(cx);
-    GjsAutoJSChar value_tmp(cx);
     if (JS_GetPropertyById(cx, obj, property_name, &prop_value) &&
-        gjs_string_to_utf8(cx, prop_value, &value_tmp)) {
-        *value = value_tmp.copy();
+        gjs_string_to_utf8(cx, prop_value, value))
         return true;
-    }
 
     throw_property_lookup_error(cx, obj, description, property_name,
                                 "it was not a valid string");
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 917aaf6..b3b6efb 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -394,7 +394,7 @@ bool gjs_object_require_property(JSContext       *cx,
                                  JS::HandleObject obj,
                                  const char      *description,
                                  JS::HandleId     property_name,
-                                 char           **value);
+                                 GjsAutoJSChar   *value);
 
 bool gjs_object_require_property(JSContext              *cx,
                                  JS::HandleObject        obj,


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