1 /* 2 * Copyright (c) 2025 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 #include "ark_native_engine.h" 17 #include "napi/native_api.h" 18 #include "napi/native_common.h" 19 #include "napi/native_node_api.h" 20 #include "native_create_env.h" 21 22 23 class NativeEngineMock { 24 public: NativeEngineMock()25 NativeEngineMock() 26 { 27 panda::RuntimeOption option; 28 option.SetGcType(panda::RuntimeOption::GC_TYPE::GEN_GC); 29 const int64_t poolSize = 0x1000000; // 16M 30 option.SetGcPoolSize(poolSize); 31 option.SetLogLevel(panda::RuntimeOption::LOG_LEVEL::ERROR); 32 option.SetDebuggerLibraryPath(""); 33 vm_ = panda::JSNApi::CreateJSVM(option); 34 if (vm_ == nullptr) { 35 return; 36 } 37 38 engine_ = new ArkNativeEngine(vm_, nullptr); 39 napi_open_handle_scope(reinterpret_cast<napi_env>(engine_), &scope_); 40 } 41 ~NativeEngineMock()42 ~NativeEngineMock() 43 { 44 napi_close_handle_scope(reinterpret_cast<napi_env>(engine_), scope_); 45 scope_ = nullptr; 46 delete engine_; 47 engine_ = nullptr; 48 panda::JSNApi::DestroyJSVM(vm_); 49 vm_ = nullptr; 50 } 51 operator ->() const52 inline ArkNativeEngine* operator->() const 53 { 54 return engine_; 55 } 56 operator napi_env() const57 inline operator napi_env() const 58 { 59 return reinterpret_cast<napi_env>(engine_); 60 } 61 62 private: 63 EcmaVM* vm_ {nullptr}; 64 ArkNativeEngine* engine_ {nullptr}; 65 napi_handle_scope scope_ = nullptr; 66 };