javascript扩展,在webapp中使用基于C的API(混乱) [英] javascript extension to use C based APIs(clutter) in a webapp
问题描述
我的目标是使用C库来构建网络应用。
我选择了使用SWIG的方法工具。
Swig工具需要三件事
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).
我使用SWIG工具编译并运行此应用程序以验证.js文件是否正确。
应用程序在XMING X11窗口上正常运行。
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.
编译时会创建_wrap.o,.o文件和libFILENAME.so
On compilation it creates _wrap.o, .o file and libFILENAME.so
现在我想在浏览器页面上运行这个 app 。
Now I want to run this app on browser page.
为此我使用了webkit杂乱给我们MxLauncher代码的端口。
我正在使用webkit_iweb_view_load_uri(WEBKIT_IWEB_VIEW(view),filename.html);用于加载我的html文件以在我的网页视图上运行该javascript的API。
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.
我正在链接在编译时创建的.so。
I'm linking the .so created at the compilation time.
错误消息:JS CONSOLE:file:///filename.js:
ReferenceError:找不到变量:示例
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);
如何实现我的目标?
推荐答案
您还需要在运行时告诉WebKit / JavaScriptCore您的绑定(除了链接filename_wrap.o之外)。
You also need to tell WebKit/JavaScriptCore at runtime about your bindings (this is in addition to linking with filename_wrap.o).
具体而言,您需要将它们绑定到全局JavaScript对象(以便根据您的.js示例进行调用)。 WebKit窗口上的回调可用于及时引用全局JavaScript上下文,然后您可以在其上注册您的函数。
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.
调整此示例挂钩到 window-object-cleared
表示代码看起来与此类似:
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");
...
}
取决于哪个分支您正在使用的SWIG可能需要自己生成 example_init
函数(检查filename.cxx);这里的参考是一个初始化函数寄存器包装C函数看起来像SWIG:
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);
...
}
注意 - SWIG没有但正式支持JavaScript;以上是指使用在制品(非生产)SWIG分支机构。
参考文献:
- SWIG-V8 来源和其 Javascript文档
- swig-jsc 来源及其示例
- SWIG JavaScriptCore GSoC项目来源(Google Summer of Code 2012)
- Webkit:扩展JavaScript文章 - 教程 / 示例代码
- 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
这篇关于javascript扩展,在webapp中使用基于C的API(混乱)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!