Created
November 4, 2013 05:03
-
-
Save boundsj/7298265 to your computer and use it in GitHub Desktop.
JavaScriptCore, Objective-C calln' javascripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var add2 = function() { | |
var two = 2; | |
return function(x) { return x + two; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ViewController.h" | |
@import JavaScriptCore; | |
@interface ViewController () | |
@property (nonatomic, strong) JSContext *context; | |
@end | |
@implementation ViewController | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
self.context = [[JSContext alloc] initWithVirtualMachine:[[JSVirtualMachine alloc] init]]; | |
NSString *path = [[NSBundle mainBundle] pathForResource:@"amazeballs" ofType:@"js"]; | |
NSString *amazeballs = [NSString stringWithContentsOfFile:path encoding:NSASCIIStringEncoding error:nil]; | |
[self.context evaluateScript:amazeballs]; | |
} | |
- (IBAction)didTapaRunThaScriptz:(id)sender { | |
JSValue *add2 = [self.context[@"add2"] callWithArguments:@[]]; | |
NSLog(@"================> result of add2(5) is %d", [[add2 callWithArguments:@[@5]] toInt32]); | |
NSLog(@"================> result of add2(10) is %d", [[add2 callWithArguments:@[@10]] toInt32]); | |
NSLog(@"================> result of add2(-10) is %d", [[add2 callWithArguments:@[@-10]] toInt32]); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment