GNOME Bugzilla – Bug 682701
Allow throwing GErrors from JS virtual functions
Last modified: 2017-09-19 03:26:43 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.
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.
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.
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.
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.
(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?
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.
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.
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.
Here's a rebase. The new patches are not a huge departure from the original ones, but maybe worth a quick review.
Review of attachment 359958 [details] [review]: LGTM
Review of attachment 359959 [details] [review]: Nice
Review of attachment 359960 [details] [review]: Looks good.
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