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

烈酒焚心 提交于 2019-12-21 03:42:28

问题


My goal is to use the C libraries to form web apps.

I have choosen the way to do that via using "SWIG" tool. The Swig tool requires three things

1) .c file which defines all the functions.

2) .i file also called interface file which is creating the
interface to load the APIs wherin I used the extern keyword.

3) APP written in javascript extension (.js file).

I used SWIG tool to compile and run this app to verify the .js file has made correctly. The application is running fine on XMING X11 window.

On compilation it creates _wrap.o, .o file and libFILENAME.so

Now I want to run this app on browser page.

For this I have used the webkit clutter port which gives us the MxLauncher code. I'm using webkit_iweb_view_load_uri(WEBKIT_IWEB_VIEW(view), "filename.html"); API to load my html file to run that javascript on my webpage view.

I'm linking the .so created at the compilation time.

Error Message: JS CONSOLE: file:///filename.js: ReferenceError: Can't find variable: example

filename.c

int gcd(int x, int y) `enter code here`{
  int g;
  g = y;
  while (x > 0) {
    g = x;
    x = y % x;
    y = g;
  }
  return g;
}

filename.i

%module example
extern int    gcd(int x, int y);

filename.js

x = 42;
y = 105;
g = example.gcd(x,y);

How to get my goal to be achieved?


回答1:


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:

  • SWIG-V8 source and its Javascript documentation
  • swig-jsc source and its example of registering bindings
  • SWIG JavaScriptCore GSoC project source (Google Summer of Code 2012)
  • Webkit: Extending JavaScript article-- tutorial / example code


来源:https://stackoverflow.com/questions/13681780/javascript-extension-to-use-c-based-apisclutter-in-a-webapp

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