From 8b418d9be73dafd25c9c528127274a3573f1a7e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCri=20Valdmann?= Date: Fri, 5 Oct 2018 14:17:05 +0200 Subject: Clone ContextType::Global as ContextType::ScriptImportedByQML Add new enum value QV4::Compiler::ContextType::ScriptImportedByQML, which behaves exactly the same as ContextType::Global. A follow-up patch will change the behavior slightly. Task-number: QTBUG-69408 Change-Id: I20d27804fd1433f2229704546bcd78a0ac108c01 Reviewed-by: Simon Hausmann --- src/qml/compiler/qv4codegen.cpp | 2 +- src/qml/compiler/qv4compilercontext.cpp | 9 +++++---- src/qml/compiler/qv4compilercontext_p.h | 5 +++-- src/qml/compiler/qv4compilerscanfunctions.cpp | 2 +- src/qml/jsruntime/qv4script.cpp | 5 +++-- src/qml/jsruntime/qv4script_p.h | 3 ++- src/qml/qml/qqmltypeloader.cpp | 2 +- 7 files changed, 16 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/qml/compiler/qv4codegen.cpp b/src/qml/compiler/qv4codegen.cpp index e55f4022d6..295ce08071 100644 --- a/src/qml/compiler/qv4codegen.cpp +++ b/src/qml/compiler/qv4codegen.cpp @@ -2992,7 +2992,7 @@ int Codegen::defineFunction(const QString &name, AST::Node *ast, ControlFlow *savedControlFlow = controlFlow; controlFlow = nullptr; - if (_context->contextType == ContextType::Global) { + if (_context->contextType == ContextType::Global || _context->contextType == ContextType::ScriptImportedByQML) { _module->blocks.append(_context); _context->blockIndex = _module->blocks.count() - 1; } diff --git a/src/qml/compiler/qv4compilercontext.cpp b/src/qml/compiler/qv4compilercontext.cpp index b423dcf0b7..572f24f148 100644 --- a/src/qml/compiler/qv4compilercontext.cpp +++ b/src/qml/compiler/qv4compilercontext.cpp @@ -207,7 +207,7 @@ void Context::emitBlockHeader(Codegen *codegen) blockIndex = codegen->module()->blocks.count() - 1; } - if (contextType == ContextType::Global) { + if (contextType == ContextType::Global || contextType == ContextType::ScriptImportedByQML) { Instruction::PushScriptContext scriptContext; scriptContext.index = blockIndex; bytecodeGenerator->addInstruction(scriptContext); @@ -256,7 +256,7 @@ void Context::emitBlockHeader(Codegen *codegen) r.storeConsumeAccumulator(); } - if (contextType == ContextType::Global || (contextType == ContextType::Eval && !isStrict)) { + if (contextType == ContextType::Global || contextType == ContextType::ScriptImportedByQML || (contextType == ContextType::Eval && !isStrict)) { // variables in global code are properties of the global context object, not locals as with other functions. for (Context::MemberMap::const_iterator it = members.constBegin(), cend = members.constEnd(); it != cend; ++it) { if (it->isLexicallyScoped()) @@ -316,7 +316,7 @@ void Context::emitBlockFooter(Codegen *codegen) QT_WARNING_PUSH QT_WARNING_DISABLE_GCC("-Wmaybe-uninitialized") // the loads below are empty structs. - if (contextType == ContextType::Global) + if (contextType == ContextType::Global || contextType == ContextType::ScriptImportedByQML) bytecodeGenerator->addInstruction(Instruction::PopScriptContext()); else if (contextType != ContextType::ESModule) bytecodeGenerator->addInstruction(Instruction::PopContext()); @@ -371,9 +371,10 @@ void Context::setupFunctionIndices(Moth::BytecodeGenerator *bytecodeGenerator) break; } case ContextType::Global: + case ContextType::ScriptImportedByQML: case ContextType::Eval: for (Context::MemberMap::iterator it = members.begin(), end = members.end(); it != end; ++it) { - if (!it->isLexicallyScoped() && (contextType == ContextType::Global || !isStrict)) + if (!it->isLexicallyScoped() && (contextType == ContextType::Global || contextType == ContextType::ScriptImportedByQML || !isStrict)) continue; if (it->canEscape) registerLocal(it); diff --git a/src/qml/compiler/qv4compilercontext_p.h b/src/qml/compiler/qv4compilercontext_p.h index 796d5e274c..d36ef0f447 100644 --- a/src/qml/compiler/qv4compilercontext_p.h +++ b/src/qml/compiler/qv4compilercontext_p.h @@ -75,7 +75,8 @@ enum class ContextType { // * return statements are allowed everywhere (like in FunctionCode) // * variable declarations are treated as true locals (like in FunctionCode) Block, - ESModule + ESModule, + ScriptImportedByQML, }; struct Context; @@ -313,7 +314,7 @@ struct Context { bool requiresImplicitReturnValue() const { return contextType == ContextType::Binding || contextType == ContextType::Eval || - contextType == ContextType::Global; + contextType == ContextType::Global || contextType == ContextType::ScriptImportedByQML; } void addUsedVariable(const QString &name) { diff --git a/src/qml/compiler/qv4compilerscanfunctions.cpp b/src/qml/compiler/qv4compilerscanfunctions.cpp index 6a56b7d1a0..2026e64929 100644 --- a/src/qml/compiler/qv4compilerscanfunctions.cpp +++ b/src/qml/compiler/qv4compilerscanfunctions.cpp @@ -819,7 +819,7 @@ void ScanFunctions::calcEscapingVariables() } if (c->allVarsEscape && c->contextType == ContextType::Block && c->members.isEmpty()) c->allVarsEscape = false; - if (c->contextType == ContextType::Global || (!c->isStrict && c->contextType == ContextType::Eval) || m->debugMode) + if (c->contextType == ContextType::Global || c->contextType == ContextType::ScriptImportedByQML || (!c->isStrict && c->contextType == ContextType::Eval) || m->debugMode) c->allVarsEscape = true; if (c->allVarsEscape) { if (c->parent) { diff --git a/src/qml/jsruntime/qv4script.cpp b/src/qml/jsruntime/qv4script.cpp index 070c048c8f..951675b468 100644 --- a/src/qml/jsruntime/qv4script.cpp +++ b/src/qml/jsruntime/qv4script.cpp @@ -174,7 +174,8 @@ Function *Script::function() QQmlRefPointer Script::precompile(QV4::Compiler::Module *module, QQmlJS::Engine *jsEngine, Compiler::JSUnitGenerator *unitGenerator, const QString &fileName, const QString &finalUrl, const QString &source, - QList *reportedErrors) + QList *reportedErrors, + QV4::Compiler::ContextType contextType) { using namespace QV4::Compiler; using namespace QQmlJS::AST; @@ -201,7 +202,7 @@ QQmlRefPointer Script::precompile(QV4::Compi Codegen cg(unitGenerator, /*strict mode*/false); cg.setUseFastLookups(false); - cg.generateFromProgram(fileName, finalUrl, source, program, module, ContextType::Global); + cg.generateFromProgram(fileName, finalUrl, source, program, module, contextType); errors = cg.qmlErrors(); if (!errors.isEmpty()) { if (reportedErrors) diff --git a/src/qml/jsruntime/qv4script_p.h b/src/qml/jsruntime/qv4script_p.h index e7189664e2..c138e4a538 100644 --- a/src/qml/jsruntime/qv4script_p.h +++ b/src/qml/jsruntime/qv4script_p.h @@ -103,7 +103,8 @@ struct Q_QML_EXPORT Script { static QQmlRefPointer precompile(QV4::Compiler::Module *module, QQmlJS::Engine *jsEngine, Compiler::JSUnitGenerator *unitGenerator, const QString &fileName, const QString &finalUrl, const QString &source, - QList *reportedErrors = nullptr); + QList *reportedErrors = nullptr, + QV4::Compiler::ContextType contextType = QV4::Compiler::ContextType::Global); static Script *createFromFileOrCache(ExecutionEngine *engine, QmlContext *qmlContext, const QString &fileName, const QUrl &originalUrl, QString *error); static ReturnedValue evaluate(ExecutionEngine *engine, const QString &script, QmlContext *qmlContext); diff --git a/src/qml/qml/qqmltypeloader.cpp b/src/qml/qml/qqmltypeloader.cpp index 0047b20346..89b023c164 100644 --- a/src/qml/qml/qqmltypeloader.cpp +++ b/src/qml/qml/qqmltypeloader.cpp @@ -3084,7 +3084,7 @@ void QQmlScriptBlob::dataReceived(const SourceCodeData &data) QList errors; unit = QV4::Script::precompile( &irUnit.jsModule, &irUnit.jsParserEngine, &irUnit.jsGenerator, urlString(), finalUrlString(), - source, &errors); + source, &errors, QV4::Compiler::ContextType::ScriptImportedByQML); // No need to addref on unit, it's initial refcount is 1 source.clear(); if (!errors.isEmpty()) { -- cgit v1.2.3