My favorites | Sign in
Project Logo
                
Search
for
Updated Oct 10, 2008 by parmanoir
JavascriptCore  

JavascriptCore is a Cocoa framework, allowing Javascript embedding in your app.

http://developer.apple.com/documentation/Carbon/Reference/WebKit_JavaScriptCore_Ref/index.html

It has a very simple and powerful mechanism to let you define custom objects : create a class specifying callbacks for each possible action, then create objects of that kind with JSObjectMake.

Here is how JSCocoa defines its boxing object, used to hold ObjC values, C functions, structs, and constants :

	JSClassDefinition jsCocoaObjectDefinition	= kJSClassDefinitionEmpty;
	jsCocoaObjectDefinition.initialize			= jsCocoaObject_initialize;
	jsCocoaObjectDefinition.finalize			= jsCocoaObject_finalize;
	jsCocoaObjectDefinition.getProperty			= jsCocoaObject_getProperty;
	jsCocoaObjectDefinition.setProperty			= jsCocoaObject_setProperty;
	jsCocoaObjectDefinition.deleteProperty		= jsCocoaObject_deleteProperty;
	jsCocoaObjectDefinition.getPropertyNames	= jsCocoaObject_getPropertyNames;
	jsCocoaObjectDefinition.callAsFunction		= jsCocoaObject_callAsFunction;
	jsCocoaObjectDefinition.callAsConstructor	= jsCocoaObject_callAsConstructor;
	jsCocoaObjectDefinition.convertToType		= jsCocoaObject_convertToType;
	
	jsCocoaObjectClass = JSClassCreate(&jsCocoaObjectDefinition);

To easily use your objects, you should specify another object class with a getProperty callback that will be used as the main object. That way, any object that JavascriptCore can't find, it will ask you. When asking for NSWorkspace, JSCocoa's global object will return the corresponding objC class — all that thanks to the getProperty callback.

JSGlobalContextCreate(JSClassCreate(&OSXObjectDefinition))

It's really like window in an HTML page. When using stuff like document, setTimeout, and the like, you're really asking for window.document, window.setTimeout. As window is the global object, it returns the same for document and window.document. It saves typing !


Sign in to add a comment
Hosted by Google Code