Don't use the array prototype.

This commit is contained in:
Paul Knopf 2018-08-18 02:21:23 -04:00
parent 9ee1fa2660
commit 290ef5e2f8
2 changed files with 32 additions and 1 deletions

View file

@ -20,6 +20,7 @@ void Heap::NetArray::init()
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);
o->defineDefaultProperty(QStringLiteral("forEach"), QV4::NetArray::method_forEach);
object = scope.engine->memoryManager->m_persistentValues->allocate();
}
@ -105,6 +106,35 @@ ReturnedValue NetArray::method_pop(const FunctionObject *b, const Value *thisObj
THROW_GENERIC_ERROR("TODO");
}
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<const FunctionObject *>(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<const NetArray*>(m);

View file

@ -21,13 +21,14 @@ struct NetArray : Object
V4_OBJECT2(NetArray, Object)
V4_NEEDS_DESTROY
Q_MANAGED_TYPE(ArrayObject)
V4_PROTOTYPE(arrayPrototype)
static ReturnedValue create(ExecutionEngine *engine, NetValue* netValue);
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);
static ReturnedValue getIndexed(const Managed *m, uint index, bool *hasProperty);
static bool putIndexed(Managed *m, uint index, const Value &value);
};