From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- js/src/jsapi-tests/testErrorLineOfContext.cpp | 74 +++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 js/src/jsapi-tests/testErrorLineOfContext.cpp (limited to 'js/src/jsapi-tests/testErrorLineOfContext.cpp') diff --git a/js/src/jsapi-tests/testErrorLineOfContext.cpp b/js/src/jsapi-tests/testErrorLineOfContext.cpp new file mode 100644 index 0000000000..ecfd895527 --- /dev/null +++ b/js/src/jsapi-tests/testErrorLineOfContext.cpp @@ -0,0 +1,74 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "js/CompilationAndEvaluation.h" +#include "js/Exception.h" +#include "js/GlobalObject.h" +#include "js/SourceText.h" +#include "jsapi-tests/tests.h" +#include "vm/ErrorReporting.h" + +BEGIN_TEST(testErrorLineOfContext) { + static const char16_t fullLineR[] = u"\n var x = @; \r "; + CHECK(testLineOfContextHasNoLineTerminator(fullLineR, ' ')); + + static const char16_t fullLineN[] = u"\n var x = @; !\n "; + CHECK(testLineOfContextHasNoLineTerminator(fullLineN, '!')); + + static const char16_t fullLineLS[] = u"\n var x = @; +\u2028 "; + CHECK(testLineOfContextHasNoLineTerminator(fullLineLS, '+')); + + static const char16_t fullLinePS[] = u"\n var x = @; #\u2029 "; + CHECK(testLineOfContextHasNoLineTerminator(fullLinePS, '#')); + + static_assert(js::ErrorMetadata::lineOfContextRadius == 60, + "current max count past offset is 60, hits 'X' below"); + + static const char16_t truncatedLine[] = + u"@ + 4567890123456789012345678901234567890123456789012345678XYZW\n"; + CHECK(testLineOfContextHasNoLineTerminator(truncatedLine, 'X')); + + return true; +} + +bool eval(const char16_t* chars, size_t len, JS::MutableHandleValue rval) { + JS::RealmOptions globalOptions; + JS::RootedObject global( + cx, JS_NewGlobalObject(cx, getGlobalClass(), nullptr, + JS::FireOnNewGlobalHook, globalOptions)); + CHECK(global); + + JSAutoRealm ar(cx, global); + + JS::SourceText srcBuf; + CHECK(srcBuf.init(cx, chars, len, JS::SourceOwnership::Borrowed)); + + JS::CompileOptions options(cx); + return JS::Evaluate(cx, options, srcBuf, rval); +} + +template +bool testLineOfContextHasNoLineTerminator(const char16_t (&chars)[N], + char16_t expectedLast) { + JS::RootedValue rval(cx); + CHECK(!eval(chars, N - 1, &rval)); + + JS::ExceptionStack exnStack(cx); + CHECK(JS::StealPendingExceptionStack(cx, &exnStack)); + + JS::ErrorReportBuilder report(cx); + CHECK(report.init(cx, exnStack, JS::ErrorReportBuilder::WithSideEffects)); + + const auto* errorReport = report.report(); + + const char16_t* lineOfContext = errorReport->linebuf(); + size_t lineOfContextLength = errorReport->linebufLength(); + + CHECK(lineOfContext[lineOfContextLength] == '\0'); + char16_t last = lineOfContext[lineOfContextLength - 1]; + CHECK(last == expectedLast); + + return true; +} +END_TEST(testErrorLineOfContext) -- cgit v1.2.3