diff --git a/js/common/bootstrap/modules.js b/js/common/bootstrap/modules.js index 3d495d79c8..abab157051 100644 --- a/js/common/bootstrap/modules.js +++ b/js/common/bootstrap/modules.js @@ -87,23 +87,33 @@ Module.prototype.require = function (path) { // locate file and read content raw = ModuleCache["/internal"].exports.readFile(path); + // test for parse errors first and fail early if a parse error detected + if (! SYS_PARSE(raw.content, path)) { + throw "Javascript parse error in file '" + path + "'"; + } + // create a new sandbox and execute module = ModuleCache[path] = new Module(path); content = "(function (module, exports, require, print) {" + raw.content + "\n});"; - + f = SYS_EXECUTE(content, undefined, path); if (f === undefined) { throw "cannot create context function"; } - - f(module, - module.exports, - function(path) { return module.require(path); }, - ModuleCache["/internal"].exports.print); + + try { + f(module, + module.exports, + function(path) { return module.require(path); }, + ModuleCache["/internal"].exports.print); + } + catch (err) { + throw "Javascript exception in file '" + path + "': " + err.stack; + } return module.exports; }; diff --git a/lib/V8/v8-utils.cpp b/lib/V8/v8-utils.cpp index d9895e6ca8..80cea00a2b 100644 --- a/lib/V8/v8-utils.cpp +++ b/lib/V8/v8-utils.cpp @@ -430,7 +430,7 @@ static bool LoadJavaScriptFile (v8::Handle context, char* content = TRI_SlurpFile(TRI_UNKNOWN_MEM_ZONE, filename); if (content == 0) { - LOG_TRACE("cannot loaded java script file '%s': %s", filename, TRI_last_error()); + LOG_TRACE("cannot load java script file '%s': %s", filename, TRI_last_error()); return false; } @@ -529,6 +529,54 @@ static bool LoadJavaScriptDirectory (v8::Handle context, char const /// @{ //////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +/// @brief parse a Javascript snippet, but do not execute it +/// +/// @FUN{internal.parse(@FA{script})} +/// +/// Parses the @FA{script} code, but does not execute it. +/// Will return @LIT{true} if the code does not have a parse error, and throw +/// an exception otherwise. +//////////////////////////////////////////////////////////////////////////////// + +static v8::Handle JS_Parse (v8::Arguments const& argv) { + v8::HandleScope scope; + v8::TryCatch tryCatch; + + if (argv.Length() < 1) { + return scope.Close(v8::ThrowException(v8::String::New("usage: parse(