2021-09-04 16:06:49 +08: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ecmascript/generator_helper.h"
|
|
|
|
|
|
|
|
#include "ecmascript/interpreter/interpreter-inl.h"
|
|
|
|
#include "ecmascript/js_iterator.h"
|
2022-07-23 18:54:34 +08:00
|
|
|
|
2021-09-04 16:06:49 +08:00
|
|
|
namespace panda::ecmascript {
|
|
|
|
JSHandle<JSObject> GeneratorHelper::Next(JSThread *thread, const JSHandle<GeneratorContext> &genContext,
|
|
|
|
JSTaggedValue value)
|
|
|
|
{
|
|
|
|
JSHandle<JSGeneratorObject> genObject(thread, genContext->GetGeneratorObject());
|
|
|
|
genObject->SetResumeResult(thread, value);
|
2022-01-25 18:05:12 +08:00
|
|
|
genObject->SetGeneratorState(JSGeneratorState::EXECUTING);
|
|
|
|
genObject->SetResumeMode(GeneratorResumeMode::NEXT);
|
2021-09-04 16:06:49 +08:00
|
|
|
|
|
|
|
JSTaggedValue next = EcmaInterpreter::GeneratorReEnterInterpreter(thread, genContext);
|
|
|
|
JSHandle<JSTaggedValue> nextValue(thread, next);
|
|
|
|
|
|
|
|
if (genObject->IsSuspendYield()) {
|
|
|
|
return JSHandle<JSObject>::Cast(nextValue);
|
|
|
|
}
|
2022-01-25 18:05:12 +08:00
|
|
|
genObject->SetGeneratorState(JSGeneratorState::COMPLETED);
|
2021-09-04 16:06:49 +08:00
|
|
|
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSObject, thread);
|
|
|
|
return JSIterator::CreateIterResultObject(thread, nextValue, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSHandle<JSObject> GeneratorHelper::Return(JSThread *thread, const JSHandle<GeneratorContext> &genContext,
|
|
|
|
JSTaggedValue value)
|
|
|
|
{
|
|
|
|
JSHandle<JSGeneratorObject> genObject(thread, genContext->GetGeneratorObject());
|
2022-01-25 18:05:12 +08:00
|
|
|
genObject->SetResumeMode(GeneratorResumeMode::RETURN);
|
2021-09-04 16:06:49 +08:00
|
|
|
genObject->SetResumeResult(thread, value);
|
|
|
|
|
|
|
|
JSTaggedValue res = EcmaInterpreter::GeneratorReEnterInterpreter(thread, genContext);
|
|
|
|
JSHandle<JSTaggedValue> returnValue(thread, res);
|
|
|
|
// change state to completed
|
|
|
|
if (genObject->IsExecuting()) {
|
2022-01-25 18:05:12 +08:00
|
|
|
genObject->SetGeneratorState(JSGeneratorState::COMPLETED);
|
2021-09-04 16:06:49 +08:00
|
|
|
}
|
|
|
|
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSObject, thread);
|
|
|
|
return JSIterator::CreateIterResultObject(thread, returnValue, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSHandle<JSObject> GeneratorHelper::Throw(JSThread *thread, const JSHandle<GeneratorContext> &genContext,
|
|
|
|
JSTaggedValue value)
|
|
|
|
{
|
|
|
|
JSHandle<JSGeneratorObject> genObject(thread, genContext->GetGeneratorObject());
|
2022-01-25 18:05:12 +08:00
|
|
|
genObject->SetResumeMode(GeneratorResumeMode::THROW);
|
2021-09-04 16:06:49 +08:00
|
|
|
genObject->SetResumeResult(thread, value);
|
|
|
|
|
|
|
|
JSTaggedValue res = EcmaInterpreter::GeneratorReEnterInterpreter(thread, genContext);
|
|
|
|
JSHandle<JSTaggedValue> throwValue(thread, res);
|
|
|
|
|
|
|
|
if (genObject->IsSuspendYield()) {
|
|
|
|
return JSHandle<JSObject>::Cast(throwValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
// change state to completed
|
2022-01-25 18:05:12 +08:00
|
|
|
genObject->SetGeneratorState(JSGeneratorState::COMPLETED);
|
2021-09-04 16:06:49 +08:00
|
|
|
RETURN_HANDLE_IF_ABRUPT_COMPLETION(JSObject, thread);
|
|
|
|
return JSIterator::CreateIterResultObject(thread, throwValue, true);
|
|
|
|
}
|
|
|
|
} // namespace panda::ecmascript
|