Commit 7cceb727 authored by Igor Sheludko's avatar Igor Sheludko Committed by Commit Bot

[builtins] Port %TypedArray%.of to Torque

Bug: v8:8996
Change-Id: I822c945c56738a1bb0561c208e321d70fd96f863
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1895568Reviewed-by: 's avatarToon Verwaest <verwaest@chromium.org>
Commit-Queue: Igor Sheludko <ishell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#64768}
parent 50c40a28
......@@ -997,6 +997,7 @@ torque_files = [
"src/builtins/typed-array-findindex.tq",
"src/builtins/typed-array-foreach.tq",
"src/builtins/typed-array-from.tq",
"src/builtins/typed-array-of.tq",
"src/builtins/typed-array-reduce.tq",
"src/builtins/typed-array-reduceright.tq",
"src/builtins/typed-array-slice.tq",
......
......@@ -938,8 +938,6 @@ namespace internal {
TFJ(TypedArrayPrototypeToStringTag, 0, kReceiver) \
/* ES6 %TypedArray%.prototype.map */ \
TFJ(TypedArrayPrototypeMap, SharedFunctionInfo::kDontAdaptArgumentsSentinel) \
/* ES6 %TypedArray%.of */ \
TFJ(TypedArrayOf, SharedFunctionInfo::kDontAdaptArgumentsSentinel) \
\
/* Wasm */ \
ASM(WasmCompileLazy, Dummy) \
......
......@@ -756,72 +756,5 @@ TF_BUILTIN(TypedArrayPrototypeKeys, TypedArrayBuiltinsAssembler) {
context, receiver, "%TypedArray%.prototype.keys()", IterationKind::kKeys);
}
// ES6 #sec-%typedarray%.of
TF_BUILTIN(TypedArrayOf, TypedArrayBuiltinsAssembler) {
TNode<Context> context = CAST(Parameter(Descriptor::kContext));
// 1. Let len be the actual number of arguments passed to this function.
TNode<IntPtrT> length = ChangeInt32ToIntPtr(
UncheckedCast<Int32T>(Parameter(Descriptor::kJSActualArgumentsCount)));
// 2. Let items be the List of arguments passed to this function.
CodeStubArguments args(this, length);
Label if_not_constructor(this, Label::kDeferred),
if_detached(this, Label::kDeferred);
// 3. Let C be the this value.
// 4. If IsConstructor(C) is false, throw a TypeError exception.
TNode<Object> receiver = args.GetReceiver();
GotoIf(TaggedIsSmi(receiver), &if_not_constructor);
GotoIfNot(IsConstructor(CAST(receiver)), &if_not_constructor);
// 5. Let newObj be ? TypedArrayCreate(C, len).
TNode<JSTypedArray> new_typed_array = TypedArrayCreateByLength(
context, CAST(receiver), SmiTag(length), "%TypedArray%.of");
TNode<Int32T> elements_kind = LoadElementsKind(new_typed_array);
// 6. Let k be 0.
// 7. Repeat, while k < len
// a. Let kValue be items[k].
// b. Let Pk be ! ToString(k).
// c. Perform ? Set(newObj, Pk, kValue, true).
// d. Increase k by 1.
DispatchTypedArrayByElementsKind(
elements_kind,
[&](ElementsKind kind, int size, int typed_array_fun_index) {
BuildFastLoop<IntPtrT>(
IntPtrConstant(0), length,
[&](TNode<IntPtrT> index) {
TNode<Object> item = args.AtIndex(index);
Node* value =
PrepareValueForWriteToTypedArray(item, kind, context);
// ToNumber/ToBigInt may execute JavaScript code, which could
// detach the array's buffer.
TNode<JSArrayBuffer> buffer =
LoadJSArrayBufferViewBuffer(new_typed_array);
GotoIf(IsDetachedBuffer(buffer), &if_detached);
// GC may move backing store in ToNumber, thus load backing
// store everytime in this loop.
TNode<RawPtrT> data_ptr =
LoadJSTypedArrayDataPtr(new_typed_array);
StoreElement(data_ptr, kind, index, value, INTPTR_PARAMETERS);
},
1, IndexAdvanceMode::kPost);
});
// 8. Return newObj.
args.PopAndReturn(new_typed_array);
BIND(&if_not_constructor);
ThrowTypeError(context, MessageTemplate::kNotConstructor, receiver);
BIND(&if_detached);
ThrowTypeError(context, MessageTemplate::kDetachedOperation,
"%TypedArray%.of");
}
} // namespace internal
} // namespace v8
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include 'src/builtins/builtins-typed-array-gen.h'
namespace typed_array {
const kBuiltinNameOf: constexpr string = '%TypedArray%.of';
// %TypedArray%.of ( ...items )
// https://tc39.github.io/ecma262/#sec-%typedarray%.of
transitioning javascript builtin
TypedArrayOf(js-implicit context: Context, receiver: JSAny)(...arguments):
JSTypedArray {
try {
// 1. Let len be the actual number of arguments passed to this function.
const len: uintptr = Unsigned(arguments.length);
// 2. Let items be the List of arguments passed to this function.
// 3. Let C be the this value.
// 4. If IsConstructor(C) is false, throw a TypeError exception.
const constructor = Cast<Constructor>(receiver) otherwise NotConstructor;
// 5. Let newObj be ? TypedArrayCreate(C, len).
const newObj = TypedArrayCreateByLength(
constructor, Convert<Number>(len), kBuiltinNameOf);
const accessor: TypedArrayAccessor =
GetTypedArrayAccessor(newObj.elements_kind);
// 6. Let k be 0.
// 7. Repeat, while k < len
for (let k: uintptr = 0; k < len; k++) {
// 7a. Let kValue be items[k].
const kValue: JSAny = arguments[Signed(k)];
// 7b. Let Pk be ! ToString(k).
// 7c. Perform ? Set(newObj, Pk, kValue, true).
// Buffer may be detached during executing ToNumber/ToBigInt.
accessor.StoreJSAny(context, newObj, k, kValue) otherwise IfDetached;
// 7d. Increase k by 1. (done by the loop).
}
// 8. Return newObj.
return newObj;
}
label NotConstructor deferred {
ThrowTypeError(kNotConstructor, receiver);
}
label IfDetached deferred {
ThrowTypeError(kDetachedOperation, kBuiltinNameOf);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment