[geary/bug/728002-webkit2: 104/140] Rework util code for extracting string/double values from JS.Values.



commit 69da046ff3ab0ffd583da8f4652faa0468c54d49
Author: Michael James Gratton <mike vee net>
Date:   Mon Jan 23 23:53:09 2017 +1100

    Rework util code for extracting string/double values from JS.Values.
    
    * src/client/util/util-webkit.vala (to_number, to_string): Move code for
      extracting actual vales to functions in engine/util/util-js.vala,
      rework to use those functions.

 src/client/util/util-webkit.vala |   31 ++++++-----------------------
 src/engine/util/util-js.vala     |   39 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 24 deletions(-)
---
diff --git a/src/client/util/util-webkit.vala b/src/client/util/util-webkit.vala
index cfce6f3..79bdd3b 100644
--- a/src/client/util/util-webkit.vala
+++ b/src/client/util/util-webkit.vala
@@ -32,18 +32,10 @@ namespace WebKitUtil {
      * This will raise a {@link Geary.JS.Error.TYPE} error if the
      * result is not a JavaScript `Number`.
      */
-    public double to_number(WebKit.JavascriptResult result)
+    public inline double to_number(WebKit.JavascriptResult result)
         throws Geary.JS.Error {
-        JS.GlobalContext context = result.get_global_context();
-        JS.Value value = result.get_value();
-        if (!value.is_number(context)) {
-            throw new Geary.JS.Error.TYPE("Result is not a JS Number object");
-        }
-
-        JS.Value? err = null;
-        double number = value.to_number(context, out err);
-        Geary.JS.check_exception(context, err);
-        return number;
+        return Geary.JS.to_number(result.get_global_context(),
+                                  result.get_value());
     }
 
     /**
@@ -52,19 +44,10 @@ namespace WebKitUtil {
      * This will raise a {@link Geary.JS.Error.TYPE} error if the
      * result is not a JavaScript `String`.
      */
-    public string? to_string(WebKit.JavascriptResult result)
+    public inline string to_string(WebKit.JavascriptResult result)
         throws Geary.JS.Error {
-        JS.GlobalContext context = result.get_global_context();
-        JS.Value js_str_value = result.get_value();
-        if (!js_str_value.is_string(context)) {
-            throw new Geary.JS.Error.TYPE("Result is not a JS String object");
-        }
-
-        JS.Value? err = null;
-        JS.String js_str = js_str_value.to_string_copy(context, out err);
-        Geary.JS.check_exception(context, err);
-
-        return Geary.JS.to_string_released(js_str);
+        return Geary.JS.to_string(result.get_global_context(),
+                                  result.get_value());
     }
 
     /**
@@ -74,7 +57,7 @@ namespace WebKitUtil {
      * result to a string, effectively by calling the JavaScript
      * `toString()` method on it, and returning that value.
      */
-    public string? as_string(WebKit.JavascriptResult result)
+    public string as_string(WebKit.JavascriptResult result)
         throws Geary.JS.Error {
         JS.GlobalContext context = result.get_global_context();
         JS.Value js_str_value = result.get_value();
diff --git a/src/engine/util/util-js.vala b/src/engine/util/util-js.vala
index ddcf8d4..a25e330 100644
--- a/src/engine/util/util-js.vala
+++ b/src/engine/util/util-js.vala
@@ -37,6 +37,45 @@ namespace Geary.JS {
     }
 
     /**
+     * Returns a JSC Value as a number.
+     *
+     * This will raise a {@link Geary.JS.Error.TYPE} error if the
+     * value is not a JavaScript `Number`.
+     */
+    public double to_number(global::JS.Context context,
+                            global::JS.Value value)
+        throws Geary.JS.Error {
+        if (!value.is_number(context)) {
+            throw new Geary.JS.Error.TYPE("Value is not a JS Number object");
+        }
+
+        global::JS.Value? err = null;
+        double number = value.to_number(context, out err);
+        Geary.JS.check_exception(context, err);
+        return number;
+    }
+
+    /**
+     * Returns a JSC Value as a string.
+     *
+     * This will raise a {@link Geary.JS.Error.TYPE} error if the
+     * value is not a JavaScript `String`.
+     */
+    public string to_string(global::JS.Context context,
+                            global::JS.Value value)
+        throws Geary.JS.Error {
+        if (!value.is_string(context)) {
+            throw new Geary.JS.Error.TYPE("Value is not a JS String object");
+        }
+
+        global::JS.Value? err = null;
+        global::JS.String js_str = value.to_string_copy(context, out err);
+        Geary.JS.check_exception(context, err);
+
+        return Geary.JS.to_string_released(js_str);
+    }
+
+    /**
      * Returns a JSC {@link JS.String} as a Vala {@link string}.
      */
     public inline string to_string_released(global::JS.String js) {


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