From 84533f6bcfea41c494bad64ab2ed07ebb58ad7d5 Mon Sep 17 00:00:00 2001 From: Tooru Fujisawa Date: Thu, 24 Nov 2022 15:44:40 +0000 Subject: [PATCH] Bug 1797024 - Part 1: Move OOM handling out of frontend::WellKnownParserAtoms. r=nbp Differential Revision: https://phabricator.services.mozilla.com/D160206 --- js/src/frontend/ParserAtom.cpp | 33 ++++++++++++++++----------------- js/src/frontend/ParserAtom.h | 5 ++--- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/js/src/frontend/ParserAtom.cpp b/js/src/frontend/ParserAtom.cpp index 485772f53a38..9833379a032b 100644 --- a/js/src/frontend/ParserAtom.cpp +++ b/js/src/frontend/ParserAtom.cpp @@ -1238,8 +1238,7 @@ TaggedParserAtomIndex WellKnownParserAtoms::lookupTinyIndexUTF8( return lookupTinyIndex(reinterpret_cast(utf8Ptr), nbyte); } -bool WellKnownParserAtoms::initSingle(JSContext* cx, - const WellKnownAtomInfo& info, +bool WellKnownParserAtoms::initSingle(const WellKnownAtomInfo& info, TaggedParserAtomIndex index) { unsigned int len = info.length; const Latin1Char* str = reinterpret_cast(info.content); @@ -1259,34 +1258,33 @@ bool WellKnownParserAtoms::initSingle(JSContext* cx, // Save name for returning after moving entry into set. if (!wellKnownMap_.putNew(lookup, &info, index)) { - js::ReportOutOfMemory(cx); return false; } return true; } -bool WellKnownParserAtoms::init(JSContext* cx) { +bool WellKnownParserAtoms::init() { // Add well-known strings to the HashMap. The HashMap is used for dynamic // lookups later and does not change once this init method is complete. -#define COMMON_NAME_INIT_(_, NAME, _2) \ - if (!initSingle(cx, GetWellKnownAtomInfo(WellKnownAtomId::NAME), \ - TaggedParserAtomIndex::WellKnown::NAME())) { \ - return false; \ +#define COMMON_NAME_INIT_(_, NAME, _2) \ + if (!initSingle(GetWellKnownAtomInfo(WellKnownAtomId::NAME), \ + TaggedParserAtomIndex::WellKnown::NAME())) { \ + return false; \ } FOR_EACH_NONTINY_COMMON_PROPERTYNAME(COMMON_NAME_INIT_) #undef COMMON_NAME_INIT_ -#define COMMON_NAME_INIT_(NAME, _) \ - if (!initSingle(cx, GetWellKnownAtomInfo(WellKnownAtomId::NAME), \ - TaggedParserAtomIndex::WellKnown::NAME())) { \ - return false; \ +#define COMMON_NAME_INIT_(NAME, _) \ + if (!initSingle(GetWellKnownAtomInfo(WellKnownAtomId::NAME), \ + TaggedParserAtomIndex::WellKnown::NAME())) { \ + return false; \ } JS_FOR_EACH_PROTOTYPE(COMMON_NAME_INIT_) #undef COMMON_NAME_INIT_ -#define COMMON_NAME_INIT_(NAME) \ - if (!initSingle(cx, GetWellKnownAtomInfo(WellKnownAtomId::NAME), \ - TaggedParserAtomIndex::WellKnown::NAME())) { \ - return false; \ +#define COMMON_NAME_INIT_(NAME) \ + if (!initSingle(GetWellKnownAtomInfo(WellKnownAtomId::NAME), \ + TaggedParserAtomIndex::WellKnown::NAME())) { \ + return false; \ } JS_FOR_EACH_WELL_KNOWN_SYMBOL(COMMON_NAME_INIT_) #undef COMMON_NAME_INIT_ @@ -1307,7 +1305,8 @@ bool JSRuntime::initializeParserAtoms(JSContext* cx) { UniquePtr names( js_new()); - if (!names || !names->init(cx)) { + if (!names || !names->init()) { + js::ReportOutOfMemory(cx); return false; } diff --git a/js/src/frontend/ParserAtom.h b/js/src/frontend/ParserAtom.h index 016a89733c7b..e9726c8359e6 100644 --- a/js/src/frontend/ParserAtom.h +++ b/js/src/frontend/ParserAtom.h @@ -579,11 +579,10 @@ class WellKnownParserAtoms { WellKnownAtomInfoHasher, js::SystemAllocPolicy>; EntryMap wellKnownMap_; - bool initSingle(JSContext* cx, const WellKnownAtomInfo& info, - TaggedParserAtomIndex index); + bool initSingle(const WellKnownAtomInfo& info, TaggedParserAtomIndex index); public: - bool init(JSContext* cx); + bool init(); // Maximum length of any well known atoms. This can be increased if needed. static constexpr size_t MaxWellKnownLength = 32;