[gjs/wip/package: 75/83] jsapi-util: add an utility method for defining a integer array
- From: Giovanni Campagna <gcampagna src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/wip/package: 75/83] jsapi-util: add an utility method for defining a integer array
- Date: Tue, 7 May 2013 22:01:41 +0000 (UTC)
commit 1eac873cc6ae9369d2d288be84e8f47c05c5cdfb
Author: Giovanni Campagna <gcampagna src gnome org>
Date: Fri Feb 22 03:15:49 2013 +0100
jsapi-util: add an utility method for defining a integer array
Similar to gjs_define_string_array(), but for integers.
gjs/jsapi-util.c | 41 +++++++++++++++++++++++++++++++++++++++++
gjs/jsapi-util.h | 6 ++++++
2 files changed, 47 insertions(+), 0 deletions(-)
---
diff --git a/gjs/jsapi-util.c b/gjs/jsapi-util.c
index 5ba0711..74a54a5 100644
--- a/gjs/jsapi-util.c
+++ b/gjs/jsapi-util.c
@@ -278,6 +278,47 @@ gjs_define_string_array(JSContext *context,
return array;
}
+JSObject*
+gjs_define_int_array(JSContext *context,
+ JSObject *in_object,
+ const char *array_name,
+ gssize array_length,
+ int *array_values,
+ unsigned attrs)
+{
+ GArray *elems;
+ JSObject *array;
+ int i;
+
+ JS_BeginRequest(context);
+
+ if (!JS_EnterLocalRootScope(context))
+ return JS_FALSE;
+
+ elems = g_array_sized_new(FALSE, FALSE, sizeof(jsval), array_length);
+
+ for (i = 0; i < array_length; ++i) {
+ jsval element;
+ element = INT_TO_JSVAL(array_values[i]);
+ g_array_append_val(elems, element);
+ }
+
+ array = JS_NewArrayObject(context, elems->len, (jsval*) elems->data);
+ g_array_free(elems, TRUE);
+
+ if (array != NULL) {
+ if (!JS_DefineProperty(context, in_object,
+ array_name, OBJECT_TO_JSVAL(array),
+ NULL, NULL, attrs))
+ array = NULL;
+ }
+
+ JS_LeaveLocalRootScope(context);
+
+ JS_EndRequest(context);
+ return array;
+}
+
/**
* gjs_string_readable:
*
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 62cffed..5b8fe8a 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -244,6 +244,12 @@ JSObject* gjs_define_string_array (JSContext *context,
gssize array_length,
const char **array_values,
unsigned attrs);
+JSObject* gjs_define_int_array (JSContext *context,
+ JSObject *in_object,
+ const char *array_name,
+ gssize array_length,
+ int *array_values,
+ unsigned attrs);
void gjs_throw (JSContext *context,
const char *format,
...) G_GNUC_PRINTF (2, 3);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]