Made sure you can't pop a .NET array.

This commit is contained in:
Paul Knopf 2018-08-17 23:21:30 -04:00
parent bb656e0780
commit 9ee1fa2660
3 changed files with 49 additions and 1 deletions

View file

@ -19,6 +19,7 @@ void Heap::NetArray::init()
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_push);
object = scope.engine->memoryManager->m_persistentValues->allocate();
}
@ -77,7 +78,31 @@ ReturnedValue NetArray::method_push(const FunctionObject *b, const Value *thisOb
THROW_GENERIC_ERROR("Can't modify a fixed .NET list type.");
}
return Encode(arrayFacade->getLength(netValue->getNetReference()));
THROW_GENERIC_ERROR("TODO");
}
ReturnedValue NetArray::method_pop(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc)
{
Scope scope(b);
Scoped<QV4::NetArray> netArray(scope, thisObject->as<QV4::NetArray>());
Scoped<QV4::QObjectWrapper> wrapper(scope, netArray->d()->object);
if (!wrapper) {
THROW_GENERIC_ERROR("No reference to the wrapped QObject exists.");
}
NetValue* netValue = reinterpret_cast<NetValue*>(wrapper->d()->object());
QSharedPointer<NetTypeArrayFacade> 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.");
}
THROW_GENERIC_ERROR("TODO");
}
ReturnedValue NetArray::getIndexed(const Managed *m, uint index, bool *hasProperty)

View file

@ -27,6 +27,7 @@ struct NetArray : Object
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 getIndexed(const Managed *m, uint index, bool *hasProperty);
static bool putIndexed(Managed *m, uint index, const Value &value);
};

View file

@ -115,5 +115,27 @@ namespace Qml.Net.Tests.Qml
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<object>()));
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);
}
}
}