• 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/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         TaggedObject *errorKey = nullptr;
96         switch (errorType) {
97             case ErrorType::RANGE_ERROR:
98                 errorKey = reinterpret_cast<TaggedObject *>(*globalConst->GetHandledRangeErrorString());
99                 break;
100             case ErrorType::EVAL_ERROR:
101                 errorKey = reinterpret_cast<TaggedObject *>(*globalConst->GetHandledEvalErrorString());
102                 break;
103             case ErrorType::REFERENCE_ERROR:
104                 errorKey = reinterpret_cast<TaggedObject *>(*globalConst->GetHandledReferenceErrorString());
105                 break;
106             case ErrorType::TYPE_ERROR:
107                 errorKey = reinterpret_cast<TaggedObject *>(*globalConst->GetHandledTypeErrorString());
108                 break;
109             case ErrorType::AGGREGATE_ERROR:
110                 errorKey = reinterpret_cast<TaggedObject *>(*globalConst->GetHandledAggregateErrorString());
111                 break;
112             case ErrorType::URI_ERROR:
113                 errorKey = reinterpret_cast<TaggedObject *>(*globalConst->GetHandledURIErrorString());
114                 break;
115             case ErrorType::SYNTAX_ERROR:
116                 errorKey = reinterpret_cast<TaggedObject *>(*globalConst->GetHandledSyntaxErrorString());
117                 break;
118             case ErrorType::OOM_ERROR:
119                 errorKey = reinterpret_cast<TaggedObject *>(*globalConst->GetHandledOOMErrorString());
120                 break;
121             default:
122                 errorKey = reinterpret_cast<TaggedObject *>(*globalConst->GetHandledErrorString());
123                 break;
124         }
125         return JSHandle<JSTaggedValue>(thread, JSTaggedValue(errorKey));
126     }
127     return JSHandle<JSTaggedValue>::Cast(JSTaggedValue::ToString(thread, name));
128 }
129 
ErrorCommonConstructor(EcmaRuntimeCallInfo * argv,const ErrorType & errorType)130 JSTaggedValue ErrorHelper::ErrorCommonConstructor(EcmaRuntimeCallInfo *argv,
131                                                   [[maybe_unused]] const ErrorType &errorType)
132 {
133     JSThread *thread = argv->GetThread();
134     [[maybe_unused]] EcmaHandleScope handleScope(thread);
135 
136     // 1. If NewTarget is undefined, let newTarget be the active function object, else let newTarget be NewTarget.
137     auto ecmaVm = thread->GetEcmaVM();
138     ObjectFactory *factory = ecmaVm->GetFactory();
139     JSHandle<JSTaggedValue> ctor = BuiltinsBase::GetConstructor(argv);
140     JSHandle<JSTaggedValue> newTarget = BuiltinsBase::GetNewTarget(argv);
141     if (newTarget->IsUndefined()) {
142         newTarget = ctor;
143     }
144     JSHandle<JSTaggedValue> message = BuiltinsBase::GetCallArg(argv, 0);
145 
146     // 2. Let O be OrdinaryCreateFromConstructor(newTarget, "%ErrorPrototype%", «[[ErrorData]]»).
147     JSHandle<JSObject> nativeInstanceObj = factory->NewJSObjectByConstructor(JSHandle<JSFunction>(ctor), newTarget);
148 
149     // 3. ReturnIfAbrupt(O).
150     RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
151 
152     // 4. If message is not undefined, then
153     //    a. Let msg be ToString(message).
154     //    b. ReturnIfAbrupt(msg).
155     //    c. Let msgDesc be the PropertyDescriptor{[[Value]]: msg, [[Writable]]: true, [[Enumerable]]: false,
156     //       [[Configurable]]: true}.
157     //    d. Let status be DefinePropertyOrThrow(O, "message", msgDesc).
158     //    e. Assert: status is not an abrupt completion
159     auto globalConst = thread->GlobalConstants();
160     if (!message->IsUndefined()) {
161         JSHandle<EcmaString> handleStr = JSTaggedValue::ToString(thread, message);
162         LOG_ECMA(DEBUG) << "Throw error: " << EcmaStringAccessor(handleStr).ToCString();
163         RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
164         JSHandle<JSTaggedValue> msgKey = globalConst->GetHandledMessageString();
165         PropertyDescriptor msgDesc(thread, JSHandle<JSTaggedValue>::Cast(handleStr), true, false, true);
166         [[maybe_unused]] bool status = JSObject::DefineOwnProperty(thread, nativeInstanceObj, msgKey, msgDesc);
167         ASSERT_PRINT(status == true, "return result exception!");
168     }
169 
170     JSHandle<JSTaggedValue> errorFunc = GetErrorJSFunction(thread);
171     if (!errorFunc->IsUndefined()) {
172         JSHandle<JSTaggedValue> errorFunckey = globalConst->GetHandledErrorFuncString();
173         PropertyDescriptor errorFuncDesc(thread, errorFunc, true, false, true);
174         [[maybe_unused]] bool status = JSObject::DefineOwnProperty(thread,
175                                                                    nativeInstanceObj, errorFunckey, errorFuncDesc);
176         ASSERT_PRINT(status == true, "return result exception!");
177     }
178 
179     JSHandle<EcmaString> handleStack = BuildEcmaStackTrace(thread);
180     JSHandle<JSTaggedValue> stackkey = globalConst->GetHandledStackString();
181     PropertyDescriptor stackDesc(thread, JSHandle<JSTaggedValue>::Cast(handleStack), true, false, true);
182     [[maybe_unused]] bool status = JSObject::DefineOwnProperty(thread, nativeInstanceObj, stackkey, stackDesc);
183     ASSERT_PRINT(status == true, "return result exception!");
184 
185     // 5. Return O.
186     return nativeInstanceObj.GetTaggedValue();
187 }
188 
DecodeFunctionName(const std::string & name)189 std::string ErrorHelper::DecodeFunctionName(const std::string &name)
190 {
191     if (name.empty()) {
192         return "anonymous";
193     }
194     return name;
195 }
196 
GetErrorJSFunction(JSThread * thread)197 JSHandle<JSTaggedValue> ErrorHelper::GetErrorJSFunction(JSThread *thread)
198 {
199     FrameHandler frameHandler(thread);
200     for (; frameHandler.HasFrame(); frameHandler.PrevJSFrame()) {
201         if (!frameHandler.IsJSFrame()) {
202             continue;
203         }
204 
205         auto function = frameHandler.GetFunction();
206         if (function.IsJSFunctionBase() || function.IsJSProxy()) {
207             Method *method = ECMAObject::Cast(function.GetTaggedObject())->GetCallTarget();
208             if (!method->IsNativeWithCallField()) {
209                 return JSHandle<JSTaggedValue>(thread, function);
210             }
211         }
212     }
213     return thread->GlobalConstants()->GetHandledUndefined();
214 }
215 
BuildEcmaStackTrace(JSThread * thread)216 JSHandle<EcmaString> ErrorHelper::BuildEcmaStackTrace(JSThread *thread)
217 {
218     std::string data = JsStackInfo::BuildJsStackTrace(thread, false);
219     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
220     LOG_ECMA(DEBUG) << data;
221     return factory->NewFromStdString(data);
222 }
223 }  // namespace panda::ecmascript::base
224