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/ecma_exceptions.h"
17 #include "ecmascript/base/error_helper.h"
18 #include "ecmascript/js_function.h"
19 #include "ecmascript/js_object.h"
20 #include "ecmascript/js_thread.h"
21 #include "ecmascript/object_factory.h"
22
23 namespace panda::ecmascript {
SetException(JSThread * thread,JSObject * error)24 void SetException(JSThread *thread, JSObject *error)
25 {
26 if (!thread->HasPendingException()) {
27 thread->SetException(JSTaggedValue(error));
28 }
29 }
30
ThrowException(JSThread * thread,const char * name,const char * msg)31 void ThrowException(JSThread *thread, const char *name, const char *msg)
32 {
33 auto jsThread = static_cast<JSThread *>(thread);
34 ObjectFactory *factory = jsThread->GetEcmaVM()->GetFactory();
35 if (std::strcmp(name, REFERENCE_ERROR_STRING) == 0) {
36 SetException(jsThread, *factory->GetJSError(base::ErrorType::REFERENCE_ERROR, msg));
37 return;
38 }
39
40 if (std::strcmp(name, TYPE_ERROR_STRING) == 0) {
41 SetException(jsThread, *factory->GetJSError(base::ErrorType::TYPE_ERROR, msg));
42 return;
43 }
44 }
45 } // namespace panda::ecmascript
46