Skip to content

Commit

Permalink
Added isSyntaxValid:
Browse files Browse the repository at this point in the history
  • Loading branch information
parmanoir committed Jul 26, 2010
1 parent 5ce4557 commit 99135c8
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 61 deletions.
3 changes: 3 additions & 0 deletions JSCocoa/JSCocoaController.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ typedef struct JSValueRefAndContextRef JSValueRefAndContextRef;
- (id)callFunction:(NSString*)name;
- (id)callFunction:(NSString*)name withArguments:(NSArray*)arguments;
- (BOOL)hasFunction:(NSString*)name;
- (BOOL)isSyntaxValid:(NSString*)script;

- (BOOL)evalJSFile:(NSString*)path;
- (BOOL)evalJSFile:(NSString*)path toJSValueRef:(JSValueRef*)returnValue;
Expand All @@ -98,6 +99,8 @@ typedef struct JSValueRefAndContextRef JSValueRefAndContextRef;
- (JSObjectRef)JSFunctionNamed:(NSString*)functionName;
- (BOOL)hasJSFunctionNamed:(NSString*)functionName;
- (NSString*)expandJSMacros:(NSString*)script url:(NSString*)url;
- (NSString*)expandJSMacros:(NSString*)script url:(NSString*)url errors:(NSMutableArray*)array;
- (BOOL)isSyntaxValid:(NSString*)script error:(NSString**)error;
- (BOOL)setObject:(id)object withName:(id)name;
- (BOOL)setObject:(id)object withName:(id)name attributes:(JSPropertyAttributes)attributes;
- (BOOL)removeObjectWithName:(id)name;
Expand Down
55 changes: 47 additions & 8 deletions JSCocoa/JSCocoaController.m
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,12 @@ - (void)accomodateWebKitInspector
//
// Quick eval of strings and functions returning ObjC objects
//
- (id)eval:(NSString*)script { return [self toObject:[self evalJSString:script]]; }
- (id)callFunction:(NSString*)name { return [self toObject:[self callJSFunctionNamed:name withArgumentsArray:nil]]; }
- (id)eval:(NSString*)script { return [self toObject:[self evalJSString:script]]; }
- (id)callFunction:(NSString*)name { return [self toObject:[self callJSFunctionNamed:name withArgumentsArray:nil]]; }
- (id)callFunction:(NSString*)name withArguments:(NSArray*)arguments { return [self toObject:[self callJSFunctionNamed:name withArgumentsArray:arguments]]; }
- (BOOL)hasFunction:(NSString*)name { return [self hasJSFunctionNamed:name]; }
- (BOOL)hasFunction:(NSString*)name { return [self hasJSFunctionNamed:name]; }

- (BOOL)isSyntaxValid:(NSString*)script { return [self isSyntaxValid:script error:nil]; }


//
Expand Down Expand Up @@ -552,8 +554,8 @@ - (BOOL)evalJSFile:(NSString*)path toJSValueRef:(JSValueRef*)returnValue
// Convert script and script URL to js strings
// JSStringRef scriptJS = JSStringCreateWithUTF8CString([script UTF8String]);
// Using CreateWithUTF8 yields wrong results on PPC
JSStringRef scriptJS = JSStringCreateWithCFString((CFStringRef)script);
JSStringRef scriptURLJS = JSStringCreateWithUTF8CString([path UTF8String]);
JSStringRef scriptJS = JSStringCreateWithCFString((CFStringRef)script);
JSStringRef scriptURLJS = JSStringCreateWithUTF8CString([path UTF8String]);
// Eval !
JSValueRef exception = NULL;
JSValueRef result = JSEvaluateScript(ctx, scriptJS, NULL, scriptURLJS, 1, &exception);
Expand Down Expand Up @@ -747,7 +749,7 @@ - (BOOL)hasJSFunctionNamed:(NSString*)name
//
// Expand macros
//
- (NSString*)expandJSMacros:(NSString*)script url:(NSString*)url
- (NSString*)expandJSMacros:(NSString*)script url:(NSString*)url errors:(NSMutableArray*)array;
{
// Normal path, with macro expansion for class definitions
// OR
Expand All @@ -757,7 +759,7 @@ - (NSString*)expandJSMacros:(NSString*)script url:(NSString*)url
BOOL hasFunction = [self hasJSFunctionNamed:functionName];
if (hasFunction && useJSLint)
{
JSValueRef v = [self callJSFunctionNamed:functionName withArguments:script, nil];
JSValueRef v = [self callJSFunctionNamed:functionName withArguments:script, array, nil];
id expandedScript = [self unboxJSValueRef:v];
// Bail if expansion failed
if (!expandedScript || ![expandedScript isKindOfClass:[NSString class]])
Expand All @@ -767,6 +769,43 @@ - (NSString*)expandJSMacros:(NSString*)script url:(NSString*)url
}
return script;
}
- (NSString*)expandJSMacros:(NSString*)script url:(NSString*)url
{
return [self expandJSMacros:script url:url errors:nil];
}

