aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/jsruntime/qv4proxy.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/qml/jsruntime/qv4proxy.cpp')
-rw-r--r--src/qml/jsruntime/qv4proxy.cpp204
1 files changed, 104 insertions, 100 deletions
diff --git a/src/qml/jsruntime/qv4proxy.cpp b/src/qml/jsruntime/qv4proxy.cpp
index 9325e2e53b..974788d420 100644
--- a/src/qml/jsruntime/qv4proxy.cpp
+++ b/src/qml/jsruntime/qv4proxy.cpp
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2018 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtQml module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2018 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qv4proxy_p.h"
@@ -49,6 +13,7 @@ using namespace QV4;
DEFINE_OBJECT_VTABLE(ProxyObject);
DEFINE_OBJECT_VTABLE(ProxyFunctionObject);
+DEFINE_OBJECT_VTABLE(ProxyConstructorObject);
void Heap::ProxyObject::init(const QV4::Object *target, const QV4::Object *handler)
{
@@ -61,12 +26,9 @@ void Heap::ProxyObject::init(const QV4::Object *target, const QV4::Object *handl
void Heap::ProxyFunctionObject::init(const QV4::FunctionObject *target, const QV4::Object *handler)
{
ExecutionEngine *e = internalClass->engine;
- FunctionObject::init(e->rootContext());
+ FunctionObject::init(e);
this->target.set(e, target->d());
this->handler.set(e, handler->d());
-
- if (!target->isConstructor())
- jsConstruct = nullptr;
}
@@ -90,12 +52,15 @@ ReturnedValue ProxyObject::virtualGet(const Managed *m, PropertyKey id, const Va
if (hasProperty)
*hasProperty = true;
- JSCallData cdata(scope, 3, nullptr, handler);
- cdata.args[0] = target;
- cdata.args[1] = id.toStringOrSymbol(scope.engine);
- cdata.args[2] = *receiver;
+ Value *args = scope.alloc(3);
+ args[0] = target;
+ args[1] = id.toStringOrSymbol(scope.engine);
+ args[2] = *receiver;
+ JSCallData cdata(handler, args, 3);
ScopedValue trapResult(scope, static_cast<const FunctionObject *>(trap.ptr)->call(cdata));
+ if (scope.hasException())
+ return Encode::undefined();
ScopedProperty targetDesc(scope);
PropertyAttributes attributes = target->getOwnProperty(id, targetDesc);
if (attributes != Attr_Invalid && !attributes.isConfigurable()) {
@@ -129,14 +94,15 @@ bool ProxyObject::virtualPut(Managed *m, PropertyKey id, const Value &value, Val
if (!trap->isFunctionObject())
return scope.engine->throwTypeError();
- JSCallData cdata(scope, 4, nullptr, handler);
- cdata.args[0] = target;
- cdata.args[1] = id.toStringOrSymbol(scope.engine);
- cdata.args[2] = value;
- cdata.args[3] = *receiver;
+ Value *args = scope.alloc(4);
+ args[0] = target;
+ args[1] = id.toStringOrSymbol(scope.engine);
+ args[2] = value;
+ args[3] = *receiver;
+ JSCallData cdata(handler, args, 4);
ScopedValue trapResult(scope, static_cast<const FunctionObject *>(trap.ptr)->call(cdata));
- if (!trapResult->toBoolean())
+ if (scope.hasException() || !trapResult->toBoolean())
return false;
ScopedProperty targetDesc(scope);
PropertyAttributes attributes = target->getOwnProperty(id, targetDesc);
@@ -170,13 +136,14 @@ bool ProxyObject::virtualDeleteProperty(Managed *m, PropertyKey id)
if (!trap->isFunctionObject())
return scope.engine->throwTypeError();
- JSCallData cdata(scope, 3, nullptr, handler);
- cdata.args[0] = target;
- cdata.args[1] = id.toStringOrSymbol(scope.engine);
- cdata.args[2] = o->d(); // ### fix receiver handling
+ Value *args = scope.alloc(3);
+ args[0] = target;
+ args[1] = id.toStringOrSymbol(scope.engine);
+ args[2] = o->d(); // ### fix receiver handling
+ JSCallData cdata(handler, args, 3);
ScopedValue trapResult(scope, static_cast<const FunctionObject *>(trap.ptr)->call(cdata));
- if (!trapResult->toBoolean())
+ if (scope.hasException() || !trapResult->toBoolean())
return false;
ScopedProperty targetDesc(scope);
PropertyAttributes attributes = target->getOwnProperty(id, targetDesc);
@@ -206,11 +173,14 @@ bool ProxyObject::virtualHasProperty(const Managed *m, PropertyKey id)
if (!trap->isFunctionObject())
return scope.engine->throwTypeError();
- JSCallData cdata(scope, 2, nullptr, handler);
- cdata.args[0] = target;
- cdata.args[1] = id.isArrayIndex() ? Value::fromUInt32(id.asArrayIndex()).toString(scope.engine) : id.asStringOrSymbol();
+ Value *args = scope.alloc(2);
+ args[0] = target;
+ args[1] = id.isArrayIndex() ? Value::fromUInt32(id.asArrayIndex()).toString(scope.engine) : id.asStringOrSymbol();
+ JSCallData cdata(handler, args, 2);
ScopedValue trapResult(scope, static_cast<const FunctionObject *>(trap.ptr)->call(cdata));
+ if (scope.hasException())
+ return false;
bool result = trapResult->toBoolean();
if (!result) {
ScopedProperty targetDesc(scope);
@@ -246,11 +216,14 @@ PropertyAttributes ProxyObject::virtualGetOwnProperty(const Managed *m, Property
return Attr_Invalid;
}
- JSCallData cdata(scope, 2, nullptr, handler);
- cdata.args[0] = target;
- cdata.args[1] = id.isArrayIndex() ? Value::fromUInt32(id.asArrayIndex()).toString(scope.engine) : id.asStringOrSymbol();
+ Value *args = scope.alloc(2);
+ args[0] = target;
+ args[1] = id.isArrayIndex() ? Value::fromUInt32(id.asArrayIndex()).toString(scope.engine) : id.asStringOrSymbol();
+ JSCallData cdata(handler, args, 2);
ScopedValue trapResult(scope, static_cast<const FunctionObject *>(trap.ptr)->call(cdata));
+ if (scope.hasException())
+ return Attr_Invalid;
if (!trapResult->isObject() && !trapResult->isUndefined()) {
scope.engine->throwTypeError();
return Attr_Invalid;
@@ -259,9 +232,9 @@ PropertyAttributes ProxyObject::virtualGetOwnProperty(const Managed *m, Property
ScopedProperty targetDesc(scope);
PropertyAttributes targetAttributes = target->getOwnProperty(id, targetDesc);
if (trapResult->isUndefined()) {
- p->value = Encode::undefined();
- if (targetAttributes == Attr_Invalid) {
+ if (p)
p->value = Encode::undefined();
+ if (targetAttributes == Attr_Invalid) {
return Attr_Invalid;
}
if (!targetAttributes.isConfigurable() || !target->isExtensible()) {
@@ -289,8 +262,10 @@ PropertyAttributes ProxyObject::virtualGetOwnProperty(const Managed *m, Property
}
}
- p->value = resultDesc->value;
- p->set = resultDesc->set;
+ if (p) {
+ p->value = resultDesc->value;
+ p->set = resultDesc->set;
+ }
return resultAttributes;
}
@@ -317,13 +292,14 @@ bool ProxyObject::virtualDefineOwnProperty(Managed *m, PropertyKey id, const Pro
return false;
}
- JSCallData cdata(scope, 3, nullptr, handler);
- cdata.args[0] = target;
- cdata.args[1] = id.isArrayIndex() ? Value::fromUInt32(id.asArrayIndex()).toString(scope.engine) : id.asStringOrSymbol();
- cdata.args[2] = ObjectPrototype::fromPropertyDescriptor(scope.engine, p, attrs);
+ Value *args = scope.alloc(3);
+ args[0] = target;
+ args[1] = id.isArrayIndex() ? Value::fromUInt32(id.asArrayIndex()).toString(scope.engine) : id.asStringOrSymbol();
+ args[2] = ObjectPrototype::fromPropertyDescriptor(scope.engine, p, attrs);
+ JSCallData cdata(handler, args, 3);
ScopedValue trapResult(scope, static_cast<const FunctionObject *>(trap.ptr)->call(cdata));
- bool result = trapResult->toBoolean();
+ bool result = !scope.hasException() && trapResult->toBoolean();
if (!result)
return false;
@@ -369,10 +345,13 @@ bool ProxyObject::virtualIsExtensible(const Managed *m)
if (!trap->isFunctionObject())
return scope.engine->throwTypeError();
- JSCallData cdata(scope, 1, nullptr, handler);
- cdata.args[0] = target;
+ Value *args = scope.alloc(1);
+ args[0] = target;
+ JSCallData cdata(handler, args, 1);
ScopedValue trapResult(scope, static_cast<const FunctionObject *>(trap.ptr)->call(cdata));
+ if (scope.hasException())
+ return false;
bool result = trapResult->toBoolean();
if (result != target->isExtensible()) {
scope.engine->throwTypeError();
@@ -400,10 +379,13 @@ bool ProxyObject::virtualPreventExtensions(Managed *m)
if (!trap->isFunctionObject())
return scope.engine->throwTypeError();
- JSCallData cdata(scope, 1, nullptr, handler);
- cdata.args[0] = target;
+ Value *args = scope.alloc(1);
+ args[0] = target;
+ JSCallData cdata(handler, args, 1);
ScopedValue trapResult(scope, static_cast<const FunctionObject *>(trap.ptr)->call(cdata));
+ if (scope.hasException())
+ return false;
bool result = trapResult->toBoolean();
if (result && target->isExtensible()) {
scope.engine->throwTypeError();
@@ -435,10 +417,13 @@ Heap::Object *ProxyObject::virtualGetPrototypeOf(const Managed *m)
return nullptr;
}
- JSCallData cdata(scope, 1, nullptr, handler);
- cdata.args[0] = target;
+ Value *args = scope.alloc(1);
+ args[0] = target;
+ JSCallData cdata(handler, args, 1);
ScopedValue trapResult(scope, static_cast<const FunctionObject *>(trap.ptr)->call(cdata));
+ if (scope.hasException())
+ return nullptr;
if (!trapResult->isNull() && !trapResult->isObject()) {
scope.engine->throwTypeError();
return nullptr;
@@ -477,12 +462,13 @@ bool ProxyObject::virtualSetPrototypeOf(Managed *m, const Object *p)
return false;
}
- JSCallData cdata(scope, 2, nullptr, handler);
- cdata.args[0] = target;
- cdata.args[1] = p ? p->asReturnedValue() : Encode::null();
+ Value *args = scope.alloc(2);
+ args[0] = target;
+ args[1] = p ? p->asReturnedValue() : Encode::null();
+ JSCallData cdata(handler, args, 2);
ScopedValue trapResult(scope, static_cast<const FunctionObject *>(trap.ptr)->call(cdata));
- bool result = trapResult->toBoolean();
+ bool result = !scope.hasException() && trapResult->toBoolean();
if (!result)
return false;
if (!target->isExtensible()) {
@@ -515,7 +501,7 @@ ProxyObjectOwnPropertyKeyIterator::ProxyObjectOwnPropertyKeyIterator(ArrayObject
PropertyKey ProxyObjectOwnPropertyKeyIterator::next(const Object *m, Property *pd, PropertyAttributes *attrs)
{
- if (index >= len)
+ if (index >= len || m == nullptr)
return PropertyKey::invalid();
Scope scope(m);
@@ -570,9 +556,12 @@ OwnPropertyKeyIterator *ProxyObject::virtualOwnPropertyKeys(const Object *m, Val
return nullptr;
}
- JSCallData cdata(scope, 1, nullptr, handler);
- cdata.args[0] = target;
+ Value *args = scope.alloc(1);
+ args[0] = target;
+ JSCallData cdata(handler, args, 1);
ScopedObject trapResult(scope, static_cast<const FunctionObject *>(trap.ptr)->call(cdata));
+ if (scope.hasException())
+ return nullptr;
if (!trapResult) {
scope.engine->throwTypeError();
return nullptr;
@@ -583,7 +572,7 @@ OwnPropertyKeyIterator *ProxyObject::virtualOwnPropertyKeys(const Object *m, Val
ScopedStringOrSymbol key(scope);
for (uint i = 0; i < len; ++i) {
key = trapResult->get(i);
- if (scope.engine->hasException)
+ if (scope.hasException())
return nullptr;
if (!key) {
scope.engine->throwTypeError();
@@ -608,8 +597,10 @@ OwnPropertyKeyIterator *ProxyObject::virtualOwnPropertyKeys(const Object *m, Val
else
targetNonConfigurableKeys->push_back(keyAsValue);
}
- if (target->isExtensible() && targetNonConfigurableKeys->getLength() == 0)
+ if (target->isExtensible() && targetNonConfigurableKeys->getLength() == 0) {
+ *iteratorTarget = *m;
return new ProxyObjectOwnPropertyKeyIterator(trapKeys);
+ }
ScopedArrayObject uncheckedResultKeys(scope, scope.engine->newArrayObject());
uncheckedResultKeys->copyArrayData(trapKeys);
@@ -623,8 +614,10 @@ OwnPropertyKeyIterator *ProxyObject::virtualOwnPropertyKeys(const Object *m, Val
}
}
- if (target->isExtensible())
+ if (target->isExtensible()) {
+ *iteratorTarget = *m;
return new ProxyObjectOwnPropertyKeyIterator(trapKeys);
+ }
len = targetConfigurableKeys->getLength();
for (uint i = 0; i < len; ++i) {
@@ -648,7 +641,8 @@ OwnPropertyKeyIterator *ProxyObject::virtualOwnPropertyKeys(const Object *m, Val
}
-ReturnedValue ProxyFunctionObject::virtualCallAsConstructor(const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
+ReturnedValue ProxyConstructorObject::virtualCallAsConstructor(
+ const FunctionObject *f, const Value *argv, int argc, const Value *newTarget)
{
Scope scope(f);
const ProxyObject *o = static_cast<const ProxyObject *>(f);
@@ -663,10 +657,8 @@ ReturnedValue ProxyFunctionObject::virtualCallAsConstructor(const FunctionObject
if (scope.hasException())
return Encode::undefined();
- if (trap->isNullOrUndefined()) {
- Q_ASSERT(target->isConstructor());
+ if (trap->isNullOrUndefined())
return target->callAsConstructor(argv, argc, newTarget);
- }
if (!trap->isFunctionObject())
return scope.engine->throwTypeError();
@@ -699,7 +691,7 @@ ReturnedValue ProxyFunctionObject::virtualCall(const FunctionObject *f, const Va
if (scope.hasException())
return Encode::undefined();
if (trap->isNullOrUndefined())
- return target->call(thisObject, argv, argc);
+ return checkedResult(scope.engine, target->call(thisObject, argv, argc));
if (!trap->isFunctionObject())
return scope.engine->throwTypeError();
@@ -713,11 +705,11 @@ ReturnedValue ProxyFunctionObject::virtualCall(const FunctionObject *f, const Va
DEFINE_OBJECT_VTABLE(Proxy);
-void Heap::Proxy::init(QV4::ExecutionContext *ctx)
+void Heap::Proxy::init(QV4::ExecutionEngine *engine)
{
- Heap::FunctionObject::init(ctx, QStringLiteral("Proxy"));
+ Heap::FunctionObject::init(engine, QStringLiteral("Proxy"));
- Scope scope(ctx);
+ Scope scope(engine);
Scoped<QV4::Proxy> ctor(scope, this);
ctor->defineDefaultProperty(QStringLiteral("revocable"), QV4::Proxy::method_revocable, 2);
ctor->defineReadonlyConfigurableProperty(scope.engine->id_length(), Value::fromInt32(2));
@@ -739,9 +731,18 @@ ReturnedValue Proxy::virtualCallAsConstructor(const FunctionObject *f, const Val
return scope.engine->throwTypeError();
const FunctionObject *targetFunction = target->as<FunctionObject>();
- if (targetFunction)
- return scope.engine->memoryManager->allocate<ProxyFunctionObject>(targetFunction, handler)->asReturnedValue();
- return scope.engine->memoryManager->allocate<ProxyObject>(target, handler)->asReturnedValue();
+ if (!targetFunction) {
+ return scope.engine->memoryManager->allocate<ProxyObject>(target, handler)
+ ->asReturnedValue();
+ }
+
+ if (targetFunction->isConstructor()) {
+ return scope.engine->memoryManager->allocate<ProxyConstructorObject>(
+ targetFunction, handler)->asReturnedValue();
+ }
+
+ return scope.engine->memoryManager->allocate<ProxyFunctionObject>(targetFunction, handler)
+ ->asReturnedValue();
}
ReturnedValue Proxy::virtualCall(const FunctionObject *f, const Value *, const Value *, int)
@@ -758,7 +759,10 @@ ReturnedValue Proxy::method_revocable(const FunctionObject *f, const Value *, co
Q_ASSERT(proxy);
ScopedString revoke(scope, scope.engine->newString(QStringLiteral("revoke")));
- ScopedFunctionObject revoker(scope, scope.engine->memoryManager->allocate<FunctionObject>(scope.engine->rootContext(), nullptr, method_revoke));
+ ScopedFunctionObject revoker(
+ scope,
+ scope.engine->memoryManager->allocate<DynamicFunctionObject>(
+ scope.engine, nullptr, method_revoke));
revoker->defineReadonlyConfigurableProperty(scope.engine->id_length(), Value::fromInt32(0));
revoker->defineDefaultProperty(scope.engine->symbol_revokableProxy(), proxy);