Skip to content
Browse files

little script to help out with jscocoa stuff.

  • Loading branch information...
1 parent 43926fe commit 785a3ae7eb197b4eaf55afa68c28314f3089e6d4 @ccgus committed Mar 7, 2009
View
6 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/.
View
11 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
//
View
51 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<nameCount; i++)
+ {
+ JSStringRef jsName = JSPropertyNameArrayGetNameAtIndex(jsNames, i);
+ id name = (id)JSStringCopyCFString(kCFAllocatorDefault, jsName);
+
+ JSValueRef jsValueRef = JSObjectGetProperty(ctx, jsObject, jsName, NULL);
+ JSStringRef valueJS = JSValueToStringCopy(ctx, jsValueRef, NULL);
+ NSString* value = (NSString*)JSStringCopyCFString(kCFAllocatorDefault, valueJS);
+ JSStringRelease(valueJS);
+
+ if ([name isEqualToString:@"line"]) line = value;
+ if ([name isEqualToString:@"sourceURL"]) sourceURL = value;
+ [NSMakeCollectable(name) release];
+ // Autorelease because we assigned it to line / sourceURL
+ [NSMakeCollectable(value) autorelease];
+ }
+ JSPropertyNameArrayRelease(jsNames);
+
+ [_delegate JSCocoa:self hadError:b onLineNumber:[line intValue]];
+}
+
#pragma mark Tests
- (BOOL)runTests:(NSString*)path
View
4 jstalk.xcodeproj/project.pbxproj
@@ -21,7 +21,6 @@
CC1587920F6316B00077A4DF /* JSCocoaFFIArgument.m in Sources */ = {isa = PBXBuildFile; fileRef = CC1587840F6316B00077A4DF /* JSCocoaFFIArgument.m */; };
CC1587930F6316B00077A4DF /* JSCocoaLib.h in Headers */ = {isa = PBXBuildFile; fileRef = CC1587850F6316B00077A4DF /* JSCocoaLib.h */; };
CC1587940F6316B00077A4DF /* JSCocoa.h in Headers */ = {isa = PBXBuildFile; fileRef = CC1587860F6316B00077A4DF /* JSCocoa.h */; };
- CC1587950F6316B00077A4DF /* class.js in Sources */ = {isa = PBXBuildFile; fileRef = CC1587870F6316B00077A4DF /* class.js */; };
CC1587960F6316B00077A4DF /* BridgeSupportController.h in Headers */ = {isa = PBXBuildFile; fileRef = CC1587880F6316B00077A4DF /* BridgeSupportController.h */; };
CC1587970F6316B00077A4DF /* JSCocoaLib.m in Sources */ = {isa = PBXBuildFile; fileRef = CC1587890F6316B00077A4DF /* JSCocoaLib.m */; };
CC1587980F6316B00077A4DF /* JSCocoaPrivateObject.h in Headers */ = {isa = PBXBuildFile; fileRef = CC15878A0F6316B00077A4DF /* JSCocoaPrivateObject.h */; };
@@ -168,6 +167,7 @@
CC1C7E080F48BB2C007A2941 /* TDWordState.m in Sources */ = {isa = PBXBuildFile; fileRef = CC1C7D7D0F48BB2C007A2941 /* TDWordState.m */; };
CC1C7E400F48C34D007A2941 /* Play.tiff in Resources */ = {isa = PBXBuildFile; fileRef = CC1C7E3F0F48C34D007A2941 /* Play.tiff */; };
CC1C7E580F48C76B007A2941 /* Clear.tiff in Resources */ = {isa = PBXBuildFile; fileRef = CC1C7E570F48C76B007A2941 /* Clear.tiff */; };
+ CC27C6730F633D24005F6CB6 /* class.js in Resources */ = {isa = PBXBuildFile; fileRef = CC1587870F6316B00077A4DF /* class.js */; };
CC41432E0F25257C00E46669 /* JSTalk_Prefix.pch in Headers */ = {isa = PBXBuildFile; fileRef = CCC5B8CB0F1EFAAA00126722 /* JSTalk_Prefix.pch */; };
CC41433B0F25259400E46669 /* JSTalk.h in Headers */ = {isa = PBXBuildFile; fileRef = CC5FB7D60F1FDDE900F4ECC2 /* JSTalk.h */; settings = {ATTRIBUTES = (Public, ); }; };
CC41433C0F25259400E46669 /* JSTalk.m in Sources */ = {isa = PBXBuildFile; fileRef = CC5FB7D70F1FDDE900F4ECC2 /* JSTalk.m */; };
@@ -916,6 +916,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
+ CC27C6730F633D24005F6CB6 /* class.js in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1000,7 +1001,6 @@
CC1587900F6316B00077A4DF /* JSCocoaFFIClosure.m in Sources */,
CC1587910F6316B00077A4DF /* JSCocoaPrivateObject.m in Sources */,
CC1587920F6316B00077A4DF /* JSCocoaFFIArgument.m in Sources */,
- CC1587950F6316B00077A4DF /* class.js in Sources */,
CC1587970F6316B00077A4DF /* JSCocoaLib.m in Sources */,
CC1587990F6316B00077A4DF /* JSCocoaController.m in Sources */,
CC15879D0F6316B00077A4DF /* BridgeSupportController.m in Sources */,

0 comments on commit 785a3ae

Please sign in to comment.
Something went wrong with that request. Please try again.