[gjs/ewlsh/text-encoding: 14/16] ByteArray: Implement ByteArray.toGBytes() in JS




commit adee5bdb8b7444472240b657dcf46bf75fe64631
Author: Evan Welsh <contact evanwelsh com>
Date:   Sun Jun 13 18:02:02 2021 -0700

    ByteArray: Implement ByteArray.toGBytes() in JS
    
    At least from what I can tell ByteArray.toGBytes() doesn't support
    ByteArray.ByteArray instances and at some point the GLib.Bytes constructor
    started working to copy data just fine.
    So now toGBytes() is just a wrapper around GLib.Bytes and I removed
    _makeBytes().

 gjs/byteArray.cpp              | 36 ------------------------------------
 modules/core/overrides/GLib.js |  9 +--------
 modules/script/byteArray.js    | 15 ++++++++++++++-
 3 files changed, 15 insertions(+), 45 deletions(-)
---
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 60826ab2..63a2f17d 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -74,41 +74,6 @@ static bool instance_to_string_func(JSContext* cx, unsigned argc,
     return bytearray_to_string(cx, this_obj, encoding.get(), args.rval());
 }
 
-GJS_JSAPI_RETURN_CONVENTION
-static bool
-to_gbytes_func(JSContext *context,
-               unsigned   argc,
-               JS::Value *vp)
-{
-    JS::CallArgs rec = JS::CallArgsFromVp(argc, vp);
-    GIBaseInfo *gbytes_info;
-    JS::RootedObject byte_array(context);
-
-    if (!gjs_parse_call_args(context, "toGBytes", rec, "o",
-                             "byteArray", &byte_array))
-        return false;
-
-    if (!JS_IsUint8Array(byte_array)) {
-        gjs_throw(context,
-                  "Argument to ByteArray.toGBytes() must be a Uint8Array");
-        return false;
-    }
-
-    GBytes* bytes = gjs_byte_array_get_bytes(byte_array);
-
-    g_irepository_require(nullptr, "GLib", "2.0", GIRepositoryLoadFlags(0),
-                          nullptr);
-    gbytes_info = g_irepository_find_by_gtype(NULL, G_TYPE_BYTES);
-    JSObject* ret_bytes_obj =
-        BoxedInstance::new_for_c_struct(context, gbytes_info, bytes);
-    g_bytes_unref(bytes);
-    if (!ret_bytes_obj)
-        return false;
-
-    rec.rval().setObject(*ret_bytes_obj);
-    return true;
-}
-
 GJS_JSAPI_RETURN_CONVENTION
 static bool define_legacy_tostring(JSContext* cx, JS::HandleObject array) {
     const GjsAtoms& atoms = GjsContextPrivate::atoms(cx);
@@ -308,7 +273,6 @@ GByteArray* gjs_byte_array_get_byte_array(JSObject* obj) {
 static JSFunctionSpec gjs_byte_array_module_funcs[] = {
     JS_FN("fromString", from_string_func, 2, 0),
     JS_FN("fromGBytes", from_gbytes_func, 1, 0),
-    JS_FN("toGBytes", to_gbytes_func, 1, 0),
     JS_FN("toString", to_string_func, 2, 0),
     JS_FS_END};
 
diff --git a/modules/core/overrides/GLib.js b/modules/core/overrides/GLib.js
index 5e3800a9..e4dca1a1 100644
--- a/modules/core/overrides/GLib.js
+++ b/modules/core/overrides/GLib.js
@@ -50,13 +50,6 @@ function _readSingleType(signature, forceSimple) {
     return [char];
 }
 
-function _makeBytes(byteArray) {
-    if (byteArray instanceof Uint8Array || byteArray instanceof ByteArray.ByteArray)
-        return ByteArray.toGBytes(byteArray);
-    else
-        return new GLib.Bytes(byteArray);
-}
-
 function _packVariant(signature, value) {
     if (signature.length === 0)
         throw new TypeError('GVariant signature cannot be empty');
@@ -113,7 +106,7 @@ function _packVariant(signature, value) {
                     byteArray = Uint8Array.of(...byteArray, 0);
                 bytes = ByteArray.toGBytes(byteArray);
             } else {
-                bytes = _makeBytes(value);
+                bytes = new GLib.Bytes(value);
             }
             return GLib.Variant.new_from_bytes(new GLib.VariantType('ay'),
                 bytes, true);
diff --git a/modules/script/byteArray.js b/modules/script/byteArray.js
index 98589d28..96201968 100644
--- a/modules/script/byteArray.js
+++ b/modules/script/byteArray.js
@@ -5,7 +5,9 @@
 // Allow toString to be declared.
 /* eslint no-redeclare: ["error", { "builtinGlobals": false }] */
 
-var {fromGBytes, fromString, toGBytes, toString} = imports._byteArrayNative;
+var {fromGBytes, fromString, toString} = imports._byteArrayNative;
+
+const {GLib} = imports.gi;
 
 // For backwards compatibility
 
@@ -18,6 +20,17 @@ function fromArray(array) {
     return new ByteArray(Uint8Array.from(array));
 }
 
+/**
+ * @param {Uint8Array} array the Uint8Array to convert to GLib.Bytes
+ * @returns {GLib.Bytes}
+ */
+function toGBytes(array) {
+    if (!(array instanceof Uint8Array))
+        throw new Error('Argument to ByteArray.toGBytes() must be a Uint8Array');
+
+    return new GLib.Bytes(array);
+}
+
 var ByteArray = class ByteArray {
     constructor(arg = 0) {
         if (arg instanceof Uint8Array)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]