1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtScript module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL-ONLY$
|
---|
10 | ** GNU Lesser General Public License Usage
|
---|
11 | ** This file may be used under the terms of the GNU Lesser
|
---|
12 | ** General Public License version 2.1 as published by the Free Software
|
---|
13 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
14 | ** packaging of this file. Please review the following information to
|
---|
15 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
16 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
17 | **
|
---|
18 | ** If you have questions regarding the use of this file, please contact
|
---|
19 | ** Nokia at [email protected].
|
---|
20 | ** $QT_END_LICENSE$
|
---|
21 | **
|
---|
22 | ****************************************************************************/
|
---|
23 |
|
---|
24 | #include "config.h"
|
---|
25 | #include "qscriptstaticscopeobject_p.h"
|
---|
26 |
|
---|
27 | namespace JSC
|
---|
28 | {
|
---|
29 | ASSERT_CLASS_FITS_IN_CELL(QT_PREPEND_NAMESPACE(QScriptStaticScopeObject));
|
---|
30 | }
|
---|
31 |
|
---|
32 | QT_BEGIN_NAMESPACE
|
---|
33 |
|
---|
34 | /*!
|
---|
35 | \class QScriptStaticScopeObject
|
---|
36 | \internal
|
---|
37 |
|
---|
38 | Represents a static scope object.
|
---|
39 |
|
---|
40 | This class allows the VM to determine at JS script compile time whether
|
---|
41 | the object has a given property or not. If the object has the property,
|
---|
42 | a fast, index-based read/write operation will be used. If the object
|
---|
43 | doesn't have the property, the compiler knows it can safely skip this
|
---|
44 | object when dynamically resolving the property. Either way, this can
|
---|
45 | greatly improve performance.
|
---|
46 |
|
---|
47 | \sa QScriptContext::pushScope()
|
---|
48 | */
|
---|
49 |
|
---|
50 | const JSC::ClassInfo QScriptStaticScopeObject::info = { "QScriptStaticScopeObject", 0, 0, 0 };
|
---|
51 |
|
---|
52 | /*!
|
---|
53 | Creates a static scope object with a fixed set of undeletable properties.
|
---|
54 |
|
---|
55 | It's not possible to add new properties to the object after construction.
|
---|
56 | */
|
---|
57 | QScriptStaticScopeObject::QScriptStaticScopeObject(WTF::NonNullPassRefPtr<JSC::Structure> structure,
|
---|
58 | int propertyCount, const PropertyInfo* props)
|
---|
59 | : JSC::JSVariableObject(structure, new Data(/*canGrow=*/false))
|
---|
60 | {
|
---|
61 | int index = growRegisterArray(propertyCount);
|
---|
62 | for (int i = 0; i < propertyCount; ++i, --index) {
|
---|
63 | const PropertyInfo& prop = props[i];
|
---|
64 | JSC::SymbolTableEntry entry(index, prop.attributes);
|
---|
65 | symbolTable().add(prop.identifier.ustring().rep(), entry);
|
---|
66 | registerAt(index) = prop.value;
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | /*!
|
---|
71 | Creates an empty static scope object.
|
---|
72 |
|
---|
73 | Properties can be added to the object after construction, either by
|
---|
74 | calling QScriptValue::setProperty(), or by pushing the object on the
|
---|
75 | scope chain; variable declarations ("var" statements) and function
|
---|
76 | declarations in JavaScript will create properties on the scope object.
|
---|
77 |
|
---|
78 | Note that once the scope object has been used in a closure and the
|
---|
79 | resulting function has been compiled, it's no longer safe to add
|
---|
80 | properties to the scope object (because the VM will bypass this
|
---|
81 | object the next time the function is executed).
|
---|
82 | */
|
---|
83 | QScriptStaticScopeObject::QScriptStaticScopeObject(WTF::NonNullPassRefPtr<JSC::Structure> structure)
|
---|
84 | : JSC::JSVariableObject(structure, new Data(/*canGrow=*/true))
|
---|
85 | {
|
---|
86 | }
|
---|
87 |
|
---|
88 | QScriptStaticScopeObject::~QScriptStaticScopeObject()
|
---|
89 | {
|
---|
90 | delete d_ptr();
|
---|
91 | }
|
---|
92 |
|
---|
93 | bool QScriptStaticScopeObject::getOwnPropertySlot(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertySlot& slot)
|
---|
94 | {
|
---|
95 | return symbolTableGet(propertyName, slot);
|
---|
96 | }
|
---|
97 |
|
---|
98 | bool QScriptStaticScopeObject::getOwnPropertyDescriptor(JSC::ExecState*, const JSC::Identifier& propertyName, JSC::PropertyDescriptor& descriptor)
|
---|
99 | {
|
---|
100 | return symbolTableGet(propertyName, descriptor);
|
---|
101 | }
|
---|
102 |
|
---|
103 | void QScriptStaticScopeObject::putWithAttributes(JSC::ExecState* exec, const JSC::Identifier &propertyName, JSC::JSValue value, unsigned attributes)
|
---|
104 | {
|
---|
105 | if (symbolTablePutWithAttributes(propertyName, value, attributes))
|
---|
106 | return;
|
---|
107 | Q_ASSERT(d_ptr()->canGrow);
|
---|
108 | addSymbolTableProperty(propertyName, value, attributes);
|
---|
109 | }
|
---|
110 |
|
---|
111 | void QScriptStaticScopeObject::put(JSC::ExecState* exec, const JSC::Identifier& propertyName, JSC::JSValue value, JSC::PutPropertySlot&)
|
---|
112 | {
|
---|
113 | if (symbolTablePut(propertyName, value))
|
---|
114 | return;
|
---|
115 | Q_ASSERT(d_ptr()->canGrow);
|
---|
116 | addSymbolTableProperty(propertyName, value, /*attributes=*/0);
|
---|
117 | }
|
---|
118 |
|
---|
119 | bool QScriptStaticScopeObject::deleteProperty(JSC::ExecState*, const JSC::Identifier&)
|
---|
120 | {
|
---|
121 | return false;
|
---|
122 | }
|
---|
123 |
|
---|
124 | void QScriptStaticScopeObject::markChildren(JSC::MarkStack& markStack)
|
---|
125 | {
|
---|
126 | JSC::Register* registerArray = d_ptr()->registerArray.get();
|
---|
127 | if (!registerArray)
|
---|
128 | return;
|
---|
129 | markStack.appendValues(reinterpret_cast<JSC::JSValue*>(registerArray), d_ptr()->registerArraySize);
|
---|
130 | }
|
---|
131 |
|
---|
132 | void QScriptStaticScopeObject::addSymbolTableProperty(const JSC::Identifier& name, JSC::JSValue value, unsigned attributes)
|
---|
133 | {
|
---|
134 | int index = growRegisterArray(1);
|
---|
135 | JSC::SymbolTableEntry newEntry(index, attributes | JSC::DontDelete);
|
---|
136 | symbolTable().add(name.ustring().rep(), newEntry);
|
---|
137 | registerAt(index) = value;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /*!
|
---|
141 | Grows the register array by \a count elements, and returns the offset of
|
---|
142 | the newly added elements (note that the register file grows downwards,
|
---|
143 | starting at index -1).
|
---|
144 | */
|
---|
145 | int QScriptStaticScopeObject::growRegisterArray(int count)
|
---|
146 | {
|
---|
147 | size_t oldSize = d_ptr()->registerArraySize;
|
---|
148 | size_t newSize = oldSize + count;
|
---|
149 | JSC::Register* registerArray = new JSC::Register[newSize];
|
---|
150 | if (d_ptr()->registerArray)
|
---|
151 | memcpy(registerArray + count, d_ptr()->registerArray.get(), oldSize * sizeof(JSC::Register));
|
---|
152 | setRegisters(registerArray + newSize, registerArray);
|
---|
153 | d_ptr()->registerArraySize = newSize;
|
---|
154 | return -oldSize - 1;
|
---|
155 | }
|
---|
156 |
|
---|
157 | QT_END_NAMESPACE
|
---|