1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ecmascript/shared_objects/js_shared_array_iterator.h"
17
18 #include "ecmascript/base/typed_array_helper-inl.h"
19 #include "ecmascript/shared_objects/concurrent_api_scope.h"
20 #include "ecmascript/shared_objects/js_shared_array.h"
21
22 namespace panda::ecmascript {
23 using BuiltinsBase = base::BuiltinsBase;
24 // 22.1.5.2.1 %ArrayIteratorPrototype%.next ( )
Next(EcmaRuntimeCallInfo * argv)25 JSTaggedValue JSSharedArrayIterator::Next(EcmaRuntimeCallInfo *argv)
26 {
27 ASSERT(argv);
28 JSThread *thread = argv->GetThread();
29 [[maybe_unused]] EcmaHandleScope handleScope(thread);
30 // 1.Let O be the this value.
31 JSHandle<JSTaggedValue> thisObj(BuiltinsBase::GetThis(argv));
32
33 // 2.If Type(O) is not Object, throw a TypeError exception.
34 // 3.If O does not have all of the internal slots of an TaggedArray Iterator Instance (22.1.5.3), throw a TypeError
35 // exception.
36 if (!thisObj->IsJSSharedArrayIterator()) {
37 THROW_TYPE_ERROR_AND_RETURN(thread, "this value is not an array iterator", JSTaggedValue::Exception());
38 }
39
40 // 4.Let a be O.[[IteratedArrayLike]].
41 JSHandle<JSSharedArrayIterator> iter(thisObj);
42 JSHandle<JSTaggedValue> array(thread, iter->GetIteratedArray(thread));
43 if (array->IsJSSharedArray()) {
44 [[maybe_unused]] ConcurrentApiScope<JSSharedArray> scope(thread, array);
45 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::Exception());
46 return NextInternal(thread, iter, array);
47 } else if (array->IsSharedTypedArray()) {
48 [[maybe_unused]] ConcurrentApiScope<JSSharedTypedArray> scope(thread, array);
49 RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, JSTaggedValue::Exception());
50 return NextInternal(thread, iter, array);
51 }
52 // 5.If a is undefined, return CreateIterResultObject(undefined, true).
53 JSHandle<JSTaggedValue> undefinedHandle = thread->GlobalConstants()->GetHandledUndefined();
54 return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
55 }
56
NextInternal(JSThread * thread,JSHandle<JSSharedArrayIterator> & iter,JSHandle<JSTaggedValue> & array)57 JSTaggedValue JSSharedArrayIterator::NextInternal(JSThread *thread, JSHandle<JSSharedArrayIterator> &iter,
58 JSHandle<JSTaggedValue> &array)
59 {
60 // 6.Let index be O.[[ArrayLikeNextIndex]].
61 uint32_t index = iter->GetNextIndex();
62 // 7.Let itemKind be O.[[ArrayLikeIterationKind]].
63 IterationKind itemKind = iter->GetIterationKind();
64
65 uint32_t length;
66 // 8. If a has a [[TypedArrayName]] internal slot, then
67 // a. Let len be the value of O’s [[ArrayLength]] internal slot.
68 if (array->IsJSSharedArray()) {
69 length = JSHandle<JSSharedArray>(array)->GetArrayLength();
70 } else if (array->IsSharedTypedArray()) {
71 length = JSHandle<JSSharedTypedArray>::Cast(array)->GetArrayLength();
72 } else {
73 // 9.Else
74 JSHandle<JSTaggedValue> lengthKey = thread->GlobalConstants()->GetHandledLengthString();
75 auto lengthValue =
76 JSTaggedValue::ToLength(thread, JSTaggedValue::GetProperty(thread, array, lengthKey).GetValue());
77 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
78 length = lengthValue.ToUint32();
79 }
80
81 JSHandle<JSTaggedValue> undefinedHandle = thread->GlobalConstants()->GetHandledUndefined();
82 // 10.If index ≥ len, then
83 if (index >= length) {
84 // Set O.[[IteratedArrayLike]] to undefined.
85 // Return CreateIterResultObject(undefined, true).
86 iter->SetIteratedArray(thread, undefinedHandle);
87 return JSIterator::CreateIterResultObject(thread, undefinedHandle, true).GetTaggedValue();
88 }
89 // 11.Set O.[[ArrayLikeNextIndex]] to index + 1.
90 iter->SetNextIndex(index + 1);
91 // 12.If itemKind is key, return CreateIterResultObject(index, false).
92 JSHandle<JSTaggedValue> key(thread, JSTaggedValue(index));
93 if (itemKind == IterationKind::KEY) {
94 return JSIterator::CreateIterResultObject(thread, key, false).GetTaggedValue();
95 }
96 JSHandle<JSTaggedValue> value = JSSharedArray::FastGetPropertyByValue(thread, array, index);
97 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
98 // 15.If itemKind is value, let result be elementValue.
99 if (itemKind == IterationKind::VALUE) {
100 return JSIterator::CreateIterResultObject(thread, value, false).GetTaggedValue();
101 }
102 // 16. Else
103 ASSERT_PRINT(itemKind == IterationKind::KEY_AND_VALUE, "itemKind is invalid");
104 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
105 JSHandle<TaggedArray> resultArray(factory->NewTaggedArray(2)); // 2 means the length of array
106 resultArray->Set(thread, 0, key);
107 resultArray->Set(thread, 1, value);
108 // fixme(hzzhouzebin) No transition supposed to happen in sendable world, it needs to discuss
109 JSHandle<JSTaggedValue> keyAndValue(JSArray::CreateArrayFromList(thread, resultArray));
110 return JSIterator::CreateIterResultObject(thread, keyAndValue, false).GetTaggedValue();
111 }
112 } // namespace panda::ecmascript
113