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 689343 - arg: Throw an explicit error if we don't know how to make an array
arg: Throw an explicit error if we don't know how to make an array
Status: RESOLVED FIXED
Product: gjs
Classification: Bindings
Component: general
unspecified
Other All
: Normal normal
: ---
Assigned To: gjs-maint
gjs-maint
Depends on:
Blocks:
 
 
Reported: 2012-11-30 13:05 UTC by Colin Walters
Modified: 2012-11-30 21:53 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
arg: Throw an explicit error if we don't know how to make an array (1.74 KB, patch)
2012-11-30 13:05 UTC, Colin Walters
reviewed Details | Review
arg: Fix memory leak in exception path in array conversion (2.47 KB, patch)
2012-11-30 16:19 UTC, Colin Walters
committed Details | Review
arg: Throw an explicit error if we don't know how to make an array (1.71 KB, patch)
2012-11-30 16:19 UTC, Colin Walters
committed Details | Review
tests: Add test for object that looks like array (919 bytes, patch)
2012-11-30 16:19 UTC, Colin Walters
accepted-commit_now Details | Review

Description Colin Walters 2012-11-30 13:05:38 UTC
Previously, we were just passing a 0 length array if you gave say {}
as the argument for a uint8 array...kind of lame.
Comment 1 Colin Walters 2012-11-30 13:05:40 UTC
Created attachment 230271 [details] [review]
arg: Throw an explicit error if we don't know how to make an array
Comment 2 Giovanni Campagna 2012-11-30 15:36:42 UTC
Review of attachment 230271 [details] [review]:

::: gi/arg.c
@@ +1097,3 @@
+                  JSVAL_IS_OBJECT(value) ? JSVAL_TO_OBJECT(value) : NULL);
+        g_free(display_name);
+        return JS_FALSE;

g_base_info_unref();

(We really need to introduce caching at some point...)

::: test/js/testGIMarshalling.js
@@ +209,3 @@
+    assertRaises(function() {
+	GIMarshallingTests.array_uint8_in(bytes);
+    });

I wonder if need also a test for:

GIMarshallingTests.array_uint8_in({ length: 4, 0: 97, 1: 98, 2: 99, 3: 100 });
Comment 3 Colin Walters 2012-11-30 16:19:03 UTC
Created attachment 230294 [details] [review]
arg: Fix memory leak in exception path in array conversion
Comment 4 Colin Walters 2012-11-30 16:19:06 UTC
Created attachment 230295 [details] [review]
arg: Throw an explicit error if we don't know how to make an array

Previously, we were just passing a 0 length array if you gave say {}
as the argument for a uint8 array...kind of lame.
Comment 5 Colin Walters 2012-11-30 16:19:10 UTC
Created attachment 230296 [details] [review]
tests: Add test for object that looks like array
Comment 6 Colin Walters 2012-11-30 16:19:58 UTC
The first patch fixes pre-existing memory leaks; I've rebased the second patch to build on top of that to not leak too.
Comment 7 Jasper St. Pierre (not reading bugmail) 2012-11-30 16:53:28 UTC
Review of attachment 230294 [details] [review]:

Sure.
Comment 8 Jasper St. Pierre (not reading bugmail) 2012-11-30 16:53:55 UTC
Review of attachment 230295 [details] [review]:

Makes sense.
Comment 9 Jasper St. Pierre (not reading bugmail) 2012-11-30 17:13:58 UTC
Review of attachment 230296 [details] [review]:

A comment would be appreciated saying that this testing the pattern used for certain types, and what those types are. We have special-cases for byte arrays, right?
Comment 10 Colin Walters 2012-11-30 21:50:49 UTC
walters> Jasper, can you say what you wanted more of in https://bugzilla.gnome.org/show_bug.cgi?id=689343#c9 ?   Basically what we're testing here is that we accept any object that implements the "array protocol" of having a "length" attribute and numeric attribute for 0..length
<walters> something like that as a comment in the test source?
<Jasper> walters, what real-world objects do we expect to go through that code path?
<Jasper> ByteArray?
<walters> all real js arrays do
<walters> just passing [1,2,3,4] to a uint8 array will
<walters> but here we're making an object that "looks like" an array
<walters> there is an explicit fastpath for passing byteArray to a uint8 array
<walters> but if say you pass a byteArray to a uint16 array, it will also hit this path
<walters> make sense?
* mhr3 has quit (Ping timeout: 600 seconds)
* stefw is now known as stefw_afk
* kerrick (~kerrick@kerrick.student.iastate.edu) has joined #introspection
<Jasper> walters, what's this test for if all JS arrays hit this path?
<walters> basically what happens when you do [1,2,3]  and {length: 3, 0: 1, 1: 2, 2:3} is semantically equivalent but different things in the JS interpreter itself
<walters> we support both
* owen (~otaylor@pool-74-104-161-15.bstnma.fios.verizon.net) has joined #introspection
<walters> even though we could call JS_IsArrayObject() to differentiate
* jrb has quit (Ping timeout: 600 seconds)
* donri has quit (Leaving)
* kerrick has quit (Read error: 104 (Connection reset by peer))
* kerrick (~kerrick@kerrick.student.iastate.edu) has joined #introspection
* owen has quit (Ping timeout: 600 seconds)
<walters> Jasper, so...right? =)
<walters> hey while we're talking about arrays
<walters> is there no js equivalent of .append() from python?
<walters> er, extend()
<Jasper> .push() --
<Jasper> still .push
<walters> hm, i guess this is http://stackoverflow.com/questions/1374126/how-to-append-an-array-to-an-existing-javascript-array
<Jasper> foo.push.apply(foo, elems)
<walters> right
<Jasper> I guess you could also do foo.splice(foo.length, 0, elems)
* walters did somearray += ['foo', 'bar'] but that really doesn't do what you expect...
* stfacc has quit (Ping timeout: 600 seconds)
<walters> anyways, do we have a conclusion on the above?
<Jasper> walters, I guess I don't see why it's necessary if we already take those same codepaths elsewhere
<walters> because in the JS interpreter they are different things
<walters> look at it this way - when we start doing typed array fastpaths, that will be another example of differentiating on array internals
<Jasper> but we don't switch on JS_IsArrayObject yet
<Jasper> yes, they're two separate things, but we iterate over them in exactly the same way
<walters> it'd actually be legal for the interpreter to give you a UInt8Array if you do [1,254,42]
<Jasper> No, that's not legal.
<walters> well i mean, one that downgrades to double if you store something outside the range
<Jasper> ES5 spec mandates that it constructs an object with prototype Array
<walters> yeah ok, sorry that's not what i meant
<Jasper> "
<Jasper>     Let array be the result of creating a new object as if by the expression new Array() where Array is the standard built-in constructor with that name."
<walters> more that there could be a more optimized variant, and we could take advantage of that
<walters> yes right, but that's JS semantics, whereas we can do more since we can look at the interpreter internals
<Jasper> Oh, I see.
<walters> \o/
<walters> persistence wins
Comment 11 Colin Walters 2012-11-30 21:53:17 UTC
Attachment 230294 [details] pushed as c1c274c - arg: Fix memory leak in exception path in array conversion
Attachment 230295 [details] pushed as 2312df2 - arg: Throw an explicit error if we don't know how to make an array