[webkit-dev] KJS::JSObject question

Darin Adler darin at apple.com
Sun May 17 10:30:06 PDT 2009

On May 15, 2009, at 7:35 AM, Jack Wootton wrote:

> 4. Create the context:
> JSGlobalContextRef context = JSGlobalContextCreate(globalObjectClass);

You will only do this if you’re using JavaScript outside of a web  
page. If you want to do this in conjunction with WebKit you’ll need to  
get the context from WebKit APIs. On Mac OS X the relevant API is - 
[WebFrame globalContext].

> 1. Who should share the context?  Is a single context used for a  
> single webpage / frame?

See my answer above.

> 2. Where do I define the class that will actually handle the  
> implementation of any methods on my new object?

I don’t understand the “where” question. Do it wherever makes sense in  
your program, and call JSClassCreate. Then when it's time to make an  
object, pass the JSClassRef to JSObjectMake.

> 3. The object has been made using "JSObjectMake", but how is it  
> added to WebKit?

A common thing to do is to create an object and then put it in a  
property of the window object, which is the global object inside the  
web browser. That’s done on Mac OS X by implementing the  
webView:windowScriptObjectAvailable: method of the frame load  
delegate. The window script object is a WebScriptObject, and you can  
get the JSObject version of it by calling the JSObject method on it.  
Then you can set your object as a property of the window object with  
the JSObjectSetProperty function.

     -- Darin

More information about the webkit-dev mailing list