# JSCocoa, a bridge from Javascript to Cocoa

Written by Patrick Geiller — parmanoir@gmail.com — Google Home — Wanna contribute ? Send me a mail !

## QuickStart

- JSCocoa.app interactive console that can execute Javascript commands. You can call anything here, provided it fits on one line. Click the help button to execute sample commands.
- JSCoreAnimation.app Core Animation process viewer written in Javascript.

## Adding JSCocoa to your project

As JSCocoa is not a framework (yet?),

- Copy (or reference) all the files from the JSCocoa folder in your project.
- Add JavascriptCore.framework to frameworks
- In 'Build' project settings, add -lffi to 'Other linker flags'

## Starting JSCocoa

Starting JSCocoa depends on the lifetime you want for your JS objects.

- program lifetime start JSCocoa in main.c

      	[[NSAutoreleasePool alloc] init];
      	id JSCocoa = [JSCocoaController sharedController];
      	[JSCocoa evalJSFile:@"myFile.js"];
      	// Standard ObjC alloc
      	return NSApplicationMain(argc,  (const char **) argv);

- use and discard lifetime for things like executing a script after
  - manually create a controller with `[[JSCocoaController alloc] init]`
  - Call `evalJSFile` or `evalJSString`
  - Destroy the allocated controller. Any object you alloc'd/retained will be dealloc'd/released when `JSCocoaController`'s `dealloc`call JS GC.
  Broken — to cleanup, delete any js property then call `JSGarbageCollect`

## Garbage Collection

JSCocoa retains objects it uses. To have an object destroyed, delete any reference to it.

    	// Standard alloc
    	var myObject = MyObjectClass.alloc.init
    	// Use object
    	...
    	// Release
    	myObject.release
    	// Delete JS reference — object is now collectable
    	myObject = null

    	// Manually collect - object will be destroyed
    	JSCocoaController.garbageCollect

You can use `instance` instead of alloc/init.

    	var object = MyObjectClass.instance
    	// Use object
    	...
    	// Delete JS reference — object is now collectable
    	object = null

    	// Manually collect - object will be destroyed
    	JSCocoaController.garbageCollect

Forgetting to set `object = null` will cause the object to live forever.

Call `JSCocoaController.logInstanceStats` to check on your objects' lifetime.