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 646852 - Make GIRepositoryFunction inherit from Function
Make GIRepositoryFunction inherit from Function
Status: RESOLVED FIXED
Product: gjs
Classification: Bindings
Component: general
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gjs-maint
gjs-maint
: 627287 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2011-04-05 19:20 UTC by Giovanni Campagna
Modified: 2011-04-26 15:30 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
GIRepositoryFunction: make it inherit from Function (2.84 KB, patch)
2011-04-05 19:21 UTC, Giovanni Campagna
needs-work Details | Review
GIRepositoryFunction: make it inherit from Function (4.02 KB, patch)
2011-04-07 16:33 UTC, Giovanni Campagna
reviewed Details | Review
GIRepositoryFunction: make it inherit from Function (4.01 KB, patch)
2011-04-21 13:54 UTC, Giovanni Campagna
committed Details | Review

Description Giovanni Campagna 2011-04-05 19:20:40 UTC
So call(), apply() and bind() are available.
Comment 1 Giovanni Campagna 2011-04-05 19:21:00 UTC
Created attachment 185227 [details] [review]
GIRepositoryFunction: make it inherit from Function

Set the prototype of GIRepositoryFunction to Function.prototype, so
methods bind(), call() and apply() are available, and instanceof
reports correct results.
Comment 2 Tommi Komulainen 2011-04-05 19:55:03 UTC
Review of attachment 185227 [details] [review]:

I thought bug 627287 made call() and apply() available. How are these two bugs related?

In any case, misses the tests to prove what commit message claims :-)

::: gi/function.c
@@ +969,3 @@
 
+static JSBool
+function_to_string (JSContext *context,

it's the longest part of the implementation in this patch and doesn't even get mentioned in the commit message :)

I'm not completely sure it's necessary though, but the code comment should probably mention whether or not the intention of toString() here is to mimic the Function.toString() output.

@@ +988,3 @@
+    priv = priv_from_js (context, self);
+
+    jsval retval;

I'm a bit fuzzy on the semantics here, but is this even possible?

maybe related, doesn't this also get called for the prototype in which case there is no code as such and might be better for toString() to not claim so?

