diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index dd5f9416acf00c738eb9a7ebaff9d86f705e2b32..0ac091c44fbbb44b93307dcb48481145db111607 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -90,6 +90,7 @@ set(SOURCES mem/panda_string.cpp mem/memory_manager.cpp mem/heap_space.cpp + native_ffi/native_invoke_interface.cpp methodtrace/trace.cpp mark_word.cpp method.cpp diff --git a/runtime/include/native_ffi/native_invoke_interface.h b/runtime/include/native_ffi/native_invoke_interface.h index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..dc6b68f804eeb917a264a5953d5f60928755c282 100644 --- a/runtime/include/native_ffi/native_invoke_interface.h +++ b/runtime/include/native_ffi/native_invoke_interface.h @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef PANDA_RUNTIME_NATIVE_FFI_NATIVE_INVOKE_INTERFACE_ +#define PANDA_RUNTIME_NATIVE_FFI_NATIVE_INVOKE_INTERFACE_ + +#include "include/runtime_options.h" +#include "runtime/include/panda_vm.h" + +/* + * PandaVM* CreatePandaVM(context); + * bool DestroyPandaVM(PandaVM*); + * + * ReturnType InvokePandaMethod(??); + */ + +namespace panda::ffi { + +PandaVM *CreatePandaVM(const RuntimeOptions &options); +bool DestroyPandaVM(PandaVM *vm_ptr); + +template +inline T InvokePandaMethod() +{ + // TODO(m.strizhak):implement it + if constexpr(std::is_same::value) { + return; + } + + T a; + return a; +} + +} // namespace panda::ffi + +#endif // PANDA_RUNTIME_NATIVE_FFI_NATIVE_INVOKE_INTERFACE_ \ No newline at end of file diff --git a/runtime/native_ffi/native_invoke_interface.cpp b/runtime/native_ffi/native_invoke_interface.cpp index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..bb673b0ca0a9fe29ad2283fb66702d9810243458 100644 --- a/runtime/native_ffi/native_invoke_interface.cpp +++ b/runtime/native_ffi/native_invoke_interface.cpp @@ -0,0 +1,51 @@ +/** + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "macros.h" +#include "runtime/include/runtime_options.h" +#include "runtime/include/panda_vm.h" +#include "runtime/include/runtime.h" + +namespace panda::ffi { + +PandaVM *CreatePandaVM(const RuntimeOptions &options) +{ + if(!Runtime::Create(options)) { + LOG(ERROR, INTEROP) << "Error: cannot create runtime"; + return nullptr; + } + + ASSERT(Runtime::GetCurrent() != nullptr); + PandaVM* vm_p = PandaVM::GetCurrent(); + ASSERT(vm_p != nullptr); + + return vm_p; +} + +bool DestroyPandaVM(PandaVM *vm_ptr) +{ + if(UNLIKELY(vm_ptr == nullptr)) { + LOG(ERROR, INTEROP) << "Error: cannot destroy vm. VM is null"; + return false; + } + + if(!Runtime::GetCurrent()->Destroy()) { + LOG(ERROR, INTEROP) << "Error: cannot destroy runtime"; + return false; + } + return true; +} + +} // namespace panda::ffi diff --git a/runtime/tests/native_ffi/CMakeLists.txt b/runtime/tests/native_ffi/CMakeLists.txt index 3f73392068c28b34cb06c443897631a9788a0f2b..6532220da6d4e6b1fe3cdf9dc0b457183e78cdf3 100644 --- a/runtime/tests/native_ffi/CMakeLists.txt +++ b/runtime/tests/native_ffi/CMakeLists.txt @@ -11,4 +11,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -cmake_minimum_required(VERSION 3.10) \ No newline at end of file +cmake_minimum_required(VERSION 3.10) + +add_gtests(native_ffi_invoke_interface_test native_ffi_invoke_interface_test.cpp) \ No newline at end of file diff --git a/runtime/tests/native_ffi/native_ffi_invoke_interface_test.cpp b/runtime/tests/native_ffi/native_ffi_invoke_interface_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d6863ecc43ab9e184af4a59742829e4d1ed9f50c --- /dev/null +++ b/runtime/tests/native_ffi/native_ffi_invoke_interface_test.cpp @@ -0,0 +1,63 @@ +/** + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "generated/base_options.h" +#include "runtime/include/managed_thread.h" +#include "runtime/include/runtime_options.h" +#include "runtime/include/native_ffi/native_invoke_interface.h" + +namespace panda::test { + +static inline std::string Separator() +{ +#ifdef _WIN32 + return "\\"; +#else + return "/"; +#endif +} + +class FFINativeInvokeTest : public testing::Test { +public: + void SetUp() override + { + Logger::Initialize(base_options::Options("")); + + options.SetBootClassSpaces({"core"}); + options.SetRuntimeType("core"); + options.SetRunGcInPlace(true); + options.SetVerifyCallStack(false); + auto exec_path = panda::os::file::File::GetExecutablePath(); + std::string panda_std_lib = + exec_path.Value() + Separator() + ".." + Separator() + "pandastdlib" + Separator() + "arkstdlib.abc"; + options.SetBootPandaFiles({panda_std_lib}); + } + +protected: + RuntimeOptions options; +}; + +TEST_F(FFINativeInvokeTest, CreateVMTest) +{ + ASSERT_NE(ffi::CreatePandaVM(options), nullptr); + ASSERT_NE(PandaVM::GetCurrent(), nullptr); + ASSERT_NE(Runtime::GetCurrent(), nullptr); + ASSERT_NE(ManagedThread::GetCurrent(), nullptr); + ASSERT_TRUE(ffi::DestroyPandaVM(PandaVM::GetCurrent())); +} + +} // namespace panda::test \ No newline at end of file