//
// Syntax validation
//
- (BOOL)isSyntaxValid:(NSString*)script error:(NSString**)error
{
id errors = [NSMutableArray array];
script = [self expandJSMacros:script url:nil errors:errors];

JSStringRef scriptJS = JSStringCreateWithUTF8CString([script UTF8String]);
JSValueRef exception = NULL;
BOOL b = JSCheckScriptSyntax(ctx, scriptJS, scriptJS, 1, &exception);
JSStringRelease(scriptJS);

if (exception)
{
NSMutableArray* errorList = [NSMutableArray array];
NSString* str = [self formatJSException:exception];
[errorList addObject:str];
for (id error in errors)
{
[errorList addObject:[error valueForKey:@"error"]];
if ([error valueForKey:@"line"]) [errorList addObject:[error valueForKey:@"line"]];
if ([error valueForKey:@"position"]) [errorList addObject:[error valueForKey:@"position"]];
}
if (error)
*error = [errorList componentsJoinedByString:@"\n"];
}

return b;
}



//
// Unbox a JSValueRef
Expand Down Expand Up @@ -1791,7 +1830,7 @@ - (NSString*)formatJSException:(JSValueRef)exception
//
// Error reporting
//
- (void) callDelegateForException:(JSValueRef)exception {
- (void)callDelegateForException:(JSValueRef)exception {
if (!_delegate || ![_delegate respondsToSelector:@selector(JSCocoa:hadError:onLineNumber:atSourceURL:)]) {
NSLog(@"JSException: %@", [self formatJSException:exception]);
return;
Expand Down
23 changes: 19 additions & 4 deletions JSCocoa/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,10 @@
var __jslint = __JSLINT()
var __lintTokens

function expandJSMacros(script)
//
// Expand script, log errors into errorArray (or to console if there are none)
//
function expandJSMacros(script, errorArray)
{
__lintTokens = []
var lines = script.split('\n')
Expand All @@ -780,12 +783,24 @@
{
var e = __JSLINT.errors[i]
if (!e) continue
log('JSLint error (' + e.line + ', ' + e.character + ')=' + e.reason)
log(lines[e.line])
var error = 'JSLint error (' + e.line + ', ' + e.character + ')=' + e.reason
var errorLine = lines[e.line]
var str = ''
for (var j=0; j<e.character-1; j++) str += ' '
str += '^'
log(str)
var errorPosition = str
if (errorArray)
{
var o = { error : error }
if (errorLine) o.line = errorLine, o.position = errorPosition
errorArray.addObject(o)
}
else
{
log(error)
log(errorLine)
log(errorPosition)
}
}
var useAutoCall = __jsc__.useAutoCall
if (typeof useAutoCall === 'function') useAutoCall = __jsc__.useAutoCall()
Expand Down
9 changes: 4 additions & 5 deletions TestsRunner/TestsRunner.xcodeproj/mini.mode2v3
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
<key>_historyCapacity</key>
<integer>0</integer>
<key>bookmark</key>
<string>3D98A4C211FD8FA100111BFE</string>
<string>3D98A4DB11FD8FFC00111BFE</string>
<key>history</key>
<array>
<string>3D98A34311FC794C00111BFE</string>
Expand Down Expand Up @@ -256,7 +256,7 @@
<key>_historyCapacity</key>
<integer>0</integer>
<key>bookmark</key>
<string>3D98A4C311FD8FA100111BFE</string>
<string>3D98A4DD11FD8FFC00111BFE</string>
<key>history</key>
<array>
<string>3D98A34511FC794C00111BFE</string>
Expand Down Expand Up @@ -355,7 +355,7 @@
<key>PBXSmartGroupTreeModuleOutlineStateSelectionKey</key>
<array>
<array>
<integer>45</integer>
<integer>33</integer>
<integer>26</integer>
<integer>25</integer>
<integer>0</integer>
Expand Down Expand Up @@ -562,15 +562,14 @@
<integer>0</integer>
<key>WindowOrderList</key>
<array>
<string>3D98A4CC11FD8FA100111BFE</string>
<string>3D98A4CD11FD8FA100111BFE</string>
<string>3D98A4CE11FD8FA100111BFE</string>
<string>1C530D54069F1CE1000CFCEE</string>
<string>1C530D52069F1CE1000CFCEE</string>
<string>3DB850A711E59E1500D03E1F</string>
<string>3D9E60CA11EB65FA0093D8C7</string>
<string>/Users/mini/Software Inexdo/JSCocoa/TestsRunner/TestsRunner.xcodeproj</string>
<string>1C530D5B069F1CE1000CFCEE</string>
<string>/Users/mini/Software Inexdo/JSCocoa/TestsRunner/TestsRunner.xcodeproj</string>
</array>
<key>WindowString</key>
<string>1090 736 449 442 0 0 1920 1178 </string>
Expand Down
66 changes: 22 additions & 44 deletions TestsRunner/TestsRunner.xcodeproj/mini.pbxuser
Original file line number Diff line number Diff line change
Expand Up @@ -66,49 +66,15 @@
PBXWorkspaceStateSaveDate = 301830039;
};
perUserProjectItems = {
3D11CF6C11ECC40C004B8518 = 3D11CF6C11ECC40C004B8518 /* PBXTextBookmark */;
3D11CF6E11ECC40C004B8518 = 3D11CF6E11ECC40C004B8518 /* PBXTextBookmark */;
3D98A34311FC794C00111BFE = 3D98A34311FC794C00111BFE /* PBXTextBookmark */;
3D98A34511FC794C00111BFE = 3D98A34511FC794C00111BFE /* PBXTextBookmark */;
3D98A4C211FD8FA100111BFE /* PBXTextBookmark */ = 3D98A4C211FD8FA100111BFE /* PBXTextBookmark */;
3D98A4C311FD8FA100111BFE /* PBXTextBookmark */ = 3D98A4C311FD8FA100111BFE /* PBXTextBookmark */;
3D98A34311FC794C00111BFE /* PBXTextBookmark */ = 3D98A34311FC794C00111BFE /* PBXTextBookmark */;
3D98A34511FC794C00111BFE /* PBXTextBookmark */ = 3D98A34511FC794C00111BFE /* PBXTextBookmark */;
3D98A4DB11FD8FFC00111BFE /* PBXTextBookmark */ = 3D98A4DB11FD8FFC00111BFE /* PBXTextBookmark */;
3D98A4DD11FD8FFC00111BFE /* PBXTextBookmark */ = 3D98A4DD11FD8FFC00111BFE /* PBXTextBookmark */;
};
sourceControlManager = 3D36E256105229C200132E23 /* Source Control */;
userBuildSettings = {
};
};
3D11CF6C11ECC40C004B8518 /* PBXTextBookmark */ = {
isa = PBXTextBookmark;
fRef = 3D11CF6D11ECC40C004B8518 /* 16 NSArray NSDictionary easy access.js */;
name = "16 NSArray NSDictionary easy access.js: 106";
rLen = 0;
rLoc = 2072;
rType = 0;
vrLen = 2089;
vrLoc = 835;
};
3D11CF6D11ECC40C004B8518 /* 16 NSArray NSDictionary easy access.js */ = {
isa = PBXFileReference;
lastKnownFileType = sourcecode.javascript;
name = "16 NSArray NSDictionary easy access.js";
path = "/Users/mini/Software Inexdo/JSCocoa/Tests/16 NSArray NSDictionary easy access.js";
sourceTree = "<absolute>";
uiCtxt = {
sepNavIntBoundsRect = "{{0, 0}, {1038, 1474}}";
sepNavSelRange = "{2072, 0}";
sepNavVisRange = "{774, 2046}";
};
};
3D11CF6E11ECC40C004B8518 /* PBXTextBookmark */ = {
isa = PBXTextBookmark;
fRef = 3D36E2B910522A2600132E23 /* ApplicationController.m */;
name = "ApplicationController.m: 150";
rLen = 0;
rLoc = 4224;
rType = 0;
vrLen = 2237;
vrLoc = 3004;
};
3D36E247105229B500132E23 /* TestsRunner */ = {
isa = PBXExecutable;
activeArgIndices = (
Expand Down Expand Up @@ -302,7 +268,7 @@
uiCtxt = {
sepNavIntBoundsRect = "{{0, 0}, {909, 9955}}";
sepNavSelRange = "{4224, 0}";
sepNavVisRange = "{3004, 2230}";
sepNavVisRange = "{3004, 2237}";
sepNavWindowFrame = "{{152, 138}, {968, 1004}}";
};
};
Expand Down Expand Up @@ -354,24 +320,36 @@
vrLen = 2237;
vrLoc = 3004;
};
3D98A4C211FD8FA100111BFE /* PBXTextBookmark */ = {
3D98A4DB11FD8FFC00111BFE /* PBXTextBookmark */ = {
isa = PBXTextBookmark;
fRef = 3D11CF6D11ECC40C004B8518 /* 16 NSArray NSDictionary easy access.js */;
fRef = 3D98A4DC11FD8FFC00111BFE /* 16 NSArray NSDictionary easy access.js */;
name = "16 NSArray NSDictionary easy access.js: 106";
rLen = 0;
rLoc = 2072;
rType = 0;
vrLen = 2046;
vrLen = 2150;
vrLoc = 774;
};
3D98A4C311FD8FA100111BFE /* PBXTextBookmark */ = {
3D98A4DC11FD8FFC00111BFE /* 16 NSArray NSDictionary easy access.js */ = {
isa = PBXFileReference;
name = "16 NSArray NSDictionary easy access.js";
path = "/Users/mini/Software Inexdo/JSCocoa/Tests/16 NSArray NSDictionary easy access.js";
sourceTree = "<absolute>";
uiCtxt = {
sepNavIntBoundsRect = "{{0, 0}, {1038, 1474}}";
sepNavSelRange = "{2072, 0}";
sepNavVisRange = "{774, 2150}";
sepNavWindowFrame = "{{130, 27}, {1097, 1041}}";
};
};
3D98A4DD11FD8FFC00111BFE /* PBXTextBookmark */ = {
isa = PBXTextBookmark;
fRef = 3D36E2B910522A2600132E23 /* ApplicationController.m */;
name = "ApplicationController.m: 150";
rLen = 0;
rLoc = 4224;
rType = 0;
vrLen = 2230;
vrLen = 2237;
vrLoc = 3004;
};
8D1107260486CEB800E47090 /* TestsRunner */ = {
Expand Down

0 comments on commit 99135c8

Please sign in to comment.