Skip to content

Commit

Permalink
engine and first line of js
Browse files Browse the repository at this point in the history
  • Loading branch information
jfahrenkrug committed Feb 27, 2012
1 parent d1b5a5e commit f798f8a
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 17 deletions.
8 changes: 8 additions & 0 deletions .gitignore
@@ -0,0 +1,8 @@
# xcode noise
build/*
*.pbxuser
*.mode1v3
*.perspectivev3
# osx noise
.DS_Store
profile
6 changes: 6 additions & 0 deletions AddressBookSpy/AddressBookSpy.xcodeproj/project.pbxproj
Expand Up @@ -19,6 +19,7 @@
8F0ED31714FC1FB500563787 /* libstdc++.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 8F0ED31614FC1FB500563787 /* libstdc++.dylib */; };
8F0ED31914FC1FC600563787 /* libicucore.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 8F0ED31814FC1FC600563787 /* libicucore.dylib */; };
8F0ED31B14FC1FD800563787 /* AddressBook.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8F0ED31A14FC1FD800563787 /* AddressBook.framework */; };
8F0ED31E14FC2A0300563787 /* ABSEngine.m in Sources */ = {isa = PBXBuildFile; fileRef = 8F0ED31D14FC2A0300563787 /* ABSEngine.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -48,6 +49,8 @@
8F0ED31614FC1FB500563787 /* libstdc++.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = "libstdc++.dylib"; path = "usr/lib/libstdc++.dylib"; sourceTree = SDKROOT; };
8F0ED31814FC1FC600563787 /* libicucore.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libicucore.dylib; path = usr/lib/libicucore.dylib; sourceTree = SDKROOT; };
8F0ED31A14FC1FD800563787 /* AddressBook.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AddressBook.framework; path = System/Library/Frameworks/AddressBook.framework; sourceTree = SDKROOT; };
8F0ED31C14FC2A0300563787 /* ABSEngine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ABSEngine.h; sourceTree = "<group>"; };
8F0ED31D14FC2A0300563787 /* ABSEngine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ABSEngine.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -109,6 +112,8 @@
8F0ED2FA14FC195800563787 /* ABSViewController.m */,
8F0ED2FC14FC195800563787 /* ABSViewController.xib */,
8F0ED2EE14FC195800563787 /* Supporting Files */,
8F0ED31C14FC2A0300563787 /* ABSEngine.h */,
8F0ED31D14FC2A0300563787 /* ABSEngine.m */,
);
path = AddressBookSpy;
sourceTree = "<group>";
Expand Down Expand Up @@ -207,6 +212,7 @@
8F0ED2F414FC195800563787 /* main.m in Sources */,
8F0ED2F814FC195800563787 /* ABSAppDelegate.m in Sources */,
8F0ED2FB14FC195800563787 /* ABSViewController.m in Sources */,
8F0ED31E14FC2A0300563787 /* ABSEngine.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "1"
version = "1.0">
<FileBreakpoints>
<FileBreakpoint
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "AddressBookSpy/ABSViewController.m"
timestampString = "352071585.881848"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "11"
endingLineNumber = "11"
landmarkName = "@implementation ABSViewController"
landmarkType = "3">
</FileBreakpoint>
</FileBreakpoints>
</Bucket>
1 change: 0 additions & 1 deletion AddressBookSpy/AddressBookSpy/ABSAppDelegate.m
Expand Up @@ -9,7 +9,6 @@
#import "ABSAppDelegate.h"

#import "ABSViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>

@implementation ABSAppDelegate

Expand Down
20 changes: 20 additions & 0 deletions AddressBookSpy/AddressBookSpy/ABSEngine.h
@@ -0,0 +1,20 @@
//
// ABSEngine.h
// AddressBookSpy
//
// Created by Johannes Fahrenkrug on 27.02.12.
// Copyright (c) 2012 Springenwerk. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <JavaScriptCore/JavaScriptCore.h>

@interface ABSEngine : NSObject {
JSGlobalContextRef _JSContext;
}

- (JSGlobalContextRef) JSContext;
- (NSString *)runJS:(NSString *)aJSString;
- (void)loadJSLibrary:(NSString*)libraryName;

@end
74 changes: 74 additions & 0 deletions AddressBookSpy/AddressBookSpy/ABSEngine.m
@@ -0,0 +1,74 @@
//
// ABSEngine.m
// AddressBookSpy
//
// Created by Johannes Fahrenkrug on 27.02.12.
// Copyright (c) 2012 Springenwerk. All rights reserved.
//

#import "ABSEngine.h"

@implementation ABSEngine

/**
Lazily initializes and returns a JS context
*/
- (JSGlobalContextRef)JSContext
{
if (_JSContext == NULL) {
_JSContext = JSGlobalContextCreate(NULL);
}

return _JSContext;
}

/**
Runs a string of JS in this instance's JS context and returns the result as a string
*/
- (NSString *)runJS:(NSString *)aJSString
{
if (!aJSString) {
NSLog(@"[JSC] JS String is empty!");
return nil;
}


JSStringRef scriptJS = JSStringCreateWithUTF8CString([aJSString UTF8String]);
JSValueRef exception = NULL;

JSValueRef result = JSEvaluateScript([self JSContext], scriptJS, NULL, NULL, 0, &exception);
NSString *res = nil;

if (!result) {
if (exception) {
JSStringRef exceptionArg = JSValueToStringCopy([self JSContext], exception, NULL);
NSString* exceptionRes = (__bridge NSString*)JSStringCopyCFString(kCFAllocatorDefault, exceptionArg);

JSStringRelease(exceptionArg);
NSLog(@"[JSC] JavaScript exception: %@", exceptionRes);
}

NSLog(@"[JSC] No result returned");
} else {
JSStringRef jstrArg = JSValueToStringCopy([self JSContext], result, NULL);
res = (__bridge NSString*)JSStringCopyCFString(kCFAllocatorDefault, jstrArg);

JSStringRelease(jstrArg);
}

JSStringRelease(scriptJS);

return res;
}

/**
Loads a JS library file from the app's bundle (without the .js extension)
*/
- (void)loadJSLibrary:(NSString*)libraryName {
NSString *library = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:libraryName ofType:@"js"] encoding:NSUTF8StringEncoding error:nil];

NSLog(@"[JSC] loading library %@...", libraryName);
[self runJS:library];
}

@end
3 changes: 3 additions & 0 deletions AddressBookSpy/AddressBookSpy/ABSViewController.h
Expand Up @@ -7,7 +7,10 @@
//

#import <UIKit/UIKit.h>
#import "ABSEngine.h"

@interface ABSViewController : UIViewController

@property (strong, nonatomic) ABSEngine *engine;

@end
27 changes: 11 additions & 16 deletions AddressBookSpy/AddressBookSpy/ABSViewController.m
Expand Up @@ -8,27 +8,22 @@

#import "ABSViewController.h"

@interface ABSViewController ()

@end

@implementation ABSViewController
@synthesize engine=_engine;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.engine = [[ABSEngine alloc] init];
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

NSLog(@"Result: %@", [self.engine runJS:@"['Welcome', 'from', 'JavaScript!'].join(' ')"]);
}

@end

0 comments on commit f798f8a

Please sign in to comment.