Title: [165424] trunk/Source/_javascript_Core
Revision
165424
Author
[email protected]
Date
2014-03-10 19:39:40 -0700 (Mon, 10 Mar 2014)

Log Message

Better JSContext API for named evaluations (other than //# sourceURL)
https://bugs.webkit.org/show_bug.cgi?id=129911

Reviewed by Geoffrey Garen.

* API/JSBase.h:
* API/JSContext.h:
* API/JSContext.mm:
(-[JSContext evaluateScript:]):
(-[JSContext evaluateScript:withSourceURL:]):
Add new evaluateScript:withSourceURL:.

* API/tests/testapi.c:
(main):
* API/tests/testapi.mm:
(testObjectiveCAPI):
Add tests for sourceURL in evaluate APIs. It should
affect the exception objects.

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/API/JSBase.h (165423 => 165424)


--- trunk/Source/_javascript_Core/API/JSBase.h	2014-03-11 02:26:36 UTC (rev 165423)
+++ trunk/Source/_javascript_Core/API/JSBase.h	2014-03-11 02:39:40 UTC (rev 165424)
@@ -101,7 +101,7 @@
 @param ctx The execution context to use.
 @param script A JSString containing the script to evaluate.
 @param thisObject The object to use as "this," or NULL to use the global object as "this."
-@param sourceURL A JSString containing a URL for the script's source file. This is only used when reporting exceptions. Pass NULL if you do not care to include source file information in exceptions.
+@param sourceURL A JSString containing a URL for the script's source file. This is used by debuggers and when reporting exceptions. Pass NULL if you do not care to include source file information.
 @param startingLineNumber An integer value specifying the script's starting line number in the file located at sourceURL. This is only used when reporting exceptions. The value is one-based, so the first line is line 1 and invalid values are clamped to 1.
 @param exception A pointer to a JSValueRef in which to store an exception, if any. Pass NULL if you do not care to store an exception.
 @result The JSValue that results from evaluating script, or NULL if an exception is thrown.

Modified: trunk/Source/_javascript_Core/API/JSContext.h (165423 => 165424)


--- trunk/Source/_javascript_Core/API/JSContext.h	2014-03-11 02:26:36 UTC (rev 165423)
+++ trunk/Source/_javascript_Core/API/JSContext.h	2014-03-11 02:39:40 UTC (rev 165424)
@@ -81,6 +81,15 @@
 - (JSValue *)evaluateScript:(NSString *)script;
 
 /*!
+@method
+@abstract Evaluate a string of _javascript_ code, with a URL for the script's source file.
+@param script A string containing the _javascript_ code to evaluate.
+@param sourceURL A URL for the script's source file. Used by debuggers and when reporting exceptions. This parameter is informative only: it does not change the behavior of the script.
+@result The last value generated by the script.
+*/
+- (JSValue *)evaluateScript:(NSString *)script withSourceURL:(NSURL *)sourceURL NS_AVAILABLE(10_10, 8_0);
+
+/*!
 @methodgroup Callback Accessors
 */
 /*!

Modified: trunk/Source/_javascript_Core/API/JSContext.mm (165423 => 165424)


--- trunk/Source/_javascript_Core/API/JSContext.mm	2014-03-11 02:26:36 UTC (rev 165423)
+++ trunk/Source/_javascript_Core/API/JSContext.mm	2014-03-11 02:39:40 UTC (rev 165424)
@@ -20,7 +20,7 @@
  * 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. 
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
 #include "config.h"
@@ -89,9 +89,17 @@
 
 - (JSValue *)evaluateScript:(NSString *)script
 {
-    JSValueRef exceptionValue = 0;
+    return [self evaluateScript:script withSourceURL:nil];
+}
+
+- (JSValue *)evaluateScript:(NSString *)script withSourceURL:(NSURL *)sourceURL
+{
+    JSValueRef exceptionValue = nullptr;
     JSStringRef scriptJS = JSStringCreateWithCFString((CFStringRef)script);
-    JSValueRef result = JSEvaluateScript(m_context, scriptJS, 0, 0, 0, &exceptionValue);
+    JSStringRef sourceURLJS = sourceURL ? JSStringCreateWithCFString((CFStringRef)[sourceURL absoluteString]) : nullptr;
+    JSValueRef result = JSEvaluateScript(m_context, scriptJS, nullptr, sourceURLJS, 0, &exceptionValue);
+    if (sourceURLJS)
+        JSStringRelease(sourceURLJS);
     JSStringRelease(scriptJS);
 
     if (exceptionValue)

Modified: trunk/Source/_javascript_Core/API/tests/testapi.c (165423 => 165424)


--- trunk/Source/_javascript_Core/API/tests/testapi.c	2014-03-11 02:26:36 UTC (rev 165423)
+++ trunk/Source/_javascript_Core/API/tests/testapi.c	2014-03-11 02:39:40 UTC (rev 165424)
@@ -1810,6 +1810,18 @@
     ASSERT(JSValueIsEqual(context, v, o, NULL));
     JSStringRelease(script);
 
+    exception = NULL;
+    script = JSStringCreateWithUTF8CString("rreturn Array;");
+    JSStringRef sourceURL = JSStringCreateWithUTF8CString("file:///foo/bar.js");
+    JSStringRef sourceURLKey = JSStringCreateWithUTF8CString("sourceURL");
+    JSEvaluateScript(context, script, NULL, sourceURL, 1, &exception);
+    ASSERT(exception);
+    v = JSObjectGetProperty(context, JSValueToObject(context, exception, NULL), sourceURLKey, NULL);
+    assertEqualsAsUTF8String(v, "file:///foo/bar.js");
+    JSStringRelease(script);
+    JSStringRelease(sourceURL);
+    JSStringRelease(sourceURLKey);
+
     // Verify that creating a constructor for a class with no static functions does not trigger
     // an assert inside putDirect or lead to a crash during GC. <https://bugs.webkit.org/show_bug.cgi?id=25785>
     nullDefinition = kJSClassDefinitionEmpty;
@@ -1838,6 +1850,7 @@
         }
 
         JSStringRelease(script);
+        exception = NULL;
         result = scriptObject ? JSScriptEvaluate(context, scriptObject, 0, &exception) : 0;
         if (result && JSValueIsUndefined(context, result))
             printf("PASS: Test script executed successfully.\n");

Modified: trunk/Source/_javascript_Core/API/tests/testapi.mm (165423 => 165424)


--- trunk/Source/_javascript_Core/API/tests/testapi.mm	2014-03-11 02:26:36 UTC (rev 165423)
+++ trunk/Source/_javascript_Core/API/tests/testapi.mm	2014-03-11 02:39:40 UTC (rev 165424)
@@ -617,6 +617,24 @@
 
     @autoreleasepool {
         JSContext *context = [[JSContext alloc] init];
+        __block bool emptyExceptionSourceURL = false;
+        context.exceptionHandler = ^(JSContext *, JSValue *exception) {
+            emptyExceptionSourceURL = [exception[@"sourceURL"] isUndefined];
+        };
+        [context evaluateScript:@"!@#$%^&*() THIS IS NOT VALID _javascript_ SYNTAX !@#$%^&*()"];
+        checkResult(@"evaluteScript: exception has no sourceURL", emptyExceptionSourceURL);
+
+        __block NSString *exceptionSourceURL = nil;
+        context.exceptionHandler = ^(JSContext *, JSValue *exception) {
+            exceptionSourceURL = [exception[@"sourceURL"] toString];
+        };
+        NSURL *url = "" fileURLWithPath:@"/foo/bar.js"];
+        [context evaluateScript:@"!@#$%^&*() THIS IS NOT VALID _javascript_ SYNTAX !@#$%^&*()" withSourceURL:url];
+        checkResult(@"evaluateScript:withSourceURL: exception has expected sourceURL", [exceptionSourceURL isEqualToString:[url absoluteString]]);
+    }
+
+    @autoreleasepool {
+        JSContext *context = [[JSContext alloc] init];
         context[@"callback"] = ^{
             JSContext *context = [JSContext currentContext];
             context.exception = [JSValue valueWithNewErrorFromMessage:@"Something went wrong." inContext:context];

Modified: trunk/Source/_javascript_Core/ChangeLog (165423 => 165424)


--- trunk/Source/_javascript_Core/ChangeLog	2014-03-11 02:26:36 UTC (rev 165423)
+++ trunk/Source/_javascript_Core/ChangeLog	2014-03-11 02:39:40 UTC (rev 165424)
@@ -1,3 +1,24 @@
+2014-03-10  Joseph Pecoraro  <[email protected]>
+
+        Better JSContext API for named evaluations (other than //# sourceURL)
+        https://bugs.webkit.org/show_bug.cgi?id=129911
+
+        Reviewed by Geoffrey Garen.
+
+        * API/JSBase.h:
+        * API/JSContext.h:
+        * API/JSContext.mm:
+        (-[JSContext evaluateScript:]):
+        (-[JSContext evaluateScript:withSourceURL:]):
+        Add new evaluateScript:withSourceURL:.
+
+        * API/tests/testapi.c:
+        (main):
+        * API/tests/testapi.mm:
+        (testObjectiveCAPI):
+        Add tests for sourceURL in evaluate APIs. It should
+        affect the exception objects.
+
 2014-03-10  Filip Pizlo  <[email protected]>
 
         Repatch should save and restore all used registers - not just temp ones - when making a call
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to