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 682701 - Allow throwing GErrors from JS virtual functions
Allow throwing GErrors from JS virtual functions
Status: RESOLVED FIXED
Product: gjs
Classification: Bindings
Component: general
unspecified
Other Linux
: Normal normal
: ---
Assigned To: gjs-maint
gjs-maint
Depends on:
Blocks:
 
 
Reported: 2012-08-25 22:31 UTC by Giovanni Campagna
Modified: 2017-09-19 03:26 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Capture JS exceptions from invoked virtual functions (10.31 KB, patch)
2012-08-25 22:31 UTC, Giovanni Campagna
none Details | Review
Use the GIVFuncInfo to hook up vfuncs instead of the GICallbackInfo (5.43 KB, patch)
2012-08-25 22:32 UTC, Giovanni Campagna
none Details | Review
Add marshalling for native JS errors (6.34 KB, patch)
2012-08-25 22:32 UTC, Giovanni Campagna
none Details | Review
Add marshalling for native JS errors (7.02 KB, patch)
2017-09-18 06:24 UTC, Philip Chimento
committed Details | Review
Use the GIVFuncInfo to hook up vfuncs instead of the GICallbackInfo (6.66 KB, patch)
2017-09-18 06:24 UTC, Philip Chimento
committed Details | Review
Capture JS exceptions from invoked virtual functions (8.22 KB, patch)
2017-09-18 06:25 UTC, Philip Chimento
committed Details | Review

Description Giovanni Campagna 2012-08-25 22:31:01 UTC
Something I had in mind for a while.
Not a priority for 3.6 or anything, just a good hack. It does get us closer to Python, though, so it's interesting if the "one language to rule them all" comes back again.
Comment 1 Giovanni Campagna 2012-08-25 22:31:56 UTC
Created attachment 222438 [details] [review]
Capture JS exceptions from invoked virtual functions

If a vfunc is annotated to throw, capture JS exceptions and turn
them into GErrors instead of logging the error and silently
returning null.
This allows "throw new GLib.Error()" to do the expected thing
inside a vfunc. If the vfunc does not throw, the exception is
reported as usual.
Comment 2 Giovanni Campagna 2012-08-25 22:32:06 UTC
Created attachment 222439 [details] [review]
Use the GIVFuncInfo to hook up vfuncs instead of the GICallbackInfo

Using GICallbackInfo loses the flags on the virtual function, such
as throws.
The difference is also that GIVFuncInfo does not include the instance
among the arguments, so we need to offset all the accesses to ffi.
It is implicitely assumed that virtual functions are hooked on GObjects
only.
Comment 3 Giovanni Campagna 2012-08-25 22:32:17 UTC
Created attachment 222440 [details] [review]
Add marshalling for native JS errors

Introduce a GError domain for representing native JS errors (such as
TypeError or ReferenceError) throw by JS code, to be reported to calling
C code.
Introduce also a way to retrieve something similar to the original C
error from this GError, in case it comes through a called C function
unmodified.
Comment 4 Philip Chimento 2017-04-04 06:04:33 UTC
Review of attachment 222440 [details] [review]:

::: gjs/gjs.h
@@ +27,2 @@
 #include <gjs/context.h>
+#include <gjs/js-error.h>

Giovanni, it seems you forgot to add this file...

I don't know if you still have it lying around somewhere after 4 years, but if you have it, it would certainly make it easier for me to rebase these patches.
Comment 5 Giovanni Campagna 2017-04-04 06:36:50 UTC
(In reply to Philip Chimento from comment #4)
> Review of attachment 222440 [details] [review] [review]:
> 
> ::: gjs/gjs.h
> @@ +27,2 @@
>  #include <gjs/context.h>
> +#include <gjs/js-error.h>
> 
> Giovanni, it seems you forgot to add this file...
> 
> I don't know if you still have it lying around somewhere after 4 years, but
> if you have it, it would certainly make it easier for me to rebase these
> patches.

It probably got lost in some of the many "git clean -fdx" I have run over the years. I don't have it here anyway.

I think it was just declarations though, of the non static functions defined in gerror.h. Maybe the enum type for pure JS errors?
Comment 6 Philip Chimento 2017-09-18 06:24:52 UTC
Created attachment 359958 [details] [review]
Add marshalling for native JS errors

Introduce a GError domain for representing native JS errors (such as
TypeError or ReferenceError) throw by JS code, to be reported to calling
C code.
Introduce also a way to retrieve something similar to the original C
error from this GError, in case it comes through a called C function
unmodified.
Comment 7 Philip Chimento 2017-09-18 06:24:57 UTC
Created attachment 359959 [details] [review]
Use the GIVFuncInfo to hook up vfuncs instead of the GICallbackInfo

Using GICallbackInfo loses the flags on the virtual function, such
as throws.
The difference is also that GIVFuncInfo does not include the instance
among the arguments, so we need to offset all the accesses to ffi.
It is implicitely assumed that virtual functions are hooked on GObjects
only.
Comment 8 Philip Chimento 2017-09-18 06:25:02 UTC
Created attachment 359960 [details] [review]
Capture JS exceptions from invoked virtual functions

If a vfunc is annotated to throw, capture JS exceptions and turn
them into GErrors instead of logging the error and silently
returning null.
This allows "throw new GLib.Error()" to do the expected thing
inside a vfunc. If the vfunc does not throw, the exception is
reported as usual.
Comment 9 Philip Chimento 2017-09-18 06:29:44 UTC
Here's a rebase. The new patches are not a huge departure from the original ones, but maybe worth a quick review.
Comment 10 Cosimo Cecchi 2017-09-18 17:50:27 UTC
Review of attachment 359958 [details] [review]:

LGTM
Comment 11 Cosimo Cecchi 2017-09-18 17:52:46 UTC
Review of attachment 359959 [details] [review]:

Nice
Comment 12 Cosimo Cecchi 2017-09-18 17:57:27 UTC
Review of attachment 359960 [details] [review]:

Looks good.
Comment 13 Philip Chimento 2017-09-19 03:26:33 UTC
Attachment 359958 [details] pushed as c3e4ae6 - Add marshalling for native JS errors
Attachment 359959 [details] pushed as bb75179 - Use the GIVFuncInfo to hook up vfuncs instead of the GICallbackInfo
Attachment 359960 [details] pushed as 9dbb7be - Capture JS exceptions from invoked virtual functions