Cupertino.js is a implementation of the Javascript programming language designed with Cocoa support as a primary consideration. It includes a static compiler and uber dynamic runtime that sits atop of the Objective-C runtime.
It exposes a simple interface to Objective-C: Javascript code is statically
compiled to Objective-C. Objects and functions are derived from NSObject,
strings are NSStrings, numbers are NSNumbers, and message dispatch uses
objc_msgSend.
var name = "Steve"var seven = 4 + 3for (var i = 99; i > 0; i--){
NSLog("%@ bottles of beer on the wall.", i)
}var tallBoy = {"label" : "Pabst", "size" : 24.0}function Todo(){
this.message = null
this.log = function(){
NSLog("%@", this.message)
}
}var beers = new Todo();
beers.message = "Drink some beers"beers.log()var bottlesOnTheWall = NSString.stringWithFormat("%@ of beer on the wall", 99)
bottlesOnTheWall.lowercaseString()
//short hand
bottlesOnTheWall.lowercaseString//Structs are casted from c structs to Javascript objects
//cast functions take the struct as argument and are named after the type or typedef
var applicationFrame = CGRect(UIScreen.mainScreen.applicationFrame)
NSLog("screen height %@", applicationFrame.size.height)applicationFrame.origin.y = 10var window = UIWindow.alloc.init
window.frame = applicationFrameTodo.entityName = "Todo"this.fruits = ["apples", "banannas", "oranges"]var TodoListController = UIViewController.extend("TodoListController")TodoListViewController.prototype.numberOfSectionsInTableView = function(tableView){
return ObjCInt(1)
}// Define function AppDelegate
function AppDelegate(){}
function DidLaunch(application, options){
NSLog("Launched! %@", this)
}
// Implement -[AppDelegate application:didFinishLaunchingWithOptions:]
// with the body of DidLaunch
//
// 'this' is an instance of AppDelegate
AppDelegate.prototype.applicationDidFinishLaunchingWithOptions = DidLaunch//Use the objc_import macro to import objective-c headers
objc_import("TDNetworkRequest.h")
// Interfaces are exported to the global namespace
var request = TDNetworkRequest.get("latest-todos")
reqest.send()The examples directory contains a iPhone app that runs in the simulator and comes complete with a UITableView implementation.
The test-js directory includes JS snippets
Although the example app builds and runs in the simulator, Cupertino.js is not yet production ready.
There several features not yet implemented including:
- Garbage collection: currently it uses retain/release/autorelease
- Standard JS lib (Strings, arrays, etc)
- Objective-C super
- Switch statements
- JS modules
- JS repl
- ECMA standard
Cupertino.js has the following dependencies:
- v8
- llvm
- libclang
- Xcode
- Foundation & the Objective-C runtime
There is an install script which sets up the development environment
./bootstrap.sh
-
install v8 to deps/ with gyp
https://code.google.com/p/v8/wiki/BuildingWithGYP
make builddeps
build/gyp_v8 -Dtarget_arch=x64
-
install llvm with clang support
brew install llvm --with-clang