1 /* 2 * Copyright (c) 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 OHOS_ABILITY_JS_ENVIRONMENT_JS_ENVIRONMENT_H 17 #define OHOS_ABILITY_JS_ENVIRONMENT_JS_ENVIRONMENT_H 18 19 #include <memory> 20 #include "ecmascript/napi/include/dfx_jsnapi.h" 21 #include "ecmascript/napi/include/jsnapi.h" 22 #include "js_environment_impl.h" 23 #include "native_engine/native_engine.h" 24 #include "source_map_operator.h" 25 #include "uncaught_exception_callback.h" 26 27 namespace OHOS { 28 namespace JsEnv { 29 class JsEnvironmentImpl; 30 class JsEnvironment final : public std::enable_shared_from_this<JsEnvironment> { 31 public: JsEnvironment()32 JsEnvironment() {} 33 explicit JsEnvironment(std::unique_ptr<JsEnvironmentImpl> impl); 34 ~JsEnvironment(); 35 36 enum class PROFILERTYPE { 37 PROFILERTYPE_CPU, 38 PROFILERTYPE_HEAP 39 }; 40 41 bool Initialize(const panda::RuntimeOption& pandaOption, void* jsEngine); 42 GetNativeEngine()43 NativeEngine* GetNativeEngine() const 44 { 45 return engine_; 46 } 47 GetVM()48 panda::ecmascript::EcmaVM* GetVM() const 49 { 50 return vm_; 51 } 52 53 void InitTimerModule(); 54 55 void InitWorkerModule(std::shared_ptr<WorkerInfo> workerInfo); 56 57 void InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperator> operatorObj); 58 59 void InitSyscapModule(); 60 61 void PostTask(const std::function<void()>& task, const std::string& name = "", int64_t delayTime = 0); 62 63 void PostSyncTask(const std::function<void()>& task, const std::string& name = ""); 64 65 void RemoveTask(const std::string& name); 66 67 void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo uncaughtExceptionInfo); 68 bool LoadScript(const std::string& path, std::vector<uint8_t>* buffer = nullptr, bool isBundle = false); 69 70 bool StartDebugger(const char* libraryPath, bool needBreakPoint, uint32_t instanceId); 71 72 void StopDebugger(); 73 74 void InitConsoleModule(); 75 76 bool InitLoop(); 77 78 void DeInitLoop(); 79 80 bool LoadScript(const std::string& path, uint8_t *buffer, size_t len, bool isBundle); 81 82 void StartProfiler(const char* libraryPath, uint32_t instanceId, PROFILERTYPE profiler, int32_t interval); 83 84 void ReInitJsEnvImpl(std::unique_ptr<JsEnvironmentImpl> impl); 85 86 void SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate>& moduleCheckerDelegate); 87 88 private: 89 std::unique_ptr<JsEnvironmentImpl> impl_ = nullptr; 90 NativeEngine* engine_ = nullptr; 91 panda::ecmascript::EcmaVM* vm_ = nullptr; 92 std::shared_ptr<SourceMapOperator> sourceMapOperator_ = nullptr; 93 }; 94 } // namespace JsEnv 95 } // namespace OHOS 96 #endif // OHOS_ABILITY_JS_ENVIRONMENT_JS_ENVIRONMENT_H 97