From 3b7593cc12fdca0c6e42dccc30fd4c89faa1be39 Mon Sep 17 00:00:00 2001 From: bloodstalker Date: Tue, 23 Jan 2018 01:52:18 +0330 Subject: fixed some issues with the way bruiser handled integers, added some initial tests. --- bruiser/bruiser.cpp | 21 +++++++++++++-------- bruiser/bruiserffi.c | 22 ++++++++++++++++------ bruiser/bruiserffi.h | 2 ++ bruiser/lua-scripts/demo1.lua | 22 ++++++++++++++++++++++ 4 files changed, 53 insertions(+), 14 deletions(-) (limited to 'bruiser') diff --git a/bruiser/bruiser.cpp b/bruiser/bruiser.cpp index ce0d6ec..1eff772 100644 --- a/bruiser/bruiser.cpp +++ b/bruiser/bruiser.cpp @@ -1303,7 +1303,6 @@ class LuaWrapper std::list floats; std::list doubles; void* values[argc]; - int numbers[argc]; int table_length_5 = lua_rawlen(__ls, 5); if (!lua_checkstack(__ls, 10)) { @@ -1320,12 +1319,18 @@ class LuaWrapper std::cout << CYAN << "table_length: " << table_length_5 << NORMAL << "\n"; for (int i = 1; i <= table_length_5; ++i) { lua_rawgeti(__ls, 5, i); - numbers[i-1] = lua_tointeger(__ls, i + numargs + argc); if (lua_type(__ls, i) == LUA_TBOOLEAN) {} else if (lua_type(__ls, i) == LUA_TLIGHTUSERDATA) {} else if (lua_type(__ls, i+numargs+argc) == LUA_TNUMBER) { - uints.push_back(lua_tointeger(__ls, i + numargs + argc)); - values[i-1]=&uints.back(); + double dummy = lua_tonumber(__ls, i + numargs + argc); + if (dummy == (long long int)dummy) { // FIXME + uints.push_back(lua_tointeger(__ls, i + numargs + argc)); + values[i-1]=&uints.back(); + } // int + else { + doubles.push_back(lua_tonumber(__ls, i + numargs + argc)); + values[i-1]=&doubles.back(); + } // float } else if (lua_type(__ls, i) == LUA_TSTRING) {} else if (lua_type(__ls, i) == LUA_TTABLE) {} @@ -1339,8 +1344,8 @@ class LuaWrapper if (x_ptr != nullptr) { std::cout << "calling xobj named " << GREEN << executioner.getvptrbyindex(x_index).second << NORMAL << "\n"; result = ffi_callX(argc, args, ret_type, x_ptr, values); - std::cout << reinterpret_cast(result) << "\n"; - lua_pushinteger(__ls, ffi_reinterpret_uint32_t(result)); + if (result == nullptr) {PRINT_WITH_COLOR_LB(RED, "ffi_callX returned null.");return 0;} + if (std::strcmp(ffi_ret_type_string.c_str(), "void") == 0) {return 0;} else if (std::strcmp(ffi_ret_type_string.c_str(), "uint8") == 0) {lua_pushinteger(__ls, ffi_reinterpret_uint8_t(result));} else if (std::strcmp(ffi_ret_type_string.c_str(), "sint8") == 0) {lua_pushinteger(__ls, ffi_reinterpret_int8_t(result));} @@ -1350,8 +1355,8 @@ class LuaWrapper else if (std::strcmp(ffi_ret_type_string.c_str(), "sint32") == 0) {lua_pushinteger(__ls, ffi_reinterpret_int32_t(result));} else if (std::strcmp(ffi_ret_type_string.c_str(), "uint64") == 0) {lua_pushinteger(__ls, ffi_reinterpret_uint64_t(result));} else if (std::strcmp(ffi_ret_type_string.c_str(), "sint64") == 0) {lua_pushinteger(__ls, ffi_reinterpret_int64_t(result));} - else if (std::strcmp(ffi_ret_type_string.c_str(), "float") == 0) {} - else if (std::strcmp(ffi_ret_type_string.c_str(), "double") == 0) {} + else if (std::strcmp(ffi_ret_type_string.c_str(), "float") == 0) {lua_pushnumber(__ls, ffi_reinterpret_float(result));} + else if (std::strcmp(ffi_ret_type_string.c_str(), "double") == 0) {lua_pushnumber(__ls, ffi_reinterpret_double(result));} else if (std::strcmp(ffi_ret_type_string.c_str(), "pointer") == 0) {lua_pushinteger(__ls, ffi_reinterpret_uintptr_t(result));} else if (std::strcmp(ffi_ret_type_string.c_str(), "struct") == 0) {} else {PRINT_WITH_COLOR_LB(RED, "unknown return type string.");return 0;} diff --git a/bruiser/bruiserffi.c b/bruiser/bruiserffi.c index 6be7376..3594af7 100644 --- a/bruiser/bruiserffi.c +++ b/bruiser/bruiserffi.c @@ -30,6 +30,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.* /**********************************************************************************************************************/ #define VOIDIFY(X) (void*)X /**********************************************************************************************************************/ +#pragma GCC diagnostic ignored "-Wpointer-to-int-cast" +#pragma GCC diagnostic push #define REINTERPRET_GENERATOR(X) \ X ffi_reinterpret_##X(void* result) {return (X)result;} @@ -51,6 +53,8 @@ X_LIST_GEN #undef X #undef X_LIST_GEN #undef REINTERPRET_GENERATOR +float ffi_reinterpret_float(void* result) {return *(float*)&result;} +double ffi_reinterpret_double(void* result) {return *(double*)&result;} void ffi_value_ctor(void** ret, int argc, ...) { va_list value_list; @@ -160,33 +164,39 @@ void* ffi_callX(int argc, const char** arg_string, ffi_type rtype, void* x_ptr, return ret; } -void* ffi_callX_var(int argc, const char** arg_string, ffi_type rtype, void* x_ptr, void** values) {} +void* ffi_callX_var(int argc, const char** arg_string, ffi_type rtype, void* x_ptr, void** values) {return NULL;} +/**********************************************************************************************************************/ /**********************************************************************************************************************/ // @DEVI-the following lines are only meant for testing. uint32_t add2(uint32_t a, uint32_t b) {return a+b;} uint32_t sub2(uint32_t a, uint32_t b) {return a-b;} +double addd(double a, double b) {return a+b;} #pragma weak main int main(int argc, char** argv) { - // @DEVI-we get these from lua void* padd = &add2; void* psub = &sub2; - // @DEVI-user input from lua + void* padd2 = &addd; int argcount = 2; - // @DEVI-we get these from user in lua ffi_type ret_type = ffi_type_uint32; - // @DEVI-we get these from user in lua const char* args[] = {"uint32", "uint32"}; const char* ret_string = "uint32"; uint32_t a = 30; uint32_t b = 20; void* values[2]; - // @DEVI-we get thsese from the user in lua ffi_value_ctor(values, 2, "uint32", &a, "uint32", &b); void* result = ffi_callX(argcount, args, ret_type, padd, values); fprintf(stdout, "result of callling add is %d\n", (uint32_t)result); result = ffi_callX(argcount, args, ret_type, psub, values); fprintf(stdout, "result of calling sub is %d\n", (uint32_t)result); + + ret_type = ffi_type_double; + double c = 111.111; + double d = 111.111; + const char* args2[] = {"double", "double"}; + void* values2[] = {&c, &d}; + result = ffi_callX(argcount, args2, ret_type, padd2, values2); + fprintf(stdout, "result of calling addd is %f\n", ffi_reinterpret_double(result)); return 0; } /**********************************************************************************************************************/ diff --git a/bruiser/bruiserffi.h b/bruiser/bruiserffi.h index f9abdd5..e9b6b27 100644 --- a/bruiser/bruiserffi.h +++ b/bruiser/bruiserffi.h @@ -49,6 +49,8 @@ X_LIST_GEN #undef X #undef X_LIST_GEN #undef REINTERPRET_GENERATOR +float ffi_reinterpret_float(void* result); +double ffi_reinterpret_double(void* result); /** * @brief constructs the arguments to be passed to ffi_call. diff --git a/bruiser/lua-scripts/demo1.lua b/bruiser/lua-scripts/demo1.lua index c5c9d27..75778d2 100644 --- a/bruiser/lua-scripts/demo1.lua +++ b/bruiser/lua-scripts/demo1.lua @@ -149,16 +149,38 @@ function main() local add2_code = codeTableByName_number("'add2'") local sub2_code = codeTableByName_number("'sub2'") + local adddouble_code = codeTableByName_number("'adddouble'") + local subdouble_code = codeTableByName_number("'subdouble'") + local triple_code = codeTableByName_number("'triple'") printFuncSizes() pwd() xobjregister(add2_code, "add2") xobjregister(sub2_code, "sub2") + xobjregister(adddouble_code, "adddouble") + xobjregister(subdouble_code, "subdouble") + xobjregister(triple_code, "triple") a=xcall(2,{"uint32","uint32"},"uint32",0, {30,20}) print("call add result", a) a=xcall(2,{"uint32", "uint32"},"uint32",1, {30,20}) print("call sub result", a) + + arg1 = 100 + arg2 = 200 + a=xcall(2,{"sint32", "sint32"},"sint32",1, {arg1,arg2}) + print("xcall returned:",a) + if a ~= -100 then print("test failed") end + a=xcall(2,{"double", "double"},"double",2, {333.333,222.222}) + print("xcall returned:",a) + -- FIXME + if a ~= 555.555 then print("test failed") end + a=xcall(2,{"double", "double"},"double",3, {333.333,222.222}) + print("xcall returned:",a) + if a ~= 111.111 then print("test failed") end + a=xcall(3,{"double", "double", "double"},"double",4, {333.333,222.222,111.111}) + print("xcall returned:",a) + if a ~= 666.666 then print("test failed") end end main() -- cgit v1.2.3