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/base/error_helper.h"
17 #include "ecmascript/base/builtins_base.h"
18 #include "ecmascript/base/error_type.h"
19 #include "ecmascript/base/number_helper.h"
20 #include "ecmascript/dfx/stackinfo/js_stackinfo.h"
21 #include "ecmascript/ecma_macros.h"
22 #include "ecmascript/ecma_vm.h"
23 #include "ecmascript/global_env.h"
24 #include "ecmascript/interpreter/frame_handler.h"
25 #include "ecmascript/interpreter/interpreter.h"
26 #include "ecmascript/js_object-inl.h"
27 #include "ecmascript/js_tagged_value-inl.h"
28 #include "ecmascript/jspandafile/js_pandafile_manager.h"
29 #include "ecmascript/object_factory.h"
30
31 namespace panda::ecmascript::base {
ErrorCommonToString(EcmaRuntimeCallInfo * argv,const ErrorType & errorType)32 JSTaggedValue ErrorHelper::ErrorCommonToString(EcmaRuntimeCallInfo *argv, const ErrorType &errorType)
33 {
34 ASSERT(argv);
35 JSThread *thread = argv->GetThread();
36 [[maybe_unused]] EcmaHandleScope handleScope(thread);
37
38 // 1. Let O be the this value.
39 // 2. If Type(O) is not Object, throw a TypeError exception
40 JSHandle<JSTaggedValue> thisValue = BuiltinsBase::GetThis(argv);
41 if (!thisValue->IsECMAObject()) {
42 THROW_TYPE_ERROR_AND_RETURN(thread, "ErrorToString:not an object", JSTaggedValue::Exception());
43 }
44 // 3. Let name be Get(O, "name").
45 // 4. ReturnIfAbrupt(name).
46 auto globalConst = thread->GlobalConstants();
47 JSHandle<JSTaggedValue> handleName = globalConst->GetHandledNameString();
48 JSHandle<JSTaggedValue> name = JSObject::GetProperty(thread, thisValue, handleName).GetValue();
49 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
50
51 // 5. If name is undefined, let name be "Error"; otherwise let name be ToString(name).
52 // 6. ReturnIfAbrupt(name).
53 name = ErrorHelper::GetErrorName(thread, name, errorType);
54 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
55
56 // 7. Let msg be Get(O, "message").
57 // 8. ReturnIfAbrupt(msg).
58 JSHandle<JSTaggedValue> handleMsg = globalConst->GetHandledMessageString();
59 JSHandle<JSTaggedValue> msg = JSObject::GetProperty(thread, thisValue, handleMsg).GetValue();
60 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
61
62 // 9. If msg is undefined, let msg be the empty String; otherwise let msg be ToString(msg).
63 // 10. ReturnIfAbrupt(msg).
64 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
65 if (msg->IsUndefined()) {
66 msg = JSHandle<JSTaggedValue>::Cast(factory->GetEmptyString());
67 } else {
68 msg = JSHandle<JSTaggedValue>::Cast(JSTaggedValue::ToString(thread, msg));
69 }
70 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
71
72 // 11. If name is the empty String, return msg.
73 // 12. If msg is the empty String, return name.
74 if (EcmaStringAccessor(JSHandle<EcmaString>::Cast(name)).GetLength() == 0) {
75 return msg.GetTaggedValue();
76 }
77 if (EcmaStringAccessor(JSHandle<EcmaString>::Cast(msg)).GetLength() == 0) {
78 return name.GetTaggedValue();
79 }
80
81 // 13. Return the result of concatenating name, the code unit 0x003A (COLON), the code unit 0x0020 (SPACE), and msg.
82 JSHandle<EcmaString> space = factory->NewFromASCII(": ");
83 JSHandle<EcmaString> jsHandleName = JSHandle<EcmaString>::Cast(name);
84 JSHandle<EcmaString> jsHandleMsg = JSHandle<EcmaString>::Cast(msg);
85 JSHandle<EcmaString> handleNameSpace = factory->ConcatFromString(jsHandleName, space);
86 JSHandle<EcmaString> result = factory->ConcatFromString(handleNameSpace, jsHandleMsg);
87 return result.GetTaggedValue();
88 }
89
GetErrorName(JSThread * thread,const JSHandle<JSTaggedValue> & name,const ErrorType & errorType)90 JSHandle<JSTaggedValue> ErrorHelper::GetErrorName(JSThread *thread, const JSHandle<JSTaggedValue> &name,
91 const ErrorType &errorType)
92 {
93 auto globalConst = thread->GlobalConstants();
94 if (name->IsUndefined()) {
95 JSHandle<JSTaggedValue> errorKey;
96 switch (errorType) {
97 case ErrorType::RANGE_ERROR:
98 errorKey = globalConst->GetHandledRangeErrorString();
99 break;
100 case ErrorType::EVAL_ERROR:
101 errorKey = globalConst->GetHandledEvalErrorString();
102 break;
103 case ErrorType::REFERENCE_ERROR:
104 errorKey = globalConst->GetHandledReferenceErrorString();
105 break;
106 case ErrorType::TYPE_ERROR:
107 errorKey = globalConst->GetHandledTypeErrorString();
108 break;
109 case ErrorType::AGGREGATE_ERROR:
110 errorKey = globalConst->GetHandledAggregateErrorString();
111 break;
112 case ErrorType::URI_ERROR:
113 errorKey = globalConst->GetHandledURIErrorString();
114 break;
115 case ErrorType::SYNTAX_ERROR:
116 errorKey = globalConst->GetHandledSyntaxErrorString();
117 break;
118 case ErrorType::OOM_ERROR:
119 errorKey = globalConst->GetHandledOOMErrorString();
120 break;
121 case ErrorType::TERMINATION_ERROR:
122 errorKey = globalConst->GetHandledTerminationErrorString();
123 break;
124 default:
125 errorKey = globalConst->GetHandledErrorString();
126 break;
127 }
128 return errorKey;
129 }
130 return JSHandle<JSTaggedValue>::Cast(JSTaggedValue::ToString(thread, name));
131 }
132
ErrorCommonConstructor(EcmaRuntimeCallInfo * argv,const ErrorType & errorType)133 JSTaggedValue ErrorHelper::ErrorCommonConstructor(EcmaRuntimeCallInfo *argv,
134 [[maybe_unused]] const ErrorType &errorType)
135 {
136 JSThread *thread = argv->GetThread();
137 [[maybe_unused]] EcmaHandleScope handleScope(thread);
138
139 // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
140 auto ecmaVm = thread->GetEcmaVM();
141 ObjectFactory *factory = ecmaVm->GetFactory();
142 JSHandle<JSTaggedValue> ctor = BuiltinsBase::GetConstructor(argv);
143 JSHandle<JSTaggedValue> newTarget = BuiltinsBase::GetNewTarget(argv);
144 if (newTarget->IsUndefined()) {
145 newTarget = ctor;
146 }
147 JSHandle<JSTaggedValue> message = BuiltinsBase::GetCallArg(argv, 0);
148
149 // 2. Let O be OrdinaryCreateFromConstructor(newTarget, "%ErrorPrototype%", «[[ErrorData]]»).
150 JSHandle<JSObject> nativeInstanceObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(ctor), newTarget);
151
152 // 3. ReturnIfAbrupt(O).
153 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
154
155 // 4. If message is not undefined, then
156 // a. Let msg be ToString(message).
157 // b. ReturnIfAbrupt(msg).
158 // c. Let msgDesc be the PropertyDescriptor{[[Value]]: msg, [[Writable]]: true, [[Enumerable]]: false,
159 // [[Configurable]]: true}.
160 // d. Let status be DefinePropertyOrThrow(O, "message", msgDesc).
161 // e. Assert: status is not an abrupt completion
162 auto globalConst = thread->GlobalConstants();
163 if (!message->IsUndefined()) {
164 JSHandle<EcmaString> handleStr = JSTaggedValue::ToString(thread, message);
165 LOG_ECMA(DEBUG) << "Throw error: " << EcmaStringAccessor(handleStr).ToCString();
166 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
167 JSHandle<JSTaggedValue> msgKey = globalConst->GetHandledMessageString();
168 PropertyDescriptor msgDesc(thread, JSHandle<JSTaggedValue>::Cast(handleStr), true, false, true);
169 [[maybe_unused]] bool status = JSObject::DefineOwnProperty(thread, nativeInstanceObj, msgKey, msgDesc);
170 ASSERT_PRINT(status == true, "return result exception!");
171 }
172 // InstallErrorCause
173 JSHandle<JSTaggedValue> options = BuiltinsBase::GetCallArg(argv, 1);
174 // If options is an Object and ? HasProperty(options, "cause") is true, then
175 // a. Let cause be ? Get(options, "cause").
176 // b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause).
177 if (options->IsECMAObject()) {
178 JSHandle<JSTaggedValue> causeKey = globalConst->GetHandledCauseString();
179 bool causePresent = JSTaggedValue::HasProperty(thread, options, causeKey);
180 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
181 if (causePresent) {
182 JSHandle<JSTaggedValue> cause = JSObject::GetProperty(thread, options, causeKey).GetValue();
183 RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
184 PropertyDescriptor causeDesc(thread, cause, true, false, true);
185 [[maybe_unused]] bool status = JSObject::DefineOwnProperty(thread, nativeInstanceObj, causeKey, causeDesc);
186 ASSERT_PRINT(status == true, "return result exception!");
187 }
188 }
189 JSHandle<JSTaggedValue> errorFunc = GetErrorJSFunction(thread);
190 if (!errorFunc->IsUndefined()) {
191 JSHandle<JSTaggedValue> errorFunckey = globalConst->GetHandledErrorFuncString();
192 PropertyDescriptor errorFuncDesc(thread, errorFunc, true, false, true);
193 [[maybe_unused]] bool status = JSObject::DefineOwnProperty(thread,
194 nativeInstanceObj, errorFunckey, errorFuncDesc);
195 ASSERT_PRINT(status == true, "return result exception!");
196 }
197
198 JSHandle<EcmaString> handleStack = BuildEcmaStackTrace(thread);
199 JSHandle<JSTaggedValue> stackkey = globalConst->GetHandledStackString();
200 PropertyDescriptor stackDesc(thread, JSHandle<JSTaggedValue>::Cast(handleStack), true, false, true);
201 [[maybe_unused]] bool status = JSObject::DefineOwnProperty(thread, nativeInstanceObj, stackkey, stackDesc);
202 ASSERT_PRINT(status == true, "return result exception!");
203
204 // 5. Return O.
205 return nativeInstanceObj.GetTaggedValue();
206 }
207
GetErrorJSFunction(JSThread * thread)208 JSHandle<JSTaggedValue> ErrorHelper::GetErrorJSFunction(JSThread *thread)
209 {
210 FrameHandler frameHandler(thread);
211 for (; frameHandler.HasFrame(); frameHandler.PrevJSFrame()) {
212 if (!frameHandler.IsJSFrame()) {
213 continue;
214 }
215
216 auto function = frameHandler.GetFunction();
217 if (function.IsJSFunctionBase() || function.IsJSProxy()) {
218 Method *method = ECMAObject::Cast(function.GetTaggedObject())->GetCallTarget();
219 if (!method->IsNativeWithCallField()) {
220 return JSHandle<JSTaggedValue>(thread, function);
221 }
222 }
223 }
224 return thread->GlobalConstants()->GetHandledUndefined();
225 }
226
BuildEcmaStackTrace(JSThread * thread)227 JSHandle<EcmaString> ErrorHelper::BuildEcmaStackTrace(JSThread *thread)
228 {
229 std::string data = JsStackInfo::BuildJsStackTrace(thread, false);
230 if (data.size() > MAX_ERROR_SIZE) {
231 // find last line break from 0 to MAX_ERROR_SIZE
232 size_t pos = data.rfind('\n', MAX_ERROR_SIZE);
233 if (pos != std::string::npos) {
234 data = data.substr(0, pos);
235 }
236 }
237 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
238 LOG_ECMA(DEBUG) << data;
239 return factory->NewFromStdString(data);
240 }
241 } // namespace panda::ecmascript::base
242