ReturnedValue Lookup::lookup(Object *thisObject, PropertyAttributes *attrs) { Heap::Object *obj = thisObject->d(); ExecutionEngine *engine = thisObject->engine(); Identifier *name = engine->currentContext()->compilationUnit->runtimeStrings[nameIndex]->identifier; int i = 0; while (i < Size && obj) { classList[i] = obj->internalClass; index = obj->internalClass->find(name); if (index != UINT_MAX) { level = i; *attrs = obj->internalClass->propertyData.at(index); return !attrs->isAccessor() ? obj->memberData->data[index].asReturnedValue() : thisObject->getValue(obj->propertyAt(index), *attrs); } obj = obj->prototype; ++i; } level = Size; while (obj) { index = obj->internalClass->find(name); if (index != UINT_MAX) { *attrs = obj->internalClass->propertyData.at(index); return !attrs->isAccessor() ? obj->memberData->data[index].asReturnedValue() : thisObject->getValue(obj->propertyAt(index), *attrs); } obj = obj->prototype; } return Primitive::emptyValue().asReturnedValue(); }
/*! Sets the value of this QJSValue's property with the given \a name to the given \a value. If this QJSValue is not an object, this function does nothing. If this QJSValue does not already have a property with name \a name, a new property is created. \sa property(), deleteProperty() */ void QJSValue::setProperty(const QString& name, const QJSValue& value) { ExecutionEngine *engine = d->engine; if (!engine) return; Scope scope(engine); Scoped<Object> o(scope, d->value); if (!o) return; if (!value.d->checkEngine(o->engine())) { qWarning("QJSValue::setProperty(%s) failed: cannot set value created in a different engine", name.toUtf8().constData()); return; } ScopedString s(scope, engine->newString(name)); uint idx = s->asArrayIndex(); if (idx < UINT_MAX) { setProperty(idx, value); return; } QV4::ExecutionContext *ctx = engine->currentContext(); s->makeIdentifier(); QV4::ScopedValue v(scope, value.d->getValue(engine)); o->put(s, v); if (scope.hasException()) ctx->catchException(); }
void QEnginioCollectionObjectPrivate::QmlCallbackFunctor::operator ()(QEnginioOperationObject *aOperation) { using namespace QV4; if (!aOperation) return; if (iCallback.isCallable()) { QJSValuePrivate *prv=QJSValuePrivate::get(iCallback); FunctionObject *f =prv->value.asFunctionObject(); if (f) { ExecutionEngine *engine = prv->engine; Q_ASSERT(engine); Scope scope(engine); ScopedCallData callData(scope, 1); // args.length()); callData->thisObject = engine->globalObject->asReturnedValue(); ScopedValue result(scope); QV4::ExecutionContext *ctx = engine->currentContext(); callData->args[0] = QObjectWrapper::wrap(engine,aOperation); result = f->call(callData); if (scope.hasException()) { result = ctx->catchException(); } QJSValue tmp(new QJSValuePrivate(engine, result)); } } delete aOperation; }
/*! Returns the value of this QJSValue's property with the given \a name. If no such property exists, an undefined QJSValue is returned. If the property is implemented using a getter function (i.e. has the PropertyGetter flag set), calling property() has side-effects on the script engine, since the getter function will be called (possibly resulting in an uncaught script exception). If an exception occurred, property() returns the value that was thrown (typically an \c{Error} object). \sa setProperty(), hasProperty(), QJSValueIterator */ QJSValue QJSValue::property(const QString& name) const { ExecutionEngine *engine = d->engine; if (!engine) return QJSValue(); QV4::Scope scope(engine); ScopedObject o(scope, d->value); if (!o) return QJSValue(); ScopedString s(scope, engine->newString(name)); uint idx = s->asArrayIndex(); if (idx < UINT_MAX) return property(idx); s->makeIdentifier(); QV4::ExecutionContext *ctx = engine->currentContext(); QV4::ScopedValue result(scope); result = o->get(s); if (scope.hasException()) result = ctx->catchException(); return new QJSValuePrivate(engine, result); }
/*! Creates a new \c{Object} and calls this QJSValue as a constructor, using the created object as the `this' object and passing \a args as arguments. If the return value from the constructor call is an object, then that object is returned; otherwise the default constructed object is returned. If this QJSValue is not a function, callAsConstructor() does nothing and returns an undefined QJSValue. Calling this function can cause an exception to occur in the script engine; in that case, the value that was thrown (typically an \c{Error} object) is returned. You can call isError() on the return value to determine whether an exception occurred. \sa call(), QJSEngine::newObject() */ QJSValue QJSValue::callAsConstructor(const QJSValueList &args) { FunctionObject *f = d->value.asFunctionObject(); if (!f) return QJSValue(); ExecutionEngine *engine = d->engine; assert(engine); Scope scope(engine); ScopedCallData callData(scope, args.size()); for (int i = 0; i < args.size(); ++i) { if (!args.at(i).d->checkEngine(engine)) { qWarning("QJSValue::callAsConstructor() failed: cannot construct function with argument created in a different engine"); return QJSValue(); } callData->args[i] = args.at(i).d->getValue(engine); } ScopedValue result(scope); QV4::ExecutionContext *ctx = engine->currentContext(); result = f->construct(callData); if (scope.hasException()) result = ctx->catchException(); return new QJSValuePrivate(engine, result); }
/*! Attempts to delete this object's property of the given \a name. Returns true if the property was deleted, otherwise returns false. The behavior of this function is consistent with the JavaScript delete operator. In particular: \list \li Non-configurable properties cannot be deleted. \li This function will return true even if this object doesn't have a property of the given \a name (i.e., non-existent properties are "trivially deletable"). \li If this object doesn't have an own property of the given \a name, but an object in the prototype() chain does, the prototype object's property is not deleted, and this function returns true. \endlist \sa setProperty(), hasOwnProperty() */ bool QJSValue::deleteProperty(const QString &name) { ExecutionEngine *engine = d->engine; ExecutionContext *ctx = engine->currentContext(); Scope scope(engine); ScopedObject o(scope, d->value.asObject()); if (!o) return false; ScopedString s(scope, engine->newString(name)); bool b = o->deleteProperty(s); if (scope.hasException()) ctx->catchException(); return b; }
/*! \overload Returns the property at the given \a arrayIndex. This function is provided for convenience and performance when working with array objects. If this QJSValue is not an Array object, this function behaves as if property() was called with the string representation of \a arrayIndex. */ QJSValue QJSValue::property(quint32 arrayIndex) const { ExecutionEngine *engine = d->engine; if (!engine) return QJSValue(); QV4::Scope scope(engine); ScopedObject o(scope, d->value); if (!o) return QJSValue(); QV4::ExecutionContext *ctx = engine->currentContext(); QV4::ScopedValue result(scope); result = arrayIndex == UINT_MAX ? o->get(engine->id_uintMax) : o->getIndexed(arrayIndex); if (scope.hasException()) result = ctx->catchException(); return new QJSValuePrivate(engine, result); }
/*! \overload Sets the property at the given \a arrayIndex to the given \a value. This function is provided for convenience and performance when working with array objects. If this QJSValue is not an Array object, this function behaves as if setProperty() was called with the string representation of \a arrayIndex. */ void QJSValue::setProperty(quint32 arrayIndex, const QJSValue& value) { ExecutionEngine *engine = d->engine; if (!engine) return; Scope scope(engine); Scoped<Object> o(scope, d->value); if (!o) return; QV4::ExecutionContext *ctx = engine->currentContext(); QV4::ScopedValue v(scope, value.d->getValue(engine)); if (arrayIndex != UINT_MAX) o->putIndexed(arrayIndex, v); else o->put(engine->id_uintMax, v); if (scope.hasException()) ctx->catchException(); }