javascript extension to use C based APIs(clutter) in a webapp

爱⌒轻易说出口 提交于 2019-12-03 12:17:56

You also need to tell WebKit/JavaScriptCore at runtime about your bindings (this is in addition to linking with filename_wrap.o).

Specifically you need to bind them to the global JavaScript object (in order to invoke per your .js examples). A callback on the WebKit window can be used to get a timely reference to the global JavaScript context, and then you can register your functions onto it.

Adapting this example of hooking into the window-object-cleared signal the code could look similar to this:

/* the window callback - 
     fired when the JavaScript window object has been cleared */
static void window_object_cleared_cb(WebKitWebView  *web_view,
                                     WebKitWebFrame *frame,
                                     gpointer        context,
                                     gpointer        window_object,
                                     gpointer        user_data)
{
  /* Add your classes to JavaScriptCore */
  example_init(context); // example_init generated by SWIG
}


/* ... and in your main application set up */
void yourmainfunc()
{
    ....

    g_signal_connect (G_OBJECT (web_view), "window-object-cleared",
        G_CALLBACK(window_object_cleared_cb), web_view);

    webkit_web_view_load_uri (WEBKIT_WEB_VIEW (web_view), "file://filename.html");

    ...
}

Depending on which branch of SWIG you are using you may need to generate the example_init function yourself (check filename.cxx); for reference here is what an initializer function to register wrapped C functions would look like in SWIG:

int example_init(JSContextRef context) {
  JSObjectRef global = JSContextGetGlobalObject(context);
 ...
  jsc_registerFunction(context, global,  "gcd", _wrap_gcd);
 ...
}

NOTE -- SWIG does not yet officially support JavaScript; the above refers to using the work-in-progress (non-production) SWIG branches.

References:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!