• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ecmascript/builtins/builtins_errors.h"
17 
18 #include "ecmascript/base/error_helper.h"
19 #include "ecmascript/ecma_macros.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/js_tagged_value-inl.h"
22 #include "ecmascript/js_array.h"
23 #include "ecmascript/js_primitive_ref.h"
24 #include "ecmascript/js_iterator.h"
25 
26 namespace panda::ecmascript::builtins {
27 using ErrorHelper = base::ErrorHelper;
28 using ErrorType = base::ErrorType;
29 // Error
ErrorConstructor(EcmaRuntimeCallInfo * argv)30 JSTaggedValue BuiltinsError::ErrorConstructor(EcmaRuntimeCallInfo *argv)
31 {
32     return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::ERROR);
33 }
34 
ToString(EcmaRuntimeCallInfo * argv)35 JSTaggedValue BuiltinsError::ToString(EcmaRuntimeCallInfo *argv)
36 {
37     return ErrorHelper::ErrorCommonToString(argv, ErrorType::ERROR);
38 }
39 
40 // RangeError
RangeErrorConstructor(EcmaRuntimeCallInfo * argv)41 JSTaggedValue BuiltinsRangeError::RangeErrorConstructor(EcmaRuntimeCallInfo *argv)
42 {
43     return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::RANGE_ERROR);
44 }
45 
ToString(EcmaRuntimeCallInfo * argv)46 JSTaggedValue BuiltinsRangeError::ToString(EcmaRuntimeCallInfo *argv)
47 {
48     return ErrorHelper::ErrorCommonToString(argv, ErrorType::RANGE_ERROR);
49 }
50 
51 // ReferenceError
ReferenceErrorConstructor(EcmaRuntimeCallInfo * argv)52 JSTaggedValue BuiltinsReferenceError::ReferenceErrorConstructor(EcmaRuntimeCallInfo *argv)
53 {
54     return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::REFERENCE_ERROR);
55 }
56 
ToString(EcmaRuntimeCallInfo * argv)57 JSTaggedValue BuiltinsReferenceError::ToString(EcmaRuntimeCallInfo *argv)
58 {
59     return ErrorHelper::ErrorCommonToString(argv, ErrorType::REFERENCE_ERROR);
60 }
61 
62 // TypeError
TypeErrorConstructor(EcmaRuntimeCallInfo * argv)63 JSTaggedValue BuiltinsTypeError::TypeErrorConstructor(EcmaRuntimeCallInfo *argv)
64 {
65     return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::TYPE_ERROR);
66 }
67 
ToString(EcmaRuntimeCallInfo * argv)68 JSTaggedValue BuiltinsTypeError::ToString(EcmaRuntimeCallInfo *argv)
69 {
70     return ErrorHelper::ErrorCommonToString(argv, ErrorType::TYPE_ERROR);
71 }
72 
ThrowTypeError(EcmaRuntimeCallInfo * argv)73 JSTaggedValue BuiltinsTypeError::ThrowTypeError(EcmaRuntimeCallInfo *argv)
74 {
75     JSThread *thread = argv->GetThread();
76     [[maybe_unused]] EcmaHandleScope handle_scope(thread);
77     THROW_TYPE_ERROR_AND_RETURN(thread, "type error", JSTaggedValue::Exception());
78 }
79 
80 // URIError
URIErrorConstructor(EcmaRuntimeCallInfo * argv)81 JSTaggedValue BuiltinsURIError::URIErrorConstructor(EcmaRuntimeCallInfo *argv)
82 {
83     return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::URI_ERROR);
84 }
85 
ToString(EcmaRuntimeCallInfo * argv)86 JSTaggedValue BuiltinsURIError::ToString(EcmaRuntimeCallInfo *argv)
87 {
88     return ErrorHelper::ErrorCommonToString(argv, ErrorType::URI_ERROR);
89 }
90 
91 // SyntaxError
SyntaxErrorConstructor(EcmaRuntimeCallInfo * argv)92 JSTaggedValue BuiltinsSyntaxError::SyntaxErrorConstructor(EcmaRuntimeCallInfo *argv)
93 {
94     return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::SYNTAX_ERROR);
95 }
96 
ToString(EcmaRuntimeCallInfo * argv)97 JSTaggedValue BuiltinsSyntaxError::ToString(EcmaRuntimeCallInfo *argv)
98 {
99     return ErrorHelper::ErrorCommonToString(argv, ErrorType::SYNTAX_ERROR);
100 }
101 
102 // EvalError
EvalErrorConstructor(EcmaRuntimeCallInfo * argv)103 JSTaggedValue BuiltinsEvalError::EvalErrorConstructor(EcmaRuntimeCallInfo *argv)
104 {
105     return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::EVAL_ERROR);
106 }
107 
ToString(EcmaRuntimeCallInfo * argv)108 JSTaggedValue BuiltinsEvalError::ToString(EcmaRuntimeCallInfo *argv)
109 {
110     return ErrorHelper::ErrorCommonToString(argv, ErrorType::EVAL_ERROR);
111 }
112 
113 // AggregateError
AggregateErrorConstructor(EcmaRuntimeCallInfo * argv)114 JSTaggedValue BuiltinsAggregateError::AggregateErrorConstructor(EcmaRuntimeCallInfo *argv)
115 {
116     JSThread *thread = argv->GetThread();
117     [[maybe_unused]] EcmaHandleScope scope(thread);
118     EcmaVM *ecmaVm = thread->GetEcmaVM();
119     ObjectFactory *factory = ecmaVm->GetFactory();
120     const GlobalEnvConstants *globalConst = thread->GlobalConstants();
121     // 1. If NewTarget is undefined, let newTarget be the active function object; else let newTarget be NewTarget.
122     JSHandle<JSTaggedValue> constructor = GetConstructor(argv);
123     JSMutableHandle<JSTaggedValue> newTarget(thread, GetNewTarget(argv));
124     if (newTarget->IsUndefined()) {
125         newTarget.Update(constructor.GetTaggedValue());
126     }
127     JSHandle<JSTaggedValue> errors = BuiltinsBase::GetCallArg(argv, 0);
128     JSHandle<JSTaggedValue> message = BuiltinsBase::GetCallArg(argv, 1);
129     // 2. Let O be ? OrdinaryCreateFromConstructor(newTarget, "%AggregateError.prototype%", « [[ErrorData]] »).
130     JSHandle<JSObject> objValues = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(constructor), newTarget);
131     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
132     JSHandle<JSTaggedValue> taggedObj = JSHandle<JSTaggedValue>::Cast(objValues);
133     // 3. If message is not undefined, then
134     // a. Let msg be ? ToString(message).
135     // b. Let msgDesc be the PropertyDescriptor
136     // { [[Value]]: msg, [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: true }.
137     // c. Perform ! DefinePropertyOrThrow(O, "message", msgDesc).
138     JSHandle<JSTaggedValue> msgKey = globalConst->GetHandledMessageString();
139     JSHandle<JSTaggedValue> errorsKey = globalConst->GetHandledErrorsString();
140     if (!message->IsUndefined()) {
141         JSHandle<EcmaString> handleStr = JSTaggedValue::ToString(thread, message);
142         RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
143         PropertyDescriptor msgDesc(thread, JSHandle<JSTaggedValue>::Cast(handleStr), true, false, true);
144         JSTaggedValue::DefinePropertyOrThrow(thread, taggedObj, msgKey, msgDesc);
145     }
146     // 4. Let errorsList be ? IterableToList(errors).
147     JSHandle<JSTaggedValue> errorsList = JSObject::IterableToList(thread, errors);
148     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
149     // 5. Perform ! DefinePropertyOrThrow(O, "errors", PropertyDescriptor { [[Configurable]]: true,
150     //    [[Enumerable]]: false, [[Writable]]: true, [[Value]]: !CreateArrayFromList(errorsList) }).
151     JSHandle<TaggedArray> errorsArray = JSArray::ToTaggedArray(thread, errorsList);
152     JSHandle<JSTaggedValue> errorsValues(JSArray::CreateArrayFromList(thread, errorsArray));
153     PropertyDescriptor msgDesc(thread, errorsValues, true, false, true);
154     JSTaggedValue::DefinePropertyOrThrow(thread, taggedObj, errorsKey, msgDesc);
155     // 6. Return O.
156     return taggedObj.GetTaggedValue();
157 }
158 
ToString(EcmaRuntimeCallInfo * argv)159 JSTaggedValue BuiltinsAggregateError::ToString(EcmaRuntimeCallInfo *argv)
160 {
161     return ErrorHelper::ErrorCommonToString(argv, ErrorType::AGGREGATE_ERROR);
162 }
163 
164 // OOMError
OOMErrorConstructor(EcmaRuntimeCallInfo * argv)165 JSTaggedValue BuiltinsOOMError::OOMErrorConstructor(EcmaRuntimeCallInfo *argv)
166 {
167     return ErrorHelper::ErrorCommonConstructor(argv, ErrorType::OOM_ERROR);
168 }
169 
ToString(EcmaRuntimeCallInfo * argv)170 JSTaggedValue BuiltinsOOMError::ToString(EcmaRuntimeCallInfo *argv)
171 {
172     return ErrorHelper::ErrorCommonToString(argv, ErrorType::OOM_ERROR);
173 }
174 }  // namespace panda::ecmascript::builtins
175