[gjs: 15/21] js: Avoid unnecessary gjs_string_to_utf8()



commit d0c07a37ecfe0c0c7e76f5abbc4540920276846e
Author: Philip Chimento <philip chimento gmail com>
Date:   Mon May 20 19:23:29 2019 -0700

    js: Avoid unnecessary gjs_string_to_utf8()
    
    gjs_string_to_utf8(..., JS::StringValue(...), ...) is a pattern to be
    avoided, because gjs_string_to_utf8() just checks value.isString() and
    calls value.toString() before calling JS_EncodeStringToUTF8(). Instead,
    use JS_EncodeStringToUTF8() directly, to avoid the unnecessary creation
    of JS::Value.

 gi/object.cpp       | 4 ++--
 gjs/deprecation.cpp | 6 +-----
 2 files changed, 3 insertions(+), 7 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index 04a1c5f5..3d0064b5 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -250,8 +250,8 @@ GParamSpec* ObjectPrototype::find_param_spec_from_id(JSContext* cx,
     if (entry)
         return entry->value();
 
-    JS::UniqueChars js_prop_name;
-    if (!gjs_string_to_utf8(cx, JS::StringValue(key), &js_prop_name))
+    JS::UniqueChars js_prop_name(JS_EncodeStringToUTF8(cx, key));
+    if (!js_prop_name)
         return nullptr;
 
     GjsAutoChar gname = gjs_hyphen_from_camel(js_prop_name.get());
diff --git a/gjs/deprecation.cpp b/gjs/deprecation.cpp
index b6b9ca0f..4c8d889d 100644
--- a/gjs/deprecation.cpp
+++ b/gjs/deprecation.cpp
@@ -31,7 +31,6 @@
 #include "gjs/jsapi-wrapper.h"
 
 #include "gjs/deprecation.h"
-#include "gjs/jsapi-util.h"
 #include "gjs/macros.h"
 
 const char* messages[] = {
@@ -84,10 +83,7 @@ static char* get_callsite(JSContext* cx) {
     if (!frame_string)
         return nullptr;
 
-    JS::UniqueChars frame_utf8;
-    if (!gjs_string_to_utf8(cx, JS::StringValue(frame_string), &frame_utf8))
-        return nullptr;
-    return frame_utf8.release();
+    return JS_EncodeStringToUTF8(cx, frame_string);
 }
 
 /* Note, this can only be called from the JS thread because it uses the full


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