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_OHOS_ABILITYRUNTIME_JS_RUNTIME_H 17 #define FOUNDATION_OHOS_ABILITYRUNTIME_JS_RUNTIME_H 18 19 #include <cstdint> 20 #include <functional> 21 #include <memory> 22 #include <string> 23 #include <unordered_map> 24 #include <vector> 25 26 #include "native_engine/native_engine.h" 27 28 #include "runtime.h" 29 30 namespace OHOS { 31 namespace AppExecFwk { 32 class EventHandler; 33 } // namespace AppExecFwk 34 namespace AbilityRuntime { 35 class TimerTask; 36 class JsRuntime : public Runtime { 37 public: 38 static std::unique_ptr<Runtime> Create(const Options& options); 39 40 ~JsRuntime() override = default; 41 GetNativeEngine()42 NativeEngine& GetNativeEngine() const 43 { 44 return *nativeEngine_; 45 } 46 GetLanguage()47 Language GetLanguage() const override 48 { 49 return Language::JS; 50 } 51 52 std::unique_ptr<NativeReference> LoadModule(const std::string& moduleName, const std::string& modulePath); 53 std::unique_ptr<NativeReference> LoadSystemModule( 54 const std::string& moduleName, NativeValue* const* argv = nullptr, size_t argc = 0); 55 void PostTask(const TimerTask& task, const std::string& name, int64_t delayTime); 56 void RemoveTask(const std::string& name); 57 NativeValue* SetCallbackTimer(NativeEngine& engine, NativeCallbackInfo& info, bool isInterval); 58 NativeValue* ClearCallbackTimer(NativeEngine& engine, NativeCallbackInfo& info); 59 std::string BuildNativeAndJsBackStackTrace() override; 60 61 virtual bool RunScript(const std::string& path); 62 virtual bool RunSandboxScript(const std::string& path); 63 64 protected: 65 JsRuntime() = default; 66 67 virtual bool Initialize(const Options& options); 68 void Deinitialize(); 69 70 bool isArkEngine_ = false; 71 bool debugMode_ = false; 72 std::unique_ptr<NativeEngine> nativeEngine_; 73 std::string codePath_; 74 std::unique_ptr<NativeReference> methodRequireNapiRef_; 75 76 std::shared_ptr<AppExecFwk::EventHandler> eventHandler_; 77 uint32_t callbackId_ = 0; 78 79 std::unordered_map<std::string, NativeReference*> modules_; 80 }; 81 } // namespace AbilityRuntime 82 } // namespace OHOS 83 84 #endif // FOUNDATION_OHOS_ABILITYRUNTIME_JS_RUNTIME_H 85