forked from mirrors/gecko-dev
Bug 1904747 - Do not report error or warning in JS_Utf8BufferIsCompilableUnit. a=dmeehan
Original Revision: https://phabricator.services.mozilla.com/D214913 Differential Revision: https://phabricator.services.mozilla.com/D215084
This commit is contained in:
parent
2e0b8c2bd2
commit
e2240faf0b
4 changed files with 79 additions and 10 deletions
|
|
@ -45,8 +45,8 @@ class SourceText;
|
||||||
* lines in a buffer until JS_Utf8BufferIsCompilableUnit is true, then pass it
|
* lines in a buffer until JS_Utf8BufferIsCompilableUnit is true, then pass it
|
||||||
* to the compiler.
|
* to the compiler.
|
||||||
*
|
*
|
||||||
* The provided buffer is interpreted as UTF-8 data. An error is reported if
|
* The provided buffer is interpreted as UTF-8 data. If a UTF-8 encoding error
|
||||||
* a UTF-8 encoding error is encountered.
|
* is encountered, reports an error to JSContext and returns *true*.
|
||||||
*/
|
*/
|
||||||
extern JS_PUBLIC_API bool JS_Utf8BufferIsCompilableUnit(
|
extern JS_PUBLIC_API bool JS_Utf8BufferIsCompilableUnit(
|
||||||
JSContext* cx, JS::Handle<JSObject*> obj, const char* utf8, size_t length);
|
JSContext* cx, JS::Handle<JSObject*> obj, const char* utf8, size_t length);
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,7 @@ UNIFIED_SOURCES += [
|
||||||
"testIntern.cpp",
|
"testIntern.cpp",
|
||||||
"testIntlAvailableLocales.cpp",
|
"testIntlAvailableLocales.cpp",
|
||||||
"testIntString.cpp",
|
"testIntString.cpp",
|
||||||
|
"testIsCompilableUnit.cpp",
|
||||||
"testIsInsideNursery.cpp",
|
"testIsInsideNursery.cpp",
|
||||||
"testIsISOStyleDate.cpp",
|
"testIsISOStyleDate.cpp",
|
||||||
"testIteratorObject.cpp",
|
"testIteratorObject.cpp",
|
||||||
|
|
|
||||||
69
js/src/jsapi-tests/testIsCompilableUnit.cpp
Normal file
69
js/src/jsapi-tests/testIsCompilableUnit.cpp
Normal 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);
|
||||||
|
|
@ -184,10 +184,6 @@ JS_PUBLIC_API bool JS_Utf8BufferIsCompilableUnit(JSContext* cx,
|
||||||
return true;
|
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::FullParseHandler;
|
||||||
using frontend::ParseGoal;
|
using frontend::ParseGoal;
|
||||||
using frontend::Parser;
|
using frontend::Parser;
|
||||||
|
|
@ -208,6 +204,9 @@ JS_PUBLIC_API bool JS_Utf8BufferIsCompilableUnit(JSContext* cx,
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Warnings and errors during parsing shouldn't be reported.
|
||||||
|
fc.clearAutoReport();
|
||||||
|
|
||||||
Parser<FullParseHandler, char16_t> parser(&fc, options, chars.get(), length,
|
Parser<FullParseHandler, char16_t> parser(&fc, options, chars.get(), length,
|
||||||
/* foldConstants = */ true,
|
/* foldConstants = */ true,
|
||||||
compilationState,
|
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
|
// return false so our caller knows to try to collect more buffered
|
||||||
// source.
|
// source.
|
||||||
if (parser.isUnexpectedEOF()) {
|
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 {
|
class FunctionCompiler {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue