2021-09-04 08:06:49 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2022-07-23 10:33:33 +00:00
|
|
|
|
#include "ecmascript/js_iterator.h"
|
|
|
|
|
|
2022-04-06 11:22:08 +00:00
|
|
|
|
#include "ecmascript/interpreter/interpreter.h"
|
2023-01-12 10:49:18 +00:00
|
|
|
|
#include "ecmascript/js_async_from_sync_iterator.h"
|
2023-01-18 05:52:08 +00:00
|
|
|
|
#include "ecmascript/object_fast_operator-inl.h"
|
2021-09-04 08:06:49 +00:00
|
|
|
|
|
|
|
|
|
namespace panda::ecmascript {
|
2022-03-24 07:04:57 +00:00
|
|
|
|
JSTaggedValue JSIterator::IteratorCloseAndReturn(JSThread *thread, const JSHandle<JSTaggedValue> &iter)
|
2021-09-04 08:06:49 +00:00
|
|
|
|
{
|
|
|
|
|
ASSERT(thread->HasPendingException());
|
|
|
|
|
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
2021-09-26 13:23:54 +00:00
|
|
|
|
JSTaggedValue exception = thread->GetException();
|
2022-01-25 10:05:12 +00:00
|
|
|
|
JSHandle<JSTaggedValue> record = JSHandle<JSTaggedValue>(factory->NewCompletionRecord(CompletionRecordType::THROW,
|
2021-09-26 13:23:54 +00:00
|
|
|
|
JSHandle<JSTaggedValue>(thread, exception)));
|
2021-09-04 08:06:49 +00:00
|
|
|
|
JSHandle<JSTaggedValue> result = JSIterator::IteratorClose(thread, iter, record);
|
2023-07-13 08:19:17 +00:00
|
|
|
|
RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
if (result->IsCompletionRecord()) {
|
|
|
|
|
return CompletionRecord::Cast(result->GetTaggedObject())->GetValue();
|
|
|
|
|
}
|
|
|
|
|
return result.GetTaggedValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JSHandle<JSTaggedValue> JSIterator::GetIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj)
|
|
|
|
|
{
|
|
|
|
|
// 1.ReturnIfAbrupt(obj).
|
|
|
|
|
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, obj);
|
|
|
|
|
// 2.If method was not passed, then
|
|
|
|
|
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
|
|
|
|
|
JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
|
|
|
|
|
JSHandle<JSTaggedValue> func = JSObject::GetMethod(thread, obj, iteratorSymbol);
|
|
|
|
|
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, obj);
|
|
|
|
|
|
|
|
|
|
return GetIterator(thread, obj, func);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JSHandle<JSTaggedValue> JSIterator::GetIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj,
|
|
|
|
|
const JSHandle<JSTaggedValue> &method)
|
|
|
|
|
{
|
|
|
|
|
// 1.ReturnIfAbrupt(obj).
|
|
|
|
|
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, obj);
|
|
|
|
|
// 3.Let iterator be Call(method,obj).
|
2022-04-06 11:22:08 +00:00
|
|
|
|
JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
|
2022-07-04 02:08:05 +00:00
|
|
|
|
EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, method, obj, undefined, 0);
|
|
|
|
|
JSTaggedValue ret = JSFunction::Call(info);
|
2024-01-25 06:14:12 +00:00
|
|
|
|
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
JSHandle<JSTaggedValue> iter(thread, ret);
|
|
|
|
|
// 5.If Type(iterator) is not Object, throw a TypeError exception
|
|
|
|
|
if (!iter->IsECMAObject()) {
|
2023-11-13 02:00:20 +00:00
|
|
|
|
THROW_TYPE_ERROR_AND_RETURN(thread, "JSIterator::GetIterator: iter is not object", undefined);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
}
|
|
|
|
|
return iter;
|
2023-01-12 10:49:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JSHandle<JSTaggedValue> JSIterator::GetAsyncIterator(JSThread *thread, const JSHandle<JSTaggedValue> &obj)
|
|
|
|
|
{
|
|
|
|
|
// 3.If method is not present, then
|
|
|
|
|
JSHandle<GlobalEnv> env = thread->GetEcmaVM()->GetGlobalEnv();
|
|
|
|
|
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
|
|
|
|
JSHandle<JSTaggedValue> asynciteratorSymbol = env->GetAsyncIteratorSymbol();
|
|
|
|
|
// i. Set method to ? GetMethod(obj, @@asyncIterator).
|
|
|
|
|
// ii. If method is undefined, then
|
|
|
|
|
JSHandle<JSTaggedValue> method = JSObject::GetMethod(thread, obj, asynciteratorSymbol);
|
|
|
|
|
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, obj);
|
|
|
|
|
if (method->IsUndefined()) {
|
|
|
|
|
JSHandle<JSTaggedValue> iteratorSymbol = env->GetIteratorSymbol();
|
|
|
|
|
JSHandle<JSTaggedValue> func = JSObject::GetMethod(thread, obj, iteratorSymbol);
|
2024-01-25 06:14:12 +00:00
|
|
|
|
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
|
2023-01-12 10:49:18 +00:00
|
|
|
|
JSHandle<JSTaggedValue> syncIterator = GetIterator(thread, obj, func);
|
|
|
|
|
JSHandle<JSTaggedValue> nextStr = thread->GlobalConstants()->GetHandledNextString();
|
|
|
|
|
JSHandle<JSTaggedValue> nextMethod = JSTaggedValue::GetProperty(thread, syncIterator, nextStr).GetValue();
|
2023-07-10 02:16:29 +00:00
|
|
|
|
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
|
2023-01-12 10:49:18 +00:00
|
|
|
|
JSHandle<AsyncIteratorRecord> syncIteratorRecord =
|
|
|
|
|
factory->NewAsyncIteratorRecord(syncIterator, nextMethod, false);
|
|
|
|
|
JSHandle<JSTaggedValue> asyncIterator =
|
|
|
|
|
JSAsyncFromSyncIterator::CreateAsyncFromSyncIterator(thread, syncIteratorRecord);
|
2024-01-30 01:47:06 +00:00
|
|
|
|
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
|
2023-01-12 10:49:18 +00:00
|
|
|
|
return asyncIterator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4.Let iterator be Call(method,obj).
|
|
|
|
|
JSHandle<JSTaggedValue> undefined = thread->GlobalConstants()->GetHandledUndefined();
|
|
|
|
|
EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, method, obj, undefined, 0);
|
|
|
|
|
JSTaggedValue ret = JSFunction::Call(info);
|
2023-07-13 08:19:17 +00:00
|
|
|
|
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
|
2023-01-12 10:49:18 +00:00
|
|
|
|
JSHandle<JSTaggedValue> iterator(thread, ret);
|
|
|
|
|
// 5.If Type(iterator) is not Object, throw a TypeError exception
|
|
|
|
|
if (!iterator->IsECMAObject()) {
|
2024-01-03 12:39:12 +00:00
|
|
|
|
THROW_TYPE_ERROR_AND_RETURN(thread, "Is not object", undefined);
|
2023-01-12 10:49:18 +00:00
|
|
|
|
}
|
|
|
|
|
return iterator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
|
// 7.4.2
|
2022-09-15 01:17:49 +00:00
|
|
|
|
JSHandle<JSTaggedValue> JSIterator::IteratorNext(JSThread *thread, const JSHandle<JSTaggedValue> &iter)
|
2021-09-04 08:06:49 +00:00
|
|
|
|
{
|
2022-05-31 08:29:55 +00:00
|
|
|
|
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
|
|
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
|
// 1.If value was not passed, then Let result be Invoke(iterator, "next", « »).
|
2022-05-31 08:29:55 +00:00
|
|
|
|
JSHandle<JSTaggedValue> key(globalConst->GetHandledNextString());
|
2021-09-04 08:06:49 +00:00
|
|
|
|
JSHandle<JSTaggedValue> next(JSObject::GetMethod(thread, iter, key));
|
2023-07-13 08:19:17 +00:00
|
|
|
|
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
|
2022-05-31 08:29:55 +00:00
|
|
|
|
JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
|
2022-07-04 02:08:05 +00:00
|
|
|
|
EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, next, iter, undefined, 0);
|
|
|
|
|
JSTaggedValue ret = JSFunction::Call(info);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
// 3.ReturnIfAbrupt(result)
|
2022-09-15 01:17:49 +00:00
|
|
|
|
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
// 4.If Type(result) is not Object, throw a TypeError exception.
|
2022-09-15 01:17:49 +00:00
|
|
|
|
if (!ret.IsECMAObject()) {
|
|
|
|
|
THROW_TYPE_ERROR_AND_RETURN(thread, "Is not Object", undefined);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
}
|
2022-09-15 01:17:49 +00:00
|
|
|
|
JSHandle<JSTaggedValue> result(thread, ret);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-15 01:17:49 +00:00
|
|
|
|
JSHandle<JSTaggedValue> JSIterator::IteratorNext(JSThread *thread, const JSHandle<JSTaggedValue> &iter,
|
2022-09-23 03:17:33 +00:00
|
|
|
|
const JSHandle<JSTaggedValue> &value)
|
2021-09-04 08:06:49 +00:00
|
|
|
|
{
|
2022-05-31 08:29:55 +00:00
|
|
|
|
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
|
2021-09-04 08:06:49 +00:00
|
|
|
|
// 2.Let result be Invoke(iterator, "next", «value»).
|
2022-05-31 08:29:55 +00:00
|
|
|
|
JSHandle<JSTaggedValue> key(globalConst->GetHandledNextString());
|
2021-09-04 08:06:49 +00:00
|
|
|
|
JSHandle<JSTaggedValue> next(JSObject::GetMethod(thread, iter, key));
|
2023-07-13 08:19:17 +00:00
|
|
|
|
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
|
2022-05-31 08:29:55 +00:00
|
|
|
|
JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
|
2022-07-04 02:08:05 +00:00
|
|
|
|
EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, next, iter, undefined, 1);
|
2023-07-13 03:23:30 +00:00
|
|
|
|
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
|
2022-07-04 02:08:05 +00:00
|
|
|
|
info->SetCallArg(value.GetTaggedValue());
|
|
|
|
|
JSTaggedValue ret = JSFunction::Call(info);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
// 3.ReturnIfAbrupt(result)
|
2022-09-15 01:17:49 +00:00
|
|
|
|
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
// 4.If Type(result) is not Object, throw a TypeError exception.
|
2022-09-15 01:17:49 +00:00
|
|
|
|
if (!ret.IsECMAObject()) {
|
|
|
|
|
THROW_TYPE_ERROR_AND_RETURN(thread, "Is not Object", undefined);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
}
|
2022-09-15 01:17:49 +00:00
|
|
|
|
JSHandle<JSTaggedValue> result(thread, ret);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
2023-01-12 10:49:18 +00:00
|
|
|
|
|
|
|
|
|
JSHandle<JSTaggedValue> JSIterator::IteratorNext(JSThread *thread, const JSHandle<AsyncIteratorRecord> &iter,
|
|
|
|
|
const JSHandle<JSTaggedValue> &value)
|
|
|
|
|
{
|
|
|
|
|
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
|
|
|
|
|
// 2.Let result be Invoke(iterator, "next", «value»).
|
|
|
|
|
JSHandle<JSTaggedValue> iterator(thread, iter->GetIterator());
|
|
|
|
|
JSHandle<JSTaggedValue> next(thread, iter->GetNextMethod());
|
|
|
|
|
JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
|
|
|
|
|
EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, next, iterator, undefined, 1);
|
2023-07-13 03:23:30 +00:00
|
|
|
|
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
|
2023-01-12 10:49:18 +00:00
|
|
|
|
info->SetCallArg(value.GetTaggedValue());
|
|
|
|
|
JSTaggedValue ret = JSFunction::Call(info);
|
|
|
|
|
// 3.ReturnIfAbrupt(result)
|
|
|
|
|
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
|
|
|
|
|
// 4.If Type(result) is not Object, throw a TypeError exception.
|
|
|
|
|
if (!ret.IsECMAObject()) {
|
|
|
|
|
THROW_TYPE_ERROR_AND_RETURN(thread, "Is not Object", undefined);
|
|
|
|
|
}
|
|
|
|
|
JSHandle<JSTaggedValue> result(thread, ret);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JSHandle<JSTaggedValue> JSIterator::IteratorNext(JSThread *thread, const JSHandle<AsyncIteratorRecord> &iter)
|
|
|
|
|
{
|
|
|
|
|
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
|
|
|
|
|
// 2.Let result be Invoke(iterator, "next", «value»).
|
|
|
|
|
JSHandle<JSTaggedValue> iterator(thread, iter->GetIterator());
|
|
|
|
|
JSHandle<JSTaggedValue> next(thread, iter->GetNextMethod());
|
|
|
|
|
JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
|
|
|
|
|
EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, next, iterator, undefined, 0);
|
|
|
|
|
JSTaggedValue ret = JSFunction::Call(info);
|
|
|
|
|
// 3.ReturnIfAbrupt(result)
|
|
|
|
|
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, undefined);
|
|
|
|
|
// 4.If Type(result) is not Object, throw a TypeError exception.
|
|
|
|
|
if (!ret.IsECMAObject()) {
|
|
|
|
|
THROW_TYPE_ERROR_AND_RETURN(thread, "Is not Object", undefined);
|
|
|
|
|
}
|
|
|
|
|
JSHandle<JSTaggedValue> result(thread, ret);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
|
// 7.4.3
|
|
|
|
|
bool JSIterator::IteratorComplete(JSThread *thread, const JSHandle<JSTaggedValue> &iterResult)
|
|
|
|
|
{
|
|
|
|
|
ASSERT_PRINT(iterResult->IsECMAObject(), "iterResult must be JSObject");
|
|
|
|
|
// Return ToBoolean(Get(iterResult, "done")).
|
|
|
|
|
JSHandle<JSTaggedValue> doneStr = thread->GlobalConstants()->GetHandledDoneString();
|
|
|
|
|
JSHandle<JSTaggedValue> done = JSTaggedValue::GetProperty(thread, iterResult, doneStr).GetValue();
|
2022-04-29 03:12:16 +00:00
|
|
|
|
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, false);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
return done->ToBoolean();
|
|
|
|
|
}
|
|
|
|
|
// 7.4.4
|
|
|
|
|
JSHandle<JSTaggedValue> JSIterator::IteratorValue(JSThread *thread, const JSHandle<JSTaggedValue> &iterResult)
|
|
|
|
|
{
|
|
|
|
|
ASSERT_PRINT(iterResult->IsECMAObject(), "iterResult must be JSObject");
|
|
|
|
|
// Return Get(iterResult, "value").
|
|
|
|
|
JSHandle<JSTaggedValue> valueStr = thread->GlobalConstants()->GetHandledValueString();
|
|
|
|
|
JSHandle<JSTaggedValue> value = JSTaggedValue::GetProperty(thread, iterResult, valueStr).GetValue();
|
2023-07-10 02:16:29 +00:00
|
|
|
|
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
// 7.4.5
|
|
|
|
|
JSHandle<JSTaggedValue> JSIterator::IteratorStep(JSThread *thread, const JSHandle<JSTaggedValue> &iter)
|
|
|
|
|
{
|
|
|
|
|
// 1.Let result be IteratorNext(iterator).
|
2022-09-15 01:17:49 +00:00
|
|
|
|
JSHandle<JSTaggedValue> result = IteratorNext(thread, iter);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
// 2.ReturnIfAbrupt(result).
|
|
|
|
|
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, result);
|
|
|
|
|
// 3.Let done be IteratorComplete(result).
|
|
|
|
|
bool done = IteratorComplete(thread, result);
|
|
|
|
|
// 4.ReturnIfAbrupt(done).
|
|
|
|
|
JSHandle<JSTaggedValue> doneHandle(thread, JSTaggedValue(done));
|
|
|
|
|
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, doneHandle);
|
|
|
|
|
// 5.If done is true, return false.
|
|
|
|
|
if (done) {
|
|
|
|
|
JSHandle<JSTaggedValue> falseHandle(thread, JSTaggedValue::False());
|
|
|
|
|
return falseHandle;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
// 7.4.6
|
|
|
|
|
JSHandle<JSTaggedValue> JSIterator::IteratorClose(JSThread *thread, const JSHandle<JSTaggedValue> &iter,
|
|
|
|
|
const JSHandle<JSTaggedValue> &completion)
|
|
|
|
|
{
|
|
|
|
|
// 1.Assert: Type(iterator) is Object.
|
|
|
|
|
ASSERT_PRINT(iter->IsECMAObject(), "iter must be JSObject");
|
|
|
|
|
const GlobalEnvConstants *globalConst = thread->GlobalConstants();
|
|
|
|
|
JSHandle<JSTaggedValue> exceptionOnThread;
|
|
|
|
|
if (thread->HasPendingException()) {
|
|
|
|
|
exceptionOnThread = JSHandle<JSTaggedValue>(thread, thread->GetException());
|
|
|
|
|
thread->ClearException();
|
|
|
|
|
}
|
2022-07-05 07:35:45 +00:00
|
|
|
|
JSTaggedValue returnStr = globalConst->GetReturnString();
|
2021-09-04 08:06:49 +00:00
|
|
|
|
// 3.Let return be GetMethod(iterator, "return").
|
2023-01-18 05:52:08 +00:00
|
|
|
|
JSTaggedValue func = ObjectFastOperator::FastGetPropertyByName(thread, iter.GetTaggedValue(), returnStr);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
// 4.ReturnIfAbrupt(return).
|
2022-07-05 07:35:45 +00:00
|
|
|
|
JSHandle<JSTaggedValue> returnFunc(thread, func);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
RETURN_VALUE_IF_ABRUPT_COMPLETION(thread, returnFunc);
|
2022-07-05 07:35:45 +00:00
|
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
|
// 5.If return is undefined, return Completion(completion).
|
2022-07-05 07:35:45 +00:00
|
|
|
|
if (returnFunc->IsUndefined() || returnFunc->IsNull()) {
|
2021-09-04 08:06:49 +00:00
|
|
|
|
if (!exceptionOnThread.IsEmpty()) {
|
|
|
|
|
thread->SetException(exceptionOnThread.GetTaggedValue());
|
|
|
|
|
}
|
|
|
|
|
return completion;
|
|
|
|
|
}
|
2022-07-05 07:35:45 +00:00
|
|
|
|
|
|
|
|
|
if (!returnFunc->IsCallable()) {
|
|
|
|
|
THROW_TYPE_ERROR_AND_RETURN(thread, "return function is not Callable", returnFunc);
|
|
|
|
|
}
|
2021-09-04 08:06:49 +00:00
|
|
|
|
// 6.Let innerResult be Call(return, iterator, « »).
|
2022-04-06 11:22:08 +00:00
|
|
|
|
JSHandle<JSTaggedValue> undefined = globalConst->GetHandledUndefined();
|
2022-07-04 02:08:05 +00:00
|
|
|
|
EcmaRuntimeCallInfo *info = EcmaInterpreter::NewRuntimeCallInfo(thread, returnFunc, iter, undefined, 0);
|
|
|
|
|
JSTaggedValue ret = JSFunction::Call(info);
|
2023-07-13 08:19:17 +00:00
|
|
|
|
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSTaggedValue, thread);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
if (!exceptionOnThread.IsEmpty()) {
|
|
|
|
|
thread->SetException(exceptionOnThread.GetTaggedValue());
|
|
|
|
|
}
|
|
|
|
|
JSHandle<JSTaggedValue> innerResult(thread, ret);
|
|
|
|
|
JSHandle<CompletionRecord> completionRecord(thread, globalConst->GetUndefined());
|
|
|
|
|
if (completion->IsCompletionRecord()) {
|
|
|
|
|
completionRecord = JSHandle<CompletionRecord>::Cast(completion);
|
|
|
|
|
}
|
|
|
|
|
// 7.If completion.[[type]] is throw, return Completion(completion).
|
|
|
|
|
if (!completionRecord.GetTaggedValue().IsUndefined() && completionRecord->IsThrow()) {
|
|
|
|
|
if (!exceptionOnThread.IsEmpty()) {
|
|
|
|
|
thread->SetException(exceptionOnThread.GetTaggedValue());
|
|
|
|
|
}
|
|
|
|
|
return completion;
|
|
|
|
|
}
|
|
|
|
|
// 8.If innerResult.[[type]] is throw, return Completion(innerResult).
|
|
|
|
|
if (thread->HasPendingException()) {
|
|
|
|
|
return innerResult;
|
|
|
|
|
}
|
|
|
|
|
// 9.If Type(innerResult.[[value]]) is not Object, throw a TypeError exception.
|
|
|
|
|
if (!innerResult->IsECMAObject()) {
|
2024-01-03 12:39:12 +00:00
|
|
|
|
THROW_TYPE_ERROR_AND_RETURN(thread, "Is not object", undefined);
|
2021-09-04 08:06:49 +00:00
|
|
|
|
}
|
|
|
|
|
if (!exceptionOnThread.IsEmpty()) {
|
|
|
|
|
thread->SetException(exceptionOnThread.GetTaggedValue());
|
|
|
|
|
}
|
|
|
|
|
return completion;
|
|
|
|
|
}
|
|
|
|
|
// 7.4.7
|
|
|
|
|
JSHandle<JSObject> JSIterator::CreateIterResultObject(JSThread *thread, const JSHandle<JSTaggedValue> &value, bool done)
|
|
|
|
|
{
|
|
|
|
|
ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
|
|
|
|
|
auto globalConst = thread->GlobalConstants();
|
|
|
|
|
// 2. Let obj be OrdinaryObjectCreate(%Object.prototype%).
|
2022-07-05 07:35:45 +00:00
|
|
|
|
JSHandle<JSHClass> klass = JSHandle<JSHClass>::Cast(globalConst->GetHandledIteratorResultClass());
|
|
|
|
|
JSHandle<JSObject> obj = factory->NewJSObject(klass);
|
|
|
|
|
|
2021-09-04 08:06:49 +00:00
|
|
|
|
// 3. Perform ! CreateDataPropertyOrThrow(obj, "value", value).
|
|
|
|
|
// 4. Perform ! CreateDataPropertyOrThrow(obj, "done", done).
|
2022-07-05 07:35:45 +00:00
|
|
|
|
obj->SetPropertyInlinedProps(thread, VALUE_INLINE_PROPERTY_INDEX, value.GetTaggedValue());
|
|
|
|
|
obj->SetPropertyInlinedProps(thread, DONE_INLINE_PROPERTY_INDEX, JSTaggedValue(done));
|
2021-09-04 08:06:49 +00:00
|
|
|
|
// 5. Return obj.
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
} // namespace panda::ecmascript
|