1 /*
2 * Copyright (c) 2021-2022 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 #ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_V8_V8_UTILS_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_V8_V8_UTILS_H
18
19 #include <sstream>
20
21 #include "third_party/v8/include/v8.h"
22
23 #include "base/log/log.h"
24 #include "frameworks/bridge/common/utils/source_map.h"
25
26 namespace OHOS::Ace::Framework {
27 namespace V8Utils {
28
29 class ScopedString {
30 public:
ScopedString(v8::Local<v8::Value> str)31 explicit ScopedString(v8::Local<v8::Value> str)
32 {
33 auto isolate = v8::Isolate::GetCurrent();
34 v8::String::Utf8Value s(isolate, str);
35 str_ = *s;
36 }
37
38 ~ScopedString() = default;
39
get()40 const char* get()
41 {
42 return str_.c_str();
43 }
44
str()45 std::string str()
46 {
47 return str_;
48 }
49
string()50 operator std::string()
51 {
52 return str_;
53 }
54
55 operator const char*()
56 {
57 return str_.c_str();
58 }
59
60 private:
61 std::string str_;
62 };
63
64 std::vector<std::string> GetObjectKeys(v8::Local<v8::Object> obj);
65 void JsStdDumpErrorAce(v8::Isolate* isolate, const v8::TryCatch* tryCatch);
66 std::string JsStdDumpSourceFile(const std::string& tempStack, const RefPtr<RevSourceMap>& pageMap,
67 const RefPtr<RevSourceMap>& appMap);
68
69 void ExtractEachInfo(const std::string& tempStack, std::vector<std::string>& res);
70 void GetPosInfo(const std::string& temp, std::string& line, std::string& column);
71 std::string GetSourceInfo(const std::string& line, const std::string& column, const RefPtr<RevSourceMap>& pageMap,
72 const RefPtr<RevSourceMap>& appMap, bool isAppPage);
73
74 // snprintf without a string literal (-Wformat-security)
75 template<typename... Args>
ThrowException(Args...args)76 v8::Local<v8::Value> ThrowException(Args... args)
77 {
78 v8::Isolate* isolate = v8::Isolate::GetCurrent();
79 std::stringstream ss;
80 (ss << ... << args);
81 return isolate->ThrowException(v8::String::NewFromUtf8(isolate, ss.str().c_str()).ToLocalChecked());
82 }
83
84 std::string ValueTypeAsString(v8::Local<v8::Value> val);
85
86 } // namespace V8Utils
87 } // namespace OHOS::Ace::Framework
88
89 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_V8_V8_UTILS_H
90