diff options
author | Lars Knoll <lars.knoll@qt.io> | 2018-08-14 20:41:55 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2018-08-23 08:13:45 +0000 |
commit | 1a92454c0e6d4b0d77294c0e4112594d8f53379e (patch) | |
tree | f52ad30db7922bca4fd30b8fb2c72fe167ee6ade /src/qml/jsruntime/qv4typedarray.cpp | |
parent | c1ba78d79647b42e6846edd3de27ff767291d6ee (diff) |
Implement %TypedArray%.prototype.map
Change-Id: Ifd2e4585cd3b9fedf2a2a4573c4f3ea68e0257fb
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/qml/jsruntime/qv4typedarray.cpp')
-rw-r--r-- | src/qml/jsruntime/qv4typedarray.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/qml/jsruntime/qv4typedarray.cpp b/src/qml/jsruntime/qv4typedarray.cpp index 68ac5645ae..5037b8a061 100644 --- a/src/qml/jsruntime/qv4typedarray.cpp +++ b/src/qml/jsruntime/qv4typedarray.cpp @@ -972,6 +972,48 @@ ReturnedValue IntrinsicTypedArrayPrototype::method_lastIndexOf(const FunctionObj return Encode(-1); } +ReturnedValue IntrinsicTypedArrayPrototype::method_map(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) +{ + Scope scope(b); + Scoped<TypedArray> instance(scope, thisObject); + if (!instance || instance->d()->buffer->isDetachedBuffer()) + return scope.engine->throwTypeError(); + + uint len = instance->length(); + + if (!argc || !argv->isFunctionObject()) + THROW_TYPE_ERROR(); + const FunctionObject *callback = static_cast<const FunctionObject *>(argv); + + const FunctionObject *constructor = instance->speciesConstructor(scope, scope.engine->typedArrayCtors + instance->d()->arrayType); + if (!constructor) + return scope.engine->throwTypeError(); + + Value *arguments = scope.alloc(1); + arguments[0] = Encode(len); + Scoped<TypedArray> a(scope, constructor->callAsConstructor(arguments, 1)); + if (!a || a->d()->buffer->isDetachedBuffer()) + return scope.engine->throwTypeError(); + if (a->length() < len) + return scope.engine->throwTypeError(); + + ScopedValue v(scope); + ScopedValue mapped(scope); + ScopedValue that(scope, argc > 1 ? argv[1] : Primitive::undefinedValue()); + arguments = scope.alloc(3); + + for (uint k = 0; k < len; ++k) { + if (instance->d()->buffer->isDetachedBuffer()) + return scope.engine->throwTypeError(); + arguments[0] = instance->get(k); + + arguments[1] = Primitive::fromDouble(k); + arguments[2] = instance; + mapped = callback->call(that, arguments, 3); + a->put(k, mapped); + } + return a.asReturnedValue(); +} ReturnedValue IntrinsicTypedArrayPrototype::method_reduce(const FunctionObject *b, const Value *thisObject, const Value *argv, int argc) { @@ -1402,6 +1444,7 @@ void IntrinsicTypedArrayPrototype::init(ExecutionEngine *engine, IntrinsicTypedA defineDefaultProperty(QStringLiteral("join"), method_join, 1); defineDefaultProperty(QStringLiteral("keys"), method_keys, 0); defineDefaultProperty(QStringLiteral("lastIndexOf"), method_lastIndexOf, 1); + defineDefaultProperty(QStringLiteral("map"), method_map, 1); defineDefaultProperty(QStringLiteral("reduce"), method_reduce, 1); defineDefaultProperty(QStringLiteral("reduceRight"), method_reduceRight, 1); defineDefaultProperty(QStringLiteral("reverse"), method_reverse, 0); |