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.
Created attachment 591357 [details] [diff] [review]
Here's a simple patch. Seems ... not unreasonable. Anyone opposed? I'll probably r? someone tomorrow.
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.
Comment on attachment 591357 [details] [diff] [review]
Review of attachment 591357 [details] [diff] [review]:
@@ +798,5 @@
> + return false;
> + }
> + JSString *code = JS_ValueToString(cx, JS_ARGV(cx, vp));
> + 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",
> "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.
fwiw, the bug number in the commit message is wrong