Hi, I am trying to extend the javascript to invoke custom C code in webkit. Basically my requirement is to load a webpage that contains the UI built in Java script and on some user actions, I need to call C library functions on Linux. I looked at the testapi.c in API directory and added similar functions in GtkLauncher program(main.c) to add "MyObject". But when a I load a webpage containing "MyObject" I am getting error "ReferenceError: Can't find variable: MyObject". Then I copied the entire testapi.c into GtkLauncher main.c, replaced main() with test_main() and invoked the test_api() (along with a few tweaks for the header files .). This way I am able to get all the tests (testapi.js) succeed, but I am still getting the "ReferenceError". Looking at the code, I see that there is a context that is passed, which I believe is not passed to the webview. Is there a way in which I can use the same context as the webview and add my object? Regards, WebK
Hi I am not really sure which example/reference you are following. If u need to call some native functions in JavaScript, JavaScript interpreter should be aware of that functions/native component you are trying to access. So process would be to implement such mechanism in JavaScriptCore. You can look at example how its been done for some standard JavaScript objects and their properties. You will find corresponding source in WebCore/bindings , WebCore/bindings/js and JavaScriptCore/kjs/bindings. Thanks & Regards Niilesh 2009/1/17 <webkitdev@aol.com>:
Hi,
I am trying to extend the javascript to invoke custom C code in webkit. Basically my requirement is to load a webpage that contains the UI built in Java script and on some user actions, I need to call C library functions on Linux. I looked at the testapi.c in API directory and added similar functions in GtkLauncher program(main.c) to add "MyObject". But when a I load a webpage containing "MyObject" I am getting error "ReferenceError: Can't find variable: MyObject". Then I copied the entire testapi.c into GtkLauncher main.c, replaced main() with test_main() and invoked the test_api() (along with a few tweaks for the header files .). This way I am able to get all the tests (testapi.js) succeed, but I am still getting the "ReferenceError". Looking at the code, I see that there is a context that is passed, which I believe is not passed to the webview. Is there a way in which I can use the same context as the webview and add my object?
Regards, WebK
________________________________ Which stars will make the biggest headlines in 2009? Get Hollywood predictions, celebrity holiday photos and more with the PopEater Toolbar. _______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
On 2009-01-18, at 20:43, Nilesh Patil wrote:
I am not really sure which example/reference you are following. If u need to call some native functions in JavaScript, JavaScript interpreter should be aware of that functions/native component you are trying to access. So process would be to implement such mechanism in JavaScriptCore. You can look at example how its been done for some standard JavaScript objects and their properties.
JavaScriptCore provides a public API so it is *not* necessary to implement anything inside JavaScriptCore. In fact, it would be highly undesirable to do so.
You will find corresponding source in WebCore/bindings , WebCore/bindings/js and JavaScriptCore/kjs/bindings.
These directories are not at all relevant to the question that the original poster asked. - Mark
2009/1/17 <webkitdev@aol.com>:
Hi,
I am trying to extend the javascript to invoke custom C code in webkit. Basically my requirement is to load a webpage that contains the UI built in Java script and on some user actions, I need to call C library functions on Linux. I looked at the testapi.c in API directory and added similar functions in GtkLauncher program(main.c) to add "MyObject". But when a I load a webpage containing "MyObject" I am getting error "ReferenceError: Can't find variable: MyObject". Then I copied the entire testapi.c into GtkLauncher main.c, replaced main() with test_main() and invoked the test_api() (along with a few tweaks for the header files .). This way I am able to get all the tests (testapi.js) succeed, but I am still getting the "ReferenceError". Looking at the code, I see that there is a context that is passed, which I believe is not passed to the webview. Is there a way in which I can use the same context as the webview and add my object?
Regards, WebK
________________________________ Which stars will make the biggest headlines in 2009? Get Hollywood predictions, celebrity holiday photos and more with the PopEater Toolbar. _______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
_______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
On 2009-01-17, at 07:57, webkitdev@aol.com wrote:
Hi,
I am trying to extend the javascript to invoke custom C code in webkit. Basically my requirement is to load a webpage that contains the UI built in Java script and on some user actions, I need to call C library functions on Linux. I looked at the testapi.c in API directory and added similar functions in GtkLauncher program(main.c) to add "MyObject". But when a I load a webpage containing "MyObject" I am getting error "ReferenceError: Can't find variable: MyObject". Then I copied the entire testapi.c into GtkLauncher main.c, replaced main() with test_main() and invoked the test_api() (along with a few tweaks for the header files .). This way I am able to get all the tests (testapi.js) succeed, but I am still getting the "ReferenceError". Looking at the code, I see that there is a context that is passed, which I believe is not passed to the webview. Is there a way in which I can use the same context as the webview and add my object?
The window-object-cleared signal is emitted when the global context of a frame is initialized. One of the arguments that is passed to this signal handler is the JSGlobalContextRef corresponding to the frame's execution environment. You can set your properties on the global object of this context to expose them to JavaScript that executes in the context of this frame. Hope this helps, - Mark
OK Thank you for information ..... and correcting me :) Thanks & Regards Niilesh On Mon, Jan 19, 2009 at 10:34 AM, Mark Rowe <mrowe@apple.com> wrote:
On 2009-01-17, at 07:57, webkitdev@aol.com wrote:
Hi,
I am trying to extend the javascript to invoke custom C code in webkit. Basically my requirement is to load a webpage that contains the UI built in Java script and on some user actions, I need to call C library functions on Linux. I looked at the testapi.c in API directory and added similar functions in GtkLauncher program(main.c) to add "MyObject". But when a I load a webpage containing "MyObject" I am getting error "ReferenceError: Can't find variable: MyObject". Then I copied the entire testapi.c into GtkLauncher main.c, replaced main() with test_main() and invoked the test_api() (along with a few tweaks for the header files .). This way I am able to get all the tests (testapi.js) succeed, but I am still getting the "ReferenceError". Looking at the code, I see that there is a context that is passed, which I believe is not passed to the webview. Is there a way in which I can use the same context as the webview and add my object?
The window-object-cleared signal is emitted when the global context of a frame is initialized. One of the arguments that is passed to this signal handler is the JSGlobalContextRef corresponding to the frame's execution environment. You can set your properties on the global object of this context to expose them to JavaScript that executes in the context of this frame. Hope this helps, - Mark
_______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
... reposting, forgot to send this from my webkit email address (moderators can ignore the rejected posting)... I'm not positive this is the best way, but this is the way we implemented augmenting the JavaScript context... We declare a "Bar" function for the object's "bar" function and create two structs to describe the "foo" object and it's properties. static JSValueRef Bar(JSContextRef ctx, JSObjectRef /*function*/, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception); static JSStaticFunction sFooStaticFunctions[] = { { "bar", Bar, kJSPropertyAttributeNone }, { 0, 0, 0 } }; JSClassDefinition fooDef = { 0, kJSClassAttributeNone, "foo", 0, 0, // currently no foo properties... sFooStaticFunctions, 0, // Initialize 0, // Finalize 0, // has Property 0, // get Property 0, // set Property callback... 0, 0, 0, 0, 0, 0 }; Here's a simple Bar function that just does a printf() static JSValueRef Bar(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) { printf("Bar Called\n"); return JSValueMakeUndefined(context); } Then we wait until we get the didFinishDocumentLoad notification (which occurs after the document is loaded, but before the OnLoad() handlers are called in JavaScript). Then we register our object in the global context... Frame* coreFrame; WebFrame* targetFrame; // Make sure to set this from the loading client coreFrame = core(targetFrame); JSContextRef scriptCtx; scriptCtx = toRef(coreFrame->script()->globalObject()->globalExec()); JSObjectRef global = JSContextGetGlobalObject(scriptCtx); JSClassRef fooClass = JSClassCreate(fooDef); JSObjectRef fooScriptObject = JSObjectMake(scriptCtx, fooClass, NULL); JSObjectSetProperty(scriptCtx, global, jsStringRef("foo", fooScriptObject, kJSPropertyAttributeNone, 0); Most of this was stolen/figured-out from the testapi.c code and the InspectorController.cpp code. Within a JavaScript function in the page, you could now do "foo.bar()" to call the Bar() function. Feel free to point out things I've done wrong here. On Jan 17, 2009, at 7:57 AM, webkitdev@aol.com wrote:
Hi,
I am trying to extend the javascript to invoke custom C code in webkit. Basically my requirement is to load a webpage that contains the UI built in Java script and on some user actions, I need to call C library functions on Linux. I looked at the testapi.c in API directory and added similar functions in GtkLauncher program(main.c) to add "MyObject". But when a I load a webpage containing "MyObject" I am getting error "ReferenceError: Can't find variable: MyObject". Then I copied the entire testapi.c into GtkLauncher main.c, replaced main() with test_main() and invoked the test_api() (along with a few tweaks for the header files .). This way I am able to get all the tests (testapi.js) succeed, but I am still getting the "ReferenceError". Looking at the code, I see that there is a context that is passed, which I believe is not passed to the webview. Is there a way in which I can use the same context as the webview and add my object?
Regards, WebK
Which stars will make the biggest headlines in 2009? Get Hollywood predictions, celebrity holiday photos and more with the PopEater Toolbar. _______________________________________________ webkit-dev mailing list webkit-dev@lists.webkit.org http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
On 2009-01-18, at 22:54, Erik Walter wrote:
Then we wait until we get the didFinishDocumentLoad notification (which occurs after the document is loaded, but before the OnLoad() handlers are called in JavaScript).
As I mentioned in my previous message, the most appropriate time to register things with the context of a frame is typically immediately after the window object is cleared (- webView:didClearWindowObject:forFrame: on Mac OS X, window-object- cleared for Gtk, etc). It will always be called before any scripts are executed in the document. The same is not true of other callbacks. For instance, didFinishDocumentLoad is called when the document has been parsed, which is after inline script tags have been executed.
Then we register our object in the global context...
Frame* coreFrame; WebFrame* targetFrame; // Make sure to set this from the loading client coreFrame = core(targetFrame);
JSContextRef scriptCtx;
scriptCtx = toRef(coreFrame->script()->globalObject()->globalExec());
JSObjectRef global = JSContextGetGlobalObject(scriptCtx); JSClassRef fooClass = JSClassCreate(fooDef); JSObjectRef fooScriptObject = JSObjectMake(scriptCtx, fooClass, NULL); JSObjectSetProperty(scriptCtx, global, jsStringRef("foo", fooScriptObject, kJSPropertyAttributeNone, 0);
WebCore types should not be used by clients of the WebKit API. The WebKit API provides methods to access the underlying context of a frame, and should be used instead. - Mark
participants (4)
-
Erik Walter
-
Mark Rowe
-
Nilesh Patil
-
webkitdev@aol.com