diff --git a/src/native/QmlNet/QmlNet/qml/JsNetArray.h b/src/native/QmlNet/QmlNet/qml/JsNetArray.h deleted file mode 100644 index f4bdb3d7..00000000 --- a/src/native/QmlNet/QmlNet/qml/JsNetArray.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef JSNETARRAY_H -#define JSNETARRAY_H - -#include -#include - -namespace QV4 { - -namespace Heap { - -struct NetArray : Object { - void init(); - void destroy(); - QV4::Value* object; -}; - -} - -struct NetArray : Object -{ - V4_OBJECT2(NetArray, Object) - V4_NEEDS_DESTROY - Q_MANAGED_TYPE(ArrayObject) - - static ReturnedValue create(ExecutionEngine *engine, NetValue* netValue); - - #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0) - static void method_length(const BuiltinFunction *, Scope &, CallData *callData); - static void method_push(const BuiltinFunction *, Scope &, CallData *callData); - static void method_pop(const BuiltinFunction *, Scope &, CallData *callData); - static void method_forEach(const BuiltinFunction *, Scope &, CallData *callData); - #else - static ReturnedValue method_length(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); - static ReturnedValue method_push(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); - static ReturnedValue method_pop(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); - static ReturnedValue method_forEach(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); - #endif - - static ReturnedValue getIndexed(const Managed *m, uint index, bool *hasProperty); - static bool putIndexed(Managed *m, uint index, const Value &value); -}; - -} - -#endif // JSNETARRAY_H diff --git a/src/native/QmlNet/QmlNet/qml/JsNetArray510.cpp b/src/native/QmlNet/QmlNet/qml/JsNetArray510.cpp deleted file mode 100644 index c11995a2..00000000 --- a/src/native/QmlNet/QmlNet/qml/JsNetArray510.cpp +++ /dev/null @@ -1,182 +0,0 @@ -#include -#include -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE - -using namespace QV4; - -DEFINE_OBJECT_VTABLE(NetArray); - -void Heap::NetArray::init() -{ - Object::init(); - QV4::Scope scope(internalClass->engine); - QV4::ScopedObject o(scope, this); - o->setArrayType(Heap::ArrayData::Custom); - o->defineAccessorProperty(QStringLiteral("length"), QV4::NetArray::method_length, nullptr); - o->defineDefaultProperty(QStringLiteral("push"), QV4::NetArray::method_push); - o->defineDefaultProperty(QStringLiteral("pop"), QV4::NetArray::method_pop); - o->defineDefaultProperty(QStringLiteral("forEach"), QV4::NetArray::method_forEach); - object = scope.engine->memoryManager->m_persistentValues->allocate(); -} - -void Heap::NetArray::destroy() -{ - QV4::PersistentValueStorage::free(object); - Object::destroy(); -} - -ReturnedValue NetArray::create(ExecutionEngine *engine, NetValue* netValue) -{ - Scope scope(engine); - Scoped r(scope, engine->memoryManager->allocObject()); - *r->d()->object = QObjectWrapper::wrap(scope.engine, netValue); - return r.asReturnedValue(); -} - -void NetArray::method_length(const BuiltinFunction *, Scope &scope, CallData *callData) -{ - ScopedObject instance(scope, callData->thisObject.toObject(scope.engine)); - NetArray* netArray = instance->as(); - Scoped wrapper(scope, netArray->d()->object); - NetValue* netValue = reinterpret_cast(wrapper->d()->object()); - QSharedPointer arrayFacade = netValue->getNetReference()->getTypeInfo()->getArrayFacade(); - - if(arrayFacade == nullptr) { - THROW_GENERIC_ERROR("The wrapped object can't be treated as an array."); - } - - scope.result = Encode(arrayFacade->getLength(netValue->getNetReference())); -} - -void NetArray::method_push(const BuiltinFunction *, Scope &scope, CallData *callData) -{ - ScopedObject instance(scope, callData->thisObject.toObject(scope.engine)); - NetArray* netArray = instance->as(); - Scoped wrapper(scope, netArray->d()->object); - NetValue* netValue = reinterpret_cast(wrapper->d()->object()); - QSharedPointer arrayFacade = netValue->getNetReference()->getTypeInfo()->getArrayFacade(); - - if(arrayFacade == nullptr) { - THROW_GENERIC_ERROR("The wrapped object can't be treated as an array."); - } - - if(arrayFacade->isFixed()) { - THROW_GENERIC_ERROR("Can't modify a fixed .NET list type."); - } - - uint len = instance->getLength(); - if(len == 0) { - RETURN_UNDEFINED(); - } - - for (int i = 0, ei = callData->argc; i < ei; ++i) { - QV4::ScopedValue valueScope(scope, callData->args[i]); - QJSValue valueJsValue(scope.engine, valueScope->asReturnedValue()); - arrayFacade->push(netValue->getNetReference(), NetVariant::fromQJSValue(valueJsValue)); - } - - scope.result = Encode(instance->getLength()); -} - -void NetArray::method_pop(const BuiltinFunction *, Scope &scope, CallData *callData) -{ - ScopedObject instance(scope, callData->thisObject.toObject(scope.engine)); - NetArray* netArray = instance->as(); - Scoped wrapper(scope, netArray->d()->object); - NetValue* netValue = reinterpret_cast(wrapper->d()->object()); - QSharedPointer arrayFacade = netValue->getNetReference()->getTypeInfo()->getArrayFacade(); - - if(arrayFacade == nullptr) { - THROW_GENERIC_ERROR("The wrapped object can't be treated as an array."); - } - - if(arrayFacade->isFixed()) { - THROW_GENERIC_ERROR("Can't modify a fixed .NET list type."); - } - - uint len = instance->getLength(); - if(len == 0) { - RETURN_UNDEFINED(); - } - - QSharedPointer result = arrayFacade->pop(netValue->getNetReference()); - if(result == nullptr) { - scope.result = Encode::null(); - } - QJSValue resultJsValue = result->toQJSValue(scope.engine->jsEngine()); - scope.result = scope.engine->fromVariant(resultJsValue.toVariant()); -} - -void NetArray::method_forEach(const BuiltinFunction *, Scope &scope, CallData *callData) -{ - ScopedObject instance(scope, callData->thisObject.toObject(scope.engine)); - if (!instance) - RETURN_UNDEFINED(); - - uint len = instance->getLength(); - - ScopedFunctionObject callback(scope, callData->argument(0)); - if (!callback) - THROW_TYPE_ERROR(); - - ScopedCallData cData(scope, 3); - cData->thisObject = callData->argument(1); - cData->args[2] = instance; - - ScopedValue v(scope); - for (uint k = 0; k < len; ++k) { - bool exists; - v = instance->getIndexed(k, &exists); - if (!exists) - continue; - - cData->args[0] = v; - cData->args[1] = Primitive::fromDouble(k); - callback->call(scope, cData); - } - RETURN_UNDEFINED(); -} - -ReturnedValue NetArray::getIndexed(const Managed *m, uint index, bool *hasProperty) -{ - const NetArray *netArray = static_cast(m); - Scope scope(netArray->engine()); - QV4::Scoped wrapper(scope, netArray->d()->object); - NetValue* netValue = reinterpret_cast(wrapper->d()->object()); - QSharedPointer arrayFacade = netValue->getNetReference()->getTypeInfo()->getArrayFacade(); - - if(hasProperty) - *hasProperty = true; - - if(arrayFacade == nullptr) { - return scope.engine->throwError(QString::fromUtf8("The wrapped object can't be treated as an array.")); - } - - QSharedPointer result = arrayFacade->getIndexed(netValue->getNetReference(), index); - QJSValue resultJsValue = result->toQJSValue(scope.engine->jsEngine()); - return scope.engine->fromVariant(resultJsValue.toVariant()); -} - -bool NetArray::putIndexed(Managed *m, uint index, const Value &value) -{ - const NetArray *netArray = static_cast(m); - Scope scope(netArray->engine()); - QV4::Scoped wrapper(scope, netArray->d()->object); - NetValue* netValue = reinterpret_cast(wrapper->d()->object()); - QSharedPointer arrayFacade = netValue->getNetReference()->getTypeInfo()->getArrayFacade(); - - if(arrayFacade == nullptr) { - return scope.engine->throwError(QString::fromUtf8("The wrapped object can't be treated as an array.")); - } - - QV4::ScopedValue valueScope(scope, value); - QJSValue valueJsValue(scope.engine, valueScope->asReturnedValue()); - arrayFacade->setIndexed(netValue->getNetReference(), index, NetVariant::fromQJSValue(valueJsValue)); - - return true; -} diff --git a/src/native/QmlNet/QmlNet/qml/JsNetArray511.cpp b/src/native/QmlNet/QmlNet/qml/JsNetArray511.cpp deleted file mode 100644 index 27758c7b..00000000 --- a/src/native/QmlNet/QmlNet/qml/JsNetArray511.cpp +++ /dev/null @@ -1,203 +0,0 @@ -#include -#include -#include -#include -#include -#include - -QT_BEGIN_NAMESPACE - -using namespace QV4; - -DEFINE_OBJECT_VTABLE(NetArray); - -void Heap::NetArray::init() -{ - Object::init(); - QV4::Scope scope(internalClass->engine); - QV4::ScopedObject o(scope, this); - o->setArrayType(Heap::ArrayData::Custom); - o->defineAccessorProperty(QStringLiteral("length"), QV4::NetArray::method_length, nullptr); - o->defineDefaultProperty(QStringLiteral("push"), QV4::NetArray::method_push); - o->defineDefaultProperty(QStringLiteral("pop"), QV4::NetArray::method_pop); - o->defineDefaultProperty(QStringLiteral("forEach"), QV4::NetArray::method_forEach); - object = scope.engine->memoryManager->m_persistentValues->allocate(); -} - -void Heap::NetArray::destroy() -{ - QV4::PersistentValueStorage::free(object); - Object::destroy(); -} - -ReturnedValue NetArray::create(ExecutionEngine *engine, NetValue* netValue) -{ - Scope scope(engine); - Scoped r(scope, engine->memoryManager->allocObject()); - *r->d()->object = QObjectWrapper::wrap(scope.engine, netValue); - return r.asReturnedValue(); -} - -ReturnedValue NetArray::method_length(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) -{ - Scope scope(b); - - Scoped netArray(scope, thisObject->as()); - Scoped wrapper(scope, netArray->d()->object); - if (!wrapper) { - THROW_GENERIC_ERROR("No reference to the wrapped QObject exists."); - } - - NetValue* netValue = reinterpret_cast(wrapper->d()->object()); - QSharedPointer arrayFacade = netValue->getNetReference()->getTypeInfo()->getArrayFacade(); - - if(arrayFacade == nullptr) { - THROW_GENERIC_ERROR("The wrapped object can't be treated as an array."); - } - - return Encode(arrayFacade->getLength(netValue->getNetReference())); -} - -ReturnedValue NetArray::method_push(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) -{ - Scope scope(b); - - ScopedObject instance(scope, thisObject->toObject(scope.engine)); - Scoped netArray(scope, thisObject->as()); - Scoped wrapper(scope, netArray->d()->object); - if (!wrapper) { - THROW_GENERIC_ERROR("No reference to the wrapped QObject exists."); - } - - NetValue* netValue = reinterpret_cast(wrapper->d()->object()); - QSharedPointer arrayFacade = netValue->getNetReference()->getTypeInfo()->getArrayFacade(); - - if(arrayFacade == nullptr) { - THROW_GENERIC_ERROR("The wrapped object can't be treated as an array."); - } - - if(arrayFacade->isFixed()) { - THROW_GENERIC_ERROR("Can't modify a fixed .NET list type."); - } - - for (int i = 0, ei = argc; i < ei; ++i) { - QV4::ScopedValue valueScope(scope, argv[i]); - QJSValue valueJsValue(scope.engine, valueScope->asReturnedValue()); - arrayFacade->push(netValue->getNetReference(), NetVariant::fromQJSValue(valueJsValue)); - } - - return Encode(instance->getLength()); -} - -ReturnedValue NetArray::method_pop(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) -{ - Scope scope(b); - - ScopedObject instance(scope, thisObject->toObject(scope.engine)); - Scoped netArray(scope, thisObject->as()); - Scoped wrapper(scope, netArray->d()->object); - if (!wrapper) { - THROW_GENERIC_ERROR("No reference to the wrapped QObject exists."); - } - - NetValue* netValue = reinterpret_cast(wrapper->d()->object()); - QSharedPointer arrayFacade = netValue->getNetReference()->getTypeInfo()->getArrayFacade(); - - if(arrayFacade == nullptr) { - THROW_GENERIC_ERROR("The wrapped object can't be treated as an array."); - } - - if(arrayFacade->isFixed()) { - THROW_GENERIC_ERROR("Can't modify a fixed .NET list type."); - } - - uint len = instance->getLength(); - if(len == 0) { - RETURN_UNDEFINED(); - } - - QSharedPointer result = arrayFacade->pop(netValue->getNetReference()); - if(result == nullptr) { - return Encode::null(); - } - QJSValue resultJsValue = result->toQJSValue(scope.engine->jsEngine()); - return scope.engine->fromVariant(resultJsValue.toVariant()); -} - -ReturnedValue NetArray::method_forEach(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) -{ - Scope scope(b); - ScopedObject instance(scope, thisObject->toObject(scope.engine)); - if (!instance) - RETURN_UNDEFINED(); - - uint len = instance->getLength(); - - if (!argc || !argv->isFunctionObject()) - THROW_TYPE_ERROR(); - const FunctionObject *callback = static_cast(argv); - - ScopedValue that(scope, argc > 1 ? argv[1] : Primitive::undefinedValue()); - Value *arguments = scope.alloc(3); - - for (uint k = 0; k < len; ++k) { - bool exists; - arguments[0] = instance->getIndexed(k, &exists); - if (!exists) - continue; - - arguments[1] = Primitive::fromDouble(k); - arguments[2] = instance; - callback->call(that, arguments, 3); - } - RETURN_UNDEFINED(); -} - -ReturnedValue NetArray::getIndexed(const Managed *m, uint index, bool *hasProperty) -{ - const NetArray *netArray = static_cast(m); - Scope scope(netArray->engine()); - if(hasProperty) - *hasProperty = true; - - QV4::Scoped wrapper(scope, netArray->d()->object); - if (!wrapper) { - THROW_GENERIC_ERROR("No reference to the wrapped QObject exists."); - } - - NetValue* netValue = reinterpret_cast(wrapper->d()->object()); - QSharedPointer arrayFacade = netValue->getNetReference()->getTypeInfo()->getArrayFacade(); - - if(arrayFacade == nullptr) { - THROW_GENERIC_ERROR("The wrapped object can't be treated as an array."); - } - - QSharedPointer result = arrayFacade->getIndexed(netValue->getNetReference(), static_cast(index)); - QJSValue resultJsValue = result->toQJSValue(scope.engine->jsEngine()); - return scope.engine->fromVariant(resultJsValue.toVariant()); -} - -bool NetArray::putIndexed(Managed *m, uint index, const Value &value) -{ - const NetArray *netArray = static_cast(m); - Scope scope(netArray->engine()); - QV4::ScopedValue valueScope(scope, value); - QJSValue valueJsValue(scope.engine, valueScope->asReturnedValue()); - - QV4::Scoped wrapper(scope, netArray->d()->object); - if (!wrapper) { - THROW_GENERIC_ERROR("No reference to the wrapped QObject exists."); - } - - NetValue* netValue = reinterpret_cast(wrapper->d()->object()); - QSharedPointer arrayFacade = netValue->getNetReference()->getTypeInfo()->getArrayFacade(); - - if(arrayFacade == nullptr) { - THROW_GENERIC_ERROR("The wrapped object can't be treated as an array."); - } - - arrayFacade->setIndexed(netValue->getNetReference(), - index, - NetVariant::fromQJSValue(valueJsValue)); - return true; -} diff --git a/src/native/QmlNet/QmlNet/qml/JsNetObject.cpp b/src/native/QmlNet/QmlNet/qml/JsNetObject.cpp index 0b3795e7..bc1f1ad1 100644 --- a/src/native/QmlNet/QmlNet/qml/JsNetObject.cpp +++ b/src/native/QmlNet/QmlNet/qml/JsNetObject.cpp @@ -1,345 +1,121 @@ #include +#include #include -#include -#include #include -#include #include #include -#include -using namespace QV4; - -DEFINE_OBJECT_VTABLE(NetObject); - -void Heap::NetObject::init() { - Scope scope(internalClass->engine); - ScopedObject o(scope, this); - o->defineDefaultProperty(QStringLiteral("gcCollect"), QV4::NetObject::method_gccollect); - o->defineDefaultProperty(QStringLiteral("await"), QV4::NetObject::method_await); - o->defineDefaultProperty(QStringLiteral("cancelTokenSource"), QV4::NetObject::method_cancelTokenSource); - o->defineDefaultProperty(QStringLiteral("serialize"), QV4::NetObject::method_serialize); - o->defineDefaultProperty(QStringLiteral("toJsArray"), QV4::NetObject::method_toJsArray); - o->defineDefaultProperty(QStringLiteral("toListModel"), QV4::NetObject::method_toListModel); -} - -#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0) - -void NetObject::method_gccollect(const BuiltinFunction *, Scope &scope, CallData *callData) { - int maxGeneration = 0; - if(callData->argc > 0) { - maxGeneration = callData->args[0].toNumber(); - } - gcCollect(maxGeneration); - scope.result = QV4::Encode::undefined(); -} - -void NetObject::method_await(const BuiltinFunction *, Scope &scope, CallData *callData) { - scope.result = QV4::Encode::undefined(); - - if (callData->argc != 2 && callData->argc != 3) { - qWarning() << "Invalid number of parameters passed to Net.await(task, callback)"; - return; - } - - ScopedValue task(scope, callData->args[0]); - ScopedValue successCallback(scope, callData->args[1]); - ScopedValue failureCallback(scope); - - if(callData->argc == 3) { - failureCallback = ScopedValue(scope, callData->args[2]); - } - - if(task->isNullOrUndefined()) { - qWarning() << "No task for Net.await(task, callback)"; - return; - } - - if(successCallback->isNullOrUndefined()) { - qWarning() << "No callback for Net.await(task, callback)"; - return; - } - - QJSValue taskJsValue(scope.engine, task->asReturnedValue()); - QJSValue callbackJsValue(scope.engine, successCallback->asReturnedValue()); - QJSValue failureJsValue(scope.engine, failureCallback->asReturnedValue()); - - QObject* qObject = taskJsValue.toQObject(); - NetValueInterface* netValue = qobject_cast(qObject); - - if(!netValue) { - qWarning() << "Invalid task object passed to Net.await(task, callback)"; - return; - } - - // Send the method to .NET, await the task, and call the callback. - awaitTask(netValue->getNetReference(), - QSharedPointer(new NetJSValue(callbackJsValue)), - failureJsValue.isNull() - ? QSharedPointer(nullptr) - : QSharedPointer(new NetJSValue(failureJsValue))); -} - -void NetObject::method_cancelTokenSource(const BuiltinFunction *, Scope &scope, CallData *callData) { - Q_UNUSED(callData); - QSharedPointer typeInfo = NetTypeManager::getTypeInfo("System.Threading.CancellationTokenSource"); - if(typeInfo == nullptr) { - qWarning() << "Couldn't get cancellation token type for platform, please file a bug"; - scope.result = QV4::Encode::undefined(); - return; - } - QSharedPointer netReference = instantiateType(typeInfo); - if(netReference == nullptr) { - qWarning() << "Couldn't create cancellation token for platform, please file a bug"; - scope.result = QV4::Encode::undefined(); - return; - } - NetValue* netValue = NetValue::forInstance(netReference); - scope.result = QV4::QObjectWrapper::wrap(scope.engine, netValue); -} - -void NetObject::method_serialize(const BuiltinFunction *, Scope &scope, CallData *callData) +JsNetObject::JsNetObject() { - if(callData->argc != 1) { - THROW_GENERIC_ERROR("Net.serialize(): Missing instance parameter"); + +} + +QString JsNetObject::serialize(QJSValue value) +{ + if(value.isNull() || value.isUndefined()) { + qWarning("Net.serialize(): Instance parameter must not be null or undefined"); + return QString(); } - ScopedValue instance(scope, callData->args[0]); - if(instance->isNullOrUndefined()) { - THROW_GENERIC_ERROR("Net.serialize(): Instance parameter must not be null or undefined"); - } - - QJSValue instanceJsValue(scope.engine, instance->asReturnedValue()); - QSharedPointer value = NetVariant::fromQJSValue(instanceJsValue); - if(value->getVariantType() != NetVariantTypeEnum_Object) { - THROW_GENERIC_ERROR("Net.serialize(): Parameter is not a .NET object"); + QSharedPointer netVaraint = NetVariant::fromQJSValue(value); + if(netVaraint->getVariantType() != NetVariantTypeEnum_Object) { + qWarning("Net.serialize(): Parameter is not a .NET object"); + return QString(); } QSharedPointer result = QSharedPointer(new NetVariant()); - bool serializationResult = serializeNetToString(value->getNetReference(), result); + bool serializationResult = QmlNet::serializeNetToString(netVaraint->getNetReference(), result); if(!serializationResult) { - THROW_GENERIC_ERROR("Net.serialize(): Could not serialize object."); + qWarning("Net.serialize(): Could not serialize object."); + return QString(); } - scope.result = scope.engine->newString(result->getString()); + return result->getString(); } -void NetObject::method_toJsArray(const BuiltinFunction *, Scope &scope, CallData *callData) +QVariant JsNetObject::cancelTokenSource() { - if(callData->argc != 1) { - THROW_GENERIC_ERROR("Net.toJsArray(): Missing instance parameter"); - } - QV4::ScopedValue instance(scope, callData->args[0]); - if(instance->isNullOrUndefined()) { - THROW_GENERIC_ERROR("Net.toJsArray(): Instance parameter must not be null or undefined"); - } - QJSValue instanceJsValue(scope.engine, instance->asReturnedValue()); - QSharedPointer value = NetVariant::fromQJSValue(instanceJsValue); - if(value->getVariantType() != NetVariantTypeEnum_Object) { - THROW_GENERIC_ERROR("Net.toJsArray(): Parameter is not a .NET object"); - } - - QSharedPointer netReference = value->getNetReference(); - if(!netReference->getTypeInfo()->isArray() && !netReference->getTypeInfo()->isList()) { - THROW_GENERIC_ERROR("Net.toJsArray(): Parameter is not a type that can be wrapped on a JavaScript list."); - } - - scope.result = NetArray::create(scope.engine, NetValue::forInstance(netReference)); -} - -void NetObject::method_toListModel(const BuiltinFunction *, Scope &scope, CallData *callData) -{ - if(callData->argc != 1) { - THROW_GENERIC_ERROR("Net.toListModel(): Missing instance parameter"); - } - qInfo("TOJSARRAYY"); - QV4::ScopedValue instance(scope, callData->args[0]); - if(instance->isNullOrUndefined()) { - THROW_GENERIC_ERROR("Net.toListModel(): Instance parameter must not be null or undefined"); - } - QJSValue instanceJsValue(scope.engine, instance->asReturnedValue()); - QSharedPointer value = NetVariant::fromQJSValue(instanceJsValue); - if(value->getVariantType() != NetVariantTypeEnum_Object) { - THROW_GENERIC_ERROR("Net.toListModel(): Parameter is not a .NET object"); - } - - QSharedPointer netReference = value->getNetReference(); - NetListModel* listModel = NetListModel::fromReference(netReference); - if(listModel == nullptr) { - THROW_GENERIC_ERROR("Net.toListModel(): Parameter is not a type that can be wrapped by a list model."); - } - - QQmlEngine::setObjectOwnership(listModel, QQmlEngine::JavaScriptOwnership); - scope.result = QObjectWrapper::wrap(scope.engine, listModel); -} - -#else - -ReturnedValue NetObject::method_gccollect(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) { - Q_UNUSED(b); - Q_UNUSED(thisObject); - int maxGeneration = 0; - if(argc > 0) { - maxGeneration = static_cast(argv[0].toNumber()); - } - gcCollect(maxGeneration); - return Encode::undefined(); -} - -ReturnedValue NetObject::method_await(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) { - QV4::Scope scope(b); - - if(argc != 2 && argc != 3) { - qWarning() << "Invalid number of parameters passed to Net.await(task, callback)"; - return Encode::undefined(); - } - - QV4::ScopedValue task(scope, argv[0]); - QV4::ScopedValue successCallback(scope, argv[1]); - QV4::ScopedValue failureCallback(scope, Encode::null()); - - if(argc == 3) { - // We are passing in a failure callback - failureCallback = argv[2]; - } - - if(task->isNullOrUndefined()) { - qWarning() << "No task for Net.await(task, callback)"; - return Encode::undefined(); - } - - if(successCallback->isNullOrUndefined()) { - qWarning() << "No callback for Net.await(task, callback)"; - return Encode::undefined(); - } - - QJSValue taskJsValue(scope.engine, task->asReturnedValue()); - QJSValue successCallbackJsValue(scope.engine, successCallback->asReturnedValue()); - QJSValue failureCallbackJsValue(scope.engine, failureCallback->asReturnedValue()); - - if(!taskJsValue.isQObject()) { - qWarning() << "Invalid task object passed to Net.await(task, callback)"; - return Encode::undefined(); - } - - if(!successCallbackJsValue.isCallable()) { - qWarning() << "Invalid function passed for success callback"; - return Encode::undefined(); - } - - if(!failureCallbackJsValue.isNull() && !failureCallbackJsValue.isCallable()) { - qWarning() << "Invalid function passed for failure callback"; - return Encode::undefined(); - } - - QObject* qObject = taskJsValue.toQObject(); - NetValueInterface* netValue = qobject_cast(qObject); - - if(!netValue) { - qWarning() << "Invalid task object passed to Net.await(task, callback)"; - return Encode::undefined(); - } - - // Send the method to .NET, await the task, and call the callback. - awaitTask(netValue->getNetReference(), - QSharedPointer(new NetJSValue(successCallbackJsValue)), - failureCallbackJsValue.isNull() - ? QSharedPointer(nullptr) - : QSharedPointer(new NetJSValue(failureCallbackJsValue))); - - return Encode::undefined(); -} - -ReturnedValue NetObject::method_cancelTokenSource(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) { - QV4::Scope scope(b); QSharedPointer typeInfo = NetTypeManager::getTypeInfo("System.Threading.CancellationTokenSource"); if(typeInfo == nullptr) { - qWarning() << "Couldn't get cancellation token type for platform, please file a bug"; - return Encode::undefined(); + qWarning("Couldn't get cancellation token type for platform, please file a bug"); + return QVariant(); } - QSharedPointer netReference = instantiateType(typeInfo); + QSharedPointer netReference = QmlNet::instantiateType(typeInfo); if(netReference == nullptr) { - qWarning() << "Couldn't create cancellation token for platform, please file a bug"; - return Encode::undefined(); + qWarning("Couldn't create cancellation token for platform, please file a bug"); + return QVariant(); } - NetValue* netValue = NetValue::forInstance(netReference); - return QV4::QObjectWrapper::wrap(scope.engine, netValue); + QSharedPointer netVariant = QSharedPointer(new NetVariant()); + netVariant->setNetReference(netReference); + return netVariant->toQVariant(); } -ReturnedValue NetObject::method_serialize(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) +void JsNetObject::gcCollect(int maxGeneration) { - QV4::Scope scope(b); - if(argc != 1) { - THROW_GENERIC_ERROR("Net.serialize(): Missing instance parameter"); - } - QV4::ScopedValue instance(scope, argv[0]); - if(instance->isNullOrUndefined()) { - THROW_GENERIC_ERROR("Net.serialize(): Instance parameter must not be null or undefined"); - } - QJSValue instanceJsValue(scope.engine, instance->asReturnedValue()); - QSharedPointer value = NetVariant::fromQJSValue(instanceJsValue); - if(value->getVariantType() != NetVariantTypeEnum_Object) { - THROW_GENERIC_ERROR("Net.serialize(): Parameter is not a .NET object"); - } - QSharedPointer result = QSharedPointer(new NetVariant()); - bool serializationResult = serializeNetToString(value->getNetReference(), result); - if(!serializationResult) { - THROW_GENERIC_ERROR("Net.serialize(): Could not serialize object."); - } - - return Encode(scope.engine->newString(result->getString())); + QmlNet::gcCollect(maxGeneration); } -ReturnedValue NetObject::method_toJsArray(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) +QVariant JsNetObject::toListModel(QJSValue value) { - QV4::Scope scope(b); - - if(argc != 1) { - THROW_GENERIC_ERROR("Net.toJsArray(): Missing instance parameter"); - } - QV4::ScopedValue instance(scope, argv[0]); - if(instance->isNullOrUndefined()) { - THROW_GENERIC_ERROR("Net.toJsArray(): Instance parameter must not be null or undefined"); - } - QJSValue instanceJsValue(scope.engine, instance->asReturnedValue()); - QSharedPointer value = NetVariant::fromQJSValue(instanceJsValue); - if(value->getVariantType() != NetVariantTypeEnum_Object) { - THROW_GENERIC_ERROR("Net.toJsArray(): Parameter is not a .NET object"); + if(value.isNull() || value.isUndefined()) { + qWarning("Net.toListModel(): Instance parameter must not be null or undefined"); + return QVariant(); } - QSharedPointer netReference = value->getNetReference(); - if(!netReference->getTypeInfo()->isArray() && !netReference->getTypeInfo()->isList()) { - THROW_GENERIC_ERROR("Net.toJsArray(): Parameter is not a type that can be wrapped on a JavaScript list."); + QSharedPointer netVaraint = NetVariant::fromQJSValue(value); + if(netVaraint->getVariantType() != NetVariantTypeEnum_Object) { + qWarning("Net.toListModel(): Parameter is not a .NET object"); + return QVariant(); } - return NetArray::create(scope.engine, NetValue::forInstance(netReference)); -} - -ReturnedValue NetObject::method_toListModel(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) -{ - QV4::Scope scope(b); - - if(argc != 1) { - THROW_GENERIC_ERROR("Net.toListModel(): Missing instance parameter"); - } - QV4::ScopedValue instance(scope, argv[0]); - if(instance->isNullOrUndefined()) { - THROW_GENERIC_ERROR("Net.toListModel(): Instance parameter must not be null or undefined"); - } - QJSValue instanceJsValue(scope.engine, instance->asReturnedValue()); - QSharedPointer value = NetVariant::fromQJSValue(instanceJsValue); - if(value->getVariantType() != NetVariantTypeEnum_Object) { - THROW_GENERIC_ERROR("Net.toListModel(): Parameter is not a .NET object"); - } - - QSharedPointer netReference = value->getNetReference(); - NetListModel* listModel = NetListModel::fromReference(netReference); + NetListModel* listModel = NetListModel::fromReference(netVaraint->getNetReference()); if(listModel == nullptr) { - THROW_GENERIC_ERROR("Net.toListModel(): Parameter is not a type that can be wrapped by a list model."); + qWarning("Net.toListModel(): Parameter is not a type that can be wrapped by a list model."); + return QVariant(); } QQmlEngine::setObjectOwnership(listModel, QQmlEngine::JavaScriptOwnership); - return QObjectWrapper::wrap(scope.engine, listModel); + return QVariant::fromValue(listModel); } -#endif +void JsNetObject::toJsArray() +{ + qWarning("Net.toJsArray(): Not supported anymore. Use Net.toListModel()."); +} + +void JsNetObject::await(QJSValue task, QJSValue successCallback, QJSValue failureCallback) +{ + if(task.isNull() || task.isUndefined()) { + qWarning("Net.await(): No task object provided."); + return; + } + + if(successCallback.isNull() || successCallback.isUndefined()) { + qWarning("Net.await(): Not success callback given"); + return; + } + + if(!successCallback.isCallable()) { + qWarning("Net.await(): Success callback invalid type."); + return; + } + + if(!failureCallback.isNull() && !failureCallback.isUndefined()) { + if(!failureCallback.isCallable()) { + qWarning("Net.await(): Failure callback invalid type."); + return; + } + } + + QSharedPointer taskVariant = NetVariant::fromQJSValue(task); + if(taskVariant->getVariantType() != NetVariantTypeEnum_Object) { + qWarning("Net.await(): Task is invalid type."); + return; + } + + QSharedPointer successCallbackVariant = NetVariant::fromQJSValue(successCallback); + QSharedPointer failureCallbackVariant = NetVariant::fromQJSValue(failureCallback); + QmlNet::awaitTask(taskVariant->getNetReference(), + successCallbackVariant->getJsValue(), + failureCallbackVariant != nullptr ? failureCallbackVariant->getJsValue() : nullptr); +} diff --git a/src/native/QmlNet/QmlNet/qml/JsNetObject.h b/src/native/QmlNet/QmlNet/qml/JsNetObject.h index f3094c45..cf93799d 100644 --- a/src/native/QmlNet/QmlNet/qml/JsNetObject.h +++ b/src/native/QmlNet/QmlNet/qml/JsNetObject.h @@ -1,38 +1,22 @@ #ifndef JSNETOBJECT_H #define JSNETOBJECT_H -#include +#include +#include +#include +#include -namespace QV4 { - -namespace Heap { - -struct NetObject : Object { - void init(); -}; - -} - -struct NetObject : Object +class JsNetObject : public QObject { - V4_OBJECT2(NetObject, Object) - #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0) - static void method_gccollect(const BuiltinFunction *, Scope &scope, CallData *callData); - static void method_await(const BuiltinFunction *, Scope &scope, CallData *callData); - static void method_cancelTokenSource(const BuiltinFunction *, Scope &scope, CallData *callData); - static void method_serialize(const BuiltinFunction *, Scope &scope, CallData *callData); - static void method_toJsArray(const BuiltinFunction *, Scope &scope, CallData *callData); - static void method_toListModel(const BuiltinFunction *, Scope &scope, CallData *callData); - #else - static ReturnedValue method_gccollect(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); - static ReturnedValue method_await(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); - static ReturnedValue method_cancelTokenSource(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); - static ReturnedValue method_serialize(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); - static ReturnedValue method_toJsArray(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); - static ReturnedValue method_toListModel(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc); - #endif + Q_OBJECT +public: + JsNetObject(); + Q_INVOKABLE QString serialize(QJSValue value); + Q_INVOKABLE QVariant cancelTokenSource(); + Q_INVOKABLE void gcCollect(int maxGeneration = 0); + Q_INVOKABLE QVariant toListModel(QJSValue value); + Q_INVOKABLE void toJsArray(); + Q_INVOKABLE void await(QJSValue task, QJSValue successCallback, QJSValue failureCallback = QJSValue()); }; -} - #endif // JSNETOBJECT_H diff --git a/src/native/QmlNet/QmlNet/qml/NetValueMetaObject.cpp b/src/native/QmlNet/QmlNet/qml/NetValueMetaObject.cpp index d46632ce..cf6cf2d7 100644 --- a/src/native/QmlNet/QmlNet/qml/NetValueMetaObject.cpp +++ b/src/native/QmlNet/QmlNet/qml/NetValueMetaObject.cpp @@ -112,7 +112,7 @@ int NetValueMetaObject::metaCall(QMetaObject::Call c, int idx, void **a) QSharedPointer propertyType = propertyInfo->getReturnType(); QSharedPointer result = QSharedPointer(new NetVariant()); - readProperty(propertyInfo, instance, nullptr, result); + QmlNet::readProperty(propertyInfo, instance, nullptr, result); NetMetaValuePack(propertyType->getPrefVariantType(), result, a[0]); } @@ -130,7 +130,7 @@ int NetValueMetaObject::metaCall(QMetaObject::Call c, int idx, void **a) QSharedPointer newValue = QSharedPointer(new NetVariant()); NetMetaValueUnpack(propertyType->getPrefVariantType(), newValue, a[0]); - writeProperty(propertyInfo, instance, nullptr, newValue); + QmlNet::writeProperty(propertyInfo, instance, nullptr, newValue); } break; case InvokeMetaMethod: @@ -170,7 +170,7 @@ int NetValueMetaObject::metaCall(QMetaObject::Call c, int idx, void **a) result = QSharedPointer(new NetVariant()); } - invokeNetMethod(methodInfo, instance, parameters, result); + QmlNet::invokeNetMethod(methodInfo, instance, parameters, result); if(result != nullptr) { NetMetaValuePack(returnType->getPrefVariantType(), result, a[0]); @@ -199,7 +199,7 @@ int NetValueMetaObject::metaCall(QMetaObject::Call c, int idx, void **a) } } - raiseNetSignals(instance, signalInfo->getName(), parameters); + QmlNet::raiseNetSignals(instance, signalInfo->getName(), parameters); } } break; diff --git a/src/native/QmlNet/QmlNet/qml/NetValueType.h b/src/native/QmlNet/QmlNet/qml/NetValueType.h index 25746aff..d78502e7 100644 --- a/src/native/QmlNet/QmlNet/qml/NetValueType.h +++ b/src/native/QmlNet/QmlNet/qml/NetValueType.h @@ -12,7 +12,7 @@ class NetValueType : public NetValue public: NetValueType() - : NetValue(instantiateType(typeInfo), nullptr) {} + : NetValue(QmlNet::instantiateType(typeInfo), nullptr) {} static void init(QSharedPointer info) { diff --git a/src/native/QmlNet/QmlNet/qml/NetVariant.cpp b/src/native/QmlNet/QmlNet/qml/NetVariant.cpp index d9fa1750..8f601b24 100644 --- a/src/native/QmlNet/QmlNet/qml/NetVariant.cpp +++ b/src/native/QmlNet/QmlNet/qml/NetVariant.cpp @@ -69,7 +69,10 @@ void NetVariant::setNetReference(QSharedPointer netReference) QSharedPointer NetVariant::getNetReference() { - return variant.value().netReference; + if(getVariantType() == NetVariantTypeEnum_Object) { + return variant.value().netReference; + } + return nullptr; } void NetVariant::setBool(bool value) @@ -194,7 +197,10 @@ void NetVariant::setJsValue(QSharedPointer jsValue) QSharedPointer NetVariant::getJsValue() { - return variant.value().jsValue; + if(getVariantType() == NetVariantTypeEnum_JSValue) { + return variant.value().jsValue; + } + return nullptr; } void NetVariant::clear() diff --git a/src/native/QmlNet/QmlNet/qml/QQmlApplicationEngine.cpp b/src/native/QmlNet/QmlNet/qml/QQmlApplicationEngine.cpp index e065c06d..f449ce9a 100644 --- a/src/native/QmlNet/QmlNet/qml/QQmlApplicationEngine.cpp +++ b/src/native/QmlNet/QmlNet/qml/QQmlApplicationEngine.cpp @@ -24,14 +24,13 @@ Q_DECL_EXPORT QQmlApplicationEngineContainer* qqmlapplicationengine_create(QQmlA ownsEngine = true; } - QV4::ExecutionEngine* v4Engine = QQmlEnginePrivate::getV4Engine(engine); + JsNetObject* netObject = new JsNetObject(); - QV4::Scope scope(v4Engine); - QV4::ScopedObject net(scope, v4Engine->memoryManager->allocObject()); - v4Engine->globalObject->defineDefaultProperty("Net", net);; + engine->rootContext()->setContextProperty("Net", netObject); return new QQmlApplicationEngineContainer{ engine, + netObject, ownsEngine }; } @@ -40,6 +39,7 @@ Q_DECL_EXPORT void qqmlapplicationengine_destroy(QQmlApplicationEngineContainer* if(container->ownsEngine) { delete container->qmlEngine; } + delete container->netObject; delete container; } diff --git a/src/native/QmlNet/QmlNet/qml/QQmlApplicationEngine.h b/src/native/QmlNet/QmlNet/qml/QQmlApplicationEngine.h index 88a10671..4ae3e63e 100644 --- a/src/native/QmlNet/QmlNet/qml/QQmlApplicationEngine.h +++ b/src/native/QmlNet/QmlNet/qml/QQmlApplicationEngine.h @@ -3,9 +3,11 @@ #include #include +#include struct QQmlApplicationEngineContainer { QQmlApplicationEngine* qmlEngine; + JsNetObject* netObject; bool ownsEngine; }; diff --git a/src/native/QmlNet/QmlNet/qml/qml.pri b/src/native/QmlNet/QmlNet/qml/qml.pri index f6da808a..a0097949 100644 --- a/src/native/QmlNet/QmlNet/qml/qml.pri +++ b/src/native/QmlNet/QmlNet/qml/qml.pri @@ -13,7 +13,6 @@ HEADERS += \ $$PWD/QQuickStyle.h \ $$PWD/NetValueMetaObjectPacker.h \ $$PWD/QCommon.h \ - $$PWD/JsNetArray.h \ $$PWD/NetListModel.h \ $$PWD/QtWebEngine.h @@ -34,11 +33,3 @@ SOURCES += \ $$PWD/QCommon.cpp \ $$PWD/NetListModel.cpp \ $$PWD/QtWebEngine.cpp - -versionAtLeast(QT_VERSION, 5.11.0) { - message('511') - SOURCES += $$PWD/JsNetArray511.cpp -} else { - message('510') - SOURCES += $$PWD/JsNetArray510.cpp -} diff --git a/src/native/QmlNet/QmlNet/types/Callbacks.cpp b/src/native/QmlNet/QmlNet/types/Callbacks.cpp index 3cfed3d2..105edfd5 100644 --- a/src/native/QmlNet/QmlNet/types/Callbacks.cpp +++ b/src/native/QmlNet/QmlNet/types/Callbacks.cpp @@ -1,5 +1,7 @@ #include +namespace QmlNet { + typedef bool (*isTypeValidCb)(LPWSTR typeName); typedef void (*createLazyTypeInfoCb)(NetTypeInfoContainer* typeInfo); typedef void (*loadTypeInfoCb)(NetTypeInfoContainer* typeInfo); @@ -152,29 +154,31 @@ bool serializeNetToString(QSharedPointer instance, QSharedPointer< new NetVariantContainer{result}); } +} + extern "C" { -Q_DECL_EXPORT void type_info_callbacks_registerCallbacks(NetTypeInfoManagerCallbacks* callbacks) { - sharedCallbacks = *callbacks; +Q_DECL_EXPORT void type_info_callbacks_registerCallbacks(QmlNet::NetTypeInfoManagerCallbacks* callbacks) { + QmlNet::sharedCallbacks = *callbacks; } Q_DECL_EXPORT bool type_info_callbacks_isTypeValid(LPWSTR typeName) { - return sharedCallbacks.isTypeValid(typeName); + return QmlNet::sharedCallbacks.isTypeValid(typeName); } Q_DECL_EXPORT void type_info_callbacks_releaseNetReferenceGCHandle(uint64_t objectId) { - sharedCallbacks.releaseNetReference(objectId); + QmlNet::sharedCallbacks.releaseNetReference(objectId); } Q_DECL_EXPORT void type_info_callbacks_releaseNetDelegateGCHandle(NetGCHandle* handle) { - sharedCallbacks.releaseNetDelegateGCHandle(handle); + QmlNet::sharedCallbacks.releaseNetDelegateGCHandle(handle); } Q_DECL_EXPORT NetReferenceContainer* type_info_callbacks_instantiateType(NetTypeInfoContainer* type) { // The parameters have to be copied to new containers, because the callback // will delete them. NetTypeInfoContainer* typeCopy = new NetTypeInfoContainer{type->netTypeInfo}; - return sharedCallbacks.instantiateType(typeCopy); + return QmlNet::sharedCallbacks.instantiateType(typeCopy); } Q_DECL_EXPORT void type_info_callbacks_invokeMethod(NetMethodInfoContainer* method, NetReferenceContainer* target, NetVariantListContainer* parameters, NetVariantContainer* result) { @@ -193,7 +197,7 @@ Q_DECL_EXPORT void type_info_callbacks_invokeMethod(NetMethodInfoContainer* meth resultCopy = new NetVariantContainer{result->variant}; } - sharedCallbacks.invokeMethod( + QmlNet::sharedCallbacks.invokeMethod( methodCopy, targetCopy, parametersCopy, diff --git a/src/native/QmlNet/QmlNet/types/Callbacks.h b/src/native/QmlNet/QmlNet/types/Callbacks.h index a1f11978..c5088a9f 100644 --- a/src/native/QmlNet/QmlNet/types/Callbacks.h +++ b/src/native/QmlNet/QmlNet/types/Callbacks.h @@ -11,6 +11,8 @@ #include #include +namespace QmlNet { + bool isTypeValid(QString type); void releaseNetReference(uint64_t objectId); @@ -37,4 +39,6 @@ void awaitTask(QSharedPointer target, QSharedPointer s bool serializeNetToString(QSharedPointer instance, QSharedPointer result); +} + #endif // NET_TYPE_INFO_MANAGER_H diff --git a/src/native/QmlNet/QmlNet/types/NetDelegate.cpp b/src/native/QmlNet/QmlNet/types/NetDelegate.cpp index 7e238a7b..115fb837 100644 --- a/src/native/QmlNet/QmlNet/types/NetDelegate.cpp +++ b/src/native/QmlNet/QmlNet/types/NetDelegate.cpp @@ -7,7 +7,7 @@ NetDelegate::NetDelegate(NetGCHandle* gcHandle) : } NetDelegate::~NetDelegate() { - releaseNetDelegateGCHandle(gcHandle); + QmlNet::releaseNetDelegateGCHandle(gcHandle); } NetGCHandle* NetDelegate::getGCHandle() { diff --git a/src/native/QmlNet/QmlNet/types/NetReference.cpp b/src/native/QmlNet/QmlNet/types/NetReference.cpp index bb6f6202..8aaa8ef8 100644 --- a/src/native/QmlNet/QmlNet/types/NetReference.cpp +++ b/src/native/QmlNet/QmlNet/types/NetReference.cpp @@ -11,7 +11,7 @@ NetReference::NetReference(uint64_t objectId, QSharedPointer typeIn NetReference::~NetReference() { - releaseNetReference(objectId); + QmlNet::releaseNetReference(objectId); } uint64_t NetReference::getObjectId() diff --git a/src/native/QmlNet/QmlNet/types/NetTypeArrayFacadeArray.cpp b/src/native/QmlNet/QmlNet/types/NetTypeArrayFacadeArray.cpp index 13636b61..577faa7f 100644 --- a/src/native/QmlNet/QmlNet/types/NetTypeArrayFacadeArray.cpp +++ b/src/native/QmlNet/QmlNet/types/NetTypeArrayFacadeArray.cpp @@ -47,7 +47,7 @@ bool NetTypeArrayFacade_Array::isFixed() uint NetTypeArrayFacade_Array::getLength(QSharedPointer reference) { QSharedPointer result = QSharedPointer(new NetVariant()); - readProperty(_lengthProperty, reference, nullptr, result); + QmlNet::readProperty(_lengthProperty, reference, nullptr, result); return static_cast(result->getInt()); } @@ -58,7 +58,7 @@ QSharedPointer NetTypeArrayFacade_Array::getIndexed(QSharedPointersetInt(static_cast(index)); parameters->add(parameter); QSharedPointer result = QSharedPointer(new NetVariant()); - invokeNetMethod(_getIndexed, reference, parameters, result); + QmlNet::invokeNetMethod(_getIndexed, reference, parameters, result); return result; } @@ -70,5 +70,5 @@ void NetTypeArrayFacade_Array::setIndexed(QSharedPointer reference parameters->add(parameter); parameters->add(value); QSharedPointer result = QSharedPointer(new NetVariant()); - invokeNetMethod(_setIndexed, reference, parameters, result); + QmlNet::invokeNetMethod(_setIndexed, reference, parameters, result); } diff --git a/src/native/QmlNet/QmlNet/types/NetTypeArrayFacadeList.cpp b/src/native/QmlNet/QmlNet/types/NetTypeArrayFacadeList.cpp index 953c459d..60b64717 100644 --- a/src/native/QmlNet/QmlNet/types/NetTypeArrayFacadeList.cpp +++ b/src/native/QmlNet/QmlNet/types/NetTypeArrayFacadeList.cpp @@ -49,7 +49,7 @@ bool NetTypeArrayFacade_List::isFixed() uint NetTypeArrayFacade_List::getLength(QSharedPointer reference) { QSharedPointer result = QSharedPointer(new NetVariant()); - readProperty(_lengthProperty, reference, nullptr, result); + QmlNet::readProperty(_lengthProperty, reference, nullptr, result); return static_cast(result->getInt()); } @@ -58,7 +58,7 @@ QSharedPointer NetTypeArrayFacade_List::getIndexed(QSharedPointer result = QSharedPointer(new NetVariant()); QSharedPointer indexParameter = QSharedPointer(new NetVariant()); indexParameter->setInt(index); - readProperty(_itemProperty, reference, indexParameter, result); + QmlNet::readProperty(_itemProperty, reference, indexParameter, result); return result; } @@ -66,14 +66,14 @@ void NetTypeArrayFacade_List::setIndexed(QSharedPointer reference, { QSharedPointer indexParameter = QSharedPointer(new NetVariant()); indexParameter->setInt(static_cast(index)); - writeProperty(_itemProperty, reference, indexParameter, value); + QmlNet::writeProperty(_itemProperty, reference, indexParameter, value); } void NetTypeArrayFacade_List::push(QSharedPointer reference, QSharedPointer value) { QSharedPointer parameters = QSharedPointer(new NetVariantList()); parameters->add(value); - invokeNetMethod(_addMethod, reference, parameters, nullptr); + QmlNet::invokeNetMethod(_addMethod, reference, parameters, nullptr); } QSharedPointer NetTypeArrayFacade_List::pop(QSharedPointer reference) @@ -90,5 +90,5 @@ void NetTypeArrayFacade_List::deleteAt(QSharedPointer reference, u QSharedPointer parameter = QSharedPointer(new NetVariant()); parameter->setInt(static_cast(index)); parameters->add(parameter); - invokeNetMethod(_removeAtMethod, reference, parameters, nullptr); + QmlNet::invokeNetMethod(_removeAtMethod, reference, parameters, nullptr); } diff --git a/src/native/QmlNet/QmlNet/types/NetTypeInfo.cpp b/src/native/QmlNet/QmlNet/types/NetTypeInfo.cpp index 53cc9e43..c25c3e1c 100644 --- a/src/native/QmlNet/QmlNet/types/NetTypeInfo.cpp +++ b/src/native/QmlNet/QmlNet/types/NetTypeInfo.cpp @@ -6,6 +6,8 @@ #include #include +using namespace QmlNet; + NetTypeInfo::NetTypeInfo(QString fullTypeName) : metaObject(nullptr), _fullTypeName(fullTypeName), diff --git a/src/native/QmlNet/QmlNet/types/NetTypeManager.cpp b/src/native/QmlNet/QmlNet/types/NetTypeManager.cpp index bd7e004d..431abe44 100644 --- a/src/native/QmlNet/QmlNet/types/NetTypeManager.cpp +++ b/src/native/QmlNet/QmlNet/types/NetTypeManager.cpp @@ -2,6 +2,8 @@ #include #include +using namespace QmlNet; + QMap> NetTypeManager::types; NetTypeManager::NetTypeManager() { diff --git a/src/net/Qml.Net.Tests/Qml/ArrayTests.cs b/src/net/Qml.Net.Tests/Qml/ArrayTests.cs deleted file mode 100644 index 136d276a..00000000 --- a/src/net/Qml.Net.Tests/Qml/ArrayTests.cs +++ /dev/null @@ -1,276 +0,0 @@ -using System; -using System.Collections.Generic; -using FluentAssertions; -using Moq; -using Xunit; - -namespace Qml.Net.Tests.Qml -{ - public class ArrayTests - { - public class ArrayType : BaseQmlTests - { - public class ArrayTestsQml - { - public virtual int[] GetArray() - { - return null; - } - - public virtual void Test(object value) - { - - } - } - - [Fact] - public void Can_get_length() - { - var array = new[] {3, 4, 6}; - Mock.Setup(x => x.GetArray()).Returns(array); - Mock.Setup(x => x.Test(3)); - - RunQmlTest( - "test", - @" - var array = Net.toJsArray(test.getArray()) - test.test(array.length) - "); - - Mock.Verify(x => x.GetArray(), Times.Once); - Mock.Verify(x => x.Test(3), Times.Once); - } - - [Fact] - public void Can_get_indexed() - { - var array = new[] {3, 4, 6}; - Mock.Setup(x => x.GetArray()).Returns(array); - Mock.Setup(x => x.Test(4)); - - RunQmlTest( - "test", - @" - var array = Net.toJsArray(test.getArray()) - test.test(array[1]) - "); - - Mock.Verify(x => x.GetArray(), Times.Once); - Mock.Verify(x => x.Test(4), Times.Once); - } - - [Fact] - public void Can_set_indexed() - { - var array = new[] {3, 4, 6}; - Mock.Setup(x => x.GetArray()).Returns(array); - - RunQmlTest( - "test", - @" - var array = Net.toJsArray(test.getArray()) - array[2] = 234 - "); - - Mock.Verify(x => x.GetArray(), Times.Once); - array[2].Should().Be(234); - } - - [Fact] - public void Can_forEach() - { - var array = new[] {3, 4, 7}; - Mock.Setup(x => x.GetArray()).Returns(array); - - RunQmlTest( - "test", - @" - var array = Net.toJsArray(test.getArray()) - array.forEach(function(value) { - test.test(value) - }) - "); - - Mock.Verify(x => x.GetArray(), Times.Once); - Mock.Verify(x => x.Test(3), Times.Once); - Mock.Verify(x => x.Test(4), Times.Once); - Mock.Verify(x => x.Test(7), Times.Once); - } - - [Fact] - public void Can_not_push_for_array() - { - var array = new[] {3, 4, 7}; - Mock.Setup(x => x.GetArray()).Returns(array); - Mock.Setup(x => x.Test(It.IsAny())); - - RunQmlTest( - "test", - @" - var array = Net.toJsArray(test.getArray()) - try { - array.push(23) - } catch(err) { - test.test(true) - } - "); - - Mock.Verify(x => x.GetArray(), Times.Once); - Mock.Verify(x => x.Test(true), Times.Once); - } - - [Fact] - public void Can_not_pop_for_array() - { - var array = new[] {3, 4, 7}; - Mock.Setup(x => x.GetArray()).Returns(array); - Mock.Setup(x => x.Test(It.IsAny())); - - RunQmlTest( - "test", - @" - var array = Net.toJsArray(test.getArray()) - try { - array.pop() - } catch(err) { - test.test(true) - } - "); - - Mock.Verify(x => x.GetArray(), Times.Once); - Mock.Verify(x => x.Test(true), Times.Once); - } - } - - public class ListType : BaseQmlTests - { - public class ListTestsQml - { - public virtual List GetList() - { - return null; - } - - public virtual void Test(object value) - { - - } - } - - [Fact] - public void Can_get_length() - { - var list = new List {4, 6, 8}; - Mock.Setup(x => x.GetList()).Returns(list); - Mock.Setup(x => x.Test(3)); - - RunQmlTest( - "test", - @" - var array = Net.toJsArray(test.getList()) - test.test(array.length) - "); - - Mock.Verify(x => x.GetList(), Times.Once); - Mock.Verify(x => x.Test(3), Times.Once); - } - - [Fact] - public void Can_get_indexed() - { - var list = new List{3, 4, 6}; - Mock.Setup(x => x.GetList()).Returns(list); - Mock.Setup(x => x.Test(4)); - - RunQmlTest( - "test", - @" - var array = Net.toJsArray(test.getList()) - test.test(array[1]) - "); - - Mock.Verify(x => x.GetList(), Times.Once); - Mock.Verify(x => x.Test(4), Times.Once); - } - - [Fact] - public void Can_set_indexed() - { - var list = new List{3, 4, 6}; - Mock.Setup(x => x.GetList()).Returns(list); - - RunQmlTest( - "test", - @" - var array = Net.toJsArray(test.getList()) - array[2] = 234 - "); - - Mock.Verify(x => x.GetList(), Times.Once); - list[2].Should().Be(234); - } - - [Fact] - public void Can_pop() - { - var list = new List{3, 4, 6}; - Mock.Setup(x => x.GetList()).Returns(list); - - RunQmlTest( - "test", - @" - var array = Net.toJsArray(test.getList()) - test.test(array.pop()) - test.test(array.pop()) - test.test(array.pop()) - var t = array.pop() - test.test(typeof t) - "); - - Mock.Verify(x => x.GetList(), Times.Once); - Mock.Verify(x => x.Test(6), Times.Once); - Mock.Verify(x => x.Test(4), Times.Once); - Mock.Verify(x => x.Test(3), Times.Once); - Mock.Verify(x => x.Test("undefined"), Times.Once); - list.Count.Should().Be(0); - } - - [Fact] - public void Can_push() - { - var list = new List{3, 4, 6}; - Mock.Setup(x => x.GetList()).Returns(list); - - RunQmlTest( - "test", - @" - var array = Net.toJsArray(test.getList()) - test.test(array.push(34)) - "); - - Mock.Verify(x => x.GetList(), Times.Once); - Mock.Verify(x => x.Test(4), Times.Once); - list.Count.Should().Be(4); - list[3].Should().Be(34); - - Mock.Reset(); - - list = new List{3, 4, 6}; - Mock.Setup(x => x.GetList()).Returns(list); - - RunQmlTest( - "test", - @" - var array = Net.toJsArray(test.getList()) - test.test(array.push(34,45)) - "); - - Mock.Verify(x => x.GetList(), Times.Once); - Mock.Verify(x => x.Test(5), Times.Once); - list.Count.Should().Be(5); - list[3].Should().Be(34); - list[4].Should().Be(45); - } - } - } -} \ No newline at end of file