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