[gjs/wip/ptomato/mozjs38: 9/26] js: Refactor gjs_object_*_property()
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/wip/ptomato/mozjs38: 9/26] js: Refactor gjs_object_*_property()
- Date: Tue, 24 Jan 2017 02:33:39 +0000 (UTC)
commit 873dd38dfc89b7d38450f6ed7014751da8eaaf5c
Author: Philip Chimento <philip endlessm com>
Date: Mon Jan 23 17:57:51 2017 -0800
js: Refactor gjs_object_*_property()
This code was due for some refactoring, and changing GjsConstString to be
rooted in the following commit was the opportunity for it. We now have
gjs_object_get_property(), gjs_object_has_property(),
gjs_object_set_property(), and gjs_object_define_property() as wrappers
for JS_GetPropertyById(), etc., that take a GjsConstString constant
instead of a jsid.
In addition, we rename gjs_object_require_property_value() to be an
overload of gjs_object_require_property(); the old name was confusing
because the name _without_ "value" was the one that dealt with a
JS::Value!
Same rename for gjs_object_require_converted_property_value().
This whole thing allows us to get rid of some code, moving a bunch of
roots into these new functions. These roots are strictly speaking still
necessary, but in the next commit we're going to root all the interned
strings permanently, which should have been done all along.
https://bugzilla.gnome.org/show_bug.cgi?id=776966
gi/arg.cpp | 21 +++----
gi/boxed.cpp | 7 +--
gi/fundamental.cpp | 15 +++---
gi/gerror.cpp | 42 +++++---------
gi/object.cpp | 13 ++---
gi/param.cpp | 4 +-
gi/repo.cpp | 64 +++++++++-------------
gi/value.cpp | 12 ++--
gjs/context.cpp | 10 ----
gjs/coverage.cpp | 30 +++++-----
gjs/importer.cpp | 63 +++++++++-------------
gjs/jsapi-dynamic-class.cpp | 7 +--
gjs/jsapi-util-error.cpp | 5 +-
gjs/jsapi-util.cpp | 112 ++++++++++++++++++++++++++------------
gjs/jsapi-util.h | 127 +++++++++++++++++++++++++++++--------------
gjs/stack.cpp | 16 +++---
modules/cairo-region.cpp | 8 ++--
17 files changed, 295 insertions(+), 261 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index 8f6ba96..805fa1c 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -1257,9 +1257,6 @@ gjs_array_to_explicit_array_internal(JSContext *context,
return false;
}
- JS::RootedId length_name(context,
- gjs_context_get_const_string(context, GJS_STRING_LENGTH));
-
if (value.isNull()) {
*contents = NULL;
*length_p = 0;
@@ -1270,12 +1267,13 @@ gjs_array_to_explicit_array_internal(JSContext *context,
goto out;
} else {
JS::RootedObject array_obj(context, &value.toObject());
- if (JS_HasPropertyById(context, array_obj, length_name, &found_length) &&
+ if (gjs_object_has_property(context, array_obj, GJS_STRING_LENGTH,
+ &found_length) &&
found_length) {
guint32 length;
- if (!gjs_object_require_converted_property_value(context, array_obj, NULL,
- length_name, &length)) {
+ if (!gjs_object_require_converted_property(context, array_obj, NULL,
+ GJS_STRING_LENGTH, &length)) {
goto out;
} else {
if (!gjs_array_to_array(context,
@@ -1791,16 +1789,15 @@ gjs_value_to_g_argument(JSContext *context,
if (value.isObject()) {
bool found_length;
JS::RootedObject array_obj(context, &value.toObject());
- JS::RootedId length_name(context,
- gjs_context_get_const_string(context, GJS_STRING_LENGTH));
- if (JS_HasPropertyById(context, array_obj, length_name, &found_length) &&
+ if (gjs_object_has_property(context, array_obj,
+ GJS_STRING_LENGTH, &found_length) &&
found_length) {
guint32 length;
- if (!gjs_object_require_converted_property_value(context, array_obj,
- NULL, length_name,
- &length)) {
+ if (!gjs_object_require_converted_property(context, array_obj,
+ NULL, GJS_STRING_LENGTH,
+ &length)) {
wrong = true;
} else {
GList *list;
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index 9a47769..c6d8ddc 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -310,12 +310,11 @@ boxed_invoke_constructor(JSContext *context,
JS::HandleId constructor_name,
JS::CallArgs& args)
{
- JS::RootedId constructor_const(context,
- gjs_context_get_const_string(context, GJS_STRING_CONSTRUCTOR));
JS::RootedObject js_constructor(context);
- if (!gjs_object_require_property_value(context, obj, NULL, constructor_const,
- &js_constructor))
+ if (!gjs_object_require_property(context, obj, NULL,
+ GJS_STRING_CONSTRUCTOR,
+ &js_constructor))
return false;
JS::RootedValue js_constructor_func(context);
diff --git a/gi/fundamental.cpp b/gi/fundamental.cpp
index 91d2efe..9970328 100644
--- a/gi/fundamental.cpp
+++ b/gi/fundamental.cpp
@@ -375,12 +375,11 @@ fundamental_invoke_constructor(FundamentalInstance *priv,
const JS::HandleValueArray& args,
GIArgument *rvalue)
{
- JS::RootedId constructor_const(context,
- gjs_context_get_const_string(context, GJS_STRING_CONSTRUCTOR));
JS::RootedObject js_constructor(context);
- if (!gjs_object_require_property_value(context, obj, NULL,
- constructor_const, &js_constructor) ||
+ if (!gjs_object_require_property(context, obj, NULL,
+ GJS_STRING_CONSTRUCTOR,
+ &js_constructor) ||
priv->prototype->constructor_name.get() == JSID_VOID) {
gjs_throw (context,
"Couldn't find a constructor for type %s.%s",
@@ -391,8 +390,8 @@ fundamental_invoke_constructor(FundamentalInstance *priv,
JS::RootedObject constructor(context);
JS::RootedId constructor_name(context, priv->prototype->constructor_name);
- if (!gjs_object_require_property_value(context, js_constructor, NULL,
- constructor_name, &constructor)) {
+ if (!gjs_object_require_property(context, js_constructor, NULL,
+ constructor_name, &constructor)) {
gjs_throw (context,
"Couldn't find a constructor for type %s.%s",
g_base_info_get_namespace((GIBaseInfo*) priv->prototype->info),
@@ -604,8 +603,8 @@ gjs_lookup_fundamental_prototype(JSContext *context,
g_assert(constructor != NULL);
- if (!gjs_object_get_property_const(context, constructor,
- GJS_STRING_PROTOTYPE, &value))
+ if (!gjs_object_get_property(context, constructor,
+ GJS_STRING_PROTOTYPE, &value))
return NULL;
if (G_UNLIKELY (!value.isObjectOrNull()))
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index c069a19..6b19e2f 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -96,17 +96,14 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(error)
priv->domain = proto_priv->domain;
JS::RootedObject params_obj(context, &argv[0].toObject());
- JS::RootedId message_name(context,
- gjs_context_get_const_string(context, GJS_STRING_MESSAGE));
- JS::RootedId code_name(context,
- gjs_context_get_const_string(context, GJS_STRING_CODE));
- if (!gjs_object_require_property_value(context, params_obj,
- "GError constructor", message_name,
- &message))
+ if (!gjs_object_require_property(context, params_obj,
+ "GError constructor",
+ GJS_STRING_MESSAGE, &message))
return false;
- if (!gjs_object_require_property_value(context, params_obj,
- "GError constructor", code_name,
- &code))
+
+ if (!gjs_object_require_property(context, params_obj,
+ "GError constructor",
+ GJS_STRING_CODE, &code))
return false;
priv->gerror = g_error_new_literal(priv->domain, code, message);
@@ -246,12 +243,10 @@ error_constructor_value_of(JSContext *context,
{
GJS_GET_THIS(context, argc, vp, rec, self);
Error *priv;
- JS::RootedId prototype_name(context,
- gjs_context_get_const_string(context, GJS_STRING_PROTOTYPE));
JS::RootedObject prototype(context);
- if (!gjs_object_require_property_value(context, self, "constructor",
- prototype_name, &prototype)) {
+ if (!gjs_object_require_property(context, self, "constructor",
+ GJS_STRING_PROTOTYPE, &prototype)) {
/* This error message will be more informative */
JS_ClearPendingException(context);
gjs_throw(context, "GLib.Error.valueOf() called on something that is not"
@@ -409,21 +404,14 @@ define_error_properties(JSContext *context,
if (!gjs_context_get_frame_info(context, m_stack, m_file, m_line))
return;
- JS::RootedId stack_name(context,
- gjs_context_get_const_string(context, GJS_STRING_STACK));
- JS::RootedId filename_name(context,
- gjs_context_get_const_string(context, GJS_STRING_FILENAME));
- JS::RootedId linenumber_name(context,
- gjs_context_get_const_string(context, GJS_STRING_LINE_NUMBER));
-
- JS_DefinePropertyById(context, obj, stack_name, stack,
- NULL, NULL, JSPROP_ENUMERATE);
+ gjs_object_define_property(context, obj, GJS_STRING_STACK, stack,
+ JSPROP_ENUMERATE);
- JS_DefinePropertyById(context, obj, filename_name, fileName,
- NULL, NULL, JSPROP_ENUMERATE);
+ gjs_object_define_property(context, obj, GJS_STRING_FILENAME,
+ fileName, JSPROP_ENUMERATE);
- JS_DefinePropertyById(context, obj, linenumber_name, lineNumber,
- NULL, NULL, JSPROP_ENUMERATE);
+ gjs_object_define_property(context, obj, GJS_STRING_LINE_NUMBER,
+ lineNumber, JSPROP_ENUMERATE);
}
JSObject*
diff --git a/gi/object.cpp b/gi/object.cpp
index 3806ce4..bac99d1 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1466,9 +1466,8 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(object_instance)
* might be traced and we will end up dereferencing a null pointer */
init_object_private(context, object);
- JS::RootedId object_init_name(context,
- gjs_context_get_const_string(context, GJS_STRING_GOBJECT_INIT));
- if (!gjs_object_require_property(context, object, "GObject instance", object_init_name, &initer))
+ if (!gjs_object_require_property(context, object, "GObject instance",
+ GJS_STRING_GOBJECT_INIT, &initer))
return false;
argv.rval().setUndefined();
@@ -1642,8 +1641,8 @@ gjs_lookup_object_prototype_from_info(JSContext *context,
return NULL;
JS::RootedValue value(context);
- if (!gjs_object_get_property_const(context, constructor,
- GJS_STRING_PROTOTYPE, &value))
+ if (!gjs_object_get_property(context, constructor,
+ GJS_STRING_PROTOTYPE, &value))
return NULL;
if (G_UNLIKELY (!value.isObjectOrNull()))
@@ -2739,8 +2738,8 @@ gjs_object_custom_init(GTypeInstance *instance,
associate_js_gobject(context, object, G_OBJECT (instance));
JS::RootedValue v(context);
- if (!gjs_object_get_property_const(context, object,
- GJS_STRING_INSTANCE_INIT, &v)) {
+ if (!gjs_object_get_property(context, object,
+ GJS_STRING_INSTANCE_INIT, &v)) {
gjs_log_exception(context);
return;
}
diff --git a/gi/param.cpp b/gi/param.cpp
index fd10ceb..3e6db92 100644
--- a/gi/param.cpp
+++ b/gi/param.cpp
@@ -190,8 +190,8 @@ gjs_lookup_param_prototype(JSContext *context)
JS::RootedObject constructor(context, &value.toObject());
g_assert(constructor != NULL);
- if (!gjs_object_get_property_const(context, constructor,
- GJS_STRING_PROTOTYPE, &value))
+ if (!gjs_object_get_property(context, constructor,
+ GJS_STRING_PROTOTYPE, &value))
return NULL;
if (G_UNLIKELY (!value.isObjectOrNull()))
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 7b8a1ed..cd80cd3 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -64,16 +64,13 @@ get_version_for_ns (JSContext *context,
char **version)
{
JS::RootedObject versions(context);
- JS::RootedValue version_val(context);
- JS::RootedId versions_name(context,
- gjs_context_get_const_string(context, GJS_STRING_GI_VERSIONS));
- if (!gjs_object_require_property_value(context, repo_obj,
- "GI repository object", versions_name,
- &versions))
+ if (!gjs_object_require_property(context, repo_obj,
+ "GI repository object",
+ GJS_STRING_GI_VERSIONS, &versions))
return false;
- if (!gjs_object_require_property_value(context, versions, NULL, ns_id, version)) {
+ if (!gjs_object_require_property(context, versions, NULL, ns_id, version)) {
/* Property not actually required, so clear an exception */
JS_ClearPendingException(context);
*version = NULL;
@@ -291,21 +288,15 @@ repo_new(JSContext *context)
"repo constructor, obj %p priv %p", repo.get(), priv);
versions = JS_NewObject(context, NULL, JS::NullPtr(), global);
- JS::RootedId versions_name(context,
- gjs_context_get_const_string(context, GJS_STRING_GI_VERSIONS));
- JS_DefinePropertyById(context, repo,
- versions_name,
- JS::ObjectValue(*versions),
- NULL, NULL,
- JSPROP_PERMANENT);
+ JS::RootedValue v_versions(context, JS::ObjectValue(*versions));
+ gjs_object_define_property(context, repo, GJS_STRING_GI_VERSIONS,
+ v_versions, JSPROP_PERMANENT);
private_ns = JS_NewObject(context, NULL, JS::NullPtr(), global);
- JS::RootedId private_ns_name(context,
- gjs_context_get_const_string(context, GJS_STRING_PRIVATE_NS_MARKER));
- JS_DefinePropertyById(context, repo,
- private_ns_name,
- JS::ObjectValue(*private_ns),
- NULL, NULL, JSPROP_PERMANENT);
+ JS::RootedValue v_ns(context, JS::ObjectValue(*private_ns));
+ gjs_object_define_property(context, repo,
+ GJS_STRING_PRIVATE_NS_MARKER, v_ns,
+ JSPROP_PERMANENT);
/* FIXME - hack to make namespaces load, since
* gobject-introspection does not yet search a path properly.
@@ -578,12 +569,11 @@ error_has_name(JSContext *cx,
return false;
JS::AutoSaveExceptionState saved_exc(cx);
- JS::RootedId name_id(cx, gjs_context_get_const_string(cx, GJS_STRING_NAME));
JS::RootedObject exc(cx, &thrown_value.toObject());
JS::RootedValue exc_name(cx);
bool retval = false;
- if (!JS_GetPropertyById(cx, exc, name_id, &exc_name))
+ if (!gjs_object_get_property(cx, exc, GJS_STRING_NAME, &exc_name))
goto out;
int32_t cmp_result;
@@ -609,19 +599,17 @@ lookup_override_function(JSContext *cx,
JS::RootedValue importer(cx, gjs_get_global_slot(cx, GJS_GLOBAL_SLOT_IMPORTS));
g_assert(importer.isObject());
- JS::RootedId overrides_name(cx,
- gjs_context_get_const_string(cx, GJS_STRING_GI_OVERRIDES));
- JS::RootedId object_init_name(cx,
- gjs_context_get_const_string(cx, GJS_STRING_GOBJECT_INIT));
JS::RootedObject overridespkg(cx), module(cx);
JS::RootedObject importer_obj(cx, &importer.toObject());
- if (!gjs_object_require_property_value(cx, importer_obj, "importer",
- overrides_name, &overridespkg))
+ if (!gjs_object_require_property(cx, importer_obj, "importer",
+ GJS_STRING_GI_OVERRIDES,
+ &overridespkg))
goto fail;
- if (!gjs_object_require_property_value(cx, overridespkg, "GI repository object",
- ns_name, &module)) {
+ if (!gjs_object_require_property(cx, overridespkg,
+ "GI repository object", ns_name,
+ &module)) {
JS::RootedValue exc(cx);
JS_GetPendingException(cx, &exc);
@@ -636,7 +624,7 @@ lookup_override_function(JSContext *cx,
}
if (!gjs_object_require_property(cx, module, "override module",
- object_init_name, function) ||
+ GJS_STRING_GOBJECT_INIT, function) ||
!function.isObjectOrNull()) {
gjs_throw(cx, "Unexpected value for _init in overrides module");
goto fail;
@@ -658,20 +646,18 @@ gjs_lookup_namespace_object_by_name(JSContext *context,
gjs_get_global_slot(context, GJS_GLOBAL_SLOT_IMPORTS));
g_assert(importer.isObject());
- JS::RootedId gi_name(context,
- gjs_context_get_const_string(context, GJS_STRING_GI_MODULE));
JS::RootedObject repo(context), importer_obj(context, &importer.toObject());
- if (!gjs_object_require_property_value(context, importer_obj, "importer",
- gi_name, &repo)) {
+ if (!gjs_object_require_property(context, importer_obj, "importer",
+ GJS_STRING_GI_MODULE, &repo)) {
gjs_log_exception(context);
gjs_throw(context, "No gi property in importer");
return NULL;
}
JS::RootedObject retval(context);
- if (!gjs_object_require_property_value(context, repo, "GI repository object",
- ns_name, &retval))
+ if (!gjs_object_require_property(context, repo, "GI repository object",
+ ns_name, &retval))
return NULL;
return retval;
@@ -807,8 +793,8 @@ gjs_lookup_generic_prototype(JSContext *context,
return NULL;
JS::RootedValue value(context);
- if (!gjs_object_get_property_const(context, constructor,
- GJS_STRING_PROTOTYPE, &value))
+ if (!gjs_object_get_property(context, constructor,
+ GJS_STRING_PROTOTYPE, &value))
return NULL;
if (G_UNLIKELY (!value.isObjectOrNull()))
diff --git a/gi/value.cpp b/gi/value.cpp
index d32a6ff..3e5d55f 100644
--- a/gi/value.cpp
+++ b/gi/value.cpp
@@ -479,20 +479,20 @@ gjs_value_to_g_value_internal(JSContext *context,
g_value_set_object(gvalue, gobj);
} else if (gtype == G_TYPE_STRV) {
bool found_length;
- JS::RootedId length_name(context,
- gjs_context_get_const_string(context, GJS_STRING_LENGTH));
if (value.isNull()) {
/* do nothing */
} else {
JS::RootedObject array_obj(context, &value.toObject());
- if (JS_HasPropertyById(context, array_obj, length_name, &found_length) &&
+ if (gjs_object_has_property(context, array_obj,
+ GJS_STRING_LENGTH, &found_length) &&
found_length) {
guint32 length;
- if (!gjs_object_require_converted_property_value(context, array_obj,
- NULL, length_name,
- &length)) {
+ if (!gjs_object_require_converted_property(context, array_obj,
+ NULL,
+ GJS_STRING_LENGTH,
+ &length)) {
JS_ClearPendingException(context);
gjs_throw(context,
"Wrong type %s; strv expected",
diff --git a/gjs/context.cpp b/gjs/context.cpp
index dcade98..093df64 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -850,16 +850,6 @@ gjs_context_get_const_string(JSContext *context,
return gjs_context->const_strings[name];
}
-bool
-gjs_object_get_property_const(JSContext *cx,
- JS::HandleObject obj,
- GjsConstString property_name,
- JS::MutableHandleValue value_p)
-{
- JS::RootedId pname(cx, gjs_context_get_const_string(cx, property_name));
- return JS_GetPropertyById(cx, obj, pname, value_p);
-}
-
/**
* gjs_get_import_global:
* @context: a #JSContext
diff --git a/gjs/coverage.cpp b/gjs/coverage.cpp
index 5775c35..f75a065 100644
--- a/gjs/coverage.cpp
+++ b/gjs/coverage.cpp
@@ -517,13 +517,13 @@ get_hit_count_and_line_data(JSContext *cx,
int32_t *line)
{
JS::RootedId hit_count_name(cx, gjs_intern_string_to_id(cx, "hitCount"));
- if (!gjs_object_require_property_value(cx, obj, "function element",
- hit_count_name, hit_count))
+ if (!gjs_object_require_property(cx, obj, "function element",
+ hit_count_name, hit_count))
return false;
JS::RootedId line_number_name(cx, gjs_intern_string_to_id(cx, "line"));
- return gjs_object_require_property_value(cx, obj, "function_element",
- line_number_name, line);
+ return gjs_object_require_property(cx, obj, "function_element",
+ line_number_name, line);
}
static bool
@@ -538,10 +538,8 @@ convert_and_insert_function_decl(GArray *array,
JS::RootedObject object(context, &element.toObject());
JS::RootedValue function_name_property_value(context);
- JS::RootedId function_name(context,
- gjs_context_get_const_string(context, GJS_STRING_NAME));
- if (!gjs_object_require_property(context, object, NULL, function_name,
+ if (!gjs_object_require_property(context, object, NULL, GJS_STRING_NAME,
&function_name_property_value))
return false;
@@ -665,15 +663,17 @@ convert_and_insert_branch_info(GArray *array,
int32_t branch_point;
JS::RootedId point_name(context, gjs_intern_string_to_id(context, "point"));
- if (!gjs_object_require_property_value(context, object, "branch array element",
- point_name, &branch_point))
+ if (!gjs_object_require_property(context, object,
+ "branch array element",
+ point_name, &branch_point))
return false;
bool was_hit;
JS::RootedId hit_name(context, gjs_intern_string_to_id(context, "hit"));
- if (!gjs_object_require_property_value(context, object, "branch array element",
- hit_name, &was_hit))
+ if (!gjs_object_require_property(context, object,
+ "branch array element",
+ hit_name, &was_hit))
return false;
JS::RootedValue branch_exits_value(context);
@@ -1648,10 +1648,10 @@ bootstrap_coverage(GjsCoverage *coverage)
JS::RootedObject coverage_statistics_constructor(context);
JS::RootedId coverage_statistics_name(context,
gjs_intern_string_to_id(context, "CoverageStatistics"));
- if (!gjs_object_require_property_value(context, debugger_compartment,
- "debugger compartment",
- coverage_statistics_name,
- &coverage_statistics_constructor))
+ if (!gjs_object_require_property(context, debugger_compartment,
+ "debugger compartment",
+ coverage_statistics_name,
+ &coverage_statistics_constructor))
return false;
/* Create value for holding the cache. This will be undefined if
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index f997267..e549890 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -61,8 +61,8 @@ importer_to_string(JSContext *cx,
const JSClass *klass = JS_GetClass(importer);
JS::RootedValue module_path(cx);
- if (!gjs_object_get_property_const(cx, importer, GJS_STRING_MODULE_PATH,
- &module_path))
+ if (!gjs_object_get_property(cx, importer, GJS_STRING_MODULE_PATH,
+ &module_path))
return false;
g_autofree char *path = NULL;
@@ -117,9 +117,9 @@ define_meta_properties(JSContext *context,
parent_module_val.setObject(*parent);
JS::RootedValue parent_module_path(context);
- if (!gjs_object_get_property_const(context, parent,
- GJS_STRING_MODULE_PATH,
- &parent_module_path))
+ if (!gjs_object_get_property(context, parent,
+ GJS_STRING_MODULE_PATH,
+ &parent_module_path))
return false;
g_autofree char *module_path_buf = NULL;
@@ -265,8 +265,8 @@ module_to_string(JSContext *cx,
GJS_GET_THIS(cx, argc, vp, args, module);
JS::RootedValue module_path(cx);
- if (!gjs_object_get_property_const(cx, module, GJS_STRING_MODULE_PATH,
- &module_path))
+ if (!gjs_object_get_property(cx, module, GJS_STRING_MODULE_PATH,
+ &module_path))
return false;
g_assert(!module_path.isNull());
@@ -367,15 +367,15 @@ load_module_init(JSContext *context,
const char *full_path)
{
bool found;
- GFile *file;
+ g_autoptr(GFile) file = NULL;
/* First we check if js module has already been loaded */
- JS::RootedId module_init_name(context,
- gjs_context_get_const_string(context, GJS_STRING_MODULE_INIT));
- if (JS_HasPropertyById(context, in_object, module_init_name, &found) && found) {
+ if (gjs_object_has_property(context, in_object, GJS_STRING_MODULE_INIT,
+ &found) && found) {
JS::RootedValue module_obj_val(context);
- if (JS_GetPropertyById(context, in_object,
- module_init_name, &module_obj_val)) {
+ if (gjs_object_get_property(context, in_object,
+ GJS_STRING_MODULE_INIT,
+ &module_obj_val)) {
return &module_obj_val.toObject();
}
}
@@ -383,16 +383,13 @@ load_module_init(JSContext *context,
JS::RootedObject module_obj(context, create_module_object(context));
file = g_file_new_for_commandline_arg(full_path);
if (!import_file (context, "__init__", file, module_obj))
- goto out;
+ return module_obj;
- if (!JS_DefinePropertyById(context, in_object,
- module_init_name, JS::ObjectValue(*module_obj),
- NULL, NULL,
- GJS_MODULE_PROP_FLAGS & ~JSPROP_PERMANENT))
- goto out;
+ JS::RootedValue v_module(context, JS::ObjectValue(*module_obj));
+ gjs_object_define_property(context, in_object,
+ GJS_STRING_MODULE_INIT, v_module,
+ GJS_MODULE_PROP_FLAGS & ~JSPROP_PERMANENT);
- out:
- g_object_unref (file);
return module_obj;
}
@@ -477,13 +474,9 @@ do_import(JSContext *context,
GFile *gfile;
bool exists;
- JS::RootedId search_path_name(context,
- gjs_context_get_const_string(context, GJS_STRING_SEARCH_PATH));
-
- if (!gjs_object_require_property_value(context, obj, "importer",
- search_path_name, &search_path)) {
+ if (!gjs_object_require_property(context, obj, "importer",
+ GJS_STRING_SEARCH_PATH, &search_path))
return false;
- }
if (!JS_IsArrayObject(context, search_path)) {
gjs_throw(context, "searchPath property on importer is not an array");
@@ -725,10 +718,9 @@ importer_new_enumerate(JSContext *context,
/* we are enumerating the prototype properties */
return true;
- JS::RootedId search_path_name(context,
- gjs_context_get_const_string(context, GJS_STRING_SEARCH_PATH));
- if (!gjs_object_require_property_value(context, object, "importer",
- search_path_name, &search_path))
+ if (!gjs_object_require_property(context, object, "importer",
+ GJS_STRING_SEARCH_PATH,
+ &search_path))
return false;
if (!JS_IsArrayObject(context, search_path)) {
@@ -1176,12 +1168,9 @@ gjs_define_root_importer_object(JSContext *context,
JS::RootedValue importer (JS_GetRuntime(context),
JS::ObjectValue(*root_importer));
- JS::RootedId imports_name(context,
- gjs_context_get_const_string(context, GJS_STRING_IMPORTS));
- if (!JS_DefinePropertyById(context, in_object,
- imports_name, importer,
- NULL, NULL,
- GJS_MODULE_PROP_FLAGS)) {
+ if (!gjs_object_define_property(context, in_object,
+ GJS_STRING_IMPORTS, importer,
+ GJS_MODULE_PROP_FLAGS)) {
gjs_debug(GJS_DEBUG_IMPORTER, "DefineProperty imports on %p failed",
in_object.get());
return false;
diff --git a/gjs/jsapi-dynamic-class.cpp b/gjs/jsapi-dynamic-class.cpp
index 57fdfc3..ab4cc3b 100644
--- a/gjs/jsapi-dynamic-class.cpp
+++ b/gjs/jsapi-dynamic-class.cpp
@@ -174,12 +174,11 @@ gjs_construct_object_dynamic(JSContext *context,
{
JSAutoRequest ar(context);
- JS::RootedId constructor_name(context,
- gjs_context_get_const_string(context, GJS_STRING_CONSTRUCTOR));
JS::RootedObject constructor(context);
- if (!gjs_object_require_property_value(context, proto, "prototype",
- constructor_name, &constructor))
+ if (!gjs_object_require_property(context, proto, "prototype",
+ GJS_STRING_CONSTRUCTOR,
+ &constructor))
return NULL;
return JS_New(context, constructor, args);
diff --git a/gjs/jsapi-util-error.cpp b/gjs/jsapi-util-error.cpp
index de890f1..2925f7d 100644
--- a/gjs/jsapi-util-error.cpp
+++ b/gjs/jsapi-util-error.cpp
@@ -101,11 +101,10 @@ gjs_throw_valist(JSContext *context,
goto out;
if (error_name != NULL) {
- JS::RootedId name_id(context,
- gjs_context_get_const_string(context, GJS_STRING_NAME));
JS::RootedValue name_value(context);
if (!gjs_string_from_utf8(context, error_name, -1, &name_value) ||
- !JS_SetPropertyById(context, new_exc, name_id, name_value))
+ !gjs_object_set_property(context, new_exc, GJS_STRING_NAME,
+ name_value))
goto out;
}
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 493bab4..d8ca3ed 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -142,6 +142,49 @@ gjs_get_global_slot (JSContext *context,
return JS_GetReservedSlot(global, JSCLASS_GLOBAL_SLOT_COUNT + slot);
}
+bool
+gjs_object_get_property(JSContext *cx,
+ JS::HandleObject obj,
+ GjsConstString property_name,
+ JS::MutableHandleValue value_p)
+{
+ JS::RootedId id(cx, gjs_context_get_const_string(cx, property_name));
+ return JS_GetPropertyById(cx, obj, id, value_p);
+}
+
+bool
+gjs_object_set_property(JSContext *cx,
+ JS::HandleObject obj,
+ GjsConstString property_name,
+ JS::HandleValue value)
+{
+ JS::RootedId id(cx, gjs_context_get_const_string(cx, property_name));
+ return JS_SetPropertyById(cx, obj, id, value);
+}
+
+bool
+gjs_object_has_property(JSContext *cx,
+ JS::HandleObject obj,
+ GjsConstString property_name,
+ bool *found)
+{
+ JS::RootedId id(cx, gjs_context_get_const_string(cx, property_name));
+ return JS_HasPropertyById(cx, obj, id, found);
+}
+
+bool gjs_object_define_property(JSContext *cx,
+ JS::HandleObject obj,
+ GjsConstString property_name,
+ JS::HandleValue value,
+ unsigned flags,
+ JSPropertyOp getter,
+ JSStrictPropertyOp setter)
+{
+ return JS_DefinePropertyById(cx, obj,
+ gjs_context_get_const_string(cx, property_name),
+ value, getter, setter, flags);
+}
+
static void
throw_property_lookup_error(JSContext *cx,
JS::HandleObject obj,
@@ -194,11 +237,11 @@ gjs_object_require_property(JSContext *context,
}
bool
-gjs_object_require_property_value(JSContext *cx,
- JS::HandleObject obj,
- const char *description,
- JS::HandleId property_name,
- bool *value)
+gjs_object_require_property(JSContext *cx,
+ JS::HandleObject obj,
+ const char *description,
+ JS::HandleId property_name,
+ bool *value)
{
JS::RootedValue prop_value(cx);
if (JS_GetPropertyById(cx, obj, property_name, &prop_value) &&
@@ -213,11 +256,11 @@ gjs_object_require_property_value(JSContext *cx,
}
bool
-gjs_object_require_property_value(JSContext *cx,
- JS::HandleObject obj,
- const char *description,
- JS::HandleId property_name,
- int32_t *value)
+gjs_object_require_property(JSContext *cx,
+ JS::HandleObject obj,
+ const char *description,
+ JS::HandleId property_name,
+ int32_t *value)
{
JS::RootedValue prop_value(cx);
if (JS_GetPropertyById(cx, obj, property_name, &prop_value) &&
@@ -233,11 +276,11 @@ gjs_object_require_property_value(JSContext *cx,
/* Converts JS string value to UTF-8 string. value must be freed with JS_free. */
bool
-gjs_object_require_property_value(JSContext *cx,
- JS::HandleObject obj,
- const char *description,
- JS::HandleId property_name,
- char **value)
+gjs_object_require_property(JSContext *cx,
+ JS::HandleObject obj,
+ const char *description,
+ JS::HandleId property_name,
+ char **value)
{
JS::RootedValue prop_value(cx);
if (JS_GetPropertyById(cx, obj, property_name, &prop_value) &&
@@ -251,11 +294,11 @@ gjs_object_require_property_value(JSContext *cx,
}
bool
-gjs_object_require_property_value(JSContext *cx,
- JS::HandleObject obj,
- const char *description,
- JS::HandleId property_name,
- JS::MutableHandleObject value)
+gjs_object_require_property(JSContext *cx,
+ JS::HandleObject obj,
+ const char *description,
+ JS::HandleId property_name,
+ JS::MutableHandleObject value)
{
JS::RootedValue prop_value(cx);
if (JS_GetPropertyById(cx, obj, property_name, &prop_value) &&
@@ -270,11 +313,11 @@ gjs_object_require_property_value(JSContext *cx,
}
bool
-gjs_object_require_converted_property_value(JSContext *cx,
- JS::HandleObject obj,
- const char *description,
- JS::HandleId property_name,
- uint32_t *value)
+gjs_object_require_converted_property(JSContext *cx,
+ JS::HandleObject obj,
+ const char *description,
+ JS::HandleId property_name,
+ uint32_t *value)
{
JS::RootedValue prop_value(cx);
if (JS_GetPropertyById(cx, obj, property_name, &prop_value) &&
@@ -303,8 +346,8 @@ gjs_throw_abstract_constructor_error(JSContext *context,
JS::RootedObject callee(context, &args.callee());
JS::RootedValue prototype(context);
- if (gjs_object_get_property_const(context, callee,
- GJS_STRING_PROTOTYPE, &prototype)) {
+ if (gjs_object_get_property(context, callee, GJS_STRING_PROTOTYPE,
+ &prototype)) {
proto_class = JS_GetClass(&prototype.toObject());
name = proto_class->name;
}
@@ -543,8 +586,8 @@ gjs_log_exception_full(JSContext *context,
JS::RootedValue js_name(context);
char *utf8_name;
- if (gjs_object_get_property_const(context, exc_obj,
- GJS_STRING_NAME, &js_name) &&
+ if (gjs_object_get_property(context, exc_obj,
+ GJS_STRING_NAME, &js_name) &&
js_name.isString() &&
gjs_string_to_utf8(context, js_name, &utf8_name)) {
is_syntax = strcmp("SyntaxError", utf8_name) == 0;
@@ -569,10 +612,10 @@ gjs_log_exception_full(JSContext *context,
unsigned lineNumber;
char *utf8_fileName;
- gjs_object_get_property_const(context, exc_obj, GJS_STRING_LINE_NUMBER,
- &js_lineNumber);
- gjs_object_get_property_const(context, exc_obj, GJS_STRING_FILENAME,
- &js_fileName);
+ gjs_object_get_property(context, exc_obj, GJS_STRING_LINE_NUMBER,
+ &js_lineNumber);
+ gjs_object_get_property(context, exc_obj, GJS_STRING_FILENAME,
+ &js_fileName);
if (js_fileName.isString())
gjs_string_to_utf8(context, js_fileName, &utf8_fileName);
@@ -595,7 +638,8 @@ gjs_log_exception_full(JSContext *context,
JS::RootedValue stack(context);
if (exc.isObject() &&
- gjs_object_get_property_const(context, exc_obj, GJS_STRING_STACK, &stack) &&
+ gjs_object_get_property(context, exc_obj, GJS_STRING_STACK,
+ &stack) &&
stack.isString())
gjs_string_to_utf8(context, stack, &utf8_stack);
else
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index c26e40f..1fc33d8 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -294,19 +294,6 @@ void gjs_set_global_slot (JSContext *context,
GjsGlobalSlot slot,
JS::Value value);
-bool gjs_object_require_property(JSContext *context,
- JS::HandleObject obj,
- const char *obj_description,
- JS::HandleId property_name,
- JS::MutableHandleValue value);
-
-/* This is intended to be overloaded with more types as the opportunity arises */
-bool gjs_object_require_converted_property_value(JSContext *context,
- JS::HandleObject obj,
- const char *description,
- JS::HandleId property_name,
- uint32_t *value);
-
bool gjs_init_class_dynamic(JSContext *context,
JS::HandleObject in_object,
JS::HandleObject parent_proto,
@@ -471,42 +458,100 @@ typedef enum {
jsid gjs_context_get_const_string (JSContext *context,
GjsConstString string);
-bool gjs_object_get_property_const(JSContext *cx,
- JS::HandleObject obj,
- GjsConstString property_name,
- JS::MutableHandleValue value_p);
-
const char * gjs_strip_unix_shebang(const char *script,
gssize *script_len,
int *new_start_line_number);
+/* These four functions wrap JS_GetPropertyById(), etc., but with a
+ * GjsConstString constant instead of a jsid. */
+
+bool gjs_object_get_property(JSContext *cx,
+ JS::HandleObject obj,
+ GjsConstString property_name,
+ JS::MutableHandleValue value_p);
+
+bool gjs_object_set_property(JSContext *cx,
+ JS::HandleObject obj,
+ GjsConstString property_name,
+ JS::HandleValue value);
+
+bool gjs_object_has_property(JSContext *cx,
+ JS::HandleObject obj,
+ GjsConstString property_name,
+ bool *found);
+
+bool gjs_object_define_property(JSContext *cx,
+ JS::HandleObject obj,
+ GjsConstString property_name,
+ JS::HandleValue value,
+ unsigned flags,
+ JSPropertyOp getter = nullptr,
+ JSStrictPropertyOp setter = nullptr);
+
G_END_DECLS
/* Overloaded functions, must be outside G_DECLS. More types are intended to be
* added as the opportunity arises. */
-bool gjs_object_require_property_value(JSContext *cx,
- JS::HandleObject obj,
- const char *description,
- JS::HandleId property_name,
- bool *value);
-
-bool gjs_object_require_property_value(JSContext *cx,
- JS::HandleObject obj,
- const char *description,
- JS::HandleId property_name,
- int32_t *value);
-
-bool gjs_object_require_property_value(JSContext *cx,
- JS::HandleObject obj,
- const char *description,
- JS::HandleId property_name,
- char **value);
-
-bool gjs_object_require_property_value(JSContext *cx,
- JS::HandleObject obj,
- const char *description,
- JS::HandleId property_name,
- JS::MutableHandleObject value);
+bool gjs_object_require_property(JSContext *context,
+ JS::HandleObject obj,
+ const char *obj_description,
+ JS::HandleId property_name,
+ JS::MutableHandleValue value);
+
+bool gjs_object_require_property(JSContext *cx,
+ JS::HandleObject obj,
+ const char *description,
+ JS::HandleId property_name,
+ bool *value);
+
+bool gjs_object_require_property(JSContext *cx,
+ JS::HandleObject obj,
+ const char *description,
+ JS::HandleId property_name,
+ int32_t *value);
+
+bool gjs_object_require_property(JSContext *cx,
+ JS::HandleObject obj,
+ const char *description,
+ JS::HandleId property_name,
+ char **value);
+
+bool gjs_object_require_property(JSContext *cx,
+ JS::HandleObject obj,
+ const char *description,
+ JS::HandleId property_name,
+ JS::MutableHandleObject value);
+
+bool gjs_object_require_converted_property(JSContext *context,
+ JS::HandleObject obj,
+ const char *description,
+ JS::HandleId property_name,
+ uint32_t *value);
+
+/* Here, too, we have wrappers that take a GjsConstString. */
+
+template<typename T>
+bool gjs_object_require_property(JSContext *cx,
+ JS::HandleObject obj,
+ const char *description,
+ GjsConstString property_name,
+ T value)
+{
+ JS::RootedId id(cx, gjs_context_get_const_string(cx, property_name));
+ return gjs_object_require_property(cx, obj, description, id, value);
+}
+
+template<typename T>
+bool gjs_object_require_converted_property(JSContext *cx,
+ JS::HandleObject obj,
+ const char *description,
+ GjsConstString property_name,
+ T value)
+{
+ JS::RootedId id(cx, gjs_context_get_const_string(cx, property_name));
+ return gjs_object_require_converted_property(cx, obj, description,
+ id, value);
+}
#endif /* __GJS_JSAPI_UTIL_H__ */
diff --git a/gjs/stack.cpp b/gjs/stack.cpp
index 7dba9b1..3dceac2 100644
--- a/gjs/stack.cpp
+++ b/gjs/stack.cpp
@@ -59,26 +59,26 @@ gjs_context_get_frame_info(JSContext *context,
JSAutoCompartment ac(context, global);
JS::RootedId error_id(context, gjs_intern_string_to_id(context, "Error"));
- if (!gjs_object_require_property_value(context, global, "global object",
- error_id, &constructor))
+ if (!gjs_object_require_property(context, global, "global object",
+ error_id, &constructor))
return false;
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,
- stack.ref()))
+ !gjs_object_get_property(context, err_obj, GJS_STRING_STACK,
+ stack.ref()))
return false;
if (!fileName.empty() &&
- !gjs_object_get_property_const(context, err_obj, GJS_STRING_FILENAME,
- fileName.ref()))
+ !gjs_object_get_property(context, err_obj, GJS_STRING_FILENAME,
+ fileName.ref()))
return false;
if (!lineNumber.empty() &&
- !gjs_object_get_property_const(context, err_obj, GJS_STRING_LINE_NUMBER,
- lineNumber.ref()))
+ !gjs_object_get_property(context, err_obj, GJS_STRING_LINE_NUMBER,
+ lineNumber.ref()))
return false;
return true;
diff --git a/modules/cairo-region.cpp b/modules/cairo-region.cpp
index 39ff07e..676b1b7 100644
--- a/modules/cairo-region.cpp
+++ b/modules/cairo-region.cpp
@@ -120,22 +120,22 @@ fill_rectangle(JSContext *context,
{
JS::RootedValue val(context);
- if (!gjs_object_get_property_const(context, obj, GJS_STRING_X, &val))
+ if (!gjs_object_get_property(context, obj, GJS_STRING_X, &val))
return false;
if (!JS::ToInt32(context, val, &rect->x))
return false;
- if (!gjs_object_get_property_const(context, obj, GJS_STRING_Y, &val))
+ if (!gjs_object_get_property(context, obj, GJS_STRING_Y, &val))
return false;
if (!JS::ToInt32(context, val, &rect->y))
return false;
- if (!gjs_object_get_property_const(context, obj, GJS_STRING_WIDTH, &val))
+ if (!gjs_object_get_property(context, obj, GJS_STRING_WIDTH, &val))
return false;
if (!JS::ToInt32(context, val, &rect->width))
return false;
- if (!gjs_object_get_property_const(context, obj, GJS_STRING_HEIGHT, &val))
+ if (!gjs_object_get_property(context, obj, GJS_STRING_HEIGHT, &val))
return false;
if (!JS::ToInt32(context, val, &rect->height))
return false;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]