aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/compiler/qv4codegen.cpp
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@qt.io>2024-12-11 16:36:10 +0100
committerUlf Hermann <ulf.hermann@qt.io>2024-12-17 20:49:39 +0100
commitce0a16c0800fb1d5bb64783c424274b0a8bd4d43 (patch)
treec761d31445f7c11c3c9ee5411b2dd72d4d96a44f /src/qml/compiler/qv4codegen.cpp
parentf446f31079f7ac20e6d9ac0a28abba6d44a3709b (diff)
QtQml: Generalize the global/illegal names
Instead of passing them around everywhere, use the ones we statically know and only validate them when creating a new engine. Task-number: QTBUG-131721 Change-Id: I7fb93d15eb6e4194c46249727bcf7a48f5dce730 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/qml/compiler/qv4codegen.cpp')
-rw-r--r--src/qml/compiler/qv4codegen.cpp169
1 files changed, 87 insertions, 82 deletions
diff --git a/src/qml/compiler/qv4codegen.cpp b/src/qml/compiler/qv4codegen.cpp
index de08c4fd08..27205fc623 100644
--- a/src/qml/compiler/qv4codegen.cpp
+++ b/src/qml/compiler/qv4codegen.cpp
@@ -108,77 +108,87 @@ Codegen::Codegen(QV4::Compiler::JSUnitGenerator *jsUnitGenerator, bool strict,
pushExpr();
}
-const char *Codegen::s_globalNames[] = {
- "Array",
- "ArrayBuffer",
- "Atomics",
- "Boolean",
- "DOMException",
- "DataView",
- "Date",
- "Error",
- "EvalError",
- "Float32Array",
- "Float64Array",
- "Function",
- "Infinity",
- "Int16Array",
- "Int32Array",
- "Int8Array",
- "JSON",
- "Map",
- "Math",
- "NaN",
- "Number",
- "Object",
- "Promise",
- "Proxy",
- "QT_TRANSLATE_NOOP",
- "QT_TRID_NOOP",
- "QT_TR_NOOP",
- "Qt",
- "RangeError",
- "ReferenceError",
- "Reflect",
- "RegExp",
- "SQLException",
- "Set",
- "SharedArrayBuffer",
- "String",
- "Symbol",
- "SyntaxError",
- "TypeError",
- "URIError",
- "URL",
- "URLSearchParams",
- "Uint16Array",
- "Uint32Array",
- "Uint8Array",
- "Uint8ClampedArray",
- "WeakMap",
- "WeakSet",
- "XMLHttpRequest",
- "console",
- "decodeURI",
- "decodeURIComponent",
- "encodeURI",
- "encodeURIComponent",
- "escape",
- "eval",
- "gc",
- "isFinite",
- "isNaN",
- "parseFloat",
- "parseInt",
- "print",
- "qsTr",
- "qsTrId",
- "qsTranslate",
- "undefined",
- "unescape",
- nullptr
+static constexpr const QLatin1StringView s_globalNames[] = {
+ QLatin1StringView("Array"),
+ QLatin1StringView("ArrayBuffer"),
+ QLatin1StringView("Atomics"),
+ QLatin1StringView("Boolean"),
+ QLatin1StringView("DOMException"),
+ QLatin1StringView("DataView"),
+ QLatin1StringView("Date"),
+ QLatin1StringView("Error"),
+ QLatin1StringView("EvalError"),
+ QLatin1StringView("Float32Array"),
+ QLatin1StringView("Float64Array"),
+ QLatin1StringView("Function"),
+ QLatin1StringView("Infinity"),
+ QLatin1StringView("Int16Array"),
+ QLatin1StringView("Int32Array"),
+ QLatin1StringView("Int8Array"),
+ QLatin1StringView("JSON"),
+ QLatin1StringView("Map"),
+ QLatin1StringView("Math"),
+ QLatin1StringView("NaN"),
+ QLatin1StringView("Number"),
+ QLatin1StringView("Object"),
+ QLatin1StringView("Promise"),
+ QLatin1StringView("Proxy"),
+ QLatin1StringView("QT_TRANSLATE_NOOP"),
+ QLatin1StringView("QT_TRID_NOOP"),
+ QLatin1StringView("QT_TR_NOOP"),
+ QLatin1StringView("Qt"),
+ QLatin1StringView("RangeError"),
+ QLatin1StringView("ReferenceError"),
+ QLatin1StringView("Reflect"),
+ QLatin1StringView("RegExp"),
+ QLatin1StringView("SQLException"),
+ QLatin1StringView("Set"),
+ QLatin1StringView("SharedArrayBuffer"),
+ QLatin1StringView("String"),
+ QLatin1StringView("Symbol"),
+ QLatin1StringView("SyntaxError"),
+ QLatin1StringView("TypeError"),
+ QLatin1StringView("URIError"),
+ QLatin1StringView("URL"),
+ QLatin1StringView("URLSearchParams"),
+ QLatin1StringView("Uint16Array"),
+ QLatin1StringView("Uint32Array"),
+ QLatin1StringView("Uint8Array"),
+ QLatin1StringView("Uint8ClampedArray"),
+ QLatin1StringView("WeakMap"),
+ QLatin1StringView("WeakSet"),
+ QLatin1StringView("XMLHttpRequest"),
+ QLatin1StringView("console"),
+ QLatin1StringView("decodeURI"),
+ QLatin1StringView("decodeURIComponent"),
+ QLatin1StringView("encodeURI"),
+ QLatin1StringView("encodeURIComponent"),
+ QLatin1StringView("escape"),
+ QLatin1StringView("eval"),
+ QLatin1StringView("gc"),
+ QLatin1StringView("isFinite"),
+ QLatin1StringView("isNaN"),
+ QLatin1StringView("parseFloat"),
+ QLatin1StringView("parseInt"),
+ QLatin1StringView("print"),
+ QLatin1StringView("qsTr"),
+ QLatin1StringView("qsTrId"),
+ QLatin1StringView("qsTranslate"),
+ QLatin1StringView("undefined"),
+ QLatin1StringView("unescape"),
};
+bool Codegen::isNameGlobal(QAnyStringView name)
+{
+ return std::binary_search(std::begin(s_globalNames), std::end(s_globalNames), name);
+}
+
+void Codegen::forEachGlobalName(qxp::function_ref<void (QLatin1StringView)> &&handler)
+{
+ for (QLatin1StringView name : s_globalNames)
+ handler(name);
+}
+
void Codegen::generateFromProgram(const QString &fileName,
const QString &finalUrl,
const QString &sourceCode,
@@ -195,18 +205,6 @@ void Codegen::generateFromProgram(const QString &fileName,
_module->fileName = fileName;
_module->finalUrl = finalUrl;
- if (contextType == ContextType::ScriptImportedByQML) {
- // the global object is frozen, so we know that members of it are
- // pointing to the global object. This is important so that references
- // to Math etc. do not go through the expensive path in the context wrapper
- // that tries to see whether we have a matching type
- //
- // Since this can be called from the loader thread we can't get the list
- // directly from the engine, so let's hardcode the most important ones here
- for (const char **g = s_globalNames; *g != nullptr; ++g)
- m_globalNames << QString::fromLatin1(*g);
- }
-
ScanFunctions scan(this, sourceCode, contextType);
scan(node);
@@ -2742,8 +2740,15 @@ Codegen::Reference Codegen::referenceForName(const QString &name, bool isLhs, co
r.global = useFastLookups && (resolved.type == Context::ResolvedName::Global || resolved.type == Context::ResolvedName::QmlGlobal);
r.qmlGlobal = resolved.type == Context::ResolvedName::QmlGlobal;
r.sourceLocation = accessLocation;
- if (!r.global && !r.qmlGlobal && m_globalNames.contains(name))
+ if (!r.global && !r.qmlGlobal
+ && _context->contextType == ContextType::ScriptImportedByQML
+ && Codegen::isNameGlobal(name)) {
+ // the global object is frozen, so we know that members of it are
+ // pointing to the global object. This is important so that references
+ // to Math etc. do not go through the expensive path in the context wrapper
+ // that tries to see whether we have a matching type
r.global = true;
+ }
return r;
}