[webkit-dev] Extending webkit javascript

Erik Walter erik.walter at ebooktechnologies.com
Sun Jan 18 22:54:41 PST 2009


... 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 at 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 at lists.webkit.org
> http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20090118/45b000c2/attachment.html>


More information about the webkit-dev mailing list