[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[556] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the QtDeclarative 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 "qscriptdeclarativeclass_p.h"
|
---|
| 25 | #include "qscriptdeclarativeobject_p.h"
|
---|
| 26 | #include "qscriptobject_p.h"
|
---|
[846] | 27 | #include "qscriptstaticscopeobject_p.h"
|
---|
[556] | 28 | #include <QtScript/qscriptstring.h>
|
---|
| 29 | #include <QtScript/qscriptengine.h>
|
---|
| 30 | #include <QtScript/qscriptengineagent.h>
|
---|
| 31 | #include <private/qscriptengine_p.h>
|
---|
| 32 | #include <private/qscriptvalue_p.h>
|
---|
| 33 | #include <private/qscriptqobject_p.h>
|
---|
| 34 | #include <private/qscriptactivationobject_p.h>
|
---|
| 35 | #include <QtCore/qstringlist.h>
|
---|
| 36 |
|
---|
| 37 | QT_BEGIN_NAMESPACE
|
---|
| 38 |
|
---|
[846] | 39 | /*!
|
---|
| 40 | \class QScriptDeclarativeClass::Value
|
---|
| 41 | \internal
|
---|
| 42 | \brief The QScriptDeclarativeClass::Value class acts as a container for JavaScript data types.
|
---|
| 43 |
|
---|
| 44 | QScriptDeclarativeClass::Value class is similar to QScriptValue, but it is slightly faster.
|
---|
| 45 | Unlike QScriptValue, however, Value instances cannot be stored as they may not survive garbage
|
---|
| 46 | collection. If you need to store a Value, convert it to a QScriptValue and store that.
|
---|
| 47 | */
|
---|
| 48 |
|
---|
| 49 | QScriptDeclarativeClass::Value::Value()
|
---|
| 50 | {
|
---|
| 51 | new (this) JSC::JSValue(JSC::jsUndefined());
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | QScriptDeclarativeClass::Value::Value(const Value &other)
|
---|
| 55 | {
|
---|
| 56 | new (this) JSC::JSValue((JSC::JSValue &)other);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | static QScriptDeclarativeClass::Value jscToValue(const JSC::JSValue &val)
|
---|
| 60 | {
|
---|
| 61 | return QScriptDeclarativeClass::Value((QScriptDeclarativeClass::Value &)val);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | QScriptDeclarativeClass::Value::Value(QScriptContext *ctxt, int value)
|
---|
| 65 | {
|
---|
| 66 | new (this) JSC::JSValue(QScriptEnginePrivate::frameForContext(ctxt), value);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | QScriptDeclarativeClass::Value::Value(QScriptContext *ctxt, uint value)
|
---|
| 70 | {
|
---|
| 71 | new (this) JSC::JSValue(QScriptEnginePrivate::frameForContext(ctxt), value);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | QScriptDeclarativeClass::Value::Value(QScriptContext *, bool value)
|
---|
| 75 | {
|
---|
| 76 | if (value)
|
---|
| 77 | new (this) JSC::JSValue(JSC::JSValue::JSTrue);
|
---|
| 78 | else
|
---|
| 79 | new (this) JSC::JSValue(JSC::JSValue::JSFalse);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | QScriptDeclarativeClass::Value::Value(QScriptContext *ctxt, double value)
|
---|
| 83 | {
|
---|
| 84 | new (this) JSC::JSValue(QScriptEnginePrivate::frameForContext(ctxt), value);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | QScriptDeclarativeClass::Value::Value(QScriptContext *ctxt, float value)
|
---|
| 88 | {
|
---|
| 89 | new (this) JSC::JSValue(QScriptEnginePrivate::frameForContext(ctxt), value);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | QScriptDeclarativeClass::Value::Value(QScriptContext *ctxt, const QString &value)
|
---|
| 93 | {
|
---|
| 94 | new (this) JSC::JSValue(JSC::jsString(QScriptEnginePrivate::frameForContext(ctxt), value));
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | QScriptDeclarativeClass::Value::Value(QScriptContext *ctxt, const QScriptValue &value)
|
---|
| 98 | {
|
---|
| 99 | new (this) JSC::JSValue(QScriptEnginePrivate::get(ctxt->engine())->scriptValueToJSCValue(value));
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | QScriptDeclarativeClass::Value::Value(QScriptEngine *eng, int value)
|
---|
| 103 | {
|
---|
| 104 | new (this) JSC::JSValue(QScriptEnginePrivate::get(eng)->currentFrame, value);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | QScriptDeclarativeClass::Value::Value(QScriptEngine *eng, uint value)
|
---|
| 108 | {
|
---|
| 109 | new (this) JSC::JSValue(QScriptEnginePrivate::get(eng)->currentFrame, value);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | QScriptDeclarativeClass::Value::Value(QScriptEngine *eng, bool value)
|
---|
| 113 | {
|
---|
| 114 | if (value)
|
---|
| 115 | new (this) JSC::JSValue(JSC::JSValue::JSTrue);
|
---|
| 116 | else
|
---|
| 117 | new (this) JSC::JSValue(JSC::JSValue::JSFalse);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | QScriptDeclarativeClass::Value::Value(QScriptEngine *eng, double value)
|
---|
| 121 | {
|
---|
| 122 | new (this) JSC::JSValue(QScriptEnginePrivate::get(eng)->currentFrame, value);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | QScriptDeclarativeClass::Value::Value(QScriptEngine *eng, float value)
|
---|
| 126 | {
|
---|
| 127 | new (this) JSC::JSValue(QScriptEnginePrivate::get(eng)->currentFrame, value);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | QScriptDeclarativeClass::Value::Value(QScriptEngine *eng, const QString &value)
|
---|
| 131 | {
|
---|
| 132 | new (this) JSC::JSValue(JSC::jsString(QScriptEnginePrivate::get(eng)->currentFrame, value));
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | QScriptDeclarativeClass::Value::Value(QScriptEngine *eng, const QScriptValue &value)
|
---|
| 136 | {
|
---|
| 137 | new (this) JSC::JSValue(QScriptEnginePrivate::get(eng)->scriptValueToJSCValue(value));
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | QScriptDeclarativeClass::Value::~Value()
|
---|
| 141 | {
|
---|
| 142 | ((JSC::JSValue *)(this))->~JSValue();
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | QScriptValue QScriptDeclarativeClass::Value::toScriptValue(QScriptEngine *engine) const
|
---|
| 146 | {
|
---|
| 147 | return QScriptEnginePrivate::get(engine)->scriptValueFromJSCValue((JSC::JSValue &)*this);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[556] | 150 | QScriptDeclarativeClass::PersistentIdentifier::PersistentIdentifier()
|
---|
[846] | 151 | : identifier(0), engine(0)
|
---|
[556] | 152 | {
|
---|
| 153 | new (&d) JSC::Identifier();
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | QScriptDeclarativeClass::PersistentIdentifier::~PersistentIdentifier()
|
---|
| 157 | {
|
---|
[846] | 158 | if (engine) {
|
---|
| 159 | QScript::APIShim shim(engine);
|
---|
| 160 | ((JSC::Identifier &)d).JSC::Identifier::~Identifier();
|
---|
| 161 | } else {
|
---|
| 162 | ((JSC::Identifier &)d).JSC::Identifier::~Identifier();
|
---|
| 163 | }
|
---|
[556] | 164 | }
|
---|
| 165 |
|
---|
| 166 | QScriptDeclarativeClass::PersistentIdentifier::PersistentIdentifier(const PersistentIdentifier &other)
|
---|
| 167 | {
|
---|
| 168 | identifier = other.identifier;
|
---|
[846] | 169 | engine = other.engine;
|
---|
[556] | 170 | new (&d) JSC::Identifier((JSC::Identifier &)(other.d));
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | QScriptDeclarativeClass::PersistentIdentifier &
|
---|
| 174 | QScriptDeclarativeClass::PersistentIdentifier::operator=(const PersistentIdentifier &other)
|
---|
| 175 | {
|
---|
| 176 | identifier = other.identifier;
|
---|
[846] | 177 | engine = other.engine;
|
---|
[556] | 178 | ((JSC::Identifier &)d) = (JSC::Identifier &)(other.d);
|
---|
| 179 | return *this;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | QScriptDeclarativeClass::QScriptDeclarativeClass(QScriptEngine *engine)
|
---|
| 183 | : d_ptr(new QScriptDeclarativeClassPrivate)
|
---|
| 184 | {
|
---|
| 185 | Q_ASSERT(sizeof(void*) == sizeof(JSC::Identifier));
|
---|
| 186 | d_ptr->q_ptr = this;
|
---|
| 187 | d_ptr->engine = engine;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | QScriptValue QScriptDeclarativeClass::newObject(QScriptEngine *engine,
|
---|
| 191 | QScriptDeclarativeClass *scriptClass,
|
---|
| 192 | Object *object)
|
---|
| 193 | {
|
---|
| 194 | Q_ASSERT(engine);
|
---|
| 195 | Q_ASSERT(scriptClass);
|
---|
| 196 |
|
---|
[846] | 197 | QScriptEnginePrivate *p = static_cast<QScriptEnginePrivate *>(QObjectPrivate::get(engine));
|
---|
| 198 | QScript::APIShim shim(p);
|
---|
[556] | 199 |
|
---|
| 200 | JSC::ExecState* exec = p->currentFrame;
|
---|
| 201 | QScriptObject *result = new (exec) QScriptObject(p->scriptObjectStructure);
|
---|
| 202 | result->setDelegate(new QScript::DeclarativeObjectDelegate(scriptClass, object));
|
---|
| 203 | return p->scriptValueFromJSCValue(result);
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[846] | 206 | QScriptDeclarativeClass::Value
|
---|
| 207 | QScriptDeclarativeClass::newObjectValue(QScriptEngine *engine,
|
---|
| 208 | QScriptDeclarativeClass *scriptClass,
|
---|
| 209 | Object *object)
|
---|
| 210 | {
|
---|
| 211 | Q_ASSERT(engine);
|
---|
| 212 | Q_ASSERT(scriptClass);
|
---|
| 213 |
|
---|
| 214 | QScriptEnginePrivate *p = static_cast<QScriptEnginePrivate *>(QObjectPrivate::get(engine));
|
---|
| 215 | QScript::APIShim shim(p);
|
---|
| 216 |
|
---|
| 217 | JSC::ExecState* exec = p->currentFrame;
|
---|
| 218 | QScriptObject *result = new (exec) QScriptObject(p->scriptObjectStructure);
|
---|
| 219 | result->setDelegate(new QScript::DeclarativeObjectDelegate(scriptClass, object));
|
---|
| 220 | return jscToValue(JSC::JSValue(result));
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[556] | 223 | QScriptDeclarativeClass *QScriptDeclarativeClass::scriptClass(const QScriptValue &v)
|
---|
| 224 | {
|
---|
| 225 | QScriptValuePrivate *d = QScriptValuePrivate::get(v);
|
---|
[846] | 226 | if (!d || !d->isJSC())
|
---|
[556] | 227 | return 0;
|
---|
[846] | 228 | return QScriptEnginePrivate::declarativeClass(d->jscValue);
|
---|
[556] | 229 | }
|
---|
| 230 |
|
---|
| 231 | QScriptDeclarativeClass::Object *QScriptDeclarativeClass::object(const QScriptValue &v)
|
---|
| 232 | {
|
---|
| 233 | QScriptValuePrivate *d = QScriptValuePrivate::get(v);
|
---|
[846] | 234 | if (!d || !d->isJSC())
|
---|
[556] | 235 | return 0;
|
---|
[846] | 236 | return QScriptEnginePrivate::declarativeObject(d->jscValue);
|
---|
[556] | 237 | }
|
---|
| 238 |
|
---|
| 239 | QScriptValue QScriptDeclarativeClass::function(const QScriptValue &v, const Identifier &name)
|
---|
| 240 | {
|
---|
| 241 | QScriptValuePrivate *d = QScriptValuePrivate::get(v);
|
---|
| 242 |
|
---|
| 243 | if (!d->isObject())
|
---|
| 244 | return QScriptValue();
|
---|
| 245 |
|
---|
[846] | 246 | QScript::APIShim shim(d->engine);
|
---|
[556] | 247 | JSC::ExecState *exec = d->engine->currentFrame;
|
---|
| 248 | JSC::JSObject *object = d->jscValue.getObject();
|
---|
| 249 | JSC::PropertySlot slot(const_cast<JSC::JSObject*>(object));
|
---|
| 250 | JSC::JSValue result;
|
---|
| 251 |
|
---|
| 252 | JSC::Identifier id(exec, (JSC::UString::Rep *)name);
|
---|
| 253 |
|
---|
| 254 | if (const_cast<JSC::JSObject*>(object)->getOwnPropertySlot(exec, id, slot)) {
|
---|
| 255 | result = slot.getValue(exec, id);
|
---|
| 256 | if (QScript::isFunction(result))
|
---|
| 257 | return d->engine->scriptValueFromJSCValue(result);
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | return QScriptValue();
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | QScriptValue QScriptDeclarativeClass::property(const QScriptValue &v, const Identifier &name)
|
---|
| 264 | {
|
---|
| 265 | QScriptValuePrivate *d = QScriptValuePrivate::get(v);
|
---|
| 266 |
|
---|
| 267 | if (!d->isObject())
|
---|
| 268 | return QScriptValue();
|
---|
| 269 |
|
---|
[846] | 270 | QScript::APIShim shim(d->engine);
|
---|
[556] | 271 | JSC::ExecState *exec = d->engine->currentFrame;
|
---|
| 272 | JSC::JSObject *object = d->jscValue.getObject();
|
---|
| 273 | JSC::PropertySlot slot(const_cast<JSC::JSObject*>(object));
|
---|
| 274 | JSC::JSValue result;
|
---|
| 275 |
|
---|
| 276 | JSC::Identifier id(exec, (JSC::UString::Rep *)name);
|
---|
| 277 |
|
---|
| 278 | if (const_cast<JSC::JSObject*>(object)->getOwnPropertySlot(exec, id, slot)) {
|
---|
| 279 | result = slot.getValue(exec, id);
|
---|
| 280 | return d->engine->scriptValueFromJSCValue(result);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | return QScriptValue();
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[846] | 286 | QScriptDeclarativeClass::Value
|
---|
| 287 | QScriptDeclarativeClass::functionValue(const QScriptValue &v, const Identifier &name)
|
---|
| 288 | {
|
---|
| 289 | QScriptValuePrivate *d = QScriptValuePrivate::get(v);
|
---|
| 290 |
|
---|
| 291 | if (!d->isObject())
|
---|
| 292 | return Value();
|
---|
| 293 |
|
---|
| 294 | QScript::APIShim shim(d->engine);
|
---|
| 295 | JSC::ExecState *exec = d->engine->currentFrame;
|
---|
| 296 | JSC::JSObject *object = d->jscValue.getObject();
|
---|
| 297 | JSC::PropertySlot slot(const_cast<JSC::JSObject*>(object));
|
---|
| 298 | JSC::JSValue result;
|
---|
| 299 |
|
---|
| 300 | JSC::Identifier id(exec, (JSC::UString::Rep *)name);
|
---|
| 301 |
|
---|
| 302 | if (const_cast<JSC::JSObject*>(object)->getOwnPropertySlot(exec, id, slot)) {
|
---|
| 303 | result = slot.getValue(exec, id);
|
---|
| 304 | if (QScript::isFunction(result))
|
---|
| 305 | return jscToValue(result);
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | return Value();
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | QScriptDeclarativeClass::Value
|
---|
| 312 | QScriptDeclarativeClass::propertyValue(const QScriptValue &v, const Identifier &name)
|
---|
| 313 | {
|
---|
| 314 | QScriptValuePrivate *d = QScriptValuePrivate::get(v);
|
---|
| 315 |
|
---|
| 316 | if (!d->isObject())
|
---|
| 317 | return Value();
|
---|
| 318 |
|
---|
| 319 | QScript::APIShim shim(d->engine);
|
---|
| 320 | JSC::ExecState *exec = d->engine->currentFrame;
|
---|
| 321 | JSC::JSObject *object = d->jscValue.getObject();
|
---|
| 322 | JSC::PropertySlot slot(const_cast<JSC::JSObject*>(object));
|
---|
| 323 | JSC::JSValue result;
|
---|
| 324 |
|
---|
| 325 | JSC::Identifier id(exec, (JSC::UString::Rep *)name);
|
---|
| 326 |
|
---|
| 327 | if (const_cast<JSC::JSObject*>(object)->getOwnPropertySlot(exec, id, slot)) {
|
---|
| 328 | result = slot.getValue(exec, id);
|
---|
| 329 | return jscToValue(result);
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | return Value();
|
---|
| 333 | }
|
---|
| 334 |
|
---|
[556] | 335 | /*
|
---|
| 336 | Returns the scope chain entry at \a index. If index is less than 0, returns
|
---|
| 337 | entries starting at the end. For example, scopeChainValue(context, -1) will return
|
---|
| 338 | the value last in the scope chain.
|
---|
| 339 | */
|
---|
| 340 | QScriptValue QScriptDeclarativeClass::scopeChainValue(QScriptContext *context, int index)
|
---|
| 341 | {
|
---|
| 342 | context->activationObject(); //ensure the creation of the normal scope for native context
|
---|
| 343 | const JSC::CallFrame *frame = QScriptEnginePrivate::frameForContext(context);
|
---|
| 344 | QScriptEnginePrivate *engine = QScript::scriptEngineFromExec(frame);
|
---|
[846] | 345 | QScript::APIShim shim(engine);
|
---|
[556] | 346 |
|
---|
| 347 | JSC::ScopeChainNode *node = frame->scopeChain();
|
---|
| 348 | JSC::ScopeChainIterator it(node);
|
---|
| 349 |
|
---|
| 350 | if (index < 0) {
|
---|
| 351 | int count = 0;
|
---|
| 352 | for (it = node->begin(); it != node->end(); ++it)
|
---|
| 353 | ++count;
|
---|
| 354 |
|
---|
| 355 | index = qAbs(index);
|
---|
| 356 | if (index > count)
|
---|
| 357 | return QScriptValue();
|
---|
| 358 | else
|
---|
| 359 | index = count - index;
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | for (it = node->begin(); it != node->end(); ++it) {
|
---|
| 363 |
|
---|
| 364 | if (index == 0) {
|
---|
| 365 |
|
---|
| 366 | JSC::JSObject *object = *it;
|
---|
| 367 | if (!object) return QScriptValue();
|
---|
| 368 |
|
---|
| 369 | if (object->inherits(&QScript::QScriptActivationObject::info)
|
---|
| 370 | && (static_cast<QScript::QScriptActivationObject*>(object)->delegate() != 0)) {
|
---|
| 371 | // Return the object that property access is being delegated to
|
---|
| 372 | object = static_cast<QScript::QScriptActivationObject*>(object)->delegate();
|
---|
| 373 | }
|
---|
| 374 | return engine->scriptValueFromJSCValue(object);
|
---|
| 375 |
|
---|
| 376 | } else {
|
---|
| 377 | --index;
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | return QScriptValue();
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 | /*!
|
---|
| 386 | Enters a new execution context and returns the associated
|
---|
| 387 | QScriptContext object.
|
---|
| 388 |
|
---|
| 389 | Once you are done with the context, you should call popContext() to
|
---|
| 390 | restore the old context.
|
---|
| 391 |
|
---|
| 392 | By default, the `this' object of the new context is the Global Object.
|
---|
| 393 | The context's \l{QScriptContext::callee()}{callee}() will be invalid.
|
---|
| 394 |
|
---|
| 395 | Unlike pushContext(), the default scope chain is reset to include
|
---|
| 396 | only the global object and the QScriptContext's activation object.
|
---|
| 397 |
|
---|
| 398 | \sa QScriptEngine::popContext()
|
---|
| 399 | */
|
---|
| 400 | QScriptContext * QScriptDeclarativeClass::pushCleanContext(QScriptEngine *engine)
|
---|
| 401 | {
|
---|
| 402 | if (!engine)
|
---|
| 403 | return 0;
|
---|
| 404 |
|
---|
| 405 | QScriptEnginePrivate *d = QScriptEnginePrivate::get(engine);
|
---|
[846] | 406 | QScript::APIShim shim(d);
|
---|
[556] | 407 |
|
---|
| 408 | JSC::CallFrame* newFrame = d->pushContext(d->currentFrame,
|
---|
| 409 | d->currentFrame->globalData().dynamicGlobalObject,
|
---|
| 410 | JSC::ArgList(), /*callee = */0, false, true);
|
---|
| 411 |
|
---|
| 412 | if (engine->agent())
|
---|
| 413 | engine->agent()->contextPush();
|
---|
| 414 |
|
---|
| 415 | return d->contextForFrame(newFrame);
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| |
---|