Skip to content

Commit 7709586

Browse files
committed
added a test case for inter-module exchange of bound types
1 parent 6459c41 commit 7709586

7 files changed

+67
-0
lines changed

Diff for: .gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ libnanobind-abi3.so
66
nanobind.dll
77
nanobind-abi3.dll
88

9+
libinter_module.dylib
10+
libinter_module.so
11+
inter_module.dll
12+
913
/.ninja_deps
1014
/.ninja_log
1115
/build.ninja

Diff for: tests/CMakeLists.txt

+16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ nanobind_add_module(test_intrusive_ext test_intrusive.cpp object.cpp object.h ${
1919
nanobind_add_module(test_exception_ext test_exception.cpp object.cpp object.h ${NB_EXTRA_ARGS})
2020
nanobind_add_module(test_make_iterator_ext test_make_iterator.cpp ${NB_EXTRA_ARGS})
2121

22+
add_library(
23+
inter_module
24+
SHARED
25+
inter_module.h
26+
inter_module.cpp
27+
)
28+
target_compile_definitions(inter_module PRIVATE -DSHARED_BUILD)
29+
nanobind_cpp17(inter_module)
30+
nanobind_headers(inter_module)
31+
32+
nanobind_add_module(test_inter_module_1_ext test_inter_module_1.cpp ${NB_EXTRA_ARGS})
33+
nanobind_add_module(test_inter_module_2_ext test_inter_module_2.cpp ${NB_EXTRA_ARGS})
34+
target_link_libraries(test_inter_module_1_ext PRIVATE inter_module)
35+
target_link_libraries(test_inter_module_2_ext PRIVATE inter_module)
36+
2237
set(TEST_FILES
2338
test_functions.py
2439
test_classes.py
@@ -29,6 +44,7 @@ set(TEST_FILES
2944
test_intrusive.py
3045
test_exception.py
3146
test_make_iterator.py
47+
test_inter_module.py
3248
)
3349

3450
if (NOT (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR) OR MSVC)

Diff for: tests/inter_module.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "inter_module.h"
2+
3+
Shared create_shared() {
4+
return { 123 };
5+
}
6+
7+
bool check_shared(const Shared &shared) {
8+
return shared.value == 123;
9+
}

Diff for: tests/inter_module.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <nanobind/nb_defs.h>
2+
3+
#if defined(SHARED_BUILD)
4+
# define EXPORT_SHARED NB_EXPORT
5+
#else
6+
# define EXPORT_SHARED NB_IMPORT
7+
#endif
8+
9+
struct EXPORT_SHARED Shared {
10+
int value;
11+
};
12+
13+
extern EXPORT_SHARED Shared create_shared();
14+
extern EXPORT_SHARED bool check_shared(const Shared &shared);

Diff for: tests/test_inter_module.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import test_inter_module_1_ext as t1
2+
import test_inter_module_2_ext as t2
3+
4+
5+
def test01_inter_module():
6+
s = t1.create_shared()
7+
assert t2.check_shared(s)

Diff for: tests/test_inter_module_1.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <nanobind/nanobind.h>
2+
#include "inter_module.h"
3+
4+
namespace nb = nanobind;
5+
6+
NB_MODULE(test_inter_module_1_ext, m) {
7+
m.def("create_shared", &create_shared);
8+
}

Diff for: tests/test_inter_module_2.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <nanobind/nanobind.h>
2+
#include "inter_module.h"
3+
4+
namespace nb = nanobind;
5+
6+
NB_MODULE(test_inter_module_2_ext, m) {
7+
nb::class_<Shared>(m, "Shared");
8+
m.def("check_shared", &check_shared);
9+
}

0 commit comments

Comments
 (0)