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 "ecmascript/builtins/builtins_errors.h"
174514f5e3Sopenharmony_ci#include "ecmascript/js_array.h"
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_cinamespace panda::ecmascript::builtins {
204514f5e3Sopenharmony_ciusing ErrorHelper = base::ErrorHelper;
214514f5e3Sopenharmony_ciusing ErrorType = base::ErrorType;
224514f5e3Sopenharmony_ci// Error
234514f5e3Sopenharmony_ciJSTaggedValue BuiltinsError::ErrorConstructor(EcmaRuntimeCallInfo *argv)
244514f5e3Sopenharmony_ci{
254514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, ErrorConstructor);
264514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::ERROR);
274514f5e3Sopenharmony_ci}
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_ciJSTaggedValue BuiltinsError::ToString(EcmaRuntimeCallInfo *argv)
304514f5e3Sopenharmony_ci{
314514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, ErrorToString);
324514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonToString(argv, ErrorType::ERROR);
334514f5e3Sopenharmony_ci}
344514f5e3Sopenharmony_ci
354514f5e3Sopenharmony_ci// RangeError
364514f5e3Sopenharmony_ciJSTaggedValue BuiltinsRangeError::RangeErrorConstructor(EcmaRuntimeCallInfo *argv)
374514f5e3Sopenharmony_ci{
384514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, RangeErrorConstructor);
394514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::RANGE_ERROR);
404514f5e3Sopenharmony_ci}
414514f5e3Sopenharmony_ci
424514f5e3Sopenharmony_ciJSTaggedValue BuiltinsRangeError::ToString(EcmaRuntimeCallInfo *argv)
434514f5e3Sopenharmony_ci{
444514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, RangeErrorToString);
454514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonToString(argv, ErrorType::RANGE_ERROR);
464514f5e3Sopenharmony_ci}
474514f5e3Sopenharmony_ci
484514f5e3Sopenharmony_ci// ReferenceError
494514f5e3Sopenharmony_ciJSTaggedValue BuiltinsReferenceError::ReferenceErrorConstructor(EcmaRuntimeCallInfo *argv)
504514f5e3Sopenharmony_ci{
514514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, ReferenceErrorConstructor);
524514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::REFERENCE_ERROR);
534514f5e3Sopenharmony_ci}
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_ciJSTaggedValue BuiltinsReferenceError::ToString(EcmaRuntimeCallInfo *argv)
564514f5e3Sopenharmony_ci{
574514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, ReferenceErrorToString);
584514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonToString(argv, ErrorType::REFERENCE_ERROR);
594514f5e3Sopenharmony_ci}
604514f5e3Sopenharmony_ci
614514f5e3Sopenharmony_ci// TypeError
624514f5e3Sopenharmony_ciJSTaggedValue BuiltinsTypeError::TypeErrorConstructor(EcmaRuntimeCallInfo *argv)
634514f5e3Sopenharmony_ci{
644514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, TypeErrorConstructor);
654514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::TYPE_ERROR);
664514f5e3Sopenharmony_ci}
674514f5e3Sopenharmony_ci
684514f5e3Sopenharmony_ciJSTaggedValue BuiltinsTypeError::ToString(EcmaRuntimeCallInfo *argv)
694514f5e3Sopenharmony_ci{
704514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, TypeErrorToString);
714514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonToString(argv, ErrorType::TYPE_ERROR);
724514f5e3Sopenharmony_ci}
734514f5e3Sopenharmony_ci
744514f5e3Sopenharmony_ciJSTaggedValue BuiltinsTypeError::ThrowTypeError(EcmaRuntimeCallInfo *argv)
754514f5e3Sopenharmony_ci{
764514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
774514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(thread, Error, ThrowTypeError);
784514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope handleScope(thread);
794514f5e3Sopenharmony_ci    THROW_TYPE_ERROR_AND_RETURN(thread, "type error", JSTaggedValue::Exception());
804514f5e3Sopenharmony_ci}
814514f5e3Sopenharmony_ci
824514f5e3Sopenharmony_ci// URIError
834514f5e3Sopenharmony_ciJSTaggedValue BuiltinsURIError::URIErrorConstructor(EcmaRuntimeCallInfo *argv)
844514f5e3Sopenharmony_ci{
854514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, URIErrorConstructor);
864514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::URI_ERROR);
874514f5e3Sopenharmony_ci}
884514f5e3Sopenharmony_ci
894514f5e3Sopenharmony_ciJSTaggedValue BuiltinsURIError::ToString(EcmaRuntimeCallInfo *argv)
904514f5e3Sopenharmony_ci{
914514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, URIErrorToString);
924514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonToString(argv, ErrorType::URI_ERROR);
934514f5e3Sopenharmony_ci}
944514f5e3Sopenharmony_ci
954514f5e3Sopenharmony_ci// SyntaxError
964514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSyntaxError::SyntaxErrorConstructor(EcmaRuntimeCallInfo *argv)
974514f5e3Sopenharmony_ci{
984514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, SyntaxErrorConstructor);
994514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::SYNTAX_ERROR);
1004514f5e3Sopenharmony_ci}
1014514f5e3Sopenharmony_ci
1024514f5e3Sopenharmony_ciJSTaggedValue BuiltinsSyntaxError::ToString(EcmaRuntimeCallInfo *argv)
1034514f5e3Sopenharmony_ci{
1044514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, SyntaxErrorToString);
1054514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonToString(argv, ErrorType::SYNTAX_ERROR);
1064514f5e3Sopenharmony_ci}
1074514f5e3Sopenharmony_ci
1084514f5e3Sopenharmony_ci// EvalError
1094514f5e3Sopenharmony_ciJSTaggedValue BuiltinsEvalError::EvalErrorConstructor(EcmaRuntimeCallInfo *argv)
1104514f5e3Sopenharmony_ci{
1114514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, EvalErrorConstructor);
1124514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::EVAL_ERROR);
1134514f5e3Sopenharmony_ci}
1144514f5e3Sopenharmony_ci
1154514f5e3Sopenharmony_ciJSTaggedValue BuiltinsEvalError::ToString(EcmaRuntimeCallInfo *argv)
1164514f5e3Sopenharmony_ci{
1174514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, EvalErrorToString);
1184514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonToString(argv, ErrorType::EVAL_ERROR);
1194514f5e3Sopenharmony_ci}
1204514f5e3Sopenharmony_ci
1214514f5e3Sopenharmony_ci// AggregateError
1224514f5e3Sopenharmony_ciJSTaggedValue BuiltinsAggregateError::AggregateErrorConstructor(EcmaRuntimeCallInfo *argv)
1234514f5e3Sopenharmony_ci{
1244514f5e3Sopenharmony_ci    JSThread *thread = argv->GetThread();
1254514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, AggregateErrorConstructor);
1264514f5e3Sopenharmony_ci    [[maybe_unused]] EcmaHandleScope scope(thread);
1274514f5e3Sopenharmony_ci    EcmaVM *ecmaVm = thread->GetEcmaVM();
1284514f5e3Sopenharmony_ci    ObjectFactory *factory = ecmaVm->GetFactory();
1294514f5e3Sopenharmony_ci    const GlobalEnvConstants *globalConst = thread->GlobalConstants();
1304514f5e3Sopenharmony_ci    // 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget.
1314514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
1324514f5e3Sopenharmony_ci    JSMutableHandle<JSTaggedValue> newTarget(thread, GetNewTarget(argv));
1334514f5e3Sopenharmony_ci    if (newTarget->IsUndefined()) {
1344514f5e3Sopenharmony_ci        newTarget.Update(constructor.GetTaggedValue());
1354514f5e3Sopenharmony_ci    }
1364514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> errors = BuiltinsBase::GetCallArg(argv, 0);
1374514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> message = BuiltinsBase::GetCallArg(argv, 1);
1384514f5e3Sopenharmony_ci    // 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%AggregateError.prototype%", « [[ErrorData]] »).
1394514f5e3Sopenharmony_ci    JSHandle<JSObject> objValues = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
1404514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
1414514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> taggedObj = JSHandle<JSTaggedValue>::Cast(objValues);
1424514f5e3Sopenharmony_ci    // 3. If message is not undefined, then
1434514f5e3Sopenharmony_ci    // a. Let msg be ? ToString(message).
1444514f5e3Sopenharmony_ci    // b. Let msgDesc be the PropertyDescriptor
1454514f5e3Sopenharmony_ci    // { [[Value]]: msg, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }.
1464514f5e3Sopenharmony_ci    // c. Perform ! DefinePropertyOrThrow(O, "message", msgDesc).
1474514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> msgKey = globalConst->GetHandledMessageString();
1484514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> errorsKey = globalConst->GetHandledErrorsString();
1494514f5e3Sopenharmony_ci    if (!message->IsUndefined()) {
1504514f5e3Sopenharmony_ci        JSHandle<EcmaString> handleStr = JSTaggedValue::ToString(thread, message);
1514514f5e3Sopenharmony_ci        RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
1524514f5e3Sopenharmony_ci        PropertyDescriptor msgDesc(thread, JSHandle<JSTaggedValue>::Cast(handleStr), true, false, true);
1534514f5e3Sopenharmony_ci        JSTaggedValue::DefinePropertyOrThrow(thread, taggedObj, msgKey, msgDesc);
1544514f5e3Sopenharmony_ci    }
1554514f5e3Sopenharmony_ci    // InstallErrorCause
1564514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> options = BuiltinsBase::GetCallArg(argv, 2); // 2 : Third parameter
1574514f5e3Sopenharmony_ci    // If options is an Object and ? HasProperty(options, "cause") is true, then
1584514f5e3Sopenharmony_ci    //   a. Let cause be ? Get(options, "cause").
1594514f5e3Sopenharmony_ci    //   b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause).
1604514f5e3Sopenharmony_ci    if (options->IsECMAObject()) {
1614514f5e3Sopenharmony_ci        JSHandle<JSTaggedValue> causeKey = globalConst->GetHandledCauseString();
1624514f5e3Sopenharmony_ci        bool causePresent = JSTaggedValue::HasProperty(thread, options, causeKey);
1634514f5e3Sopenharmony_ci        RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
1644514f5e3Sopenharmony_ci        if (causePresent) {
1654514f5e3Sopenharmony_ci            JSHandle<JSTaggedValue> cause = JSObject::GetProperty(thread, options, causeKey).GetValue();
1664514f5e3Sopenharmony_ci            RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
1674514f5e3Sopenharmony_ci            PropertyDescriptor causeDesc(thread, cause, true, false, true);
1684514f5e3Sopenharmony_ci            JSTaggedValue::DefinePropertyOrThrow(thread, taggedObj, causeKey, causeDesc);
1694514f5e3Sopenharmony_ci        }
1704514f5e3Sopenharmony_ci    }
1714514f5e3Sopenharmony_ci    // 4. Let errorsList be ? IterableToList(errors).
1724514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> errorsList = JSObject::IterableToList(thread, errors);
1734514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
1744514f5e3Sopenharmony_ci    // 5. Perform ! DefinePropertyOrThrow(O, "errors", PropertyDescriptor { [[Configurable]]: true,
1754514f5e3Sopenharmony_ci    //    [[Enumerable]]: false, [[Writable]]: true, [[Value]]: !CreateArrayFromList(errorsList) }).
1764514f5e3Sopenharmony_ci    JSHandle<TaggedArray> errorsArray = JSArray::ToTaggedArray(thread, errorsList);
1774514f5e3Sopenharmony_ci    JSHandle<JSTaggedValue> errorsValues(JSArray::CreateArrayFromList(thread, errorsArray));
1784514f5e3Sopenharmony_ci    PropertyDescriptor msgDesc(thread, errorsValues, true, false, true);
1794514f5e3Sopenharmony_ci    JSTaggedValue::DefinePropertyOrThrow(thread, taggedObj, errorsKey, msgDesc);
1804514f5e3Sopenharmony_ci    RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
1814514f5e3Sopenharmony_ci    // 6. Return O.
1824514f5e3Sopenharmony_ci    return taggedObj.GetTaggedValue();
1834514f5e3Sopenharmony_ci}
1844514f5e3Sopenharmony_ci
1854514f5e3Sopenharmony_ciJSTaggedValue BuiltinsAggregateError::ToString(EcmaRuntimeCallInfo *argv)
1864514f5e3Sopenharmony_ci{
1874514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, AggregateErrorToString);
1884514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonToString(argv, ErrorType::AGGREGATE_ERROR);
1894514f5e3Sopenharmony_ci}
1904514f5e3Sopenharmony_ci
1914514f5e3Sopenharmony_ci// OOMError
1924514f5e3Sopenharmony_ciJSTaggedValue BuiltinsOOMError::OOMErrorConstructor(EcmaRuntimeCallInfo *argv)
1934514f5e3Sopenharmony_ci{
1944514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, OOMErrorConstructor);
1954514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::OOM_ERROR);
1964514f5e3Sopenharmony_ci}
1974514f5e3Sopenharmony_ci
1984514f5e3Sopenharmony_ciJSTaggedValue BuiltinsOOMError::ToString(EcmaRuntimeCallInfo *argv)
1994514f5e3Sopenharmony_ci{
2004514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, OOMErrorToString);
2014514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonToString(argv, ErrorType::OOM_ERROR);
2024514f5e3Sopenharmony_ci}
2034514f5e3Sopenharmony_ci
2044514f5e3Sopenharmony_ci// TerminationError
2054514f5e3Sopenharmony_ciJSTaggedValue BuiltinsTerminationError::TerminationErrorConstructor(EcmaRuntimeCallInfo *argv)
2064514f5e3Sopenharmony_ci{
2074514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, TerminationErrorConstructor);
2084514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::TERMINATION_ERROR);
2094514f5e3Sopenharmony_ci}
2104514f5e3Sopenharmony_ci
2114514f5e3Sopenharmony_ciJSTaggedValue BuiltinsTerminationError::ToString(EcmaRuntimeCallInfo *argv)
2124514f5e3Sopenharmony_ci{
2134514f5e3Sopenharmony_ci    BUILTINS_API_TRACE(argv->GetThread(), Error, TerminationErrorToString);
2144514f5e3Sopenharmony_ci    return ErrorHelper::ErrorCommonToString(argv, ErrorType::TERMINATION_ERROR);
2154514f5e3Sopenharmony_ci}
2164514f5e3Sopenharmony_ci}  // namespace panda::ecmascript::builtins
217