Closed Bug 720611 Opened 12 years ago Closed 12 years ago

JS shell function to eval with filename

Categories

(Core :: JavaScript Engine, defect)

x86
macOS
defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla12

People

(Reporter: dherman, Assigned: dherman)

Details

Attachments

(1 file)

The easiest way to shim a CommonJS-like functionality into the JS shell would be to provide a function

    evalAt(src, filename, lineno)

that evaluates source code with a given file name and line number. This would make it possible to wrap CommonJS source code loaded from a file with the necessary CommonJS boilerplate without losing source location information.

Dave
Assignee: general → dherman
Here's a simple patch. Seems ... not unreasonable. Anyone opposed? I'll probably r? someone tomorrow.

Dave
Not opposition per se, but why?  The JS shell is basically our own little internal testing repl, not particularly for public use; and slightly a demonstration of good, kosher JSAPI use.  What's the reason we'd be shimming something CommonJSy into it?
We run the dom.js test suite with the spidermonkey shell; it sure would be nice to have sane tracebacks. Right now, when you get a traceback that came from a <script> tag inside an html page that dom.js parsed and executed, you get dom.js as the filename and a line number that doesn't exist at all, instead of the url of the html file as the filename and the correct line number to the script inside the html file.
Ah, that makes sense.  Carry on, Mr. Bowditch.
> Not opposition per se, but why?  The JS shell is basically our own little
> internal testing repl, not particularly for public use; and slightly a
> demonstration of good, kosher JSAPI use.  What's the reason we'd be shimming
> something CommonJSy into it?

Narcissus's REPL is built to work with the SpiderMonkey shell, and I want to refactor the modules to be CommonJS so they can also work out-of-the-box in Node.

Dave
Attachment #591357 - Flags: review?(jwalden+bmo)
Comment on attachment 591357 [details] [diff] [review]
initial implementation

Review of attachment 591357 [details] [diff] [review]:
-----------------------------------------------------------------

::: js/src/shell/js.cpp
@@ +798,5 @@
> +        return false;
> +    }
> +
> +    JSString *code = JS_ValueToString(cx, JS_ARGV(cx, vp)[0]);
> +    if (!code)

You need to have a JS::Anchor<JSString*> anch(code) here, else (depending on the compiler) nothing will root |code|, which could cause its characters to be deallocated underneath you before you're done with them.

@@ +4118,5 @@
>  "revertVersion()          Revert previously set version number",
>  "options([option ...])    Get or toggle JavaScript options",
>  "load(['foo.js' ...])     Load files named by string arguments",
>  "evaluate(code)           Evaluate code as though it were the contents of a file",
> +"evalFrom(code, filename, lineno)\n"

I'd lean toward evalWithLocation as the name.  "from" descriptively just doesn't cut it for me.  If you're especially attached to the current name, tho, feel free to keep using it.
Attachment #591357 - Flags: review?(jwalden+bmo) → review+
Whiteboard: [inbound]
https://hg.mozilla.org/mozilla-central/rev/8d39654512d5

fwiw, the bug number in the commit message is wrong
Status: NEW → RESOLVED
Closed: 12 years ago
Resolution: --- → FIXED
Whiteboard: [inbound]
Target Milestone: --- → mozilla12
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: