14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#include "ecmascript/builtins/builtins_async_generator.h"
174514f5e3Sopenharmony_ci#include "ecmascript/js_async_generator_object.h"
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins {
204514f5e3Sopenharmony_ci// ecma 27.6.1.1
214514f5e3Sopenharmony_ciJSTaggedValue BuiltinsAsyncGenerator::AsyncGeneratorFunctionConstructor(EcmaRuntimeCallInfo *argv)
224514f5e3Sopenharmony_ci{
234514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), AsyncGenerator, Constructor);
244514f5e3Sopenharmony_ci    // not support
254514f5e3Sopenharmony_ci    THROW_TYPE_ERROR_AND_RETURN(argv->GetThread(), "Not support eval. Forbidden using new AsyncGeneratorFunction().",
264514f5e3Sopenharmony_ci                                JSTaggedValue::Exception());
274514f5e3Sopenharmony_ci}
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_ci// ecma 27.6.1.2 AsyncGenerator.prototype.next
304514f5e3Sopenharmony_ciJSTaggedValue BuiltinsAsyncGenerator::AsyncGeneratorPrototypeNext(EcmaRuntimeCallInfo *argv)
314514f5e3Sopenharmony_ci{
324514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
334514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(thread, AsyncGenerator, PrototypeNext);
344514f5e3Sopenharmony_ci    // 1.Let g be the this value.
354514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
364514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> msg = GetThis(argv);
374514f5e3Sopenharmony_ci
384514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
394514f5e3Sopenharmony_ci    // 2. Let completion be NormalCompletion(value).
404514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
414514f5e3Sopenharmony_ci    JSHandle<CompletionRecord> completionRecord =
424514f5e3Sopenharmony_ci        factory->NewCompletionRecord(CompletionRecordType::NORMAL, value);
434514f5e3Sopenharmony_ci    // 3. Return ! AsyncGeneratorEnqueue(generator, completion, empty).
444514f5e3Sopenharmony_ci    return JSAsyncGeneratorObject::AsyncGeneratorEnqueue(thread, msg, completionRecord);
454514f5e3Sopenharmony_ci}
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_ci// ecma 27.6.1.3 AsyncGenerator.prototype.return
484514f5e3Sopenharmony_ciJSTaggedValue BuiltinsAsyncGenerator::AsyncGeneratorPrototypeReturn(EcmaRuntimeCallInfo *argv)
494514f5e3Sopenharmony_ci{
504514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
514514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(thread, AsyncGenerator, PrototypeReturn);
524514f5e3Sopenharmony_ci    // 1.Let generator be the this value.
534514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
544514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> msg = GetThis(argv);
554514f5e3Sopenharmony_ci    // 2.Let completion be Completion { [[Type]]: return, [[Value]]: value, [[Target]]: empty }.
564514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
574514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
584514f5e3Sopenharmony_ci    JSHandle<CompletionRecord> completionRecord =
594514f5e3Sopenharmony_ci        factory->NewCompletionRecord(CompletionRecordType::RETURN, value);
604514f5e3Sopenharmony_ci    // 3.Return ! AsyncGeneratorEnqueue(generator, completion, empty).
614514f5e3Sopenharmony_ci    return JSAsyncGeneratorObject::AsyncGeneratorEnqueue(thread, msg, completionRecord);
624514f5e3Sopenharmony_ci}
634514f5e3Sopenharmony_ci
644514f5e3Sopenharmony_ci// ecma 27.6.1.4 AsyncGenerator.prototype.throw
654514f5e3Sopenharmony_ciJSTaggedValue BuiltinsAsyncGenerator::AsyncGeneratorPrototypeThrow(EcmaRuntimeCallInfo *argv)
664514f5e3Sopenharmony_ci{
674514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
684514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(thread, AsyncGenerator, PrototypeThrow);
694514f5e3Sopenharmony_ci    // 1.Let generator be the this value.
704514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
714514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> msg = GetThis(argv);
724514f5e3Sopenharmony_ci    // 2.Let completion be ThrowCompletion(exception).
734514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> exception = GetCallArg(argv, 0);
744514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
754514f5e3Sopenharmony_ci    JSHandle<CompletionRecord> completionRecord =
764514f5e3Sopenharmony_ci        factory->NewCompletionRecord(CompletionRecordType::THROW, exception);
774514f5e3Sopenharmony_ci    // 3.Return ! AsyncGeneratorEnqueue(generator, completion, empty).
784514f5e3Sopenharmony_ci    return JSAsyncGeneratorObject::AsyncGeneratorEnqueue(thread, msg, completionRecord);
794514f5e3Sopenharmony_ci}
804514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::builtins
81