[gjs: 7/9] jsapi-util: Remove gjs_get_type_name()
- From: Cosimo Cecchi <cosimoc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs: 7/9] jsapi-util: Remove gjs_get_type_name()
- Date: Sat, 12 May 2018 17:40:20 +0000 (UTC)
commit 212001838c27544efd7d64a5e73fb2b310a11045
Author: Philip Chimento <philip chimento gmail com>
Date: Thu May 10 00:12:48 2018 -0700
jsapi-util: Remove gjs_get_type_name()
There's an almost identical function in SpiderMonkey,
JS::InformalValueTypeName(). The only difference is it prints out the
internal JSClass name for an object, but since we are only using the
string in debug messages anyway, that's fine.
Cleans up some bits of duplicated code in value.cpp.
gi/arg.cpp | 6 +--
gi/function.cpp | 2 +-
gi/value.cpp | 106 ++++++++++++++++-------------------------------------
gjs/jsapi-util.cpp | 23 ------------
gjs/jsapi-util.h | 2 -
5 files changed, 35 insertions(+), 104 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index cec0e9a3..ba1db1a4 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -1244,7 +1244,7 @@ throw_invalid_argument(JSContext *context,
gjs_throw(context, "Expected type %s for %s but got type '%s'",
type_tag_to_human_string(arginfo),
- display_name, gjs_get_type_name(value));
+ display_name, JS::InformalValueTypeName(value));
g_free(display_name);
}
@@ -1796,7 +1796,7 @@ gjs_value_to_g_argument(JSContext *context,
if (arg->v_pointer == NULL) {
gjs_debug(GJS_DEBUG_GFUNCTION,
"conversion of JSObject %p type %s to type %s failed",
- &value.toObject(), gjs_get_type_name(value),
+ &value.toObject(), JS::InformalValueTypeName(value),
g_base_info_get_name ((GIBaseInfo *)interface_info));
/* gjs_throw should have been called already */
@@ -1836,7 +1836,7 @@ gjs_value_to_g_argument(JSContext *context,
} else {
gjs_debug(GJS_DEBUG_GFUNCTION,
"JSObject type '%s' is neither null nor an object",
- gjs_get_type_name(value));
+ JS::InformalValueTypeName(value));
wrong = true;
report_type_mismatch = true;
}
diff --git a/gi/function.cpp b/gi/function.cpp
index 94129802..06014f29 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -944,7 +944,7 @@ gjs_invoke_c_function(JSContext *context,
g_base_info_get_namespace( (GIBaseInfo*) function->info),
g_base_info_get_name( (GIBaseInfo*) function->info),
g_base_info_get_name( (GIBaseInfo*) &arg_info),
- gjs_get_type_name(current_arg));
+ JS::InformalValueTypeName(current_arg));
failed = true;
break;
}
diff --git a/gi/value.cpp b/gi/value.cpp
index 861b00f3..d93935f3 100644
--- a/gi/value.cpp
+++ b/gi/value.cpp
@@ -342,6 +342,19 @@ gjs_value_guess_g_type(JSContext *context,
return G_TYPE_INVALID;
}
+static inline bool
+throw_expect_type(JSContext *cx,
+ JS::HandleValue value,
+ const char *expected_type,
+ GType gtype = 0)
+{
+ gjs_throw(cx, "Wrong type %s; %s%s%s expected",
+ JS::InformalValueTypeName(value), expected_type,
+ gtype ? " " : "",
+ gtype ? g_type_name(gtype) : "");
+ return false; /* for convenience */
+}
+
static bool
gjs_value_to_g_value_internal(JSContext *context,
JS::HandleValue value,
@@ -386,70 +399,49 @@ gjs_value_to_g_value_internal(JSContext *context,
g_value_take_string(gvalue, utf8_string.copy());
} else {
- gjs_throw(context,
- "Wrong type %s; string expected",
- gjs_get_type_name(value));
- return false;
+ return throw_expect_type(context, value, "string");
}
} else if (gtype == G_TYPE_CHAR) {
gint32 i;
if (JS::ToInt32(context, value, &i) && i >= SCHAR_MIN && i <= SCHAR_MAX) {
g_value_set_schar(gvalue, (signed char)i);
} else {
- gjs_throw(context,
- "Wrong type %s; char expected",
- gjs_get_type_name(value));
- return false;
+ return throw_expect_type(context, value, "char");
}
} else if (gtype == G_TYPE_UCHAR) {
guint16 i;
if (JS::ToUint16(context, value, &i) && i <= UCHAR_MAX) {
g_value_set_uchar(gvalue, (unsigned char)i);
} else {
- gjs_throw(context,
- "Wrong type %s; unsigned char expected",
- gjs_get_type_name(value));
- return false;
+ return throw_expect_type(context, value, "unsigned char");
}
} else if (gtype == G_TYPE_INT) {
gint32 i;
if (JS::ToInt32(context, value, &i)) {
g_value_set_int(gvalue, i);
} else {
- gjs_throw(context,
- "Wrong type %s; integer expected",
- gjs_get_type_name(value));
- return false;
+ return throw_expect_type(context, value, "integer");
}
} else if (gtype == G_TYPE_DOUBLE) {
gdouble d;
if (JS::ToNumber(context, value, &d)) {
g_value_set_double(gvalue, d);
} else {
- gjs_throw(context,
- "Wrong type %s; double expected",
- gjs_get_type_name(value));
- return false;
+ return throw_expect_type(context, value, "double");
}
} else if (gtype == G_TYPE_FLOAT) {
gdouble d;
if (JS::ToNumber(context, value, &d)) {
g_value_set_float(gvalue, d);
} else {
- gjs_throw(context,
- "Wrong type %s; float expected",
- gjs_get_type_name(value));
- return false;
+ return throw_expect_type(context, value, "float");
}
} else if (gtype == G_TYPE_UINT) {
guint32 i;
if (JS::ToUint32(context, value, &i)) {
g_value_set_uint(gvalue, i);
} else {
- gjs_throw(context,
- "Wrong type %s; unsigned integer expected",
- gjs_get_type_name(value));
- return false;
+ return throw_expect_type(context, value, "unsigned integer");
}
} else if (gtype == G_TYPE_BOOLEAN) {
/* JS::ToBoolean() can't fail */
@@ -468,11 +460,7 @@ gjs_value_to_g_value_internal(JSContext *context,
gobj = gjs_g_object_from_object(context, obj);
} else {
- gjs_throw(context,
- "Wrong type %s; object %s expected",
- gjs_get_type_name(value),
- g_type_name(gtype));
- return false;
+ return throw_expect_type(context, value, "object", gtype);
}
g_value_set_object(gvalue, gobj);
@@ -493,10 +481,7 @@ gjs_value_to_g_value_internal(JSContext *context,
GJS_STRING_LENGTH,
&length)) {
JS_ClearPendingException(context);
- gjs_throw(context,
- "Wrong type %s; strv expected",
- gjs_get_type_name(value));
- return false;
+ return throw_expect_type(context, value, "strv");
} else {
void *result;
char **strv;
@@ -510,10 +495,7 @@ gjs_value_to_g_value_internal(JSContext *context,
g_value_take_boxed (gvalue, strv);
}
} else {
- gjs_throw(context,
- "Wrong type %s; strv expected",
- gjs_get_type_name(value));
- return false;
+ return throw_expect_type(context, value, "strv");
}
}
} else if (g_type_is_a(gtype, G_TYPE_BOXED)) {
@@ -587,11 +569,7 @@ gjs_value_to_g_value_internal(JSContext *context,
}
}
} else {
- gjs_throw(context,
- "Wrong type %s; boxed type %s expected",
- gjs_get_type_name(value),
- g_type_name(gtype));
- return false;
+ return throw_expect_type(context, value, "boxed type", gtype);
}
if (no_copy)
@@ -611,11 +589,7 @@ gjs_value_to_g_value_internal(JSContext *context,
variant = (GVariant*) gjs_c_struct_from_boxed(context, obj);
} else {
- gjs_throw(context,
- "Wrong type %s; boxed type %s expected",
- gjs_get_type_name(value),
- g_type_name(gtype));
- return false;
+ return throw_expect_type(context, value, "boxed type", gtype);
}
g_value_set_variant (gvalue, variant);
@@ -639,11 +613,7 @@ gjs_value_to_g_value_internal(JSContext *context,
g_value_set_enum(gvalue, v->value);
} else {
- gjs_throw(context,
- "Wrong type %s; enum %s expected",
- gjs_get_type_name(value),
- g_type_name(gtype));
- return false;
+ return throw_expect_type(context, value, "enum", gtype);
}
} else if (g_type_is_a(gtype, G_TYPE_FLAGS)) {
int64_t value_int64;
@@ -655,11 +625,7 @@ gjs_value_to_g_value_internal(JSContext *context,
/* See arg.c:_gjs_enum_to_int() */
g_value_set_flags(gvalue, (int)value_int64);
} else {
- gjs_throw(context,
- "Wrong type %s; flags %s expected",
- gjs_get_type_name(value),
- g_type_name(gtype));
- return false;
+ return throw_expect_type(context, value, "flags", gtype);
}
} else if (g_type_is_a(gtype, G_TYPE_PARAM)) {
void *gparam;
@@ -675,22 +641,15 @@ gjs_value_to_g_value_internal(JSContext *context,
gparam = gjs_g_param_from_param(context, obj);
} else {
- gjs_throw(context,
- "Wrong type %s; param type %s expected",
- gjs_get_type_name(value),
- g_type_name(gtype));
- return false;
+ return throw_expect_type(context, value, "param type", gtype);
}
g_value_set_param(gvalue, (GParamSpec*) gparam);
} else if (g_type_is_a(gtype, G_TYPE_GTYPE)) {
GType type;
- if (!value.isObject()) {
- gjs_throw(context, "Wrong type %s; expect a GType object",
- gjs_get_type_name(value));
- return false;
- }
+ if (!value.isObject())
+ return throw_expect_type(context, value, "GType object");
JS::RootedObject obj(context, &value.toObject());
type = gjs_gtype_get_actual_gtype(context, obj);
@@ -716,10 +675,7 @@ gjs_value_to_g_value_internal(JSContext *context,
g_value_set_int(&int_value, i);
g_value_transform(&int_value, gvalue);
} else {
- gjs_throw(context,
- "Wrong type %s; integer expected",
- gjs_get_type_name(value));
- return false;
+ return throw_expect_type(context, value, "integer");
}
} else {
gjs_debug(GJS_DEBUG_GCLOSURE, "JS::Value is number %d gtype fundamental %d transformable to int %d
from int %d",
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 3f51aac5..f8723ea2 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -638,29 +638,6 @@ gjs_call_function_value(JSContext *context,
return result;
}
-/* get a debug string for type tag in JS::Value */
-const char*
-gjs_get_type_name(JS::Value value)
-{
- if (value.isNull()) {
- return "null";
- } else if (value.isUndefined()) {
- return "undefined";
- } else if (value.isInt32()) {
- return "integer";
- } else if (value.isDouble()) {
- return "double";
- } else if (value.isBoolean()) {
- return "boolean";
- } else if (value.isString()) {
- return "string";
- } else if (value.isObject()) {
- return "object";
- } else {
- return "<unknown>";
- }
-}
-
#ifdef __linux__
static void
_linux_get_self_process_size (gulong *vm_size,
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 881a221d..869e962d 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -219,8 +219,6 @@ bool gjs_unichar_from_string (JSContext *context,
JS::Value string,
gunichar *result);
-const char* gjs_get_type_name (JS::Value value);
-
/* Functions intended for more "internal" use */
void gjs_maybe_gc (JSContext *context);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]