diff --git a/bin/jstcopytojscocoafork.sh b/bin/jstcopytojscocoafork.sh new file mode 100755 index 0000000..a941b0e --- /dev/null +++ b/bin/jstcopytojscocoafork.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +SRC_DIR=`cd ${0%/*}/..; pwd` + + +cp $SRC_DIR/jscocoa/JSCocoa/*.m $SRC_DIR/jscocoa/JSCocoa/*.h $SRC_DIR/../jscocoafork/JSCocoa/. \ No newline at end of file diff --git a/jscocoa/JSCocoa/JSCocoaController.h b/jscocoa/JSCocoa/JSCocoaController.h index 74ac674..ad03ec0 100644 --- a/jscocoa/JSCocoa/JSCocoaController.h +++ b/jscocoa/JSCocoa/JSCocoaController.h @@ -30,8 +30,12 @@ typedef struct JSValueRefAndContextRef JSValueRefAndContextRef; @interface JSCocoaController : NSObject { JSGlobalContextRef ctx; + id _delegate; } +@property (assign) id delegate; + + + (id)sharedController; + (id)controllerFromContext:(JSContextRef)ctx; + (BOOL)hasSharedController; @@ -120,6 +124,13 @@ typedef struct JSValueRefAndContextRef JSValueRefAndContextRef; @end + + +@interface NSObject (JSCocoaControllerDelegateMethods) +- (void) JSCocoa:(JSCocoaController*)controller hadError:(NSString*)error onLineNumber:(NSInteger)lineNumber; +@end + + // // JSCocoa shorthand // diff --git a/jscocoa/JSCocoa/JSCocoaController.m b/jscocoa/JSCocoa/JSCocoaController.m index db4d5b6..f5627bc 100644 --- a/jscocoa/JSCocoa/JSCocoaController.m +++ b/jscocoa/JSCocoa/JSCocoaController.m @@ -57,8 +57,14 @@ // #pragma mark JSCocoaController +@interface JSCocoaController (Private) +- (void) callDelegateForException:(JSValueRef)exception; +@end + @implementation JSCocoaController +@synthesize delegate=_delegate; + // Given a jsFunction, retrieve its closure (jsFunction's pointer address is used as key) static id closureHash; // Given a jsFunction, retrieve its selector @@ -329,7 +335,7 @@ - (JSValueRefAndContextRef)evalJSString:(NSString*)script v.value = JSValueMakeNull(ctx); if (exception) { - NSLog(@"JSException in %@ : %@", @"js string", [self formatJSException:exception]); + [self callDelegateForException:exception]; return v; } @@ -1265,6 +1271,49 @@ - (NSString*)formatJSException:(JSValueRef)exception return [NSString stringWithFormat:@"%@ on line %@ of %@", b, line, sourceURL]; } +- (void) callDelegateForException:(JSValueRef)exception { + if (!_delegate || ![_delegate respondsToSelector:@selector(JSCocoa:hadError:onLineNumber:)]) { + + NSLog(@"JSException: %@", [self formatJSException:exception]); + + return; + } + + JSStringRef resultStringJS = JSValueToStringCopy(ctx, exception, NULL); + NSString* b = (NSString*)JSStringCopyCFString(kCFAllocatorDefault, resultStringJS); + JSStringRelease(resultStringJS); + [NSMakeCollectable(b) autorelease]; + + if (JSValueGetType(ctx, exception) != kJSTypeObject) { + [_delegate JSCocoa:self hadError:b onLineNumber:0]; + } + + // Iterate over all properties of the exception + JSObjectRef jsObject = JSValueToObject(ctx, exception, NULL); + JSPropertyNameArrayRef jsNames = JSObjectCopyPropertyNames(ctx, jsObject); + int i, nameCount = JSPropertyNameArrayGetCount(jsNames); + id line = nil, sourceURL = nil; + for (i=0; i