14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 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 "builtins_generator.h"
174514f5e3Sopenharmony_ci
184514f5e3Sopenharmony_ci#include "ecmascript/js_generator_object.h"
194514f5e3Sopenharmony_ci
204514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins {
214514f5e3Sopenharmony_ci// 26.2.1.1 GeneratorFunction(p1, p2, … , pn, body)
224514f5e3Sopenharmony_ciJSTaggedValue BuiltinsGenerator::GeneratorFunctionConstructor(EcmaRuntimeCallInfo *argv)
234514f5e3Sopenharmony_ci{
244514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Generator, Constructor);
254514f5e3Sopenharmony_ci    // not support
264514f5e3Sopenharmony_ci    THROW_TYPE_ERROR_AND_RETURN(argv->GetThread(), "Not support eval. Forbidden using new GeneratorFunction().",
274514f5e3Sopenharmony_ci                                JSTaggedValue::Exception());
284514f5e3Sopenharmony_ci}
294514f5e3Sopenharmony_ci
304514f5e3Sopenharmony_ci// 26.4.1.2 Generator.prototype.next(value)
314514f5e3Sopenharmony_ciJSTaggedValue BuiltinsGenerator::GeneratorPrototypeNext(EcmaRuntimeCallInfo *argv)
324514f5e3Sopenharmony_ci{
334514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Generator, PrototypeNext);
344514f5e3Sopenharmony_ci    // 1.Let g be the this value.
354514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
364514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
374514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> msg = GetThis(argv);
384514f5e3Sopenharmony_ci    if (!msg->IsGeneratorObject()) {
394514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "Not a generator object.", JSTaggedValue::Exception());
404514f5e3Sopenharmony_ci    }
414514f5e3Sopenharmony_ci    JSHandle<JSGeneratorObject> generator(thread, JSGeneratorObject::Cast(*JSTaggedValue::ToObject(thread, msg)));
424514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
434514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ci    // 2.Return ? GeneratorResume(g, value).
464514f5e3Sopenharmony_ci    JSHandle<JSObject> result = JSGeneratorObject::GeneratorResume(thread, generator, value.GetTaggedValue());
474514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
484514f5e3Sopenharmony_ci    return result.GetTaggedValue();
494514f5e3Sopenharmony_ci}
504514f5e3Sopenharmony_ci
514514f5e3Sopenharmony_ci// 26.4.1.3 Generator.prototype.return(value)
524514f5e3Sopenharmony_ciJSTaggedValue BuiltinsGenerator::GeneratorPrototypeReturn(EcmaRuntimeCallInfo *argv)
534514f5e3Sopenharmony_ci{
544514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Generator, PrototypeReturn);
554514f5e3Sopenharmony_ci    // 1.Let g be the this value.
564514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
574514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
584514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> msg = GetThis(argv);
594514f5e3Sopenharmony_ci    if (!msg->IsGeneratorObject()) {
604514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "Not a generator object.", JSTaggedValue::Exception());
614514f5e3Sopenharmony_ci    }
624514f5e3Sopenharmony_ci    JSHandle<JSGeneratorObject> generator(thread, JSGeneratorObject::Cast(*JSTaggedValue::ToObject(thread, msg)));
634514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
644514f5e3Sopenharmony_ci    // 2.Let C be Completion { [[Type]]: return, [[Value]]: value, [[Target]]: empty }.
654514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> value = GetCallArg(argv, 0);
664514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
674514f5e3Sopenharmony_ci    JSHandle<CompletionRecord> completionRecord =
684514f5e3Sopenharmony_ci        factory->NewCompletionRecord(CompletionRecordType::RETURN, value);
694514f5e3Sopenharmony_ci
704514f5e3Sopenharmony_ci    // 3.Return ? GeneratorResumeAbrupt(g, C).
714514f5e3Sopenharmony_ci    JSHandle<JSObject> result = JSGeneratorObject::GeneratorResumeAbrupt(thread, generator, completionRecord);
724514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
734514f5e3Sopenharmony_ci    return result.GetTaggedValue();
744514f5e3Sopenharmony_ci}
754514f5e3Sopenharmony_ci
764514f5e3Sopenharmony_ci// 26.4.1.4 Generator.prototype.throw(exception)
774514f5e3Sopenharmony_ciJSTaggedValue BuiltinsGenerator::GeneratorPrototypeThrow(EcmaRuntimeCallInfo *argv)
784514f5e3Sopenharmony_ci{
794514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Generator, PrototypeThrow);
804514f5e3Sopenharmony_ci    // 1.Let g be the this value.
814514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
824514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
834514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> msg = GetThis(argv);
844514f5e3Sopenharmony_ci    if (!msg->IsGeneratorObject()) {
854514f5e3Sopenharmony_ci        THROW_TYPE_ERROR_AND_RETURN(thread, "Not a generator object.", JSTaggedValue::Exception());
864514f5e3Sopenharmony_ci    }
874514f5e3Sopenharmony_ci    JSHandle<JSGeneratorObject> generator(thread, JSGeneratorObject::Cast(*JSTaggedValue::ToObject(thread, msg)));
884514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
894514f5e3Sopenharmony_ci    // 2.Let C be ThrowCompletion(exception).
904514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> exception = GetCallArg(argv, 0);
914514f5e3Sopenharmony_ci    ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
924514f5e3Sopenharmony_ci    JSHandle<CompletionRecord> completionRecord =
934514f5e3Sopenharmony_ci        factory->NewCompletionRecord(CompletionRecordType::THROW, exception);
944514f5e3Sopenharmony_ci
954514f5e3Sopenharmony_ci    // 3.Return ? GeneratorResumeAbrupt(g, C).
964514f5e3Sopenharmony_ci    JSHandle<JSObject> result = JSGeneratorObject::GeneratorResumeAbrupt(thread, generator, completionRecord);
974514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
984514f5e3Sopenharmony_ci    return result.GetTaggedValue();
994514f5e3Sopenharmony_ci}
1004514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::builtins
101