https://github.com/kelp404/coffeecocoa
Run CoffeeScript and JavaScript in Objective-C.
https://github.com/kelp404/coffeecocoa
Last synced: about 2 months ago
JSON representation
Run CoffeeScript and JavaScript in Objective-C.
- Host: GitHub
- URL: https://github.com/kelp404/coffeecocoa
- Owner: kelp404
- Created: 2013-04-05T07:09:32.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2014-02-23T07:49:42.000Z (over 11 years ago)
- Last Synced: 2025-04-11T15:27:43.060Z (about 2 months ago)
- Language: Objective-C
- Homepage:
- Size: 441 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
#CoffeeCocoa [](http://travis-ci.org/kelp404/CoffeeCocoa)
Kelp https://twitter.com/kelp404
[MIT License][mit]
[MIT]: http://www.opensource.org/licenses/mit-license.phpRun **CoffeeScript** in **Cocoa**.
This project runs JavaScript with WebKit not V8. And this version just supports OS X, it may be supported iOS in future.This idea is from NyaruDB-Control which is the NyaruDB management tool. I want to build a tool to execute NyaruDB query syntax.
But Objective-C need to be compiled. It doesn't like Python, Ruby or JavaScript.
This project is for doing that.
Enjoy It :-)##Frameworks
+ CoffeeScript 1.7.1
+ jQuery 2.1.0##Data Type
It supports `NSNull`, `NSString`, `NSNumber`, `NSDate`, `NSDictionary` and `NSArray`.Objective-C | JavaScript
:---------:|:---------:
`NSNull` | `null`
`NSString` | `string`
`NSNumber ` | `number`, `true`, `false`
`NSDate` | `Date`
`NSDictionary` | `object`
`NSArray` | `Array`##Methods
###Eval CoffeeScript
```objective-c
/**
Eval CoffeeScript.
@param coffeeScript: CoffeeScript
*/
- (void)evalCoffeeScript:(NSString *)coffeeScript;/**
Eval CoffeeScript with callback function.
You could use `callback(object)` in CoffeeScript to call Objective-C code.
@param coffeeScript: CoffeeScript
@param handler: callback handler
*/
- (void)evalCoffeeScript:(NSString *)coffeeScript callback:(void (^)(id object))handler;
```###Eval JavaScript
```objective-c
/**
Eval JavaScript.
The JavaScript will be eval in the function, like this `(function(){.........}).call(this);`.
@param javaScript: JavaScript
*/
- (void)evalJavaScript:(NSString *)javaScript;/**
Eval JavaScript with callback function.
You could use `callback(object)` in JavaScript to call Objective-C code.
The JavaScript will be eval in the function, like this `(function(){.........}).call(this);`.
@param javaScript: JavaScript
@param handler: callback handler
*/
- (void)evalJavaScript:(NSString *)javaScript callback:(void (^)(id object))handler;
```###Extend
```objective-c
/**
Extend a function in JavaScript object;
@param functionName: javascript function name
@param objectName: javascript object name
@param handler: function execute block
@return: handler id
*/
- (NSNumber *)extendFunction:(NSString *)functionName inObject:(NSString *)objectName handler:(id (^)(id object))handler;
```##Example
```objective-c
CoffeeCocoa *cc = [CoffeeCocoa new];NSDate *date = [NSDate dateWithTimeIntervalSince1970:0];
NSDictionary *subDict = @{@"title": @"title"};
NSDictionary *obj = @{@"name": @"Kelp",
@"number": @10.11,
@"null": [NSNull null],
@"date": date,
@"sub": subDict,
@"array": @[@"A", @"B"]};[cc extendFunction:@"get_object" inObject:@"window" handler:^id(id object) {
return obj;
}];
[cc evalCoffeeScript:@"func = (obj) ->\n"
" callback obj\n"
"func get_object()"
callback:^(id object) {
NSLog(@"%@", object);
}];
```##Unittest
`/CoffeeCocoa/CoffeeCocoaTests`
⌘ + U##Attention
Your should copy `/CoffeeCocoa/CoffeeCocoa/CoffeeCocoa.bundle` into your bundle.