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(std::shared_ptr<JsValue> error, std::shared_ptr<JsRuntime> runtime); 40 static std::string TransSourceStack(RefPtr<JsAcePage> runningPage, const std::string& rawStack); 41 // native implementation for js function: console.debug() 42 static shared_ptr<JsValue> AppDebugLogPrint(const shared_ptr<JsRuntime>& runtime, 43 const shared_ptr<JsValue>& thisObj, const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 44 // native implementation for js function: console.info() 45 static shared_ptr<JsValue> AppInfoLogPrint(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj, 46 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 47 // native implementation for js function: console.warn() 48 static shared_ptr<JsValue> AppWarnLogPrint(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj, 49 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 50 // native implementation for js function: console.error() 51 static shared_ptr<JsValue> AppErrorLogPrint(const shared_ptr<JsRuntime>& runtime, 52 const shared_ptr<JsValue>& thisObj, const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 53 54 // native implementation for js function: aceConsole.debug() 55 static shared_ptr<JsValue> JsDebugLogPrint(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj, 56 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 57 // native implementation for js function: aceConsole.info() 58 static shared_ptr<JsValue> JsInfoLogPrint(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj, 59 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 60 // native implementation for js function: aceConsole.warn() 61 static shared_ptr<JsValue> JsWarnLogPrint(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj, 62 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 63 // native implementation for js function: aceConsole.error() 64 static shared_ptr<JsValue> JsErrorLogPrint(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj, 65 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 66 67 // native implementation for js function: aceTrace.begin() 68 static shared_ptr<JsValue> JsTraceBegin(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj, 69 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 70 // native implementation for js function: aceTrace.end() 71 static shared_ptr<JsValue> JsTraceEnd(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& thisObj, 72 const std::vector<shared_ptr<JsValue>>& argv, int32_t argc); 73 static void GetStageSourceMap( 74 const AceType* data, std::unordered_map<std::string, RefPtr<RevSourceMap>>& sourceMaps); 75 static std::string TranslateStack(const std::string& stackStr, const std::string& pageUrl, 76 const RefPtr<RevSourceMap>& pageMap, const RefPtr<RevSourceMap>& appMap, const AceType* data = nullptr); 77 static std::string TranslateBySourceMap(const std::string& stackStr, const std::string& pageUrl, 78 const std::unordered_map<std::string, RefPtr<RevSourceMap>>& sourceMaps, const RefPtr<RevSourceMap>& appMap, 79 const AceType* data = nullptr); 80 81 private: 82 static std::string GenerateErrorMsg( 83 const std::shared_ptr<JsValue>& error, const std::shared_ptr<JsRuntime>& runtime); 84 static std::string GenerateSummaryBody( 85 const std::shared_ptr<JsValue>& error, const std::shared_ptr<JsRuntime>& runtime); 86 static JsErrorObject GenerateJsErrorObject( 87 const std::shared_ptr<JsValue>& error, const std::shared_ptr<JsRuntime>& runtime); 88 static ErrorPos GetErrorPos(const std::string& rawStack); 89 static std::string GetSourceCodeInfo( 90 std::shared_ptr<JsRuntime> runtime, const shared_ptr<JsValue>& errorFunc, ErrorPos pos); 91 static std::string TranslateRawStack(const std::string& rawStackStr); 92 static void ExtractEachInfo(const std::string& tempStack, std::vector<std::string>& res); 93 static void GetPosInfo(const std::string& temp, int32_t start, std::string& line, std::string& column); 94 static std::string GetSourceInfo(const std::string& line, const std::string& column, 95 const RefPtr<RevSourceMap>& pageMap, const RefPtr<RevSourceMap>& appMap, bool isAppPage, const AceType* data, 96 const bool isBundle = true); 97 static std::string GetRelativePath(const std::string& sources, std::string splitStr = "/\\"); 98 99 // native aceTraceObjects 100 static thread_local std::stack<std::unique_ptr<AceScopedTrace>> aceScopedTrace_; 101 }; 102 } // namespace OHOS::Ace::Framework 103 104 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_JSI_BASE_UTILS_H 105