Skip to content

Commit 13cf8bc

Browse files
committed
ffi works on linux
1 parent 11e8b79 commit 13cf8bc

3 files changed

Lines changed: 64 additions & 11 deletions

File tree

lib/libprint.cpp renamed to lib/libtest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extern "C" {
1313
int test_iiiiii_i(int a, int b, int c, int d, int e, int f);
1414
bool test_ii_b(int a, int b);
1515
bool test_bb_b(bool a, bool b);
16+
const char* test_pp_p(char* a, char* b);
1617
}
1718

1819
void print() { printf("It's working!\n"); }
@@ -63,4 +64,9 @@ bool test_bb_b(bool a, bool b) {
6364
bool r = a == b;
6465
printf("Test (%s, %s) -> %s\n", a ? "true" : "false", b ? "true" : "false", r ? "true" : "false");
6566
return r;
67+
}
68+
69+
const char* test_pp_p(char* a, char* b) {
70+
printf("Test (%s, %s)\n", a, b);
71+
return "Hello from C!";
6672
}

makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ ifdef OS
2828
# Windows
2929
EXECUTE_FILE_TARGET := ${BINDIR}/executefile.exe
3030
TESTS_TARGET := ${BINDIR}/tests.exe
31-
LIB_TARGET := ${BINDIR}/libprint.dll
31+
LIB_TARGET := ${BINDIR}/libtest.dll
3232
else
3333
# Unix
3434
EXECUTE_FILE_TARGET := ${BINDIR}/executefile
3535
TESTS_TARGET := ${BINDIR}/tests
36-
LIB_TARGET := ${BINDIR}/libprint.so
36+
LIB_TARGET := ${BINDIR}/libtest.so
3737
endif
3838

3939
EXES := ${EXECUTE_FILE_TARGET} ${TESTS_TARGET}
4040

41-
Lib: lib/libprint.cpp | bin
41+
Lib: lib/libtest.cpp | bin
4242
$(CXX) $(CXXFLAGS) -shared -fPIC $(CPPFLAGS) -o ${LIB_TARGET} $<
4343

4444
BuildExecuteFile: $(OBJS) $(MAINDIR)/ExecuteFile.o | bin
@@ -67,7 +67,7 @@ else
6767
# Linux
6868
$(foreach file, $(OBJS), rm ${file};)
6969
$(foreach file, $(EXES), rm ${file};)
70-
rm bin/libprint.so
70+
rm bin/libtest.so
7171
rm src/mains/ExecuteFile.o
7272
rm src/mains/Tests.o
7373
endif

src/mains/Tests.cpp

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
throw std::runtime_error("Test failed!"); \
2020
}
2121

22+
#define EXPECT_FALSE(condition) \
23+
if ((condition)) { \
24+
std::cerr << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << " Expected condition to be false, but it was true." << std::endl; \
25+
throw std::runtime_error("Test failed!"); \
26+
}
27+
2228
#define EXPECT_TRUE_PRINT(condition, output) \
2329
if (!(condition)) { \
2430
std::cerr << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << " Expected condition to be true, but it was false." << std::endl; \
@@ -122,11 +128,11 @@ void testFile(std::string path){
122128
}
123129

124130
void testLibrary(){
125-
std::cout << "Testing library..." << std::endl;
131+
std::cout << "Testing ffi..." << std::endl;
126132

127133
ffi::ExternalFunctions externalFunctions;
128134

129-
auto test_ii_i = externalFunctions.add("print", "test_ii_i");
135+
auto test_ii_i = externalFunctions.add("test", "test_ii_i");
130136
{
131137
ffi::Arguments args;
132138
args.addDWord(5);
@@ -135,7 +141,7 @@ void testLibrary(){
135141
EXPECT_EQ(15, r);
136142
}
137143

138-
auto test_iii_i = externalFunctions.add("print", "test_iii_i");
144+
auto test_iii_i = externalFunctions.add("test", "test_iii_i");
139145
{
140146
ffi::Arguments args;
141147
args.addDWord(5);
@@ -145,7 +151,7 @@ void testLibrary(){
145151
EXPECT_EQ(30, r);
146152
}
147153

148-
auto test_iiii_i = externalFunctions.add("print", "test_iiii_i");
154+
auto test_iiii_i = externalFunctions.add("test", "test_iiii_i");
149155
{
150156
ffi::Arguments args;
151157
args.addDWord(5);
@@ -157,7 +163,7 @@ void testLibrary(){
157163
}
158164

159165

160-
auto test_iiiii_i = externalFunctions.add("print", "test_iiiii_i");
166+
auto test_iiiii_i = externalFunctions.add("test", "test_iiiii_i");
161167
{
162168
ffi::Arguments args;
163169
args.addDWord(5);
@@ -169,7 +175,7 @@ void testLibrary(){
169175
EXPECT_EQ(75, r);
170176
}
171177

172-
auto test_iiiiii_i = externalFunctions.add("print", "test_iiiiii_i");
178+
auto test_iiiiii_i = externalFunctions.add("test", "test_iiiiii_i");
173179
{
174180
ffi::Arguments args;
175181
args.addDWord(5);
@@ -182,7 +188,48 @@ void testLibrary(){
182188
EXPECT_EQ(105, r);
183189
}
184190

185-
std::cout << "[ OK ] Library test passed." << std::endl;
191+
auto test_ii_b = externalFunctions.add("test", "test_ii_b");
192+
{
193+
ffi::Arguments args;
194+
args.addDWord(5);
195+
args.addDWord(5);
196+
bool r = (bool)externalFunctions.call(test_ii_b, args);
197+
EXPECT_TRUE(r); // true
198+
}
199+
200+
{
201+
ffi::Arguments args;
202+
args.addDWord(5);
203+
args.addDWord(6);
204+
bool r = (bool)externalFunctions.call(test_ii_b, args);
205+
EXPECT_FALSE(r); // true
206+
}
207+
208+
auto test_bb_b = externalFunctions.add("test", "test_bb_b");
209+
{
210+
ffi::Arguments args;
211+
args.addWord(1); // true
212+
args.addWord(0); // false
213+
bool r = (bool)externalFunctions.call(test_bb_b, args);
214+
EXPECT_FALSE(r);
215+
}
216+
217+
auto test_pp_p = externalFunctions.add("test", "test_pp_p");
218+
{
219+
ffi::Arguments args;
220+
std::string a = "Hello";
221+
std::string b = "World";
222+
args.addQWord(reinterpret_cast<ffi::qword_t>(const_cast<char*>(a.c_str())));
223+
args.addQWord(reinterpret_cast<ffi::qword_t>(const_cast<char*>(b.c_str())));
224+
auto r = reinterpret_cast<char*>(externalFunctions.call(test_pp_p, args));
225+
// compare strings
226+
EXPECT_TRUE(r != nullptr);
227+
std::string result(r);
228+
std::string expected = "Hello from C!";
229+
EXPECT_EQ(expected, result);
230+
}
231+
232+
std::cout << "[ OK ] ffi test passed." << std::endl;
186233
}
187234

188235
void suiteTestfiles(){

0 commit comments

Comments
 (0)