[gjs] js: Replace JS_ValueTo...() with JS::To...()
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs] js: Replace JS_ValueTo...() with JS::To...()
- Date: Mon, 3 Oct 2016 22:28:12 +0000 (UTC)
commit 2377ea12c788710076f348f768acd7aed8bcff14
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.
https://bugzilla.gnome.org/show_bug.cgi?id=742249
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 0df20ee..295e720 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);
@@ -678,7 +678,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);
@@ -1111,7 +1111,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,
@@ -1180,7 +1180,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;
@@ -1199,7 +1199,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;
@@ -1214,7 +1214,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;
@@ -1224,7 +1224,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;
@@ -1234,7 +1234,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;
@@ -1244,13 +1244,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;
@@ -1259,7 +1258,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;
@@ -1645,7 +1644,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 c1e855b..514f8ce 100644
--- a/gi/value.cpp
+++ b/gi/value.cpp
@@ -422,7 +422,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,
@@ -442,7 +442,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,
@@ -452,7 +452,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,
@@ -462,7 +462,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,
@@ -471,20 +471,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;
@@ -524,7 +512,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 7695463..16793b1 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 7a956ec..1d075af 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -821,16 +821,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,
@@ -842,7 +842,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) ||
@@ -1005,7 +1005,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";
@@ -1026,7 +1026,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 ed03cc1..3191833 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]