diff options
author | Brian Smith <brian@dbsoft.org> | 2023-05-08 18:28:18 -0500 |
---|---|---|
committer | Brian Smith <brian@dbsoft.org> | 2023-05-08 18:28:18 -0500 |
commit | 13536bf8c99ce19f3e08ea3a1287e78b1811cd42 (patch) | |
tree | 9ef4e2459c23ae0193bd1cd86b37cdb3fafca96e /js/src | |
parent | 2971cffab479097415e383366561d514086d0d0b (diff) | |
download | uxp-13536bf8c99ce19f3e08ea3a1287e78b1811cd42.tar.gz |
Issue #2236 - Fix import.meta module error in lambdas by moving parseGoal() into SharedContext.
Based on https://bugzilla.mozilla.org/show_bug.cgi?id=1604792
Also remove ParseGoal being passed through Parser introduced in #1691 Part 2.
Diffstat (limited to 'js/src')
-rw-r--r-- | js/src/builtin/ReflectParse.cpp | 2 | ||||
-rw-r--r-- | js/src/frontend/BytecodeCompiler.cpp | 24 | ||||
-rw-r--r-- | js/src/frontend/Parser.cpp | 23 | ||||
-rw-r--r-- | js/src/frontend/Parser.h | 8 | ||||
-rw-r--r-- | js/src/frontend/SharedContext.h | 8 | ||||
-rw-r--r-- | js/src/jsapi.cpp | 3 | ||||
-rw-r--r-- | js/src/jsscript.cpp | 10 | ||||
-rw-r--r-- | js/src/jsscript.h | 21 | ||||
-rw-r--r-- | js/src/shell/js.cpp | 6 | ||||
-rw-r--r-- | js/src/vm/Debugger.cpp | 3 |
10 files changed, 63 insertions, 45 deletions
diff --git a/js/src/builtin/ReflectParse.cpp b/js/src/builtin/ReflectParse.cpp index 0205887ff6..f0b2001422 100644 --- a/js/src/builtin/ReflectParse.cpp +++ b/js/src/builtin/ReflectParse.cpp @@ -4055,7 +4055,7 @@ reflect_parse(JSContext* cx, uint32_t argc, Value* vp) return false; Parser<FullParseHandler> parser(cx, cx->tempLifoAlloc(), options, chars.begin().get(), chars.length(), /* foldConstants = */ false, usedNames, - nullptr, nullptr, target); + nullptr, nullptr); if (!parser.checkOptions()) return false; diff --git a/js/src/frontend/BytecodeCompiler.cpp b/js/src/frontend/BytecodeCompiler.cpp index 7f5b705f92..cdb386f249 100644 --- a/js/src/frontend/BytecodeCompiler.cpp +++ b/js/src/frontend/BytecodeCompiler.cpp @@ -74,9 +74,8 @@ class MOZ_STACK_CLASS BytecodeCompiler bool createScriptSource(Maybe<uint32_t> parameterListEnd); bool maybeCompressSource(); bool canLazilyParse(); - bool createParser(ParseGoal goal); - bool createSourceAndParser(ParseGoal goal, - Maybe<uint32_t> parameterListEnd = Nothing()); + bool createParser(); + bool createSourceAndParser(Maybe<uint32_t> parameterListEnd = Nothing()); // If toString{Start,End} are not explicitly passed, assume the script's // offsets in the source used to parse it are the same as what should be @@ -213,7 +212,7 @@ BytecodeCompiler::canLazilyParse() } bool -BytecodeCompiler::createParser(ParseGoal goal) +BytecodeCompiler::createParser() { usedNames.emplace(cx); if (!usedNames->init()) @@ -222,14 +221,14 @@ BytecodeCompiler::createParser(ParseGoal goal) if (canLazilyParse()) { syntaxParser.emplace(cx, alloc, options, sourceBuffer.get(), sourceBuffer.length(), /* foldConstants = */ false, *usedNames, - (Parser<SyntaxParseHandler>*) nullptr, (LazyScript*) nullptr, goal); + (Parser<SyntaxParseHandler>*) nullptr, (LazyScript*) nullptr); if (!syntaxParser->checkOptions()) return false; } parser.emplace(cx, alloc, options, sourceBuffer.get(), sourceBuffer.length(), - /* foldConstants = */ true, *usedNames, syntaxParser.ptrOr(nullptr), nullptr, goal); + /* foldConstants = */ true, *usedNames, syntaxParser.ptrOr(nullptr), nullptr); parser->sct = sourceCompressor; parser->ss = scriptSource; if (!parser->checkOptions()) @@ -240,12 +239,11 @@ BytecodeCompiler::createParser(ParseGoal goal) } bool -BytecodeCompiler::createSourceAndParser(ParseGoal goal, - Maybe<uint32_t> parameterListEnd /* = Nothing() */) +BytecodeCompiler::createSourceAndParser(Maybe<uint32_t> parameterListEnd /* = Nothing() */) { return createScriptSource(parameterListEnd) && maybeCompressSource() && - createParser(goal); + createParser(); } bool @@ -324,7 +322,7 @@ BytecodeCompiler::maybeCompleteCompressSource() JSScript* BytecodeCompiler::compileScript(HandleObject environment, SharedContext* sc) { - if (!createSourceAndParser(ParseGoal::Script)) + if (!createSourceAndParser()) return nullptr; if (!createScript()) @@ -394,7 +392,7 @@ BytecodeCompiler::compileEvalScript(HandleObject environment, HandleScope enclos ModuleObject* BytecodeCompiler::compileModule() { - if (!createSourceAndParser(ParseGoal::Module)) + if (!createSourceAndParser()) return nullptr; Rooted<ModuleObject*> module(cx, ModuleObject::create(cx)); @@ -451,7 +449,7 @@ BytecodeCompiler::compileStandaloneFunction(MutableHandleFunction fun, MOZ_ASSERT(fun); MOZ_ASSERT(fun->isTenured()); - if (!createSourceAndParser(ParseGoal::Script, parameterListEnd)) + if (!createSourceAndParser(parameterListEnd)) return false; // Speculatively parse using the default directives implied by the context. @@ -651,7 +649,7 @@ frontend::CompileLazyFunction(JSContext* cx, Handle<LazyScript*> lazy, const cha if (!usedNames.init()) return false; Parser<FullParseHandler> parser(cx, cx->tempLifoAlloc(), options, chars, length, - /* foldConstants = */ true, usedNames, nullptr, lazy, lazy->parseGoal()); + /* foldConstants = */ true, usedNames, nullptr, lazy); if (!parser.checkOptions()) return false; diff --git a/js/src/frontend/Parser.cpp b/js/src/frontend/Parser.cpp index 841eb5f096..d617941503 100644 --- a/js/src/frontend/Parser.cpp +++ b/js/src/frontend/Parser.cpp @@ -491,6 +491,9 @@ FunctionBox::initFromLazyFunction() setDerivedClassConstructor(); if (fun->lazyScript()->needsHomeObject()) setNeedsHomeObject(); + if (fun->lazyScript()->hasModuleGoal()) { + setHasModuleGoal(); + } enclosingScope_ = fun->lazyScript()->enclosingScope(); initWithEnclosingScope(enclosingScope_); } @@ -557,6 +560,9 @@ FunctionBox::initWithEnclosingParseContext(ParseContext* enclosing, FunctionSynt } } + // We inherit the parse goal from our top-level. + hasModuleGoal_ = sc->hasModuleGoal(); + if (sc->inWith()) { inWith_ = true; } else { @@ -787,8 +793,7 @@ ParserBase::ParserBase(ExclusiveContext* cx, LifoAlloc& alloc, bool foldConstants, UsedNameTracker& usedNames, Parser<SyntaxParseHandler>* syntaxParser, - LazyScript* lazyOuterFunction, - ParseGoal parseGoal) + LazyScript* lazyOuterFunction) : context(cx), alloc(alloc), tokenStream(cx, options, chars, length, thisForCtor()), @@ -804,8 +809,7 @@ ParserBase::ParserBase(ExclusiveContext* cx, LifoAlloc& alloc, #endif abortedSyntaxParse(false), isUnexpectedEOF_(false), - awaitHandling_(AwaitIsName), - parseGoal_(uint8_t(parseGoal)) + awaitHandling_(AwaitIsName) { cx->perThreadData->frontendCollectionPool.addActiveCompilation(); tempPoolMark = alloc.mark(); @@ -832,10 +836,9 @@ Parser<ParseHandler>::Parser(ExclusiveContext* cx, LifoAlloc& alloc, bool foldConstants, UsedNameTracker& usedNames, Parser<SyntaxParseHandler>* syntaxParser, - LazyScript* lazyOuterFunction, - ParseGoal parseGoal) + LazyScript* lazyOuterFunction) : ParserBase(cx, alloc, options, chars, length, foldConstants, usedNames, syntaxParser, - lazyOuterFunction, parseGoal), + lazyOuterFunction), AutoGCRooter(cx, PARSER), handler(cx, alloc, tokenStream, syntaxParser, lazyOuterFunction) { @@ -949,6 +952,7 @@ ModuleSharedContext::ModuleSharedContext(ExclusiveContext* cx, ModuleObject* mod builder(builder) { thisBinding_ = ThisBinding::Module; + hasModuleGoal_ = true; } template <typename ParseHandler> @@ -2471,8 +2475,7 @@ Parser<SyntaxParseHandler>::finishFunction(bool isStandaloneFunction /* = false pc->innerFunctionsForLazy, versionNumber(), funbox->bufStart, funbox->bufEnd, funbox->toStringStart, - funbox->startLine, funbox->startColumn, - parseGoal()); + funbox->startLine, funbox->startColumn); if (!lazy) return false; @@ -2496,6 +2499,8 @@ Parser<SyntaxParseHandler>::finishFunction(bool isStandaloneFunction /* = false lazy->setShouldDeclareArguments(); if (funbox->hasThisBinding()) lazy->setHasThisBinding(); + if (funbox->hasModuleGoal()) + lazy->setHasModuleGoal(); // Flags that need to copied back into the parser when we do the full // parse. diff --git a/js/src/frontend/Parser.h b/js/src/frontend/Parser.h index 1819873308..fd1bd034c4 100644 --- a/js/src/frontend/Parser.h +++ b/js/src/frontend/Parser.h @@ -817,8 +817,6 @@ class ParserBase : public StrictModeGetter /* AwaitHandling */ uint8_t awaitHandling_:2; - uint8_t parseGoal_:1; - public: bool awaitIsKeyword() const { return awaitHandling_ == AwaitIsKeyword || awaitHandling_ == AwaitIsModuleKeyword; @@ -828,13 +826,13 @@ class ParserBase : public StrictModeGetter } ParseGoal parseGoal() const { - return ParseGoal(parseGoal_); + return pc->sc()->hasModuleGoal() ? ParseGoal::Module : ParseGoal::Script; } ParserBase(ExclusiveContext* cx, LifoAlloc& alloc, const ReadOnlyCompileOptions& options, const char16_t* chars, size_t length, bool foldConstants, UsedNameTracker& usedNames, Parser<SyntaxParseHandler>* syntaxParser, - LazyScript* lazyOuterFunction, ParseGoal parseGoal); + LazyScript* lazyOuterFunction); ~ParserBase(); const char* getFilename() const { return tokenStream.getFilename(); } @@ -1072,7 +1070,7 @@ FOR_EACH_PARSENODE_SUBCLASS(DECLARE_TYPE) public: Parser(ExclusiveContext* cx, LifoAlloc& alloc, const ReadOnlyCompileOptions& options, const char16_t* chars, size_t length, bool foldConstants, UsedNameTracker& usedNames, - Parser<SyntaxParseHandler>* syntaxParser, LazyScript* lazyOuterFunction, ParseGoal parseGoal); + Parser<SyntaxParseHandler>* syntaxParser, LazyScript* lazyOuterFunction); ~Parser(); friend class AutoAwaitIsKeyword<ParseHandler>; diff --git a/js/src/frontend/SharedContext.h b/js/src/frontend/SharedContext.h index 29a8cbd18f..a241907482 100644 --- a/js/src/frontend/SharedContext.h +++ b/js/src/frontend/SharedContext.h @@ -249,6 +249,9 @@ class SharedContext bool inWith_; bool needsThisTDZChecks_; + // Script is being parsed with a goal of Module. + bool hasModuleGoal_ : 1; + void computeAllowSyntax(Scope* scope); void computeInWith(Scope* scope); void computeThisBinding(Scope* scope); @@ -267,7 +270,8 @@ class SharedContext allowSuperCall_(false), allowArguments_(true), inWith_(false), - needsThisTDZChecks_(false) + needsThisTDZChecks_(false), + hasModuleGoal_(false) { } // If this is the outermost SharedContext, the Scope that encloses @@ -287,6 +291,7 @@ class SharedContext ThisBinding thisBinding() const { return thisBinding_; } + bool hasModuleGoal() const { return hasModuleGoal_; } bool allowNewTarget() const { return allowNewTarget_; } bool allowSuperProperty() const { return allowSuperProperty_; } bool allowSuperCall() const { return allowSuperCall_; } @@ -303,6 +308,7 @@ class SharedContext void setBindingsAccessedDynamically() { anyCxFlags.bindingsAccessedDynamically = true; } void setHasDebuggerStatement() { anyCxFlags.hasDebuggerStatement = true; } void setHasDirectEval() { anyCxFlags.hasDirectEval = true; } + void setHasModuleGoal() { hasModuleGoal_ = true; } inline bool allBindingsClosedOver(); diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp index c0fa85bc68..50e3442ae8 100644 --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -4265,8 +4265,7 @@ JS_BufferIsCompilableUnit(JSContext* cx, HandleObject obj, const char* utf8, siz frontend::Parser<frontend::FullParseHandler> parser(cx, cx->tempLifoAlloc(), options, chars, length, /* foldConstants = */ true, - usedNames, nullptr, nullptr, - frontend::ParseGoal::Script); + usedNames, nullptr, nullptr); JS::WarningReporter older = JS::SetWarningReporter(cx, nullptr); if (!parser.checkOptions() || !parser.parse()) { // We ran into an error. If it was because we ran out of source, we diff --git a/js/src/jsscript.cpp b/js/src/jsscript.cpp index a3f5f07068..49a8a3ad04 100644 --- a/js/src/jsscript.cpp +++ b/js/src/jsscript.cpp @@ -2926,6 +2926,12 @@ JSScript::fullyInitFromEmitter(ExclusiveContext* cx, HandleScript script, Byteco script->bodyScopeIndex_ = bce->bodyScopeIndex; script->hasNonSyntacticScope_ = bce->outermostScope()->hasOnChain(ScopeKind::NonSyntactic); + if(bce->sc->hasModuleGoal()) { + LazyScript* lazy = script->maybeLazyScript(); + if(lazy) + lazy->setHasModuleGoal(); + } + if (bce->sc->isFunctionBox()) initFromFunctionBox(cx, script, bce->sc->asFunctionBox()); else if (bce->sc->isModuleContext()) @@ -4231,8 +4237,7 @@ LazyScript::Create(ExclusiveContext* cx, HandleFunction fun, Handle<GCVector<JSFunction*, 8>> innerFunctions, JSVersion version, uint32_t begin, uint32_t end, - uint32_t toStringStart, uint32_t lineno, uint32_t column, - frontend::ParseGoal parseGoal) + uint32_t toStringStart, uint32_t lineno, uint32_t column) { union { PackedView p; @@ -4255,7 +4260,6 @@ LazyScript::Create(ExclusiveContext* cx, HandleFunction fun, p.isLikelyConstructorWrapper = false; p.isDerivedClassConstructor = false; p.needsHomeObject = false; - p.parseGoal = uint32_t(parseGoal); LazyScript* res = LazyScript::CreateRaw(cx, fun, packedFields, begin, end, toStringStart, lineno, column); diff --git a/js/src/jsscript.h b/js/src/jsscript.h index 5d6c09dba6..78afa2eec2 100644 --- a/js/src/jsscript.h +++ b/js/src/jsscript.h @@ -2108,7 +2108,7 @@ class LazyScript : public gc::TenuredCell uint32_t isFieldInitializer : 1; uint32_t needsHomeObject : 1; uint32_t hasRest : 1; - uint32_t parseGoal : 1; + uint32_t hasModuleGoal : 1; }; union { @@ -2150,8 +2150,7 @@ class LazyScript : public gc::TenuredCell const frontend::AtomVector& closedOverBindings, Handle<GCVector<JSFunction*, 8>> innerFunctions, JSVersion version, uint32_t begin, uint32_t end, - uint32_t toStringStart, uint32_t lineno, uint32_t column, - frontend::ParseGoal parseGoal); + uint32_t toStringStart, uint32_t lineno, uint32_t column); // Create a LazyScript and initialize the closedOverBindings and the // innerFunctions with dummy values to be replaced in a later initialization @@ -2262,8 +2261,20 @@ class LazyScript : public gc::TenuredCell p_.isExprBody = true; } - frontend::ParseGoal parseGoal() const { - return frontend::ParseGoal(p_.parseGoal); + // This was added in Issue #2236 to compensate for the lack of + // Mozilla's ImmutableFlags feature, if ImmutableFlags ever gets + // ported remove the next 2 methods. + bool hasModuleGoal() const { + return p_.hasModuleGoal; + } + + void setHasModuleGoal() { + p_.hasModuleGoal = true; + } + + js::frontend::ParseGoal parseGoal() const { + return hasModuleGoal() ? js::frontend::ParseGoal::Module + : js::frontend::ParseGoal::Script; } bool strict() const { diff --git a/js/src/shell/js.cpp b/js/src/shell/js.cpp index 356ad0a78c..bab5cbb0b5 100644 --- a/js/src/shell/js.cpp +++ b/js/src/shell/js.cpp @@ -4342,8 +4342,7 @@ Parse(JSContext* cx, unsigned argc, Value* vp) if (!usedNames.init()) return false; Parser<FullParseHandler> parser(cx, cx->tempLifoAlloc(), options, chars, length, - /* foldConstants = */ true, usedNames, nullptr, nullptr, - ParseGoal::Script); + /* foldConstants = */ true, usedNames, nullptr, nullptr); if (!parser.checkOptions()) return false; @@ -4394,8 +4393,7 @@ SyntaxParse(JSContext* cx, unsigned argc, Value* vp) return false; Parser<frontend::SyntaxParseHandler> parser(cx, cx->tempLifoAlloc(), options, chars, length, false, - usedNames, nullptr, nullptr, - ParseGoal::Script); + usedNames, nullptr, nullptr); if (!parser.checkOptions()) return false; diff --git a/js/src/vm/Debugger.cpp b/js/src/vm/Debugger.cpp index 3986083824..b5fb94ca05 100644 --- a/js/src/vm/Debugger.cpp +++ b/js/src/vm/Debugger.cpp @@ -5084,8 +5084,7 @@ Debugger::isCompilableUnit(JSContext* cx, unsigned argc, Value* vp) frontend::Parser<frontend::FullParseHandler> parser(cx, cx->tempLifoAlloc(), options, chars.twoByteChars(), length, /* foldConstants = */ true, - usedNames, nullptr, nullptr, - frontend::ParseGoal::Script); + usedNames, nullptr, nullptr); JS::WarningReporter older = JS::SetWarningReporter(cx, nullptr); if (!parser.checkOptions() || !parser.parse()) { // We ran into an error. If it was because we ran out of memory we report |