Bug 129911 - Better JSContext API for named evaluations (other than //# sourceURL)
Summary: Better JSContext API for named evaluations (other than //# sourceURL)
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2014-03-07 13:49 PST by Joseph Pecoraro
Modified: 2014-03-10 19:35 PDT (History)
4 users (show)

See Also:


Attachments
[PATCH] Proposed Fix (7.85 KB, patch)
2014-03-07 13:51 PST, Joseph Pecoraro
ggaren: review+
ggaren: commit-queue-
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Joseph Pecoraro 2014-03-07 13:49:05 PST
Useful for getting named evaluations in a JSContext Inspector.
Comment 1 Joseph Pecoraro 2014-03-07 13:49:18 PST
<rdar://problem/16257330>
Comment 2 Joseph Pecoraro 2014-03-07 13:51:34 PST
Created attachment 226162 [details]
[PATCH] Proposed Fix
Comment 3 Geoffrey Garen 2014-03-10 16:15:44 PDT
Comment on attachment 226162 [details]
[PATCH] Proposed Fix

View in context: https://bugs.webkit.org/attachment.cgi?id=226162&action=review

r=me, but I think you can do a bit better here.

> Source/JavaScriptCore/API/JSContext.h:87
> +@param sourceURL A URL for the script's source path and name. Used by debuggers and when reporting exceptions.

I think I would just say "source file" instead of "source path and name". I didn't immediately understand what you meant by "path and name".

I would also say "This parameter is informative only: it does not change the behavior of the script."

> Source/JavaScriptCore/API/JSContext.mm:113
>  - (JSValue *)evaluateScript:(NSString *)script
>  {
> -    JSValueRef exceptionValue = 0;
> +    JSValueRef exceptionValue = nullptr;
>      JSStringRef scriptJS = JSStringCreateWithCFString((CFStringRef)script);
> -    JSValueRef result = JSEvaluateScript(m_context, scriptJS, 0, 0, 0, &exceptionValue);
> +    JSValueRef result = JSEvaluateScript(m_context, scriptJS, nullptr, nullptr, 0, &exceptionValue);
> +    JSStringRelease(scriptJS);
> +
> +    if (exceptionValue)
> +        return [self valueFromNotifyException:exceptionValue];
> +
> +    return [JSValue valueWithJSValueRef:result inContext:self];
> +}
> +
> +- (JSValue *)evaluateScript:(NSString *)script withSourceURL:(NSURL *)sourceURL
> +{
> +    if (!sourceURL)
> +        return [self evaluateScript:script];
> +
> +    JSValueRef exceptionValue = nullptr;
> +    JSStringRef scriptJS = JSStringCreateWithCFString((CFStringRef)script);
> +    JSStringRef sourceURLJS = JSStringCreateWithCFString((CFStringRef)[sourceURL absoluteString]);
> +    JSValueRef result = JSEvaluateScript(m_context, scriptJS, nullptr, sourceURLJS, 0, &exceptionValue);
> +    JSStringRelease(sourceURLJS);
>      JSStringRelease(scriptJS);

I would have done this the other way around, and had -evaluateScript: call -evaluateScript:withURL: passing a null or empty URL. That way, there's only one copy of the string creation, script evaluation, and exception handling code.
Comment 4 Joseph Pecoraro 2014-03-10 19:35:06 PDT
<http://trac.webkit.org/changeset/165424>