GNOME Bugzilla – Bug 688768
gjs is inconsistent in handling binary data
Last modified: 2012-11-28 23:19:38 UTC
Try this: bytes = new GLib.Bytes('àbc'); print(bytes.toArray()[0]) print(bytes.toArray()[1]) print(bytes.get_size()) Then try this: ByteArray = imports.byteArray; byteArray = ByteArray.fromString('àbc'); print(byteArray[0]) print(byteArray[1]) print(byteArray.length) You'd expect the same results, right? Instead their different. The first one goes through gjs_string_to_intarray(), which then calls gjs_string_get_binary_data(), while the latter calls JS_GetStringCharsAndLength() and then converts to UTF-8 manually. The problem is that gjs_string_get_binary_data() calls EncodeStringToBuffer, which returns the lower byte of each UTF-16 codepoint. To me, gjs_string_get_binary_data() just looks broken. If one has a string, chances are he wants to interpret the bytes as UTF-8, and anything that returns non UTF-8 strings in the introspected API should be really returning a (array)(element-type guint8). If we dropped, we could also move to CStringsAreUTF8 and avoid a lot of manual conversions when accessing JS strings. Note that gjs_string_get_binary_data() matches the idea of gjs_string_from_binary_data(), which is used by the old dbus bindings.
I think this is a dup... *** This bug has been marked as a duplicate of bug 680730 ***