Bug 1904747 - Do not report error or warning in JS_Utf8BufferIsCompilableUnit. r=bthrall

Differential Revision: https://phabricator.services.mozilla.com/D214913
This commit is contained in:
Tooru Fujisawa 2024-06-26 22:55:43 +00:00
parent 00846d113d
commit 6d4424d72c
4 changed files with 79 additions and 10 deletions

View File

@ -45,8 +45,8 @@ class SourceText;
* lines in a buffer until JS_Utf8BufferIsCompilableUnit is true, then pass it
* to the compiler.
*
* The provided buffer is interpreted as UTF-8 data. An error is reported if
* a UTF-8 encoding error is encountered.
* The provided buffer is interpreted as UTF-8 data. If a UTF-8 encoding error
* is encountered, reports an error to JSContext and returns *true*.
*/
extern JS_PUBLIC_API bool JS_Utf8BufferIsCompilableUnit(
JSContext* cx, JS::Handle<JSObject*> obj, const char* utf8, size_t length);

View File

@ -79,6 +79,7 @@ UNIFIED_SOURCES += [
"testIntern.cpp",
"testIntlAvailableLocales.cpp",
"testIntString.cpp",
"testIsCompilableUnit.cpp",
"testIsInsideNursery.cpp",
"testIsISOStyleDate.cpp",
"testIteratorObject.cpp",

View File

@ -0,0 +1,69 @@
/* 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 "mozilla/Utf8.h" // mozilla::Utf8Unit
#include <string_view>
#include "js/CompilationAndEvaluation.h" // JS_Utf8BufferIsCompilableUnit
#include "js/ErrorReport.h" // JSErrorReport
#include "js/GlobalObject.h" // JS::CurrentGlobalOrNull
#include "js/RootingAPI.h" // JS::Rooted
#include "js/SourceText.h" // JS::SourceText, JS::SourceOwnership
#include "js/Warnings.h" // JS::SetWarningReporter
#include "jsapi-tests/tests.h"
static bool gIsCompilableUnitWarned = false;
BEGIN_TEST(testIsCompilableUnit) {
JS::SetWarningReporter(cx, WarningReporter);
// Compilable case.
{
static constexpr std::string_view src = "1";
CHECK(JS_Utf8BufferIsCompilableUnit(cx, global, src.data(), src.length()));
CHECK(!JS_IsExceptionPending(cx));
CHECK(!gIsCompilableUnitWarned);
}
// Not compilable cases.
{
static constexpr std::string_view src = "1 + ";
CHECK(!JS_Utf8BufferIsCompilableUnit(cx, global, src.data(), src.length()));
CHECK(!JS_IsExceptionPending(cx));
CHECK(!gIsCompilableUnitWarned);
}
{
static constexpr std::string_view src = "() =>";
CHECK(!JS_Utf8BufferIsCompilableUnit(cx, global, src.data(), src.length()));
CHECK(!JS_IsExceptionPending(cx));
CHECK(!gIsCompilableUnitWarned);
}
// Compilable but warned case.
{
static constexpr std::string_view src = "() => { return; unreachable(); }";
CHECK(JS_Utf8BufferIsCompilableUnit(cx, global, src.data(), src.length()));
CHECK(!JS_IsExceptionPending(cx));
CHECK(!gIsCompilableUnitWarned);
}
// Invalid UTF-8 case.
// This should report error with returning *true*.
{
static constexpr std::string_view src = "\x80";
CHECK(JS_Utf8BufferIsCompilableUnit(cx, global, src.data(), src.length()));
CHECK(JS_IsExceptionPending(cx));
CHECK(!gIsCompilableUnitWarned);
JS_ClearPendingException(cx);
}
return true;
}
static void WarningReporter(JSContext* cx, JSErrorReport* report) {
gIsCompilableUnitWarned = true;
}
END_TEST(testIsCompilableUnit);

View File

@ -184,10 +184,6 @@ JS_PUBLIC_API bool JS_Utf8BufferIsCompilableUnit(JSContext* cx,
return true;
}
// Return true on any out-of-memory error or non-EOF-related syntax error, so
// our caller doesn't try to collect more buffered source.
bool result = true;
using frontend::FullParseHandler;
using frontend::ParseGoal;
using frontend::Parser;
@ -208,6 +204,9 @@ JS_PUBLIC_API bool JS_Utf8BufferIsCompilableUnit(JSContext* cx,
return false;
}
// Warnings and errors during parsing shouldn't be reported.
fc.clearAutoReport();
Parser<FullParseHandler, char16_t> parser(&fc, options, chars.get(), length,
/* foldConstants = */ true,
compilationState,
@ -217,13 +216,13 @@ JS_PUBLIC_API bool JS_Utf8BufferIsCompilableUnit(JSContext* cx,
// return false so our caller knows to try to collect more buffered
// source.
if (parser.isUnexpectedEOF()) {
result = false;
return false;
}
cx->clearPendingException();
}
return result;
// Return true on any out-of-memory error or non-EOF-related syntax error, so
// our caller doesn't try to collect more buffered source.
return true;
}
class FunctionCompiler {