You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by sh...@apache.org on 2012/04/23 20:08:36 UTC

ios commit: Fixed CB-535 - Add a way to log JavaScript exceptions, parse errors, and get JS stack frame events (with line numbers, etc)

Updated Branches:
  refs/heads/master 39a2f4f25 -> 5d2e80490


Fixed CB-535 - Add a way to log JavaScript exceptions, parse errors, and get JS stack frame events (with line numbers, etc)


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/commit/5d2e8049
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/5d2e8049
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/5d2e8049

Branch: refs/heads/master
Commit: 5d2e8049034e832002b7607d1d7c7645e442e419
Parents: 39a2f4f
Author: Shazron Abdullah <sh...@apache.org>
Authored: Mon Apr 23 11:08:09 2012 -0700
Committer: Shazron Abdullah <sh...@apache.org>
Committed: Mon Apr 23 11:08:09 2012 -0700

----------------------------------------------------------------------
 CordovaLib/Classes/debugview/CDVDebugWebView.h     |   54 ++++
 CordovaLib/Classes/debugview/CDVDebugWebView.m     |  188 +++++++++++++++
 .../Classes/debugview/WebScriptDebugDelegate.h     |  135 +++++++++++
 LICENSE                                            |   37 +++-
 4 files changed, 411 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/5d2e8049/CordovaLib/Classes/debugview/CDVDebugWebView.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/debugview/CDVDebugWebView.h b/CordovaLib/Classes/debugview/CDVDebugWebView.h
