JS shell function to eval with filename

RESOLVED FIXED in mozilla12

Status

()

Core
JavaScript Engine
RESOLVED FIXED
5 years ago
5 years ago

People

(Reporter: dherman, Assigned: dherman)

Tracking

unspecified
mozilla12
x86
Mac OS X
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(1 attachment)

(Assignee)

Description

5 years ago
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)

Comment 1

5 years ago
Created attachment 591357 [details] [diff] [review]
initial implementation
Assignee: general → dherman
(Assignee)

Comment 2

5 years ago
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.
(Assignee)

Comment 6

5 years ago
> 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
(Assignee)

Updated

5 years ago
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+
(Assignee)

Comment 8

5 years ago
http://hg.mozilla.org/integration/mozilla-inbound/rev/8d39654512d5
(Assignee)

Updated

5 years ago
Whiteboard: [inbound]
https://hg.mozilla.org/mozilla-central/rev/8d39654512d5

fwiw, the bug number in the commit message is wrong
Status: NEW → RESOLVED
Last Resolved: 5 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.