diff options
author | isaacs <i@izs.me> | 2012-07-28 14:00:27 -0700 |
---|---|---|
committer | isaacs <i@izs.me> | 2012-07-30 08:21:39 -0700 |
commit | b3cf3f35fcf192c717e7a393fb984bf6f879d0cc (patch) | |
tree | 2272eb5e82f700ffd83afe2b167aab50d085f75a /src | |
parent | 72bc4dcda4cfa99ed064419e40d104bd1b2e0e25 (diff) | |
download | node-new-b3cf3f35fcf192c717e7a393fb984bf6f879d0cc.tar.gz |
Report errors properly from --eval and stdin
Diffstat (limited to 'src')
-rw-r--r-- | src/node.cc | 49 | ||||
-rw-r--r-- | src/node.js | 16 | ||||
-rw-r--r-- | src/node_script.cc | 1 |
3 files changed, 45 insertions, 21 deletions
diff --git a/src/node.cc b/src/node.cc index 257b96e9eb..a1e9fd8475 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1214,8 +1214,15 @@ ssize_t DecodeWrite(char *buf, return buflen; } - void DisplayExceptionLine (TryCatch &try_catch) { + // Prevent re-entry into this function. For example, if there is + // a throw from a program in vm.runInThisContext(code, filename, true), + // then we want to show the original failure, not the secondary one. + static bool displayed_error = false; + + if (displayed_error) return; + displayed_error = true; + HandleScope scope; Handle<Message> message = try_catch.Message(); @@ -1234,33 +1241,37 @@ void DisplayExceptionLine (TryCatch &try_catch) { String::Utf8Value sourceline(message->GetSourceLine()); const char* sourceline_string = *sourceline; - // HACK HACK HACK - // - // FIXME - // - // Because of how CommonJS modules work, all scripts are wrapped with a - // "function (function (exports, __filename, ...) {" + // Because of how node modules work, all scripts are wrapped with a + // "function (module, exports, __filename, ...) {" // to provide script local variables. // // When reporting errors on the first line of a script, this wrapper - // function is leaked to the user. This HACK is to remove it. The length - // of the wrapper is 62. That wrapper is defined in src/node.js + // function is leaked to the user. There used to be a hack here to + // truncate off the first 62 characters, but it caused numerous other + // problems when vm.runIn*Context() methods were used for non-module + // code. + // + // If we ever decide to re-instate such a hack, the following steps + // must be taken: // - // If that wrapper is ever changed, then this number also has to be - // updated. Or - someone could clean this up so that the two peices - // don't need to be changed. + // 1. Pass a flag around to say "this code was wrapped" + // 2. Update the stack frame output so that it is also correct. // - // Even better would be to get support into V8 for wrappers that - // shouldn't be reported to users. - int offset = linenum == 1 ? 62 : 0; + // It would probably be simpler to add a line rather than add some + // number of characters to the first line, since V8 truncates the + // sourceline to 78 characters, and we end up not providing very much + // useful debugging info to the user if we remove 62 characters. - fprintf(stderr, "%s\n", sourceline_string + offset); - // Print wavy underline (GetUnderline is deprecated). int start = message->GetStartColumn(); - for (int i = offset; i < start; i++) { + int end = message->GetEndColumn(); + + // fprintf(stderr, "---\nsourceline:%s\noffset:%d\nstart:%d\nend:%d\n---\n", sourceline_string, start, end); + + fprintf(stderr, "%s\n", sourceline_string); + // Print wavy underline (GetUnderline is deprecated). + for (int i = 0; i < start; i++) { fputc((sourceline_string[i] == '\t') ? '\t' : ' ', stderr); } - int end = message->GetEndColumn(); for (int i = start; i < end; i++) { fputc('^', stderr); } diff --git a/src/node.js b/src/node.js index eb31b4b332..071016e0d0 100644 --- a/src/node.js +++ b/src/node.js @@ -73,7 +73,7 @@ } else if (process._eval != null) { // User passed '-e' or '--eval' arguments to Node. - evalScript('eval'); + evalScript('[eval]'); } else if (process.argv[1]) { // make process.argv[1] into a full path var path = NativeModule.require('path'); @@ -267,7 +267,19 @@ var module = new Module(name); module.filename = path.join(cwd, name); module.paths = Module._nodeModulePaths(cwd); - var result = module._compile('return eval(process._eval)', name); + var script = process._eval; + if (!Module._contextLoad) { + var body = script; + script = 'global.__filename = ' + JSON.stringify(name) + ';\n' + + 'global.exports = exports;\n' + + 'global.module = module;\n' + + 'global.__dirname = __dirname;\n' + + 'global.require = require;\n' + + 'return require("vm").runInThisContext(' + + JSON.stringify(body) + ', ' + + JSON.stringify(name) + ', true);\n'; + } + var result = module._compile(script, name + '-wrapper'); if (process._print_eval) console.log(result); } diff --git a/src/node_script.cc b/src/node_script.cc index 22693dc9b2..15c56122c7 100644 --- a/src/node_script.cc +++ b/src/node_script.cc @@ -417,6 +417,7 @@ Handle<Value> WrappedScript::EvalMachine(const Arguments& args) { if (output_flag == returnResult) { result = script->Run(); if (result.IsEmpty()) { + if (display_error) DisplayExceptionLine(try_catch); return try_catch.ReThrow(); } } else { |