1 /* 2 * Copyright (c) 2021-2023 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 FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_JSI_BASE_UTILS_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_JSI_BASE_UTILS_H 18 19 #include "base/log/ace_trace.h" 20 #include "base/log/event_report.h" 21 #include "base/log/exception_handler.h" 22 #include "bridge/js_frontend/engine/common/runtime_constants.h" 23 #include "bridge/js_frontend/engine/jsi/js_runtime.h" 24 #include "bridge/js_frontend/engine/jsi/js_value.h" 25 #include "bridge/js_frontend/engine/jsi/jsi_engine.h" 26 27 namespace OHOS::Ace::Framework { 28 using ErrorPos = std::pair<uint32_t, uint32_t>; 29 int32_t GetLineOffset(const AceType* data); 30 RefPtr<JsAcePage> GetRunningPage(const AceType* data); 31 napi_value AppDebugLogPrint(napi_env env, napi_callback_info info); 32 napi_value AppInfoLogPrint(napi_env env, napi_callback_info info); 33 napi_value AppWarnLogPrint(napi_env env, napi_callback_info info); 34 napi_value AppErrorLogPrint(napi_env env, napi_callback_info info); 35 int PrintLog(int id, int level, const char* tag, const char* fmt, const char* message); 36 37 class JsiBaseUtils { 38 public: 39 static void ReportJsErrorEvent( 40 std::shared_ptr<JsValue> error, std::shared_ptr<JsRuntime> runtime, const std::string& uniqueId); 41 static std::string TransSourceStack(RefPtr<JsAcePage> runningPage, const std::string& rawStack); 42 // native implementation for js function: console.debug() 43 static shared_ptr<JsValue> AppDebugLogPrint(const shared_ptr<JsRuntime>& runtime, 44 const shared_ptr<JsValue>& thisObj, const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 45 // native implementation for js function: console.info() 46 static shared_ptr<JsValue> AppInfoLogPrint(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj, 47 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 48 // native implementation for js function: console.warn() 49 static shared_ptr<JsValue> AppWarnLogPrint(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj, 50 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 51 // native implementation for js function: console.error() 52 static shared_ptr<JsValue> AppErrorLogPrint(const shared_ptr<JsRuntime>& runtime, 53 const shared_ptr<JsValue>& thisObj, const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 54 55 // native implementation for js function: aceConsole.debug() 56 static shared_ptr<JsValue> JsDebugLogPrint(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj, 57 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 58 // native implementation for js function: aceConsole.info() 59 static shared_ptr<JsValue> JsInfoLogPrint(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj, 60 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 61 // native implementation for js function: aceConsole.warn() 62 static shared_ptr<JsValue> JsWarnLogPrint(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj, 63 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 64 // native implementation for js function: aceConsole.error() 65 static shared_ptr<JsValue> JsErrorLogPrint(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj, 66 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 67 68 // native implementation for js function: aceTrace.begin() 69 static shared_ptr<JsValue> JsTraceBegin(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj, 70 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 71 // native implementation for js function: aceTrace.end() 72 static shared_ptr<JsValue> JsTraceEnd(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj, 73 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 74 static void GetStageSourceMap( 75 const AceType* data, std::unordered_map<std::string, RefPtr<RevSourceMap>>& sourceMaps); 76 static std::string TranslateStack(const std::string& stackStr, const std::string& pageUrl, 77 const RefPtr<RevSourceMap>& pageMap, const RefPtr<RevSourceMap>& appMap, const AceType* data = nullptr); 78 static std::string TranslateBySourceMap(const std::string& stackStr, const std::string& pageUrl, 79 const std::unordered_map<std::string, RefPtr<RevSourceMap>>& sourceMaps, const RefPtr<RevSourceMap>& appMap, 80 const AceType* data = nullptr); 81 82 private: 83 static std::string GenerateErrorMsg( 84 const std::shared_ptr<JsValue>& error, const std::shared_ptr<JsRuntime>& runtime); 85 static std::string GenerateSummaryBody( 86 const std::shared_ptr<JsValue>& error, const std::shared_ptr<JsRuntime>& runtime); 87 static JsErrorObject GenerateJsErrorObject( 88 const std::shared_ptr<JsValue>& error, const std::shared_ptr<JsRuntime>& runtime); 89 static ErrorPos GetErrorPos(const std::string& rawStack); 90 static std::string GetSourceCodeInfo( 91 std::shared_ptr<JsRuntime> runtime, const shared_ptr<JsValue>& errorFunc, ErrorPos pos); 92 static std::string TranslateRawStack(const std::string& rawStackStr); 93 static void ExtractEachInfo(const std::string& tempStack, std::vector<std::string>& res); 94 static void GetPosInfo(const std::string& temp, int32_t start, std::string& line, std::string& column); 95 static std::string GetSourceInfo(const std::string& line, const std::string& column, 96 const RefPtr<RevSourceMap>& pageMap, const RefPtr<RevSourceMap>& appMap, bool isAppPage, const AceType* data, 97 const bool isBundle = true); 98 static std::string GetRelativePath(const std::string& sources, std::string splitStr = "/\\"); 99 100 // native aceTraceObjects 101 static thread_local std::stack<std::unique_ptr<AceScopedTrace>> aceScopedTrace_; 102 }; 103 } // namespace OHOS::Ace::Framework 104 105 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_JSI_BASE_UTILS_H 106