[gjs/wip/ptomato/mozjs31prep: 32/32] js: Replace JS_ValueTo...() with JS::To...()



commit 9100d8d6a95f7502aa69248fa126aa6edf99c7fb
Author: Philip Chimento <philip chimento gmail com>
Date:   Thu Sep 29 22:04:19 2016 -0700

    js: Replace JS_ValueTo...() with JS::To...()
    
    Where ... is Boolean, Number, ECMAUint32 -> Uint32, ECMAInt32 -> Int32,
    and Int16. These can be replaced one-for-one, though the new int32 APIs
    do not always take the slow route of converting via a double. Unlike
    JS_ValueToBoolean(), JS::ToBoolean() can't fail, so we can get rid of a
    couple of error paths.

 gi/arg.cpp                |   27 +++++++++++++--------------
 gi/value.cpp              |   26 +++++++-------------------
 gjs/byteArray.cpp         |    9 +++------
 gjs/jsapi-util.cpp        |   20 ++++++++++----------
 modules/cairo-context.cpp |    2 +-
 5 files changed, 34 insertions(+), 50 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index 7a2b60d..b7ea873 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -580,8 +580,8 @@ gjs_array_to_intarray(JSContext   *context,
 
         /* do whatever sign extension is appropriate */
         success = (is_signed) ?
-            JS_ValueToECMAInt32(context, elem, &(intval.i)) :
-            JS_ValueToECMAUint32(context, elem, &(intval.u));
+            JS::ToInt32(context, elem, &(intval.i)) :
+            JS::ToUint32(context, elem, &(intval.u));
 
         if (!success) {
             g_free(result);
@@ -680,7 +680,7 @@ gjs_array_to_floatarray(JSContext   *context,
         }
 
         /* do whatever sign extension is appropriate */
-        success = JS_ValueToNumber(context, elem, &val);
+        success = JS::ToNumber(context, elem, &val);
 
         if (!success) {
             g_free(result);
@@ -1113,7 +1113,7 @@ gjs_array_to_explicit_array_internal(JSContext       *context,
                                          &value.toObject(), NULL,
                                          length_name,
                                          &length_value) ||
-            !JS_ValueToECMAUint32(context, length_value, &length)) {
+            !JS::ToUint32(context, length_value, &length)) {
             goto out;
         } else {
             if (!gjs_array_to_array(context,
@@ -1182,7 +1182,7 @@ gjs_value_to_g_argument(JSContext      *context,
     }
     case GI_TYPE_TAG_UINT8: {
         guint32 i;
-        if (!JS_ValueToECMAUint32(context, value, &i))
+        if (!JS::ToUint32(context, value, &i))
             wrong = true;
         if (i > G_MAXUINT8)
             out_of_range = true;
@@ -1201,7 +1201,7 @@ gjs_value_to_g_argument(JSContext      *context,
 
     case GI_TYPE_TAG_UINT16: {
         guint32 i;
-        if (!JS_ValueToECMAUint32(context, value, &i))
+        if (!JS::ToUint32(context, value, &i))
             wrong = true;
         if (i > G_MAXUINT16)
             out_of_range = true;
@@ -1216,7 +1216,7 @@ gjs_value_to_g_argument(JSContext      *context,
 
     case GI_TYPE_TAG_UINT32: {
         gdouble i;
-        if (!JS_ValueToNumber(context, value, &i))
+        if (!JS::ToNumber(context, value, &i))
             wrong = true;
         if (i > G_MAXUINT32 || i < 0)
             out_of_range = true;
@@ -1226,7 +1226,7 @@ gjs_value_to_g_argument(JSContext      *context,
 
     case GI_TYPE_TAG_INT64: {
         double v;
-        if (!JS_ValueToNumber(context, value, &v))
+        if (!JS::ToNumber(context, value, &v))
             wrong = true;
         if (v > G_MAXINT64 || v < G_MININT64)
             out_of_range = true;
@@ -1236,7 +1236,7 @@ gjs_value_to_g_argument(JSContext      *context,
 
     case GI_TYPE_TAG_UINT64: {
         double v;
-        if (!JS_ValueToNumber(context, value, &v))
+        if (!JS::ToNumber(context, value, &v))
             wrong = true;
         if (v < 0)
             out_of_range = true;
@@ -1246,13 +1246,12 @@ gjs_value_to_g_argument(JSContext      *context,
         break;
 
     case GI_TYPE_TAG_BOOLEAN:
-        if (!JS_ValueToBoolean(context, value, &arg->v_boolean))
-            wrong = true;
+        arg->v_boolean = JS::ToBoolean(value);
         break;
 
     case GI_TYPE_TAG_FLOAT: {
         double v;
-        if (!JS_ValueToNumber(context, value, &v))
+        if (!JS::ToNumber(context, value, &v))
             wrong = true;
         if (v > G_MAXFLOAT || v < - G_MAXFLOAT)
             out_of_range = true;
@@ -1261,7 +1260,7 @@ gjs_value_to_g_argument(JSContext      *context,
         break;
 
     case GI_TYPE_TAG_DOUBLE:
-        if (!JS_ValueToNumber(context, value, &arg->v_double))
+        if (!JS::ToNumber(context, value, &arg->v_double))
             wrong = true;
         break;
 
@@ -1649,7 +1648,7 @@ gjs_value_to_g_argument(JSContext      *context,
                                              &value.toObject(), NULL,
                                              length_name,
                                              &length_value) ||
-                !JS_ValueToECMAUint32(context, length_value, &length)) {
+                !JS::ToUint32(context, length_value, &length)) {
                 wrong = true;
             } else {
                 GList *list;
diff --git a/gi/value.cpp b/gi/value.cpp
index 42bcb8d..d5c2282 100644
--- a/gi/value.cpp
+++ b/gi/value.cpp
@@ -424,7 +424,7 @@ gjs_value_to_g_value_internal(JSContext    *context,
         }
     } else if (gtype == G_TYPE_UCHAR) {
         guint16 i;
-        if (JS_ValueToUint16(context, value, &i) && i <= UCHAR_MAX) {
+        if (JS::ToUint16(context, value, &i) && i <= UCHAR_MAX) {
             g_value_set_uchar(gvalue, (unsigned char)i);
         } else {
             gjs_throw(context,
@@ -444,7 +444,7 @@ gjs_value_to_g_value_internal(JSContext    *context,
         }
     } else if (gtype == G_TYPE_DOUBLE) {
         gdouble d;
-        if (JS_ValueToNumber(context, value, &d)) {
+        if (JS::ToNumber(context, value, &d)) {
             g_value_set_double(gvalue, d);
         } else {
             gjs_throw(context,
@@ -454,7 +454,7 @@ gjs_value_to_g_value_internal(JSContext    *context,
         }
     } else if (gtype == G_TYPE_FLOAT) {
         gdouble d;
-        if (JS_ValueToNumber(context, value, &d)) {
+        if (JS::ToNumber(context, value, &d)) {
             g_value_set_float(gvalue, d);
         } else {
             gjs_throw(context,
@@ -464,7 +464,7 @@ gjs_value_to_g_value_internal(JSContext    *context,
         }
     } else if (gtype == G_TYPE_UINT) {
         guint32 i;
-        if (JS_ValueToECMAUint32(context, value, &i)) {
+        if (JS::ToUint32(context, value, &i)) {
             g_value_set_uint(gvalue, i);
         } else {
             gjs_throw(context,
@@ -473,20 +473,8 @@ gjs_value_to_g_value_internal(JSContext    *context,
             return false;
         }
     } else if (gtype == G_TYPE_BOOLEAN) {
-        JSBool b;
-
-        /* JS_ValueToBoolean() pretty much always succeeds,
-         * which is maybe surprising sometimes, but could
-         * be handy also...
-         */
-        if (JS_ValueToBoolean(context, value, &b)) {
-            g_value_set_boolean(gvalue, b);
-        } else {
-            gjs_throw(context,
-                      "Wrong type %s; boolean expected",
-                      gjs_get_type_name(value));
-            return false;
-        }
+        /* JS::ToBoolean() can't fail */
+        g_value_set_boolean(gvalue, JS::ToBoolean(value));
     } else if (g_type_is_a(gtype, G_TYPE_OBJECT) || g_type_is_a(gtype, G_TYPE_INTERFACE)) {
         GObject *gobj;
 
@@ -525,7 +513,7 @@ gjs_value_to_g_value_internal(JSContext    *context,
                                              &value.toObject(), NULL,
                                              length_name,
                                              &length_value) ||
-                !JS_ValueToECMAUint32(context, length_value, &length)) {
+                !JS::ToUint32(context, length_value, &length)) {
                 gjs_throw(context,
                           "Wrong type %s; strv expected",
                           gjs_get_type_name(value));
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 28d014c..eeb38a1 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -116,10 +116,8 @@ gjs_value_to_gsize(JSContext         *context,
 {
     guint32 val32;
 
-    /* Just JS_ValueToECMAUint32() would work. However,
-     * we special case ints for two reasons:
-     *  - JS_ValueToECMAUint32() always goes via a double which is slow
-     *  - nicer error message on negative indices
+    /* Just JS::ToUint32() would work. However, we special case ints for a nicer
+     * error message on negative indices.
      */
     if (value.isInt32()) {
         int i = value.toInt32();
@@ -136,8 +134,7 @@ gjs_value_to_gsize(JSContext         *context,
          * a number) but it's what we use elsewhere in gjs too.
          */
 
-        ret = JS_ValueToECMAUint32(context, value,
-                                   &val32);
+        ret = JS::ToUint32(context, value, &val32);
         *v_p = val32;
         return ret;
     }
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 4e28255..68b1e7c 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -827,16 +827,16 @@ gjs_get_type_name(JS::Value value)
  *
  * Converts a Javascript value into the nearest 64 bit signed value.
  *
- * This function behaves indentically for rounding to JSValToInt32(), which
+ * This function behaves indentically for rounding to JS_ValueToInt32(), which
  * means that it rounds (0.5 toward positive infinity) rather than doing
- * a C-style truncation to 0. If we change to using JSValToEcmaInt32() then
- * this should be changed to match.
+ * a C-style truncation to 0. If we change to using JS::ToInt32() then this
+ * should be changed to match.
  *
  * Return value: If the javascript value converted to a number (see
- *   JS_ValueToNumber()) is NaN, or outside the range of 64-bit signed
- *   numbers, fails and sets an exception. Otherwise returns the value
- *   rounded to the nearest 64-bit integer. Like JS_ValueToInt32(),
- *   undefined throws, but null => 0, false => 0, true => 1.
+ *   JS::ToNumber()) is NaN, or outside the range of 64-bit signed numbers,
+ *   fails and sets an exception. Otherwise returns the value rounded to the
+ *   nearest 64-bit integer. Like JS::ToInt32(), undefined throws, but
+ *   null => 0, false => 0, true => 1.
  */
 bool
 gjs_value_to_int64(JSContext      *context,
@@ -848,7 +848,7 @@ gjs_value_to_int64(JSContext      *context,
         return true;
     } else {
         double value_double;
-        if (!JS_ValueToNumber(context, val, &value_double))
+        if (!JS::ToNumber(context, val, &value_double))
             return false;
 
         if (isnan(value_double) ||
@@ -1011,7 +1011,7 @@ gjs_parse_args_valist (JSContext  *context,
             break;
         case 'u': {
             gdouble num;
-            if (!js_value.isNumber() || !JS_ValueToNumber(context, js_value, &num)) {
+            if (!js_value.isNumber() || !JS::ToNumber(context, js_value, &num)) {
                 /* Our error message is going to be more useful */
                 JS_ClearPendingException(context);
                 arg_error_message = "Couldn't convert to unsigned integer";
@@ -1032,7 +1032,7 @@ gjs_parse_args_valist (JSContext  *context,
             break;
         case 'f': {
             double num;
-            if (!JS_ValueToNumber(context, js_value, &num)) {
+            if (!JS::ToNumber(context, js_value, &num)) {
                 /* Our error message is going to be more useful */
                 JS_ClearPendingException(context);
                 arg_error_message = "Couldn't convert to double";
diff --git a/modules/cairo-context.cpp b/modules/cairo-context.cpp
index d187fb7..9a59d72 100644
--- a/modules/cairo-context.cpp
+++ b/modules/cairo-context.cpp
@@ -599,7 +599,7 @@ setDash_func(JSContext *context,
         if (elem.isUndefined())
             continue;
 
-        if (!JS_ValueToNumber(context, elem, &b))
+        if (!JS::ToNumber(context, elem, &b))
             goto out;
         if (b <= 0) {
             gjs_throw(context, "Dash value must be positive");


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