[gjs: 1/2] GObject properties with GByteArray type
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs: 1/2] GObject properties with GByteArray type
- Date: Thu, 13 May 2021 20:36:49 +0000 (UTC)
commit 53875cd91569e6a99a94f6609a1f7d478468b5a9
Author: Veena Nagar <veena nagar 92 gmail com>
Date: Thu May 13 20:36:47 2021 +0000
GObject properties with GByteArray type
Closes: #276
gi/value.cpp | 23 +++++++++++++++++++++++
installed-tests/js/testGIMarshalling.js | 3 +--
2 files changed, 24 insertions(+), 2 deletions(-)
---
diff --git a/gi/value.cpp b/gi/value.cpp
index 836baa0e..f2f3bfc0 100644
--- a/gi/value.cpp
+++ b/gi/value.cpp
@@ -22,6 +22,7 @@
#include <js/Value.h>
#include <js/ValueArray.h>
#include <jsapi.h> // for InformalValueTypeName, JS_ClearPendingException
+#include <jsfriendapi.h>
#include "gi/arg-inl.h"
#include "gi/arg.h"
@@ -38,6 +39,7 @@
#include "gi/value.h"
#include "gi/wrapperutils.h"
#include "gjs/atoms.h"
+#include "gjs/byteArray.h"
#include "gjs/context-private.h"
#include "gjs/context.h"
#include "gjs/jsapi-util.h"
@@ -574,6 +576,13 @@ gjs_value_to_g_value_internal(JSContext *context,
gboxed = ErrorBase::to_c_ptr(context, obj);
if (!gboxed)
return false;
+ } else if (g_type_is_a(gtype, G_TYPE_BYTE_ARRAY)) {
+ /* special case GByteArray */
+ JS::RootedObject obj(context, &value.toObject());
+ if (JS_IsUint8Array(obj)) {
+ g_value_set_boxed(gvalue, gjs_byte_array_get_byte_array(obj));
+ return true;
+ }
} else {
GIBaseInfo *registered = g_irepository_find_by_gtype (NULL, gtype);
@@ -855,6 +864,20 @@ gjs_value_from_g_value_internal(JSContext *context,
} else if (g_type_is_a(gtype, G_TYPE_ARRAY) ||
g_type_is_a(gtype, G_TYPE_BYTE_ARRAY) ||
g_type_is_a(gtype, G_TYPE_PTR_ARRAY)) {
+
+ if (g_type_is_a(gtype, G_TYPE_BYTE_ARRAY)) {
+ auto* byte_array = static_cast<GByteArray*>(g_value_get_boxed(gvalue));
+ JSObject* array =
+ gjs_byte_array_from_byte_array(context, byte_array);
+ if (!array) {
+ gjs_throw(context,
+ "Couldn't convert GByteArray to a Uint8Array");
+ return false;
+ }
+ value_p.setObject(*array);
+ return true;
+ }
+
if (!signal_query) {
gjs_throw(context, "Can't convert untyped array to JS value");
return false;
diff --git a/installed-tests/js/testGIMarshalling.js b/installed-tests/js/testGIMarshalling.js
index 460410b9..ba6c80a5 100644
--- a/installed-tests/js/testGIMarshalling.js
+++ b/installed-tests/js/testGIMarshalling.js
@@ -1883,8 +1883,7 @@ describe('GObject properties', function () {
testPropertyGetSet('enum', GIMarshallingTests.GEnum.VALUE2,
GIMarshallingTests.GEnum.VALUE3);
testPropertyGetSet('byte_array', Uint8Array.of(1, 2, 3),
- ByteArray.fromString('👾'),
- 'https://gitlab.gnome.org/GNOME/gjs/issues/276');
+ ByteArray.fromString('👾'));
it('gets a read-only property', function () {
expect(obj.some_readonly).toEqual(42);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]