[gjs/wip/ptomato/mozjs31prep: 3/4] js: Replace JS_Add*Root with Rooted where trivial
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/wip/ptomato/mozjs31prep: 3/4] js: Replace JS_Add*Root with Rooted where trivial
- Date: Fri, 30 Sep 2016 02:30:34 +0000 (UTC)
commit cdb92a1b3c1e7bad5331293a033c7bda57990298
Author: Philip Chimento <philip endlessm com>
Date: Thu Sep 29 19:25:01 2016 -0700
js: Replace JS_Add*Root with Rooted where trivial
Instead of using JS_AddFooRoot and JS_RemoveFooRoot on a value within the
same scope, we should use JS::Rooted which is the mozjs31-style API.
Besides being RAII and more readable, it will be required when mozjs API
functions start taking JS::Handle parameters in mozjs31. This commit
makes that change in places where it doesn't propagate into the API.
gi/repo.cpp | 24 ++++++++----------------
gjs/byteArray.cpp | 41 +++++++++++++----------------------------
gjs/jsapi-util.cpp | 7 ++-----
modules/cairo-context.cpp | 6 ++----
test/gjs-tests.cpp | 7 ++-----
5 files changed, 27 insertions(+), 58 deletions(-)
---
diff --git a/gi/repo.cpp b/gi/repo.cpp
index ce5afb0..ccd5ca8 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -96,13 +96,11 @@ resolve_namespace_object(JSContext *context,
char *version;
JSObject *override;
JS::Value result;
- JSObject *gi_namespace = NULL;
- bool ret = false;
- JS_BeginRequest(context);
+ JSAutoRequest ar(context);
if (!get_version_for_ns(context, repo_obj, ns_id, &version))
- goto out;
+ return false;
repo = g_irepository_get_default();
@@ -115,7 +113,7 @@ resolve_namespace_object(JSContext *context,
g_error_free(error);
g_free(version);
- goto out;
+ return false;
}
g_free(version);
@@ -124,8 +122,7 @@ resolve_namespace_object(JSContext *context,
* with the given namespace name, pointing to that namespace
* in the repo.
*/
- gi_namespace = gjs_create_ns(context, ns_name);
- JS_AddObjectRoot(context, &gi_namespace);
+ JS::RootedObject gi_namespace(context, gjs_create_ns(context, ns_name));
/* Define the property early, to avoid reentrancy issues if
the override module looks for namespaces that import this */
@@ -142,19 +139,14 @@ resolve_namespace_object(JSContext *context,
0, /* argc */
NULL, /* argv */
&result))
- goto out;
+ return false;
gjs_debug(GJS_DEBUG_GNAMESPACE,
- "Defined namespace '%s' %p in GIRepository %p", ns_name, gi_namespace, repo_obj);
+ "Defined namespace '%s' %p in GIRepository %p", ns_name,
+ gi_namespace.get(), repo_obj);
- ret = true;
gjs_schedule_gc_if_needed(context);
-
- out:
- if (gi_namespace)
- JS_RemoveObjectRoot(context, &gi_namespace);
- JS_EndRequest(context);
- return ret;
+ return true;
}
/*
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index e69eb85..7695463 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -578,15 +578,11 @@ from_string_func(JSContext *context,
ByteArrayInstance *priv;
char *encoding;
bool encoding_is_utf8;
- JSObject *obj;
- bool retval = false;
+ JS::RootedObject obj(context, byte_array_new(context));
- obj = byte_array_new(context);
if (obj == NULL)
return false;
- JS_AddObjectRoot(context, &obj);
-
priv = priv_from_js(context, obj);
g_assert (priv != NULL);
@@ -597,12 +593,12 @@ from_string_func(JSContext *context,
if (!argv[0].isString()) {
gjs_throw(context,
"byteArray.fromString() called with non-string as first arg");
- goto out;
+ return false;
}
if (argc > 1 && argv[1].isString()) {
if (!gjs_string_to_utf8(context, argv[1], &encoding))
- goto out;
+ return false;
/* maybe we should be smarter about utf8 synonyms here.
* doesn't matter much though. encoding_is_utf8 is
@@ -627,7 +623,7 @@ from_string_func(JSContext *context,
if (!gjs_string_to_utf8(context,
argv[0],
&utf8))
- goto out;
+ return false;
g_byte_array_set_size(priv->array, 0);
g_byte_array_append(priv->array, (guint8*) utf8, strlen(utf8));
@@ -641,7 +637,7 @@ from_string_func(JSContext *context,
u16_chars = JS_GetStringCharsAndLength(context, argv[0].toString(), &u16_len);
if (u16_chars == NULL)
- goto out;
+ return false;
error = NULL;
encoded = g_convert((char*) u16_chars,
@@ -655,7 +651,7 @@ from_string_func(JSContext *context,
if (encoded == NULL) {
/* frees the GError */
gjs_throw_g_error(context, error);
- goto out;
+ return false;
}
g_byte_array_set_size(priv->array, 0);
@@ -665,11 +661,7 @@ from_string_func(JSContext *context,
}
argv.rval().setObject(*obj);
-
- retval = true;
- out:
- JS_RemoveObjectRoot(context, &obj);
- return retval;
+ return true;
}
/* fromArray() function implementation */
@@ -682,15 +674,11 @@ from_array_func(JSContext *context,
ByteArrayInstance *priv;
guint32 len;
guint32 i;
- JSObject *obj;
- bool ret = false;
+ JS::RootedObject obj(context, byte_array_new(context));
- obj = byte_array_new(context);
if (obj == NULL)
return false;
- JS_AddObjectRoot(context, &obj);
-
priv = priv_from_js(context, obj);
g_assert (priv != NULL);
@@ -701,13 +689,13 @@ from_array_func(JSContext *context,
if (!JS_IsArrayObject(context, &argv[0].toObject())) {
gjs_throw(context,
"byteArray.fromArray() called with non-array as first arg");
- goto out;
+ return false;
}
if (!JS_GetArrayLength(context, &argv[0].toObject(), &len)) {
gjs_throw(context,
"byteArray.fromArray() can't get length of first array arg");
- goto out;
+ return false;
}
g_byte_array_set_size(priv->array, len);
@@ -721,23 +709,20 @@ from_array_func(JSContext *context,
/* this means there was an exception, while elem.isUndefined()
* means no element found
*/
- goto out;
+ return false;
}
if (elem.isUndefined())
continue;
if (!gjs_value_to_byte(context, elem, &b))
- goto out;
+ return false;
g_array_index(priv->array, guint8, i) = b;
}
- ret = true;
argv.rval().setObject(*obj);
- out:
- JS_RemoveObjectRoot(context, &obj);
- return ret;
+ return true;
}
static JSBool
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index c4c85fc..7a956ec 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -589,13 +589,12 @@ static bool
log_and_maybe_keep_exception(JSContext *context,
bool keep)
{
- JS::Value exc = JS::UndefinedValue();
bool retval = false;
JS_BeginRequest(context);
- JS_AddValueRoot(context, &exc);
- if (!JS_GetPendingException(context, &exc))
+ JS::RootedValue exc(context, JS::UndefinedValue());
+ if (!JS_GetPendingException(context, exc.address()))
goto out;
JS_ClearPendingException(context);
@@ -611,8 +610,6 @@ log_and_maybe_keep_exception(JSContext *context,
retval = true;
out:
- JS_RemoveValueRoot(context, &exc);
-
JS_EndRequest(context);
return retval;
diff --git a/modules/cairo-context.cpp b/modules/cairo-context.cpp
index 8fca420..ed03cc1 100644
--- a/modules/cairo-context.cpp
+++ b/modules/cairo-context.cpp
@@ -566,17 +566,16 @@ setDash_func(JSContext *context,
guint i;
cairo_t *cr;
- JSObject *dashes;
+ JS::RootedObject dashes(context);
double offset;
bool retval = false;
guint len;
GArray *dashes_c = NULL;
if (!gjs_parse_call_args(context, "setDash", "of", argv,
- "dashes", &dashes, "offset", &offset))
+ "dashes", dashes.address(), "offset", &offset))
return false;
- JS_AddObjectRoot(context, &dashes);
if (!JS_IsArrayObject(context, dashes)) {
gjs_throw(context, "dashes must be an array");
@@ -617,7 +616,6 @@ setDash_func(JSContext *context,
out:
if (dashes_c != NULL)
g_array_free (dashes_c, true);
- JS_RemoveObjectRoot(context, &dashes);
return retval;
}
diff --git a/test/gjs-tests.cpp b/test/gjs-tests.cpp
index 7c3837f..40e0ec5 100644
--- a/test/gjs-tests.cpp
+++ b/test/gjs-tests.cpp
@@ -168,7 +168,7 @@ gjstest_test_func_gjs_jsapi_util_error_throw(void)
GjsUnitTestFixture fixture;
JSContext *context;
JSObject *global;
- JS::Value exc, value, previous;
+ JS::Value exc, value;
char *s = NULL;
int strcmp_result;
@@ -200,8 +200,7 @@ gjstest_test_func_gjs_jsapi_util_error_throw(void)
g_error("Exception has wrong message '%s'", s);
/* keep this around before we clear it */
- previous = exc;
- JS_AddValueRoot(context, &previous);
+ JS::RootedValue previous(context, exc);
JS_ClearPendingException(context);
@@ -221,8 +220,6 @@ gjstest_test_func_gjs_jsapi_util_error_throw(void)
g_assert(!exc.isUndefined());
g_assert(&exc.toObject() == &previous.toObject());
- JS_RemoveValueRoot(context, &previous);
-
JS_LeaveCompartment(context, oldCompartment);
_gjs_unit_test_fixture_finish(&fixture);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]