summaryrefslogtreecommitdiffstats
path: root/src/script/bridge/qscriptstaticscopeobject_p.h
diff options
context:
space:
mode:
authorKent Hansen <kent.hansen@nokia.com>2010-06-23 15:22:47 (GMT)
committerKent Hansen <kent.hansen@nokia.com>2010-06-25 06:59:13 (GMT)
commita91f8d704c3ff3826f0ea8b7e73fc6d91dd5b836 (patch)
tree26c0bb1921eb8b3fc73b01be94dc1823d7322e19 /src/script/bridge/qscriptstaticscopeobject_p.h
parentf793344d17b0ec5df4987e123cc1e3b008a949bb (diff)
downloadQt-a91f8d704c3ff3826f0ea8b7e73fc6d91dd5b836.zip
Qt-a91f8d704c3ff3826f0ea8b7e73fc6d91dd5b836.tar.gz
Qt-a91f8d704c3ff3826f0ea8b7e73fc6d91dd5b836.tar.bz2
Use custom static scopes to improve QML/JavaScript performance
This commit introduces a new internal JS object type, QScriptStaticScopeObject, that enables the JS compiler to make more aggressive optimizations of scoped property access. QScriptStaticScopeObject registers all its properties in a symbol table that the JS compiler has access to. If the compiler finds the property in the symbol table, it will generate the fast index-based op_{get,put}_scoped_var bytecodes, rather than the dynamic (slow) op_resolve and friends. If the compiler _doesn't_ find the property in the symbol table, it infers that it's safe to skip the scope object when later resolving the property, which will also improve performance (see op_resolve_skip bytecode). QScriptStaticScopeObject is only safe to use when all relevant properties are known at JS compile time; that is, when a function that has the static scope object in its scope chain is compiled. It's up to the user of the class (e.g. QtDeclarative) to ensure that this constraint is not violated. The API for constructing QScriptStaticScopeObject instances is not public; it lives in QScriptDeclarativeClass for now, an internal class exported for the purpose of QML. The instance is returned as a QScriptValue and can be manipulated like any other JS object (e.g. by QScriptValue::setProperty()). The other part of this commit utilizes QScriptStaticScopeObject in QtDeclarative in the two major places where it's currently possible: 1) QML disallows adding properties to the Global Object. Furthermore, it's not possible for QML IDs and properties to "shadow" global variables. Hence, a QScriptStaticScopeObject can be used to hold all the standard ECMA properties, and this scope object can come _before_ the QML component in the scope chain. This enables binding expressions and scripts to have optimized (direct) access to e.g. Math.sin. 2) Imported scripts can have their properties (resulting from variable declarations ("var" statements) and function declarations) added to a static scope object. This enables functions in the script to have optimized (direct) access to the script's own properties, as well as to global properties such as Math. With this change, it's no longer possible to delete properties of the Global Object, nor delete properties of an imported script. It's a compromise we make in order to make the optimization safe. Task-number: QTBUG-8576 Reviewed-by: Aaron Kennedy Reviewed-by: Olivier Goffart Reviewed-by: Jedrzej Nowacki
Diffstat (limited to 'src/script/bridge/qscriptstaticscopeobject_p.h')
-rw-r--r--src/script/bridge/qscriptstaticscopeobject_p.h103
1 files changed, 103 insertions, 0 deletions
diff --git a/src/script/bridge/qscriptstaticscopeobject_p.h b/src/script/bridge/qscriptstaticscopeobject_p.h
new file mode 100644
index 0000000..0a0e7ef
--- /dev/null
+++ b/src/script/bridge/qscriptstaticscopeobject_p.h
@@ -0,0 +1,103 @@
+/****************************************************************************
+**
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtScript module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL-ONLY$
+** GNU Lesser General Public License Usage
+** This file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#ifndef QSCRIPTSTATICSCOPEOBJECT_P_H
+#define QSCRIPTSTATICSCOPEOBJECT_P_H
+
+//
+// W A R N I N G
+// -------------
+//
+// This file is not part of the Qt API. It exists purely as an
+// implementation detail. This header file may change from version to
+// version without notice, or even be removed.
+//
+// We mean it.
+//
+
+#include <QtCore/qobjectdefs.h>
+
+#include "JSVariableObject.h"
+
+QT_BEGIN_NAMESPACE
+
+class QScriptStaticScopeObject : public JSC::JSVariableObject {
+public:
+ struct PropertyInfo {
+ PropertyInfo(const JSC::Identifier& i, JSC::JSValue v, unsigned a)
+ : identifier(i), value(v), attributes(a)
+ { }
+ PropertyInfo() {}
+
+ JSC::Identifier identifier;
+ JSC::JSValue value;
+ unsigned attributes;
+ };
+
+ QScriptStaticScopeObject(WTF::NonNullPassRefPtr<JSC::Structure> structure,
+ int propertyCount, const PropertyInfo*);
+ QScriptStaticScopeObject(WTF::NonNullPassRefPtr<JSC::Structure> structure);
+ virtual ~QScriptStaticScopeObject();
+
+ virtual bool isDynamicScope() const { return false; }
+
+ virtual bool getOwnPropertySlot(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertySlot&);
+ virtual bool getOwnPropertyDescriptor(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertyDescriptor&);
+
+ virtual void putWithAttributes(JSC::ExecState *exec, const JSC::Identifier &propertyName, JSC::JSValue value, unsigned attributes);
+ virtual void put(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::JSValue value, JSC::PutPropertySlot&);
+
+ virtual bool deleteProperty(JSC::ExecState*, const JSC::Identifier& propertyName);
+
+ virtual void markChildren(JSC::MarkStack&);
+
+ virtual const JSC::ClassInfo* classInfo() const { return &info; }
+ static const JSC::ClassInfo info;
+
+ static WTF::PassRefPtr<JSC::Structure> createStructure(JSC::JSValue proto) {
+ return JSC::Structure::create(proto, JSC::TypeInfo(JSC::ObjectType, StructureFlags));
+ }
+
+protected:
+ static const unsigned StructureFlags = JSC::OverridesGetOwnPropertySlot | JSC::NeedsThisConversion | JSC::OverridesMarkChildren | JSC::OverridesGetPropertyNames | JSC::JSVariableObject::StructureFlags;
+
+ struct Data : public JSVariableObjectData {
+ Data(bool canGrow_)
+ : JSVariableObjectData(&symbolTable, /*registers=*/0),
+ canGrow(canGrow_), registerArraySize(0)
+ { }
+ bool canGrow;
+ int registerArraySize;
+ JSC::SymbolTable symbolTable;
+ };
+
+ Data* d_ptr() const { return static_cast<Data*>(JSVariableObject::d); }
+
+private:
+ void addSymbolTableProperty(const JSC::Identifier&, JSC::JSValue, unsigned attributes);
+ int growRegisterArray(int);
+};
+
+QT_END_NAMESPACE
+
+#endif