[gjs/wip/ptomato/mozjs31: 5/21] js: Adapt to mozjs31 HandleValueArray APIs
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/wip/ptomato/mozjs31: 5/21] js: Adapt to mozjs31 HandleValueArray APIs
- Date: Tue, 8 Nov 2016 02:10:21 +0000 (UTC)
commit 13424e6c8cb90ab0c122f4dcf6c9b9bcad8eff5e
Author: Philip Chimento <philip endlessm com>
Date: Thu Oct 27 17:23:36 2016 -0700
js: Adapt to mozjs31 HandleValueArray APIs
Some functions, such as JS_NewArrayObject() and JS_CallFunctionValue(),
take a JS::HandleValueArray for their parameter lists instead of
previously a number of arguments and vp pointer. This allows us to make
some simplifications.
In particular, JS_NewArrayObject() allows for one good refactor. With the
mozjs24 API, the advice was to create an empty array with
JS_NewArrayObject() and populate it with JS_DefineElement() so that the
values did not get garbage collected from the vp array while the array
was being populated [1].
However, that is not necessary anymore. Now we can simply fill up our
rooted vector and pass it to JS_NewArrayObject().
[1]
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/JSAPI_Reference/JS_NewArrayObject
gi/arg.cpp | 104 ++++++++++++++++++---------------------------
gi/function.cpp | 7 +--
gi/object.cpp | 13 +++---
gi/repo.cpp | 3 +-
gjs/byteArray.cpp | 4 +-
gjs/context.cpp | 3 +-
gjs/coverage.cpp | 57 ++++++++++--------------
gjs/jsapi-util-args.h | 3 +-
gjs/jsapi-util-error.cpp | 7 ++-
gjs/jsapi-util.cpp | 2 +-
gjs/jsapi-util.h | 2 +-
gjs/stack.cpp | 3 +-
modules/cairo-context.cpp | 6 +-
13 files changed, 90 insertions(+), 124 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index ef320a3..90212cc 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -569,26 +569,21 @@ gjs_array_from_strv(JSContext *context,
JS::MutableHandleValue value_p,
const char **strv)
{
- JS::RootedObject obj(context, JS_NewArrayObject(context, 0, NULL));
guint i;
+ JS::AutoValueVector elems(context);
+
+ for (i = 0; strv[i] != NULL; i++) {
+ elems.growBy(1);
+ if (!gjs_string_from_utf8(context, strv[i], -1, elems.handleAt(i)))
+ return false;
+ }
+ JS::RootedObject obj(context, JS_NewArrayObject(context, elems));
if (obj == NULL)
return false;
value_p.setObject(*obj);
- JS::RootedValue elem(context);
-
- for (i = 0; strv[i] != NULL; i++) {
- if (!gjs_string_from_utf8 (context, strv[i], -1, &elem))
- return false;
-
- if (!JS_DefineElement(context, obj, i, elem,
- NULL, NULL, JSPROP_ENUMERATE)) {
- return false;
- }
- }
-
return true;
}
@@ -971,7 +966,7 @@ gjs_array_from_flat_gvalue_array(JSContext *context,
if (result) {
JSObject *jsarray;
- jsarray = JS_NewArrayObject(context, length, elems.begin());
+ jsarray = JS_NewArrayObject(context, elems);
value.setObjectOrNull(jsarray);
}
@@ -2157,50 +2152,39 @@ gjs_array_from_g_list (JSContext *context,
{
unsigned int i;
GArgument arg;
-
- JS::RootedObject obj(context, JS_NewArrayObject(context, 0, NULL));
- if (obj == NULL)
- return false;
-
- value_p.setObject(*obj);
-
- JS::RootedValue elem(context);
+ JS::AutoValueVector elems(context);
i = 0;
if (list_tag == GI_TYPE_TAG_GLIST) {
for ( ; list != NULL; list = list->next) {
arg.v_pointer = list->data;
+ elems.growBy(1);
- if (!gjs_value_from_g_argument(context, &elem,
+ if (!gjs_value_from_g_argument(context, elems.handleAt(i),
param_info, &arg,
true))
return false;
-
- if (!JS_DefineElement(context, obj,
- i, elem,
- NULL, NULL, JSPROP_ENUMERATE)) {
- return false;
- }
++i;
}
} else {
for ( ; slist != NULL; slist = slist->next) {
arg.v_pointer = slist->data;
+ elems.growBy(1);
- if (!gjs_value_from_g_argument(context, &elem,
+ if (!gjs_value_from_g_argument(context, elems.handleAt(i),
param_info, &arg,
true))
return false;
-
- if (!JS_DefineElement(context, obj,
- i, elem,
- NULL, NULL, JSPROP_ENUMERATE)) {
- return false;
- }
++i;
}
}
+ JS::RootedObject obj(context, JS_NewArrayObject(context, elems));
+ if (obj == NULL)
+ return false;
+
+ value_p.setObject(*obj);
+
return true;
}
@@ -2239,21 +2223,14 @@ gjs_array_from_carray_internal (JSContext *context,
if (element_type == GI_TYPE_TAG_UNICHAR)
return gjs_string_from_ucs4(context, (gunichar *) array, length, value_p);
- JS::RootedObject obj(context, JS_NewArrayObject(context, 0, NULL));
- if (obj == NULL)
- return false;
-
- value_p.setObject(*obj);
-
- JS::RootedValue elem(context);
+ JS::AutoValueVector elems(context);
+ elems.resize(length);
#define ITERATE(type) \
for (i = 0; i < length; i++) { \
arg.v_##type = *(((g##type*)array) + i); \
- if (!gjs_value_from_g_argument(context, &elem, param_info, &arg, true)) \
- return false; \
- if (!JS_DefineElement(context, obj, i, elem, NULL, NULL, \
- JSPROP_ENUMERATE)) \
+ if (!gjs_value_from_g_argument(context, elems.handleAt(i), \
+ param_info, &arg, true)) \
return false; \
}
@@ -2312,10 +2289,8 @@ gjs_array_from_carray_internal (JSContext *context,
for (i = 0; i < length; i++) {
arg.v_pointer = ((char*)array) + (struct_size * i);
- if (!gjs_value_from_g_argument(context, &elem, param_info, &arg, true))
- return false;
- if (!JS_DefineElement(context, obj, i, elem, NULL, NULL,
- JSPROP_ENUMERATE))
+ if (!gjs_value_from_g_argument(context, elems.handleAt(i),
+ param_info, &arg, true))
return false;
}
@@ -2342,6 +2317,12 @@ gjs_array_from_carray_internal (JSContext *context,
#undef ITERATE
+ JS::RootedObject obj(context, JS_NewArrayObject(context, elems));
+ if (obj == NULL)
+ return false;
+
+ value_p.setObject(*obj);
+
return true;
}
@@ -2456,23 +2437,16 @@ gjs_array_from_zero_terminated_c_array (JSContext *context,
if (element_type == GI_TYPE_TAG_UNICHAR)
return gjs_string_from_ucs4(context, (gunichar *) c_array, -1, value_p);
- JS::RootedObject obj(context, JS_NewArrayObject(context, 0, NULL));
- if (obj == NULL)
- return false;
-
- value_p.setObject(*obj);
-
- JS::RootedValue elem(context);
+ JS::AutoValueVector elems(context);
#define ITERATE(type) \
do { \
g##type *array = (g##type *) c_array; \
for (i = 0; array[i]; i++) { \
arg.v_##type = array[i]; \
- if (!gjs_value_from_g_argument(context, &elem, param_info, &arg, true)) \
- return false; \
- if (!JS_DefineElement(context, obj, i, elem, NULL, NULL, \
- JSPROP_ENUMERATE)) \
+ elems.growBy(1); \
+ if (!gjs_value_from_g_argument(context, elems.handleAt(i), \
+ param_info, &arg, true)) \
return false; \
} \
} while(0);
@@ -2533,6 +2507,12 @@ gjs_array_from_zero_terminated_c_array (JSContext *context,
#undef ITERATE
+ JS::RootedObject obj(context, JS_NewArrayObject(context, elems));
+ if (obj == NULL)
+ return false;
+
+ value_p.setObject(*obj);
+
return true;
}
diff --git a/gi/function.cpp b/gi/function.cpp
index 36e298a..d543769 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -292,8 +292,7 @@ gjs_callback_closure(ffi_cif *cif,
if (!JS_CallFunctionValue(context,
this_object,
rooted_function,
- n_jsargs,
- jsargs.begin(),
+ jsargs,
rval.address())) {
goto out;
}
@@ -1272,9 +1271,7 @@ release:
js_rval.ref().set(return_values[0]);
} else {
JSObject *array;
- array = JS_NewArrayObject(context,
- function->js_out_argc,
- &return_values[0]);
+ array = JS_NewArrayObject(context, return_values);
if (array == NULL) {
failed = true;
} else {
diff --git a/gi/object.cpp b/gi/object.cpp
index 17bcb11..8a3a02e 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1708,9 +1708,9 @@ emit_func(JSContext *context,
g_value_init(value, signal_query.param_types[i] & ~G_SIGNAL_TYPE_STATIC_SCOPE);
if ((signal_query.param_types[i] & G_SIGNAL_TYPE_STATIC_SCOPE) != 0)
- failed = !gjs_value_to_g_value_no_copy(context, argv.handleOrUndefinedAt(i + 1), value);
+ failed = !gjs_value_to_g_value_no_copy(context, argv[i + 1], value);
else
- failed = !gjs_value_to_g_value(context, argv.handleOrUndefinedAt(i + 1), value);
+ failed = !gjs_value_to_g_value(context, argv[i + 1], value);
if (failed)
break;
@@ -2429,10 +2429,11 @@ gjs_object_constructor (GType type,
construct_properties[i].value,
construct_properties[i].pspec);
- JS::RootedValue argv(context, JS::ObjectValue(*props_hash));
- object = JS_New(context, constructor, 1, argv.address());
+ JS::AutoValueArray<1> args(context);
+ args[0].set(JS::ObjectValue(*props_hash));
+ object = JS_New(context, constructor, args);
} else {
- object = JS_New(context, constructor, 0, NULL);
+ object = JS_New(context, constructor, JS::HandleValueArray::empty());
}
if (!object)
@@ -2612,7 +2613,7 @@ gjs_object_custom_init(GTypeInstance *instance,
JS::RootedValue r(context);
if (!JS_CallFunctionValue(context, object, v,
- 0, NULL, r.address()))
+ JS::HandleValueArray::empty(), r.address()))
gjs_log_exception(context);
}
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 9eaa4f4..87058f3 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -132,8 +132,7 @@ resolve_namespace_object(JSContext *context,
if (!override.isNull() &&
!JS_CallFunctionValue (context, gi_namespace, /* thisp */
override, /* callee */
- 0, /* argc */
- NULL, /* argv */
+ JS::HandleValueArray::empty(),
result.address()))
return false;
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index ed8e590..0f9d3ad 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -251,9 +251,7 @@ byte_array_length_setter(JSContext *context,
byte_array_ensure_array(priv);
- // COMPAT: Indexing JS::CallArgs should provide a handle in mozjs31
- JS::RootedValue arg(context, args[0]);
- if (!gjs_value_to_gsize(context, arg, &len)) {
+ if (!gjs_value_to_gsize(context, args[0], &len)) {
gjs_throw(context,
"Can't set ByteArray length to non-integer");
return false;
diff --git a/gjs/context.cpp b/gjs/context.cpp
index 562ed48..16ede1a 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -172,8 +172,7 @@ gjs_log_error(JSContext *context,
JS_RestoreExceptionState(context, exc_state);
}
- /* COMPAT: JS::CallArgs::operator[] will yield Handle in mozjs31 */
- gjs_log_exception_full(context, argv.handleOrUndefinedAt(0), jstr);
+ gjs_log_exception_full(context, argv[0], jstr);
JS_EndRequest(context);
argv.rval().setUndefined();
diff --git a/gjs/coverage.cpp b/gjs/coverage.cpp
index 9108af0..a2b60c7 100644
--- a/gjs/coverage.cpp
+++ b/gjs/coverage.cpp
@@ -472,12 +472,11 @@ get_executed_lines_for(JSContext *context,
{
GArray *array = NULL;
JS::RootedValue rval(context);
+ JS::AutoValueArray<1> args(context);
+ args[0].set(filename_value);
- /* Removing const with cast is OK because the function arguments are not
- * mutated in JS_CallFunction */
if (!JS_CallFunctionName(context, coverage_statistics, "getExecutedLinesFor",
- 1, (JS::Value *) filename_value.address(),
- rval.address())) {
+ args, rval.address())) {
gjs_log_exception(context);
return NULL;
}
@@ -585,12 +584,11 @@ get_functions_for(JSContext *context,
{
GArray *array = NULL;
JS::RootedValue rval(context);
+ JS::AutoValueArray<1> args(context);
+ args[0].set(filename_value);
- /* Removing const with cast is OK because the function arguments are not
- * mutated in JS_CallFunction */
if (!JS_CallFunctionName(context, coverage_statistics, "getFunctionsFor",
- 1, (JS::Value *) filename_value.address(),
- rval.address())) {
+ args, rval.address())) {
gjs_log_exception(context);
return NULL;
}
@@ -714,13 +712,12 @@ get_branches_for(JSContext *context,
JS::HandleValue filename_value)
{
GArray *array = NULL;
+ JS::AutoValueArray<1> args(context);
+ args[0].set(filename_value);
JS::RootedValue rval(context);
- /* Removing const with cast is OK because the function arguments are not
- * mutated in JS_CallFunction */
if (!JS_CallFunctionName(context, coverage_statistics, "getBranchesFor",
- 1, (JS::Value *) filename_value.address(),
- rval.address())) {
+ args, rval.address())) {
gjs_log_exception(context);
return NULL;
}
@@ -855,7 +852,7 @@ get_covered_files(GjsCoverage *coverage)
uint32_t n_files;
if (!JS_CallFunctionName(context, rooted_priv, "getCoveredFiles",
- 0, NULL, rval.address())) {
+ JS::HandleValueArray::empty(), rval.address())) {
gjs_log_exception(context);
return NULL;
}
@@ -1013,8 +1010,7 @@ gjs_serialize_statistics(GjsCoverage *coverage)
JS::RootedValue string_value_return(js_runtime);
if (!JS_CallFunctionName(js_context, rooted_priv, "stringify",
- 0,
- NULL,
+ JS::HandleValueArray::empty(),
string_value_return.address())) {
gjs_log_exception(js_context);
return NULL;
@@ -1169,8 +1165,7 @@ coverage_statistics_has_stale_cache(GjsCoverage *coverage)
JS::RootedObject rooted_priv(js_context, priv->coverage_statistics);
JS::RootedValue stale_cache_value(js_context);
if (!JS_CallFunctionName(js_context, rooted_priv, "staleCache",
- 0,
- NULL,
+ JS::HandleValueArray::empty(),
stale_cache_value.address())) {
gjs_log_exception(js_context);
g_error("Failed to call into javascript to get stale cache value. This is a bug");
@@ -1370,7 +1365,6 @@ coverage_get_file_modification_time(JSContext *context,
JS::Value *vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
- JSRuntime *runtime = JS_GetRuntime(context);
GTimeVal mtime;
bool ret = false;
char *filename = get_filename_from_filename_as_js_string(context, args);
@@ -1379,13 +1373,14 @@ coverage_get_file_modification_time(JSContext *context,
goto out;
if (gjs_get_path_mtime(filename, &mtime)) {
- JS::RootedObject mtime_values_array(runtime,
- JS_NewArrayObject(context, 0, NULL));
- if (!JS_DefineElement(context, mtime_values_array, 0, JS::Int32Value(mtime.tv_sec), NULL, NULL, 0))
+ JS::AutoValueArray<2> mtime_values_array(context);
+ mtime_values_array[0].setInt32(mtime.tv_sec);
+ mtime_values_array[1].setInt32(mtime.tv_usec);
+ JS::RootedObject array_obj(context,
+ JS_NewArrayObject(context, mtime_values_array));
+ if (array_obj == NULL)
goto out;
- if (!JS_DefineElement(context, mtime_values_array, 1, JS::Int32Value(mtime.tv_usec), NULL, NULL, 0))
- goto out;
- args.rval().setObject(*(mtime_values_array.get()));
+ args.rval().setObject(*array_obj);
} else {
args.rval().setNull();
}
@@ -1658,15 +1653,13 @@ bootstrap_coverage(GjsCoverage *coverage)
/* Now create the array to pass the desired prefixes over */
JSObject *prefixes = gjs_build_string_array(context, -1, priv->prefixes);
- JS::Value coverage_statistics_constructor_arguments[] = {
- JS::ObjectValue(*prefixes),
- cache_value.get()
- };
+ JS::AutoValueArray<2> coverage_statistics_constructor_args(context);
+ coverage_statistics_constructor_args[0].setObject(*prefixes);
+ coverage_statistics_constructor_args[1].set(cache_value);
JSObject *coverage_statistics = JS_New(context,
coverage_statistics_constructor,
- 2,
- coverage_statistics_constructor_arguments);
+ coverage_statistics_constructor_args);
if (!coverage_statistics) {
gjs_throw(context, "Failed to create coverage_statitiscs object");
@@ -1742,9 +1735,7 @@ gjs_clear_js_side_statistics_from_coverage_object(GjsCoverage *coverage)
JS::RootedObject rooted_priv(js_context, priv->coverage_statistics);
JS::RootedValue rval(JS_GetRuntime(js_context));
if (!JS_CallFunctionName(js_context, rooted_priv, "deactivate",
- 0,
- NULL,
- rval.address())) {
+ JS::HandleValueArray::empty(), rval.address())) {
gjs_log_exception(js_context);
g_error("Failed to deactivate debugger - this is a fatal error");
}
diff --git a/gjs/jsapi-util-args.h b/gjs/jsapi-util-args.h
index 87f7901..3229350 100644
--- a/gjs/jsapi-util-args.h
+++ b/gjs/jsapi-util-args.h
@@ -246,8 +246,7 @@ parse_call_args_helper(JSContext *cx,
}
try {
- /* COMPAT: JS::CallArgs::operator[] will yield Handle in mozjs31 */
- assign(cx, *fchar, nullable, args.handleOrUndefinedAt(param_ix), param_ref);
+ assign(cx, *fchar, nullable, args[param_ix], param_ref);
} catch (char *message) {
/* Our error messages are going to be more useful than whatever was
* thrown by the various conversion functions */
diff --git a/gjs/jsapi-util-error.cpp b/gjs/jsapi-util-error.cpp
index 6b21bb3..a47d7e2 100644
--- a/gjs/jsapi-util-error.cpp
+++ b/gjs/jsapi-util-error.cpp
@@ -76,10 +76,11 @@ gjs_throw_valist(JSContext *context,
JS::RootedObject constructor(context);
JS::RootedObject global(context, JS_GetGlobalForScopeChain(context));
- JS::RootedValue v_constructor(context), v_message(context), new_exc(context);
+ JS::RootedValue v_constructor(context), new_exc(context);
+ JS::AutoValueArray<1> error_args(context);
result = false;
- if (!gjs_string_from_utf8(context, s, -1, &v_message)) {
+ if (!gjs_string_from_utf8(context, s, -1, error_args[0])) {
JS_ReportError(context, "Failed to copy exception string");
goto out;
}
@@ -92,7 +93,7 @@ gjs_throw_valist(JSContext *context,
/* throw new Error(message) */
constructor = &v_constructor.toObject();
- new_exc.setObjectOrNull(JS_New(context, constructor, 1, v_message.address()));
+ new_exc.setObjectOrNull(JS_New(context, constructor, error_args));
JS_SetPendingException(context, new_exc);
result = true;
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 6baa1ac..5835051 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -316,7 +316,7 @@ gjs_build_string_array(JSContext *context,
elems.append(element);
}
- return JS_NewArrayObject(context, elems.length(), &elems[0]);
+ return JS_NewArrayObject(context, elems);
}
JSObject*
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index d5abfdd..c680e8b 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -268,7 +268,7 @@ gjs_##name##_constructor(JSContext *context, \
gjs_throw_constructor_error(context); \
return false; \
} \
- object = JS_NewObjectForConstructor(context, &gjs_##name##_class, vp); \
+ object = JS_NewObjectForConstructor(context, &gjs_##name##_class, argv); \
if (object == NULL) \
return false; \
}
diff --git a/gjs/stack.cpp b/gjs/stack.cpp
index 089ff35..8ccf301 100644
--- a/gjs/stack.cpp
+++ b/gjs/stack.cpp
@@ -63,7 +63,8 @@ gjs_context_get_frame_info(JSContext *context,
error_id, &constructor))
return false;
- JS::RootedObject err_obj(context, JS_New(context, constructor, 0, NULL));
+ JS::RootedObject err_obj(context, JS_New(context, constructor,
+ JS::HandleValueArray::empty()));
if (!stack.empty() &&
!gjs_object_get_property_const(context, err_obj, GJS_STRING_STACK,
diff --git a/modules/cairo-context.cpp b/modules/cairo-context.cpp
index 24ec1b6..ca4afb1 100644
--- a/modules/cairo-context.cpp
+++ b/modules/cairo-context.cpp
@@ -82,7 +82,7 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method) \
cfunc(cr, &arg1, &arg2); \
if (cairo_status(cr) == CAIRO_STATUS_SUCCESS) { \
JS::RootedObject array(context, \
- JS_NewArrayObject(context, 0, NULL)); \
+ JS_NewArrayObject(context, JS::HandleValueArray::empty())); \
if (!array) \
return false; \
JS::RootedValue r(context, JS::NumberValue(arg1)); \
@@ -100,7 +100,7 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method) \
cfunc(cr, &arg1, &arg2); \
if (cairo_status(cr) == CAIRO_STATUS_SUCCESS) { \
JS::RootedObject array(context, \
- JS_NewArrayObject(context, 0, NULL)); \
+ JS_NewArrayObject(context, JS::HandleValueArray::empty())); \
if (!array) \
return false; \
JS::RootedValue r(context, JS::NumberValue(arg1)); \
@@ -118,7 +118,7 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method) \
cfunc(cr, &arg1, &arg2, &arg3, &arg4); \
{ \
JS::RootedObject array(context, \
- JS_NewArrayObject(context, 0, NULL)); \
+ JS_NewArrayObject(context, JS::HandleValueArray::empty())); \
if (!array) \
return false; \
JS::RootedValue r(context, JS::NumberValue(arg1)); \
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]