[webkit-gtk] Calling a native (compiled) subprogram from JavaScript in WebKitGTK
Michael Catanzaro
mcatanzaro at igalia.com
Tue Sep 12 07:56:10 PDT 2017
Hi,
On Tue, Sep 12, 2017 at 9:29 AM, Ludovic Brenta
<ludovic at ludovic-brenta.org> wrote:
> Hello,
>
> I am extending a multi-million-line-of-code application written
> in Ada with a new WebKitGTK browser window, using the WebKit2GTK
> API.
>
> After reading [1] and [2] I am trying to expose an Ada (or C,
> C++, Vala, whatever) subprogram to the JavaScript engine in
> WebKit, so that JavaScript code can call it.
>
> [1] http://www.davedoesdev.com/wrapping-webkit-part-1-gtk+-vala/
> [2] https://karhm.com/JavaScriptCore_C_API/
This first blog post is very old and a trap (see below). The second one
might be useful, but notice the warning that it has omitted memory
management and error handling....
> The gist of it is: attach a callback to the "load-changed" signal
> of the WebKitWebView. If the Load_Event is "finished", create a
> new function object (by calling JSObjectMakeFunctionWithCallback);
> add to the global object of the WebView a new property whose name
> is the name of the function as seen from JavaScript and the value
> is the function object.
This is your problem, because in WebKit2 the JSC API is not really
useful in the UI process. You want to use it in the web process
instead. See [1] for some documentation of how to write a web
extension. You basically compile your web process code into a shared
object that will be dlopened by the web process at runtime. Then you
can communicate between the web process and the UI process using any
form of IPC you prefer (we recommend D-Bus). It's a lot of work to set
up, unfortunately, but the advantage is your whole application does not
crash when the web process crashes.
[1]
https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebExtension.html
> If I try to call the function thus exported (and aptly named
> "exported"), the JavaScript engine throws:
>
> ReferenceError: Can't find variable: exported
>
> I am suspecting that I'm attaching the property to the wrong
> global object. Since I'm stuck with Red Hat Enterprise Linux
> and the webkitgtk3-devel-2.4.9-6.el7.x86_64 package, I have no
> debugging info available for WebKit itself (only for the host
> application), so I can't easily debug what's happening.
Surely there should be debuginfo available... do you not see any
results from 'debuginfo-install webkitgtk3'?
> All I
> can tell for sure is that Get_Javascript_Global_Context,
> Get_Global_Object, String.To_JavaScript etc all return non-null
> and that Exceptions remain null after the call to Set_Property.
> Am I doing something wrong? Is this feature even supported with
> WebKit2 or should I switch to the deprecated WebKit 1 that does not
> use a separate process?
You should definitely not use the WebKit1 API, unless you are content
to target RHEL 7 forever.
Good luck,
Michael
More information about the webkit-gtk
mailing list