• 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         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             default:
122                 errorKey = globalConst->GetHandledErrorString();
123                 break;
124         }
125         return 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                                                   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     // InstallErrorCause
170     JSHandle<JSTaggedValue> options = BuiltinsBase::GetCallArg(argv, 1);
171     // If options is an Object and ? HasProperty(options, "cause") is true, then
172     //   a. Let cause be ? Get(options, "cause").
173     //   b. Perform CreateNonEnumerableDataPropertyOrThrow(O, "cause", cause).
174     if (options->IsECMAObject()) {
175         JSHandle<JSTaggedValue> causeKey = globalConst->GetHandledCauseString();
176         bool causePresent = JSTaggedValue::HasProperty(thread, options, causeKey);
177         RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
178         if (causePresent) {
179             JSHandle<JSTaggedValue> cause = JSObject::GetProperty(thread, options, causeKey).GetValue();
180             RETURN_EXCEPTION_IF_ABRUPT_COMPLETION(thread);
181             PropertyDescriptor causeDesc(thread, cause, true, false, true);
182             [[maybe_unused]] bool status = JSObject::DefineOwnProperty(thread, nativeInstanceObj, causeKey, causeDesc);
183             ASSERT_PRINT(status == true, "return result exception!");
184         }
185     }
186     JSHandle<JSTaggedValue> errorFunc = GetErrorJSFunction(thread);
187     if (!errorFunc->IsUndefined()) {
188         JSHandle<JSTaggedValue> errorFunckey = globalConst->GetHandledErrorFuncString();
189         PropertyDescriptor errorFuncDesc(thread, errorFunc, true, false, true);
190         [[maybe_unused]] bool status = JSObject::DefineOwnProperty(thread,
191                                                                    nativeInstanceObj, errorFunckey, errorFuncDesc);
192         ASSERT_PRINT(status == true, "return result exception!");
193     }
194 
195     bool isOOMError = errorType == ErrorType::OOM_ERROR;
196     JSHandle<EcmaString> handleStack = BuildEcmaStackTrace(thread, isOOMError);
197     JSHandle<JSTaggedValue> stackkey = globalConst->GetHandledStackString();
198     PropertyDescriptor stackDesc(thread, JSHandle<JSTaggedValue>::Cast(handleStack), true, false, true);
199     [[maybe_unused]] bool status = JSObject::DefineOwnProperty(thread, nativeInstanceObj, stackkey, stackDesc);
200     ASSERT_PRINT(status == true, "return result exception!");
201 
202     // 5. Return O.
203     return nativeInstanceObj.GetTaggedValue();
204 }
205 
GetErrorJSFunction(JSThread * thread)206 JSHandle<JSTaggedValue> ErrorHelper::GetErrorJSFunction(JSThread *thread)
207 {
208     FrameHandler frameHandler(thread);
209     for (; frameHandler.HasFrame(); frameHandler.PrevJSFrame()) {
210         if (!frameHandler.IsJSFrame()) {
211             continue;
212         }
213 
214         auto function = frameHandler.GetFunction();
215         if (function.IsJSFunctionBase() || function.IsJSProxy()) {
216             Method *method = ECMAObject::Cast(function.GetTaggedObject())->GetCallTarget();
217             if (!method->IsNativeWithCallField()) {
218                 return JSHandle<JSTaggedValue>(thread, function);
219             }
220         }
221     }
222     return thread->GlobalConstants()->GetHandledUndefined();
223 }
224 
BuildEcmaStackTrace(JSThread * thread,bool isOOMError)225 JSHandle<EcmaString> ErrorHelper::BuildEcmaStackTrace(JSThread *thread, bool isOOMError)
226 {
227     std::string data = JsStackInfo::BuildJsStackTrace(thread, false);
228     if (isOOMError) {
229         data = data.substr(0, MAX_ERROR_SIZE);
230     }
231     ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
232     LOG_ECMA(DEBUG) << data;
233     return factory->NewFromStdString(data);
234 }
235 }  // namespace panda::ecmascript::base
236