After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 688768 - gjs is inconsistent in handling binary data
gjs is inconsistent in handling binary data
Status: RESOLVED DUPLICATE of bug 680730
Product: gjs
Classification: Bindings
Component: general
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gjs-maint
gjs-maint
Depends on:
Blocks:
 
 
Reported: 2012-11-20 23:55 UTC by Giovanni Campagna
Modified: 2012-11-28 23:19 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Giovanni Campagna 2012-11-20 23:55:06 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.
Comment 1 Colin Walters 2012-11-28 23:19:38 UTC
I think this is a dup...

*** This bug has been marked as a duplicate of bug 680730 ***