Skip to content

Commit

Permalink
little script to help out with jscocoa stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
ccgus committed Mar 7, 2009
1 parent 43926fe commit 785a3ae
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
6 changes: 6 additions & 0 deletions 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/.
11 changes: 11 additions & 0 deletions jscocoa/JSCocoa/JSCocoaController.h
Expand Up @@ -30,8 +30,12 @@ typedef struct JSValueRefAndContextRef JSValueRefAndContextRef;
@interface JSCocoaController : NSObject { @interface JSCocoaController : NSObject {


JSGlobalContextRef ctx; JSGlobalContextRef ctx;
id _delegate;
} }


@property (assign) id delegate;


+ (id)sharedController; + (id)sharedController;
+ (id)controllerFromContext:(JSContextRef)ctx; + (id)controllerFromContext:(JSContextRef)ctx;
+ (BOOL)hasSharedController; + (BOOL)hasSharedController;
Expand Down Expand Up @@ -120,6 +124,13 @@ typedef struct JSValueRefAndContextRef JSValueRefAndContextRef;


@end @end




@interface NSObject (JSCocoaControllerDelegateMethods)
- (void) JSCocoa:(JSCocoaController*)controller hadError:(NSString*)error onLineNumber:(NSInteger)lineNumber;
@end


// //
// JSCocoa shorthand // JSCocoa shorthand
// //
Expand Down
51 changes: 50 additions & 1 deletion jscocoa/JSCocoa/JSCocoaController.m
Expand Up @@ -57,8 +57,14 @@
// //
#pragma mark JSCocoaController #pragma mark JSCocoaController


@interface JSCocoaController (Private)
- (void) callDelegateForException:(JSValueRef)exception;
@end

@implementation JSCocoaController @implementation JSCocoaController


@synthesize delegate=_delegate;

// Given a jsFunction, retrieve its closure (jsFunction's pointer address is used as key) // Given a jsFunction, retrieve its closure (jsFunction's pointer address is used as key)
static id closureHash; static id closureHash;
// Given a jsFunction, retrieve its selector // Given a jsFunction, retrieve its selector
Expand Down Expand Up @@ -329,7 +335,7 @@ - (JSValueRefAndContextRef)evalJSString:(NSString*)script
v.value = JSValueMakeNull(ctx); v.value = JSValueMakeNull(ctx);
if (exception) if (exception)
{ {
NSLog(@"JSException in %@ : %@", @"js string", [self formatJSException:exception]); [self callDelegateForException:exception];
return v; return v;
} }


