1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #ifndef QSCRIPTORIGINALGLOBALOBJECT_P_H
43 #define QSCRIPTORIGINALGLOBALOBJECT_P_H
45 #include "QtCore/qglobal.h"
46 #include "qjsvalue_p.h"
48 #include <private/qv8_p.h>
56 This class is a workaround for missing V8 API functionality. This class keeps all important
57 properties of an original (default) global object, so we can use it even if the global object was
60 FIXME this class is a container for workarounds :-) it should be replaced by proper API calls.
62 The class have to be created on the QV8Engine creation time (before any change got applied to
65 \attention All methods (apart from constructor) assumes that a context and a scope are prepared correctly.
67 class QScriptOriginalGlobalObject
70 inline QScriptOriginalGlobalObject() {}
71 inline void init(v8::Handle<v8::Context> context);
72 inline void destroy();
74 inline QJSValuePrivate::PropertyFlags getPropertyFlags(v8::Handle<v8::Object> object, v8::Handle<v8::Value> property);
75 inline v8::Local<v8::Object> getOwnPropertyDescriptor(v8::Handle<v8::Object> object, v8::Handle<v8::Value> property) const;
76 inline bool strictlyEquals(v8::Handle<v8::Object> object);
78 Q_DISABLE_COPY(QScriptOriginalGlobalObject)
80 // Copy of constructors and prototypes used in isType functions.
81 v8::Persistent<v8::Function> m_ownPropertyDescriptor;
82 v8::Persistent<v8::Object> m_globalObject;
85 void QScriptOriginalGlobalObject::init(v8::Handle<v8::Context> context)
87 // Please notice that engine is not fully initialized at this point.
89 v8::Context::Scope contextScope(context);
91 v8::HandleScope scope;
93 m_globalObject = v8::Persistent<v8::Object>::New(context->Global());
95 v8::Local<v8::Object> objectConstructor = m_globalObject->Get(v8::String::New("Object"))->ToObject();
96 Q_ASSERT(objectConstructor->IsObject());
97 { // Initialize m_ownPropertyDescriptor.
98 v8::Local<v8::Value> ownPropertyDescriptor = objectConstructor->Get(v8::String::New("getOwnPropertyDescriptor"));
99 Q_ASSERT(!ownPropertyDescriptor.IsEmpty());
100 m_ownPropertyDescriptor = v8::Persistent<v8::Function>::New(v8::Local<v8::Function>::Cast(ownPropertyDescriptor));
106 QScriptOriginalGlobalObject lives as long as QV8Engine that keeps it. In ~QSEP
107 the v8 context is removed, so we need to remove our handlers before. to break this dependency
108 destroy method should be called before or insight QSEP destructor.
110 inline void QScriptOriginalGlobalObject::destroy()
112 m_ownPropertyDescriptor.Dispose();
113 m_globalObject.Dispose();
114 // After this line this instance is unusable.
117 inline QJSValuePrivate::PropertyFlags QScriptOriginalGlobalObject::getPropertyFlags(v8::Handle<v8::Object> object, v8::Handle<v8::Value> property)
119 Q_ASSERT(object->IsObject());
120 Q_ASSERT(!property.IsEmpty());
121 v8::Local<v8::Object> descriptor = getOwnPropertyDescriptor(object, property);
122 if (descriptor.IsEmpty()) {
123 // // Property isn't owned by this object.
124 // if (!(mode & QScriptValue::ResolvePrototype))
126 v8::Local<v8::Value> prototype = object->GetPrototype();
127 if (prototype->IsNull())
129 return getPropertyFlags(v8::Local<v8::Object>::Cast(prototype), property);
131 v8::Local<v8::String> writableName = v8::String::New("writable");
132 v8::Local<v8::String> configurableName = v8::String::New("configurable");
133 v8::Local<v8::String> enumerableName = v8::String::New("enumerable");
134 // v8::Local<v8::String> getName = v8::String::New("get");
135 // v8::Local<v8::String> setName = v8::String::New("set");
139 if (!descriptor->Get(configurableName)->BooleanValue())
140 flags |= QJSValuePrivate::Undeletable;
141 if (!descriptor->Get(enumerableName)->BooleanValue())
142 flags |= QJSValuePrivate::SkipInEnumeration;
144 //"writable" is only a property of the descriptor if it is not an accessor
145 if (descriptor->Has(writableName)) {
146 if (!descriptor->Get(writableName)->BooleanValue())
147 flags |= QJSValuePrivate::ReadOnly;
149 // if (descriptor->Get(getName)->IsObject())
150 // flags |= QScriptValue::PropertyGetter;
151 // if (descriptor->Get(setName)->IsObject())
152 // flags |= QScriptValue::PropertySetter;
155 return QJSValuePrivate::PropertyFlag(flags);
158 inline v8::Local<v8::Object> QScriptOriginalGlobalObject::getOwnPropertyDescriptor(v8::Handle<v8::Object> object, v8::Handle<v8::Value> property) const
160 Q_ASSERT(object->IsObject());
161 Q_ASSERT(!property.IsEmpty());
162 // FIXME do we need try catch here?
163 v8::Handle<v8::Value> argv[] = {object, property};
164 v8::Local<v8::Value> descriptor = m_ownPropertyDescriptor->Call(m_globalObject, /* argc */ 2, argv);
165 if (descriptor.IsEmpty() || !descriptor->IsObject())
166 return v8::Local<v8::Object>();
167 return v8::Local<v8::Object>::Cast(descriptor);
170 inline bool QScriptOriginalGlobalObject::strictlyEquals(v8::Handle<v8::Object> object)
172 return m_globalObject->GetPrototype()->StrictEquals(object);