[webkit-dev] Embedding JavaScriptCore - Creating new object types ???

Maciej Stachowiak mjs at apple.com
Wed Jan 7 13:28:37 PST 2009


On Jan 7, 2009, at 11:50 AM, Martin Wengenmayer wrote:

> Hi,
> I'm want to embed the JavaScriptCore into my application but I have  
> some problems. So far I only found the JSPong example for embedding  
> which didn't answer my question.
>
> I want to create a simple Vector3f class which can be used in  
> scripts like the String or Array types.  But I couldn't figure out  
> how to set it up so that I can create objects with "new". I want the  
> class to work like this example.
>
> 	var a = new Vector3f(0,1,0);
> 	var b = new Vector3f(1,1,1);
> 	
> 	var c = a.add(b);
>
> Is that possible with JavaScriptCore?

Yes, it's possible. Here are the steps you need to take:

1) Create a Vector3f class using JSClassCreate.
2) Make a constructor for the Vector3f using JSObjectMakeConstructor,  
passing the class from step 1 and a function that uses JSObjectMake  
with the class to create the object.
3) Add the constructor from step 2 to the global namespace, using  
JSObjectSetProperty on the global object, which you can retrieve using  
JSContextGetGlobalObject.

After these 3 steps, JS code will see a global variable named Vector3f  
which holds your newly created constructor. At that point, your code  
using "new" should work.

Regards,
Maciej



More information about the webkit-dev mailing list