@@ +993,3 @@
+    } else {
+        string = g_strdup_printf("function %s(){\n\t[native code]\n}",
+    self = JS_THIS_OBJECT(context, vp);

in some cases I imagine it would be useful to include the fully qualified name of the function somewhere

@@ +1040,3 @@
 
+/* The original Function.prototype.toString complains when
+   given a GIRepository function as an argument */

curious. why does toString() complain (and how?) while apply(), bind(), call() presumably work without fail?
Comment 3 Dan Winship 2011-04-05 20:03:26 UTC
(In reply to comment #2)
> I thought bug 627287 made call() and apply() available. How are these two bugs
> related?

No relation; I think Giovanni and I are just both looking at old FIXMEs in gnome-shell this week.

> In any case, misses the tests to prove what commit message claims :-)

The tests from 627287 can be used here if this approach is preferable to the one there.
Comment 4 Giovanni Campagna 2011-04-05 20:23:47 UTC
(In reply to comment #2)
> Review of attachment 185227 [details] [review]:
> 
> I thought bug 627287 made call() and apply() available. How are these two bugs
> related?

I just didn't search before opening a new bug. Mark it duplicate if you prefer the other implementation.
 
> In any case, misses the tests to prove what commit message claims :-)
>
> ::: gi/function.c
> @@ +969,3 @@
> 
> +static JSBool
> +function_to_string (JSContext *context,
> 
> it's the longest part of the implementation in this patch and doesn't even get
> mentioned in the commit message :)

Will fix.

> I'm not completely sure it's necessary though, but the code comment should
> probably mention whether or not the intention of toString() here is to mimic
> the Function.toString() output.

Ok

> @@ +988,3 @@
> +    priv = priv_from_js (context, self);
> +
> +    jsval retval;
> 
> I'm a bit fuzzy on the semantics here, but is this even possible?

priv == NULL means "this is the prototype, not an instance"
 
> maybe related, doesn't this also get called for the prototype in which case
> there is no code as such and might be better for toString() to not claim so?

Ok, I'll change to do what Function.prototype.toString() does.

> @@ +993,3 @@
> +    } else {
> +        string = g_strdup_printf("function %s(){\n\t[native code]\n}",
> +    self = JS_THIS_OBJECT(context, vp);
> 
> in some cases I imagine it would be useful to include the fully qualified name
> of the function somewhere

So g_function_info_get_symbol instead of g_base_info_get_name? Or that exposes a C implementation detail?

> @@ +1040,3 @@
> 
> +/* The original Function.prototype.toString complains when
> +   given a GIRepository function as an argument */
> 
> curious. why does toString() complain (and how?) while apply(), bind(), call()
> presumably work without fail?

TypeError: Function.prototype.toString called on incompatible object
whereas apply(), bind() and call() just work.
Don't ask me why...
Comment 5 Giovanni Campagna 2011-04-07 16:33:19 UTC
Created attachment 185439 [details] [review]
GIRepositoryFunction: make it inherit from Function

Set the prototype of GIRepositoryFunction to Function.prototype, so
methods bind(), call() and apply() are available, and instanceof
reports correct results.
Also ensures that method invocations are applied on GObjects of the
correct type, and adds an explicit toString() method to GIRepositoryFunction,
since Function.prototype.toString() throws when called on objects
of the wrong class.
Comment 6 Colin Walters 2011-04-14 14:19:32 UTC
Review of attachment 185439 [details] [review]:

::: gi/function.c
@@ +491,3 @@
+                failed = TRUE;
+                goto release;
+            }

This should be a separate patch.

@@ +982,3 @@
+    self = JS_THIS_OBJECT(context, vp);
+    if (!self) {
+        gjs_throw (context, "this cannot be null");

No space between identifier and paren.

@@ +989,3 @@
+
+    if (priv == NULL) {
+        string = "function () {\n}";

Hmm...can this actually happen?  If it can't, we should just assert.
Comment 7 Giovanni Campagna 2011-04-14 16:39:27 UTC
(In reply to comment #6)
> Review of attachment 185439 [details] [review]:
> 
> ::: gi/function.c
> @@ +491,3 @@
> +                failed = TRUE;
> +                goto release;
> +            }
> 
> This should be a separate patch.

It's not really: you can get a this pointer of the wrong class only by using .call / .apply (unless you do some black magic).

> @@ +982,3 @@
> +    self = JS_THIS_OBJECT(context, vp);
> +    if (!self) {
> +        gjs_throw (context, "this cannot be null");
> 
> No space between identifier and paren.

Really? That's unusual. Will fix anyway.

> @@ +989,3 @@
> +
> +    if (priv == NULL) {
> +        string = "function () {\n}";
> 
> Hmm...can this actually happen?  If it can't, we should just assert.

GIRepository.prototype.toString()
(corner case, but in general JS code should never crash or assert)
Comment 8 Colin Walters 2011-04-14 16:55:44 UTC
(In reply to comment #7)
> (In reply to comment #6)
> > Review of attachment 185439 [details] [review] [details]:
> > 
> > ::: gi/function.c
> > @@ +491,3 @@
> > +                failed = TRUE;
> > +                goto release;
> > +            }
> > 
> > This should be a separate patch.
> 
> It's not really: you can get a this pointer of the wrong class only by using
> .call / .apply (unless you do some black magic).

Okay.

> > @@ +982,3 @@
> > +    self = JS_THIS_OBJECT(context, vp);
> > +    if (!self) {
> > +        gjs_throw (context, "this cannot be null");
> > 
> > No space between identifier and paren.
> 
> Really? That's unusual. Will fix anyway.

Yeah, that's the gjs style (also, indent of 4).  I know it sucks switching between C code of different indentation styles, but that's life =/

 
> GIRepository.prototype.toString()
> (corner case, but in general JS code should never crash or assert)

Ah of course, right.  And your toString here is matching Function.prototype.toString().  Looks fine to me then.
Comment 9 Giovanni Campagna 2011-04-21 13:54:26 UTC
Created attachment 186426 [details] [review]
GIRepositoryFunction: make it inherit from Function

Set the prototype of GIRepositoryFunction to Function.prototype, so
methods bind(), call() and apply() are available, and instanceof
reports correct results.
Also ensures that method invocations are applied on GObjects of the
correct type, and adds an explicit toString() method to GIRepositoryFunction,
since Function.prototype.toString() throws when called on objects
of the wrong class.
Comment 10 Colin Walters 2011-04-21 14:20:02 UTC
Review of attachment 186426 [details] [review]:

Looks fine
Comment 11 Giovanni Campagna 2011-04-21 14:25:25 UTC
Attachment 186426 [details] pushed as cb55783 - GIRepositoryFunction: make it inherit from Function
Comment 12 Dan Winship 2011-04-26 15:30:42 UTC
*** Bug 627287 has been marked as a duplicate of this bug. ***