[gjs: 2/7] error: Use JSProtoKey to get Error constructor
- From: Gitlab System User <gitlab src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs: 2/7] error: Use JSProtoKey to get Error constructor
- Date: Wed, 4 Oct 2017 16:26:58 +0000 (UTC)
commit 6b95d85f7ccddcf8a73493a921edac96fe1033be
Author: Philip Chimento <philip chimento gmail com>
Date: Mon Sep 18 00:05:24 2017 -0700
error: Use JSProtoKey to get Error constructor
Instead of looking up the Error prototype by name on the global object
when we want to throw an error, use JS_GetClassObject and JSProtoKey to
avoid the conversion of string to ID and the property lookup.
gi/boxed.cpp | 6 +++---
gi/function.cpp | 2 +-
gi/fundamental.cpp | 4 ++--
gi/object.cpp | 4 ++--
gi/param.cpp | 4 ++--
gi/union.cpp | 6 +++---
gjs/importer.cpp | 2 +-
gjs/jsapi-dynamic-class.cpp | 2 +-
gjs/jsapi-util-error.cpp | 25 +++++++++++++------------
gjs/jsapi-util.h | 2 +-
10 files changed, 29 insertions(+), 28 deletions(-)
---
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index 3924bc4..fa8d6c3 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -1284,7 +1284,7 @@ gjs_typecheck_boxed(JSContext *context,
if (priv->gboxed == NULL) {
if (throw_error) {
- gjs_throw_custom(context, "TypeError", NULL,
+ gjs_throw_custom(context, JSProto_TypeError, nullptr,
"Object is %s.%s.prototype, not an object instance - cannot convert to a boxed
instance",
g_base_info_get_namespace( (GIBaseInfo*) priv->info),
g_base_info_get_name( (GIBaseInfo*) priv->info));
@@ -1302,14 +1302,14 @@ gjs_typecheck_boxed(JSContext *context,
if (!result && throw_error) {
if (expected_info != NULL) {
- gjs_throw_custom(context, "TypeError", NULL,
+ gjs_throw_custom(context, JSProto_TypeError, nullptr,
"Object is of type %s.%s - cannot convert to %s.%s",
g_base_info_get_namespace((GIBaseInfo*) priv->info),
g_base_info_get_name((GIBaseInfo*) priv->info),
g_base_info_get_namespace((GIBaseInfo*) expected_info),
g_base_info_get_name((GIBaseInfo*) expected_info));
} else {
- gjs_throw_custom(context, "TypeError", NULL,
+ gjs_throw_custom(context, JSProto_TypeError, nullptr,
"Object is of type %s.%s - cannot convert to %s",
g_base_info_get_namespace((GIBaseInfo*) priv->info),
g_base_info_get_name((GIBaseInfo*) priv->info),
diff --git a/gi/function.cpp b/gi/function.cpp
index 876cc90..b54cc8d 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -699,7 +699,7 @@ gjs_fill_method_instance(JSContext *context,
if (transfer == GI_TRANSFER_EVERYTHING)
gjs_fundamental_ref (context, out_arg->v_pointer);
} else {
- gjs_throw_custom(context, "TypeError", NULL,
+ gjs_throw_custom(context, JSProto_TypeError, nullptr,
"%s.%s is not an object instance neither a fundamental instance of a supported
type",
g_base_info_get_namespace(container),
g_base_info_get_name(container));
diff --git a/gi/fundamental.cpp b/gi/fundamental.cpp
index 5f928ce..9379330 100644
--- a/gi/fundamental.cpp
+++ b/gi/fundamental.cpp
@@ -870,13 +870,13 @@ gjs_typecheck_fundamental(JSContext *context,
if (!result && throw_error) {
if (priv->prototype->info) {
- gjs_throw_custom(context, "TypeError", NULL,
+ gjs_throw_custom(context, JSProto_TypeError, nullptr,
"Object is of type %s.%s - cannot convert to %s",
g_base_info_get_namespace((GIBaseInfo *) priv->prototype->info),
g_base_info_get_name((GIBaseInfo *) priv->prototype->info),
g_type_name(expected_gtype));
} else {
- gjs_throw_custom(context, "TypeError", NULL,
+ gjs_throw_custom(context, JSProto_TypeError, nullptr,
"Object is of type %s - cannot convert to %s",
g_type_name(priv->prototype->gtype),
g_type_name(expected_gtype));
diff --git a/gi/object.cpp b/gi/object.cpp
index 3694a84..7126461 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -2145,13 +2145,13 @@ gjs_typecheck_object(JSContext *context,
if (!result && throw_error) {
if (priv->info) {
- gjs_throw_custom(context, "TypeError", NULL,
+ gjs_throw_custom(context, JSProto_TypeError, nullptr,
"Object is of type %s.%s - cannot convert to %s",
g_base_info_get_namespace((GIBaseInfo*) priv->info),
g_base_info_get_name((GIBaseInfo*) priv->info),
g_type_name(expected_type));
} else {
- gjs_throw_custom(context, "TypeError", NULL,
+ gjs_throw_custom(context, JSProto_TypeError, nullptr,
"Object is of type %s - cannot convert to %s",
g_type_name(priv->gtype),
g_type_name(expected_type));
diff --git a/gi/param.cpp b/gi/param.cpp
index dbfde0b..4a11142 100644
--- a/gi/param.cpp
+++ b/gi/param.cpp
@@ -304,7 +304,7 @@ gjs_typecheck_param(JSContext *context,
if (priv->gparam == NULL) {
if (throw_error) {
- gjs_throw_custom(context, "TypeError", NULL,
+ gjs_throw_custom(context, JSProto_TypeError, nullptr,
"Object is GObject.ParamSpec.prototype, not an object instance - "
"cannot convert to a GObject.ParamSpec instance");
}
@@ -318,7 +318,7 @@ gjs_typecheck_param(JSContext *context,
result = true;
if (!result && throw_error) {
- gjs_throw_custom(context, "TypeError", NULL,
+ gjs_throw_custom(context, JSProto_TypeError, nullptr,
"Object is of type %s - cannot convert to %s",
g_type_name(G_TYPE_FROM_INSTANCE (priv->gparam)),
g_type_name(expected_type));
diff --git a/gi/union.cpp b/gi/union.cpp
index b3cbf4a..0168914 100644
--- a/gi/union.cpp
+++ b/gi/union.cpp
@@ -447,7 +447,7 @@ gjs_typecheck_union(JSContext *context,
if (priv->gboxed == NULL) {
if (throw_error) {
- gjs_throw_custom(context, "TypeError", NULL,
+ gjs_throw_custom(context, JSProto_TypeError, nullptr,
"Object is %s.%s.prototype, not an object instance - cannot convert to a union
instance",
g_base_info_get_namespace( (GIBaseInfo*) priv->info),
g_base_info_get_name( (GIBaseInfo*) priv->info));
@@ -465,14 +465,14 @@ gjs_typecheck_union(JSContext *context,
if (!result && throw_error) {
if (expected_info != NULL) {
- gjs_throw_custom(context, "TypeError", NULL,
+ gjs_throw_custom(context, JSProto_TypeError, nullptr,
"Object is of type %s.%s - cannot convert to %s.%s",
g_base_info_get_namespace((GIBaseInfo*) priv->info),
g_base_info_get_name((GIBaseInfo*) priv->info),
g_base_info_get_namespace((GIBaseInfo*) expected_info),
g_base_info_get_name((GIBaseInfo*) expected_info));
} else {
- gjs_throw_custom(context, "TypeError", NULL,
+ gjs_throw_custom(context, JSProto_TypeError, nullptr,
"Object is of type %s.%s - cannot convert to %s",
g_base_info_get_namespace((GIBaseInfo*) priv->info),
g_base_info_get_name((GIBaseInfo*) priv->info),
diff --git a/gjs/importer.cpp b/gjs/importer.cpp
index 6fd886f..4f8034f 100644
--- a/gjs/importer.cpp
+++ b/gjs/importer.cpp
@@ -626,7 +626,7 @@ do_import(JSContext *context,
/* If no exception occurred, the problem is just that we got to the
* end of the path. Be sure an exception is set.
*/
- gjs_throw_custom(context, "Error", "ImportError",
+ gjs_throw_custom(context, JSProto_Error, "ImportError",
"No JS module '%s' found in search path", name);
}
diff --git a/gjs/jsapi-dynamic-class.cpp b/gjs/jsapi-dynamic-class.cpp
index a660c19..f070dee 100644
--- a/gjs/jsapi-dynamic-class.cpp
+++ b/gjs/jsapi-dynamic-class.cpp
@@ -166,7 +166,7 @@ gjs_typecheck_instance(JSContext *context,
if (throw_error) {
const JSClass *obj_class = JS_GetClass(obj);
- gjs_throw_custom(context, "TypeError", NULL,
+ gjs_throw_custom(context, JSProto_TypeError, nullptr,
"Object %p is not a subclass of %s, it's a %s",
obj.get(), static_clasp->name,
format_dynamic_class_name(obj_class->name));
diff --git a/gjs/jsapi-util-error.cpp b/gjs/jsapi-util-error.cpp
index 0af708d..9ee8e71 100644
--- a/gjs/jsapi-util-error.cpp
+++ b/gjs/jsapi-util-error.cpp
@@ -44,7 +44,7 @@
static void
G_GNUC_PRINTF(4, 0)
gjs_throw_valist(JSContext *context,
- const char *error_class,
+ JSProtoKey error_kind,
const char *error_name,
const char *format,
va_list args)
@@ -87,14 +87,10 @@ gjs_throw_valist(JSContext *context,
goto out;
}
- if (!JS_GetProperty(context, global, error_class, &v_constructor) ||
- !v_constructor.isObject()) {
- JS_ReportErrorUTF8(context, "??? Missing Error constructor in global object?");
+ if (!JS_GetClassObject(context, error_kind, &constructor))
goto out;
- }
/* throw new Error(message) */
- constructor = &v_constructor.toObject();
new_exc = JS_New(context, constructor, error_args);
if (!new_exc)
@@ -141,7 +137,7 @@ gjs_throw(JSContext *context,
va_list args;
va_start(args, format);
- gjs_throw_valist(context, "Error", NULL, format, args);
+ gjs_throw_valist(context, JSProto_Error, nullptr, format, args);
va_end(args);
}
@@ -151,16 +147,21 @@ gjs_throw(JSContext *context,
* error.
*/
void
-gjs_throw_custom(JSContext *context,
- const char *error_class,
- const char *error_name,
- const char *format,
+gjs_throw_custom(JSContext *cx,
+ JSProtoKey kind,
+ const char *error_name,
+ const char *format,
...)
{
va_list args;
+ g_return_if_fail(kind == JSProto_Error || kind == JSProto_InternalError ||
+ kind == JSProto_EvalError || kind == JSProto_RangeError ||
+ kind == JSProto_ReferenceError || kind == JSProto_SyntaxError ||
+ kind == JSProto_TypeError || kind == JSProto_URIError ||
+ kind == JSProto_StopIteration);
va_start(args, format);
- gjs_throw_valist(context, error_class, error_name, format, args);
+ gjs_throw_valist(cx, kind, error_name, format, args);
va_end(args);
}
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index cb90a2f..41ee04a 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -180,7 +180,7 @@ void gjs_throw (JSContext *context,
const char *format,
...) G_GNUC_PRINTF (2, 3);
void gjs_throw_custom (JSContext *context,
- const char *error_class,
+ JSProtoKey error_kind,
const char *error_name,
const char *format,
...) G_GNUC_PRINTF (4, 5);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]