new file mode 100644
index 0000000..232d320
--- /dev/null
+++ b/CordovaLib/Classes/debugview/CDVDebugWebView.h
@@ -0,0 +1,54 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License.  You may obtain a copy of the License at
+ 
+ http://www.apache.org/licenses/LICENSE-2.0
+ 
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied.  See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+#import "CDVCordovaView.h"
+// http://opensource.apple.com/source/WebKit/WebKit-6531.9/mac/WebView/WebScriptDebugDelegate.h
+#import "WebScriptDebugDelegate.h"
+
+@interface CDVDebugWebSourceData : NSObject {
+}
+
+@property (nonatomic, assign) WebSourceId sourceId;
+@property (nonatomic, assign) WebNSUInteger baseLineNumber;
+@property (nonatomic, retain) NSURL* fromURL;
+@property (nonatomic, retain) NSArray* sourceLines;
+
++ (NSString*) trimFilePathWithURL:(NSURL*)url;
+- (NSString*) trimFilePath;
+
+@end
+
+@interface CDVDebugWebDelegate : NSObject {
+}
+
+// key is source id, value is a CDVDebugWebSourceData object
+@property (nonatomic, retain) NSMutableDictionary* sourceDataDict;
+
+@end
+
+@interface CDVDebugWebView : CDVCordovaView {
+}
+@end
+
+@interface WebView {
+    
+}
+- (void) setScriptDebugDelegate:(id)delegate;
+
+@end

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/5d2e8049/CordovaLib/Classes/debugview/CDVDebugWebView.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/debugview/CDVDebugWebView.m b/CordovaLib/Classes/debugview/CDVDebugWebView.m
new file mode 100644
index 0000000..8714dd7
--- /dev/null
+++ b/CordovaLib/Classes/debugview/CDVDebugWebView.m
@@ -0,0 +1,188 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements.  See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership.  The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License.  You may obtain a copy of the License at
+ 
+ http://www.apache.org/licenses/LICENSE-2.0
+ 
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied.  See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+#import <Foundation/Foundation.h>
+#import "CDVDebugWebView.h"
+
+
+@implementation CDVDebugWebSourceData
+
+@synthesize sourceId, sourceLines, fromURL, baseLineNumber;
+
++ (NSString*) trimFilePathWithURL:(NSURL*)url;
+{
+    NSString* bundlePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/"];
+    NSString* urlPath = [url path];
+    
+    if ([urlPath hasPrefix:bundlePath]) {
+        urlPath = [urlPath substringFromIndex:[bundlePath length]];
+    }
+    
+    return urlPath;
+}
+
+- (NSString*) trimFilePath
+{
+    return [[self class] trimFilePathWithURL:self.fromURL];
+}
+
+@end
+
+/*
+ // in your app's MainViewController.m, override this:
+ - (CDVCordovaView*) newCordovaViewWithFrame:(CGRect)bounds
+ {
+    return [[CDVDebugWebView alloc] initWithFrame:bounds];
+ }
+ */
+
+
+@implementation CDVDebugWebDelegate
+
+@synthesize sourceDataDict;
+
+- (id) init
+{
+    self = [super init];
+    if (self) {
+        self.sourceDataDict = [NSMutableDictionary dictionaryWithCapacity:3];
+    }
+    
+    return self;
+}
+
+// some source was parsed, establishing a "source ID" (>= 0) for future reference
+// this delegate method is deprecated, please switch to the new version below
+- (void) webView:(WebView *)webView       didParseSource:(NSString *)source
+        fromURL:(NSString *)url
+       sourceId:(WebSourceId)sid
+    forWebFrame:(WebFrame *)webFrame
+{
+    NSLog(@"deprecated, use newer delegate function webView:didParseSource:baseLineNumber:fromURL:sourceId:forWebFrame"); 
+}
+
+// some source was parsed, establishing a "source ID" (>= 0) for future reference
+- (void) webView:(WebView *)webView       didParseSource:(NSString *)source
+ baseLineNumber:(WebNSUInteger)lineNumber
+        fromURL:(NSURL *)url
+       sourceId:(WebSourceId)sid
+    forWebFrame:(WebFrame *)webFrame
+{
+    CDVDebugWebSourceData* sourceData = [[[CDVDebugWebSourceData alloc] init] autorelease];
+    sourceData.sourceId = sid;
+    sourceData.sourceLines = [source componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
+    sourceData.fromURL = url;
+    // this is for example if you have JavaScript in a script tag, the baseLineNumber is the position of this script tag in the html file
+    sourceData.baseLineNumber = lineNumber;
+    
+    [self.sourceDataDict setObject:sourceData forKey:[NSNumber numberWithInt:sid]];
+}
+
+// some source failed to parse
+- (void) webView:(WebView *)webView  failedToParseSource:(NSString *)source
+ baseLineNumber:(WebNSUInteger)lineNumber
+        fromURL:(NSURL *)url
+      withError:(NSError *)error
+    forWebFrame:(WebFrame *)webFrame
+{
+    NSString* urlStr = url? [CDVDebugWebSourceData trimFilePathWithURL:url] : @"obj-c";                                     
+    NSLog(@"JavaScript parse error: (%@) - %@", urlStr, [error description]);
+}
+
+// just entered a stack frame (i.e. called a function, or started global scope)
+- (void) webView:(WebView *)webView    didEnterCallFrame:(WebScriptCallFrame *)frame
+       sourceId:(WebSourceId)sid
+           line:(int)lineno
+    forWebFrame:(WebFrame *)webFrame
+{
+    // TODO: do something? perhaps if we had a JS debugger UI thingy with breakpoint support
+}
+
+// about to execute some code
+- (void) webView:(WebView *)webView willExecuteStatement:(WebScriptCallFrame *)frame
+       sourceId:(WebSourceId)sid
+           line:(int)lineno
+    forWebFrame:(WebFrame *)webFrame
+{
+    // TODO: do something? perhaps if we had a JS debugger UI thingy with breakpoint support
+}
+
+// about to leave a stack frame (i.e. return from a function)
+- (void) webView:(WebView *)webView   willLeaveCallFrame:(WebScriptCallFrame *)frame
+       sourceId:(WebSourceId)sid
+           line:(int)lineno
+    forWebFrame:(WebFrame *)webFrame
+{
+    // TODO: do something? perhaps if we had a JS debugger UI thingy with breakpoint support
+}
+
+// exception is being thrown
+- (void) webView:(WebView *)webView   exceptionWasRaised:(WebScriptCallFrame *)frame
+       sourceId:(WebSourceId)sid
+           line:(int)lineno
+    forWebFrame:(WebFrame *)webFrame
+{
+    WebScriptObject* exception = [frame exception]; // this is the bound JavaScript Error object which has two properties: name and message
+    // we use KVC to extract the name and message
+    NSString* exceptionName = [exception valueForKey:@"name"];
+    NSString* exceptionMessage = [exception valueForKey:@"message"];
+
+    CDVDebugWebSourceData* sourceData = [self.sourceDataDict objectForKey:[NSNumber numberWithInt:sid]];
+    
+    NSString* url = sourceData.fromURL? [sourceData trimFilePath] : @"obj-c";
+    NSString* line = [sourceData.sourceLines objectAtIndex:lineno-1];
+
+    NSLog(@"JavaScript exception: (%@):%d - %@ - %@\n\tLine: %@", url, lineno, exceptionName, exceptionMessage, line);
+}
+
+@end
+
+@implementation CDVDebugWebView
+
+- (id) initWithFrame:(CGRect)bounds
+{
+    self = [super initWithFrame:bounds];
+    if (self) 
+    {
+        NSString* message = [NSString stringWithFormat:
+                             NSLocalizedString(
+                                            @"You are using the %@ class which should not be included in builds for the Apple App Store.",
+                                            @"You are using the %@ class which should not be included in builds for the Apple App Store."
+                                               ), NSStringFromClass([self class])]; 
+        
+        UIAlertView* alertView = [[UIAlertView alloc]
+                                   initWithTitle:@"WARNING!"
+                                   message:message 
+                                   delegate:self 
+                                   cancelButtonTitle:nil 
+                                   otherButtonTitles:@"OK",nil];
+        
+        [alertView show];
+        [alertView release];
+    }
+    return self;
+}
+
+- (void) webView:(id)sender didClearWindowObject:(id)windowObject forFrame:(WebFrame*)frame
+{
+    if ([sender respondsToSelector:@selector(setScriptDebugDelegate:)]) {
+        [sender setScriptDebugDelegate:[[CDVDebugWebDelegate alloc] init]];
+    }
+}
+@end

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/5d2e8049/CordovaLib/Classes/debugview/WebScriptDebugDelegate.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/debugview/WebScriptDebugDelegate.h b/CordovaLib/Classes/debugview/WebScriptDebugDelegate.h
new file mode 100644
index 0000000..ef73f3c
--- /dev/null
+++ b/CordovaLib/Classes/debugview/WebScriptDebugDelegate.h
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2005 Apple Computer, Inc.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1.  Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer. 
+ * 2.  Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution. 
+ * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
+ *     its contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission. 
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import <Foundation/Foundation.h>
+
+typedef NSUInteger WebNSUInteger;
+typedef int WebSourceId;
+
+@class WebView;
+@class WebFrame;
+@class WebScriptCallFrame;
+@class WebScriptCallFramePrivate;
+@class WebScriptObject;
+
+extern NSString * const WebScriptErrorDomain;
+extern NSString * const WebScriptErrorDescriptionKey;
+extern NSString * const WebScriptErrorLineNumberKey;
+
+enum {
+    WebScriptGeneralErrorCode = -100
+};
+
+// WebScriptDebugDelegate messages
+
+@interface NSObject (WebScriptDebugDelegate)
+
+// some source was parsed, establishing a "source ID" (>= 0) for future reference
+// this delegate method is deprecated, please switch to the new version below
+- (void)webView:(WebView *)webView       didParseSource:(NSString *)source
+                                                fromURL:(NSString *)url
+                                               sourceId:(WebSourceId)sid
+                                            forWebFrame:(WebFrame *)webFrame;
+
+// some source was parsed, establishing a "source ID" (>= 0) for future reference
+- (void)webView:(WebView *)webView       didParseSource:(NSString *)source
+                                         baseLineNumber:(WebNSUInteger)lineNumber
+                                                fromURL:(NSURL *)url
+                                               sourceId:(WebSourceId)sid
+                                            forWebFrame:(WebFrame *)webFrame;
+
+// some source failed to parse
+- (void)webView:(WebView *)webView  failedToParseSource:(NSString *)source
+                                         baseLineNumber:(WebNSUInteger)lineNumber
+                                                fromURL:(NSURL *)url
+                                              withError:(NSError *)error
+                                            forWebFrame:(WebFrame *)webFrame;
+
+// just entered a stack frame (i.e. called a function, or started global scope)
+- (void)webView:(WebView *)webView    didEnterCallFrame:(WebScriptCallFrame *)frame
+                                               sourceId:(WebSourceId)sid
+                                                   line:(int)lineno
+                                            forWebFrame:(WebFrame *)webFrame;
+
+// about to execute some code
+- (void)webView:(WebView *)webView willExecuteStatement:(WebScriptCallFrame *)frame
+                                               sourceId:(WebSourceId)sid
+                                                   line:(int)lineno
+                                            forWebFrame:(WebFrame *)webFrame;
+
+// about to leave a stack frame (i.e. return from a function)
+- (void)webView:(WebView *)webView   willLeaveCallFrame:(WebScriptCallFrame *)frame
+                                               sourceId:(WebSourceId)sid
+                                                   line:(int)lineno
+                                            forWebFrame:(WebFrame *)webFrame;
+
+// exception is being thrown
+- (void)webView:(WebView *)webView   exceptionWasRaised:(WebScriptCallFrame *)frame
+                                               sourceId:(WebSourceId)sid
+                                                   line:(int)lineno
+                                            forWebFrame:(WebFrame *)webFrame;
+@end
+
+
+
+// WebScriptCallFrame interface
+//
+// These objects are passed as arguments to the debug delegate.
+
+@interface WebScriptCallFrame : NSObject
+{
+@private
+    WebScriptCallFramePrivate* _private;
+    id                         _userInfo;
+}
+
+// associate user info with frame
+- (void)setUserInfo:(id)userInfo;
+
+// retrieve user info
+- (id)userInfo;
+
+// get next frame on call stack (or nil if this is already the "global" frame)
+- (WebScriptCallFrame *)caller;
+
+// get array of WebScriptObjects for each scope (innermost first, last is always global object)
+- (NSArray *)scopeChain;
+
+// get name of function (if available) or nil
+- (NSString *)functionName;
+
+// get pending exception (if any) or nil
+- (id)exception;
+
+// evaluate a script (as if by "eval") in the context of this frame
+- (id)evaluateWebScript:(NSString *)script;
+
+@end
+
+#undef WebNSUInteger
+

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/5d2e8049/LICENSE
----------------------------------------------------------------------
diff --git a/LICENSE b/LICENSE
index 5461491..1abba1e 100644
--- a/LICENSE
+++ b/LICENSE
@@ -204,7 +204,38 @@
  ADDITIONAL LICENSES:
  
  /*
- File: PhoneGapLib/classes/Reachability.*
+ File: CordovaLib/classes/debugview/WebScriptDebugDelegate.h
+ Abstract: Delegate functions for JavaScript execution in UIWebView.
+ *
+ * Copyright (C) 2005 Apple Computer, Inc.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1.  Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer. 
+ * 2.  Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution. 
+ * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
+ *     its contributors may be used to endorse or promote products derived
+ *     from this software without specific prior written permission. 
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+ /*
+ File: CordovaLib/classes/Reachability.*
  Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs.
  
  Version: 2.2
@@ -250,7 +281,7 @@
 */
 
 //
-//  PhoneGapLib/classes/JSONKit.*
+//  CordovaLib/classes/JSONKit.*
 //  http://github.com/johnezang/JSONKit
 //  Dual licensed under either the terms of the BSD License, or alternatively
 //  under the terms of the Apache License, Version 2.0, as specified below.
@@ -349,7 +380,7 @@
 */
 
 //
-//  PhoneGapLib/classes/NSData+Base64.*
+//  CordovaLib/classes/NSData+Base64.*
 //  base64
 //
 //  Created by Matt Gallagher on 2009/06/03.