From 83abc5af599d4531c68c2e7a84caab9545854594 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Sat, 4 Jul 2020 23:12:13 +0000 Subject: Issue #618 - Report source position information (line/column) Report source position information for module export resolution failures. Ref: BZ 1362098 --- js/src/vm/SelfHosting.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'js/src/vm/SelfHosting.cpp') diff --git a/js/src/vm/SelfHosting.cpp b/js/src/vm/SelfHosting.cpp index 89750d61a..0dfeffc36 100644 --- a/js/src/vm/SelfHosting.cpp +++ b/js/src/vm/SelfHosting.cpp @@ -345,6 +345,50 @@ intrinsic_ThrowInternalError(JSContext* cx, unsigned argc, Value* vp) return false; } +static bool +intrinsic_GetErrorMessage(JSContext* cx, unsigned argc, Value* vp) +{ + CallArgs args = CallArgsFromVp(argc, vp); + MOZ_ASSERT(args.length() == 1); + MOZ_ASSERT(args[0].isInt32()); + + const JSErrorFormatString* errorString = GetErrorMessage(nullptr, args[0].toInt32()); + MOZ_ASSERT(errorString); + + MOZ_ASSERT(errorString->argCount == 0); + RootedString message(cx, JS_NewStringCopyZ(cx, errorString->format)); + if (!message) + return false; + + args.rval().setString(message); + return true; +} + +static bool +intrinsic_CreateModuleSyntaxError(JSContext* cx, unsigned argc, Value* vp) +{ + CallArgs args = CallArgsFromVp(argc, vp); + MOZ_ASSERT(args.length() == 4); + MOZ_ASSERT(args[0].isObject()); + MOZ_ASSERT(args[1].isInt32()); + MOZ_ASSERT(args[2].isInt32()); + MOZ_ASSERT(args[3].isString()); + + RootedModuleObject module(cx, &args[0].toObject().as()); + RootedString filename(cx, JS_NewStringCopyZ(cx, module->script()->filename())); + RootedString message(cx, args[3].toString()); + + RootedValue error(cx); + if (!JS::CreateError(cx, JSEXN_SYNTAXERR, nullptr, filename, args[1].toInt32(), + args[2].toInt32(), nullptr, message, &error)) + { + return false; + } + + args.rval().set(error); + return true; +} + /** * Handles an assertion failure in self-hosted code just like an assertion * failure in C++ code. Information about the failure can be provided in args[0]. @@ -2339,6 +2383,8 @@ static const JSFunctionSpec intrinsic_functions[] = { JS_FN("ThrowTypeError", intrinsic_ThrowTypeError, 4,0), JS_FN("ThrowSyntaxError", intrinsic_ThrowSyntaxError, 4,0), JS_FN("ThrowInternalError", intrinsic_ThrowInternalError, 4,0), + JS_FN("GetErrorMessage", intrinsic_GetErrorMessage, 1,0), + JS_FN("CreateModuleSyntaxError", intrinsic_CreateModuleSyntaxError, 4,0), JS_FN("AssertionFailed", intrinsic_AssertionFailed, 1,0), JS_FN("DumpMessage", intrinsic_DumpMessage, 1,0), JS_FN("OwnPropertyKeys", intrinsic_OwnPropertyKeys, 1,0), -- cgit v1.2.3