Expand Down Expand Up @@ -1265,6 +1271,49 @@ - (NSString*)formatJSException:(JSValueRef)exception
return [NSString stringWithFormat:@"%@ on line %@ of %@", b, line, sourceURL]; 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 #pragma mark Tests
- (BOOL)runTests:(NSString*)path - (BOOL)runTests:(NSString*)path
Expand Down
4 changes: 2 additions & 2 deletions jstalk.xcodeproj/project.pbxproj
Expand Up @@ -21,7 +21,6 @@
CC1587920F6316B00077A4DF /* JSCocoaFFIArgument.m in Sources */ = {isa = PBXBuildFile; fileRef = CC1587840F6316B00077A4DF /* JSCocoaFFIArgument.m */; }; CC1587920F6316B00077A4DF /* JSCocoaFFIArgument.m in Sources */ = {isa = PBXBuildFile; fileRef = CC1587840F6316B00077A4DF /* JSCocoaFFIArgument.m */; };
CC1587930F6316B00077A4DF /* JSCocoaLib.h in Headers */ = {isa = PBXBuildFile; fileRef = CC1587850F6316B00077A4DF /* JSCocoaLib.h */; }; CC1587930F6316B00077A4DF /* JSCocoaLib.h in Headers */ = {isa = PBXBuildFile; fileRef = CC1587850F6316B00077A4DF /* JSCocoaLib.h */; };
CC1587940F6316B00077A4DF /* JSCocoa.h in Headers */ = {isa = PBXBuildFile; fileRef = CC1587860F6316B00077A4DF /* JSCocoa.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 */; }; CC1587960F6316B00077A4DF /* BridgeSupportController.h in Headers */ = {isa = PBXBuildFile; fileRef = CC1587880F6316B00077A4DF /* BridgeSupportController.h */; };
CC1587970F6316B00077A4DF /* JSCocoaLib.m in Sources */ = {isa = PBXBuildFile; fileRef = CC1587890F6316B00077A4DF /* JSCocoaLib.m */; }; CC1587970F6316B00077A4DF /* JSCocoaLib.m in Sources */ = {isa = PBXBuildFile; fileRef = CC1587890F6316B00077A4DF /* JSCocoaLib.m */; };
CC1587980F6316B00077A4DF /* JSCocoaPrivateObject.h in Headers */ = {isa = PBXBuildFile; fileRef = CC15878A0F6316B00077A4DF /* JSCocoaPrivateObject.h */; }; CC1587980F6316B00077A4DF /* JSCocoaPrivateObject.h in Headers */ = {isa = PBXBuildFile; fileRef = CC15878A0F6316B00077A4DF /* JSCocoaPrivateObject.h */; };
Expand Down Expand Up @@ -168,6 +167,7 @@
CC1C7E080F48BB2C007A2941 /* TDWordState.m in Sources */ = {isa = PBXBuildFile; fileRef = CC1C7D7D0F48BB2C007A2941 /* TDWordState.m */; }; CC1C7E080F48BB2C007A2941 /* TDWordState.m in Sources */ = {isa = PBXBuildFile; fileRef = CC1C7D7D0F48BB2C007A2941 /* TDWordState.m */; };
CC1C7E400F48C34D007A2941 /* Play.tiff in Resources */ = {isa = PBXBuildFile; fileRef = CC1C7E3F0F48C34D007A2941 /* Play.tiff */; }; CC1C7E400F48C34D007A2941 /* Play.tiff in Resources */ = {isa = PBXBuildFile; fileRef = CC1C7E3F0F48C34D007A2941 /* Play.tiff */; };
CC1C7E580F48C76B007A2941 /* Clear.tiff in Resources */ = {isa = PBXBuildFile; fileRef = CC1C7E570F48C76B007A2941 /* Clear.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 */; }; 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, ); }; }; 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 */; }; CC41433C0F25259400E46669 /* JSTalk.m in Sources */ = {isa = PBXBuildFile; fileRef = CC5FB7D70F1FDDE900F4ECC2 /* JSTalk.m */; };
Expand Down Expand Up @@ -916,6 +916,7 @@
isa = PBXResourcesBuildPhase; isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
CC27C6730F633D24005F6CB6 /* class.js in Resources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
Expand Down Expand Up @@ -1000,7 +1001,6 @@
CC1587900F6316B00077A4DF /* JSCocoaFFIClosure.m in Sources */, CC1587900F6316B00077A4DF /* JSCocoaFFIClosure.m in Sources */,
CC1587910F6316B00077A4DF /* JSCocoaPrivateObject.m in Sources */, CC1587910F6316B00077A4DF /* JSCocoaPrivateObject.m in Sources */,
CC1587920F6316B00077A4DF /* JSCocoaFFIArgument.m in Sources */, CC1587920F6316B00077A4DF /* JSCocoaFFIArgument.m in Sources */,
CC1587950F6316B00077A4DF /* class.js in Sources */,
CC1587970F6316B00077A4DF /* JSCocoaLib.m in Sources */, CC1587970F6316B00077A4DF /* JSCocoaLib.m in Sources */,
CC1587990F6316B00077A4DF /* JSCocoaController.m in Sources */, CC1587990F6316B00077A4DF /* JSCocoaController.m in Sources */,
CC15879D0F6316B00077A4DF /* BridgeSupportController.m in Sources */, CC15879D0F6316B00077A4DF /* BridgeSupportController.m in Sources */,
Expand Down

0 comments on commit 785a3ae

Please sign in to comment.