[gjs] Add a convenience API for const property access
- From: Giovanni Campagna <gcampagna src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs] Add a convenience API for const property access
- Date: Tue, 23 Apr 2013 20:56:09 +0000 (UTC)
commit d692f8e064757cd6e9134f9c9b8b0a1a6478b7dc
Author: Giovanni Campagna <gcampagna src gnome org>
Date: Fri Apr 19 17:05:23 2013 +0200
Add a convenience API for const property access
Rather than copy pasting everywhere gjs_runtime_get_const_string()
followed by JS_GetPropertyById, add a function that wraps it
using an declaration similar to JS_GetProperty().
https://bugzilla.gnome.org/show_bug.cgi?id=697592
gi/boxed.c | 5 +----
gi/gerror.c | 5 +----
gi/interface.c | 5 +----
gi/object.c | 6 +-----
gi/param.c | 6 +-----
gi/union.c | 6 +-----
gjs/gjs-module.h | 1 +
gjs/jsapi-dynamic-class.c | 6 +-----
gjs/jsapi-util.c | 7 ++-----
gjs/jsapi-util.h | 7 +++++++
gjs/native.c | 5 +----
gjs/runtime.c | 14 ++++++++++++++
12 files changed, 32 insertions(+), 41 deletions(-)
---
diff --git a/gi/boxed.c b/gi/boxed.c
index 5db3c70..191b6f8 100644
--- a/gi/boxed.c
+++ b/gi/boxed.c
@@ -1209,7 +1209,6 @@ gjs_define_boxed_class(JSContext *context,
return JS_FALSE;
if (!JSVAL_IS_VOID(value)) {
JSObject *constructor;
- jsid prototype_name;
if (!JSVAL_IS_OBJECT(value)) {
gjs_throw(context, "Existing property '%s' does not look like a constructor",
@@ -1219,9 +1218,7 @@ gjs_define_boxed_class(JSContext *context,
constructor = JSVAL_TO_OBJECT(value);
- prototype_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
- GJS_STRING_PROTOTYPE);
- JS_GetPropertyById(context, constructor, prototype_name, &value);
+ gjs_object_get_property_const(context, constructor, GJS_STRING_PROTOTYPE, &value);
if (!JSVAL_IS_OBJECT(value)) {
gjs_throw(context, "boxed %s prototype property does not appear to exist or has wrong type",
constructor_name);
return JS_FALSE;
diff --git a/gi/gerror.c b/gi/gerror.c
index 74d93ce..1be6b5a 100644
--- a/gi/gerror.c
+++ b/gi/gerror.c
@@ -428,7 +428,6 @@ gjs_define_error_class(JSContext *context,
return JS_FALSE;
if (!JSVAL_IS_VOID(value)) {
JSObject *constructor;
- jsid prototype_name;
if (!JSVAL_IS_OBJECT(value)) {
gjs_throw(context, "Existing property '%s' does not look like a constructor",
@@ -438,9 +437,7 @@ gjs_define_error_class(JSContext *context,
constructor = JSVAL_TO_OBJECT(value);
- prototype_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
- GJS_STRING_PROTOTYPE);
- JS_GetPropertyById(context, constructor, prototype_name, &value);
+ gjs_object_get_property_const(context, constructor, GJS_STRING_PROTOTYPE, &value);
if (!JSVAL_IS_OBJECT(value)) {
gjs_throw(context, "error %s prototype property does not appear to exist or has wrong type",
constructor_name);
return JS_FALSE;
diff --git a/gi/interface.c b/gi/interface.c
index 7188999..98be8e5 100644
--- a/gi/interface.c
+++ b/gi/interface.c
@@ -185,7 +185,6 @@ gjs_define_interface_class(JSContext *context,
return JS_FALSE;
if (!JSVAL_IS_VOID(value)) {
JSObject *constructor;
- jsid prototype_name;
if (!JSVAL_IS_OBJECT(value)) {
gjs_throw(context, "Existing property '%s' does not look like a constructor",
@@ -195,9 +194,7 @@ gjs_define_interface_class(JSContext *context,
constructor = JSVAL_TO_OBJECT(value);
- prototype_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
- GJS_STRING_PROTOTYPE);
- JS_GetPropertyById(context, constructor, prototype_name, &value);
+ gjs_object_get_property_const(context, constructor, GJS_STRING_PROTOTYPE, &value);
if (!JSVAL_IS_OBJECT(value)) {
gjs_throw(context, "prototype property does not appear to exist or has wrong type");
return JS_FALSE;
diff --git a/gi/object.c b/gi/object.c
index ca8924a..7426e31 100644
--- a/gi/object.c
+++ b/gi/object.c
@@ -1928,8 +1928,6 @@ gjs_define_object_class(JSContext *context,
return JS_FALSE;
}
if (!JSVAL_IS_VOID(value)) {
- jsid prototype_name;
-
if (!JSVAL_IS_OBJECT(value)) {
gjs_throw(context, "Existing property '%s' does not look like a constructor",
constructor_name);
@@ -1939,9 +1937,7 @@ gjs_define_object_class(JSContext *context,
constructor = JSVAL_TO_OBJECT(value);
- prototype_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
- GJS_STRING_PROTOTYPE);
- JS_GetPropertyById(context, constructor, prototype_name, &value);
+ gjs_object_get_property_const(context, constructor, GJS_STRING_PROTOTYPE, &value);
if (!JSVAL_IS_OBJECT(value)) {
gjs_throw(context, "prototype property does not appear to exist or has wrong type");
g_base_info_unref((GIBaseInfo*)info);
diff --git a/gi/param.c b/gi/param.c
index f676092..857bc46 100644
--- a/gi/param.c
+++ b/gi/param.c
@@ -490,8 +490,6 @@ gjs_define_param_class(JSContext *context,
if (!JS_GetProperty(context, in_object, constructor_name, &value))
return JS_FALSE;
if (!JSVAL_IS_VOID(value)) {
- jsid prototype_name;
-
if (!JSVAL_IS_OBJECT(value)) {
gjs_throw(context, "Existing property '%s' does not look like a constructor",
constructor_name);
@@ -500,9 +498,7 @@ gjs_define_param_class(JSContext *context,
constructor = JSVAL_TO_OBJECT(value);
- prototype_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
- GJS_STRING_PROTOTYPE);
- JS_GetPropertyById(context, constructor, prototype_name, &value);
+ gjs_object_get_property_const(context, constructor, GJS_STRING_PROTOTYPE, &value);
if (!JSVAL_IS_OBJECT(value)) {
gjs_throw(context, "prototype property does not appear to exist or has wrong type");
return JS_FALSE;
diff --git a/gi/union.c b/gi/union.c
index cfa7e59..ba811d1 100644
--- a/gi/union.c
+++ b/gi/union.c
@@ -416,8 +416,6 @@ gjs_define_union_class(JSContext *context,
if (!JS_GetProperty(context, in_object, constructor_name, &value))
return JS_FALSE;
if (!JSVAL_IS_VOID(value)) {
- jsid prototype_name;
-
if (!JSVAL_IS_OBJECT(value)) {
gjs_throw(context, "Existing property '%s' does not look like a constructor",
constructor_name);
@@ -426,9 +424,7 @@ gjs_define_union_class(JSContext *context,
constructor = JSVAL_TO_OBJECT(value);
- prototype_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
- GJS_STRING_PROTOTYPE);
- JS_GetPropertyById(context, constructor, prototype_name, &value);
+ gjs_object_get_property_const(context, constructor, GJS_STRING_PROTOTYPE, &value);
if (!JSVAL_IS_OBJECT(value)) {
gjs_throw(context, "union %s prototype property does not appear to exist or has wrong type",
constructor_name);
return JS_FALSE;
diff --git a/gjs/gjs-module.h b/gjs/gjs-module.h
index 38190be..f85f89b 100644
--- a/gjs/gjs-module.h
+++ b/gjs/gjs-module.h
@@ -28,6 +28,7 @@
#include <gjs/native.h>
#include <gjs/mem.h>
#include <gjs/importer.h>
+#include <gjs/runtime.h>
#include <gjs/jsapi-util.h>
#endif /* __GJS_GJS_umodule_H__ */
diff --git a/gjs/jsapi-dynamic-class.c b/gjs/jsapi-dynamic-class.c
index b7fe2ca..30fe98a 100644
--- a/gjs/jsapi-dynamic-class.c
+++ b/gjs/jsapi-dynamic-class.c
@@ -51,15 +51,11 @@ gjs_new_object_for_constructor(JSContext *context,
jsval callee;
JSObject *parent;
jsval prototype;
- jsid prototype_name;
callee = JS_CALLEE(context, vp);
parent = JS_GetParent(JSVAL_TO_OBJECT (callee));
- prototype_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
- GJS_STRING_PROTOTYPE);
- if (!JS_GetPropertyById(context, JSVAL_TO_OBJECT(callee), prototype_name,
- &prototype))
+ if (!gjs_object_get_property_const(context, JSVAL_TO_OBJECT(callee), GJS_STRING_PROTOTYPE, &prototype))
return NULL;
return JS_NewObjectWithGivenProto(context, clasp,
diff --git a/gjs/jsapi-util.c b/gjs/jsapi-util.c
index cfadd4f..bc09330 100644
--- a/gjs/jsapi-util.c
+++ b/gjs/jsapi-util.c
@@ -216,17 +216,14 @@ gjs_throw_abstract_constructor_error(JSContext *context,
{
jsval callee;
jsval prototype;
- jsid prototype_name;
JSClass *proto_class;
const char *name = "anonymous";
callee = JS_CALLEE(context, vp);
if (JSVAL_IS_OBJECT(callee)) {
- prototype_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
- GJS_STRING_PROTOTYPE);
- if (JS_GetPropertyById(context, JSVAL_TO_OBJECT(callee),
- prototype_name, &prototype)) {
+ if (gjs_object_get_property_const(context, JSVAL_TO_OBJECT(callee),
+ GJS_STRING_PROTOTYPE, &prototype)) {
proto_class = JS_GetClass(JSVAL_TO_OBJECT(prototype));
name = proto_class->name;
}
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 210f5ee..1e8aea6 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -29,6 +29,7 @@
#endif
#include <gjs/compat.h>
+#include <gjs/runtime.h>
#include <glib-object.h>
G_BEGIN_DECLS
@@ -200,6 +201,12 @@ gboolean gjs_object_require_property (JSContext *context,
const char *obj_description,
jsid property_name,
jsval *value_p);
+/* This one is defined in runtime.c, so the compiler can optimize the call
+ to get_const_string() it uses. */
+gboolean gjs_object_get_property_const (JSContext *context,
+ JSObject *obj,
+ GjsConstString property_name,
+ jsval *value_p);
JSObject *gjs_new_object_for_constructor (JSContext *context,
JSClass *clasp,
diff --git a/gjs/native.c b/gjs/native.c
index 01d836d..05d5936 100644
--- a/gjs/native.c
+++ b/gjs/native.c
@@ -81,11 +81,8 @@ module_get_parent(JSContext *context,
JSObject *module_obj)
{
jsval value;
- jsid parent_module_name;
- parent_module_name = gjs_runtime_get_const_string(JS_GetRuntime(context),
- GJS_STRING_PARENT_MODULE);
- if (JS_GetPropertyById(context, module_obj, parent_module_name, &value) &&
+ if (gjs_object_get_property_const(context, module_obj, GJS_STRING_PARENT_MODULE, &value) &&
!JSVAL_IS_NULL(value) &&
JSVAL_IS_OBJECT(value)) {
return JSVAL_TO_OBJECT(value);
diff --git a/gjs/runtime.c b/gjs/runtime.c
index e24ee4e..d9f658d 100644
--- a/gjs/runtime.c
+++ b/gjs/runtime.c
@@ -82,6 +82,20 @@ gjs_runtime_get_const_string(JSRuntime *runtime,
return get_data(runtime)->const_strings[name];
}
+gboolean
+gjs_object_get_property_const(JSContext *context,
+ JSObject *obj,
+ GjsConstString property_name,
+ jsval *value_p)
+{
+ jsid pname;
+
+ pname = gjs_runtime_get_const_string(JS_GetRuntime(context),
+ property_name);
+ return JS_GetPropertyById(context, obj,
+ pname, value_p);
+}
+
void
gjs_runtime_init_for_context(JSRuntime *runtime,
JSContext *context)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]