Skip to content

Commit

Permalink
update cmake
Browse files Browse the repository at this point in the history
and minor changes
  • Loading branch information
Vipul-Cariappa committed Jan 3, 2025
1 parent f809169 commit 39a447d
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 77 deletions.
10 changes: 8 additions & 2 deletions .github/test.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mv bin/libpylua.so tests/pylua.so
mv bin/libpylua.so tests/pylua.pyd
cp bin/libpylua.so tests/pylua.so
cp bin/libpylua.so tests/pylua.pyd
cd tests

FILE=luaunit.lua
Expand All @@ -19,25 +19,30 @@ printf "Running Lua Side Tests\n"
printf "\nTest Case 1\n"
if ! lua test0.lua; then
echo "Error"
exit -1
fi

printf "\nTest Case 2\n"
if ! lua test1.lua; then
echo "Error"
exit -1
fi

printf "\nTest Case 3\n"
if ! lua test2.lua; then
echo "Error"
exit -1
fi

printf "\nTest Case 4\n"
if ! lua test3.lua; then
echo "Error"
exit -1
fi

printf "\nTest Case 5\n"
if ! lua test4.lua; then
echo "Error"
exit -1
fi

Expand All @@ -46,6 +51,7 @@ printf "\n\nRunning Python Side Tests\n"

printf "\nTest Case 1\n"
if ! python3 testA.py; then
echo "Error"
exit -1
fi

Expand Down
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
cmake_minimum_required (VERSION 3.8)
cmake_minimum_required (VERSION 3.10)

project(pylua)

find_package(PkgConfig REQUIRED)

Expand All @@ -13,7 +15,7 @@ add_library(pylua SHARED src/convert.c src/py_lua.c src/lua_py.c)
target_include_directories(pylua PRIVATE "src/")

target_include_directories(pylua PRIVATE ${PYTHON_INCLUDE_DIRS})
target_link_libraries(pylua ${PYTHON_LIBRARIES})
target_link_libraries(pylua python3)
target_compile_options(pylua PRIVATE ${PYTHON_CFLAGS_OTHER})

target_link_libraries(pylua ${LUA_LIBRARIES})
Expand Down
29 changes: 3 additions & 26 deletions src/lua_py.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "lua_py.h"
#include <dlfcn.h>
#include <wchar.h>

#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
Expand Down Expand Up @@ -32,31 +33,7 @@ int luaopen_pylua(lua_State* L)

Py_Initialize();

wchar_t* path = Py_GetPath();

#if defined(_WIN32)
wchar_t new_path[800];
if (!new_path)
{
exit(-1);
}
new_path[0] = L'.';
new_path[1] = L';';
new_path[2] = L'\0';
#else
wchar_t new_path[800];
if (!new_path)
{
exit(-1);
}
new_path[0] = L'.';
new_path[1] = L':';
new_path[2] = L'\0';
#endif

wcsncat(new_path, path, wcslen(path));

PySys_SetPath(new_path);
PySys_SetArgv(0, NULL);

pPylua_Module = PyImport_ImportModule("pylua");
if (!pPylua_Module)
Expand Down Expand Up @@ -182,7 +159,7 @@ int call_PyFunc(lua_State* L)
LUA_MEMORY_ERROR(L);
}

int iter_PyGenerator(lua_State* L)
int iter_PyGenerator(lua_State* L, int _a1, long _a2)
{
PyLua_PyIterator* py_iter = (PyLua_PyIterator*)lua_touserdata(L, lua_upvalueindex(1));

Expand Down
8 changes: 4 additions & 4 deletions src/lua_py.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static int raise_error(lua_State* L, const char* msg)
if ((err_msg += snprintf(err_msg, err_max, "%s", traceback_msg)) < 0)
{
err_max *= 4;
err_msg = err_msg - _err_msg;
err_msg = (char*)(err_msg - _err_msg);
char* tmp = realloc(_err_msg, err_max);

if (tmp)
Expand All @@ -125,7 +125,7 @@ static int raise_error(lua_State* L, const char* msg)

PyObject* str_exc_value = PyObject_Repr(pExcValue);
PyObject* pExcValueStr = PyUnicode_AsEncodedString(str_exc_value, "utf-8", "strict");
const char* strExcValue = PyBytes_AS_STRING(pExcValueStr);
char* strExcValue = PyBytes_AS_STRING(pExcValueStr);

char* err_name = malloc(EXCEPTION_STR_LEN);
if (!err_name)
Expand All @@ -143,7 +143,7 @@ static int raise_error(lua_State* L, const char* msg)
if (snprintf(err_msg, err_max, "%s", err_name) < 0)
{
err_max *= 4;
err_msg = err_msg - _err_msg;
err_msg = (char*)(err_msg - _err_msg);
char* tmp = realloc(_err_msg, err_max);

if (tmp)
Expand Down Expand Up @@ -216,7 +216,7 @@ static int binary_base_pyobj_wrapper(lua_State* L, binary_op func)
// Python Wrapper functions

int call_PyFunc(lua_State* L);
int iter_PyGenerator(lua_State* L);
int iter_PyGenerator(lua_State* L, int _a1, long _a2);
static int get_PyObj(lua_State* L);
static int set_PyObj(lua_State* L);
static int str_PyObj(lua_State* L);
Expand Down
86 changes: 43 additions & 43 deletions src/py_lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ static PyTypeObject pLuaFunc_Type = {
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = &PyType_GenericNew,
.tp_init = &init_LuaFunc_Wrapper,
.tp_call = &call_LuaFunc,
.tp_iter = &iter_LuaCoroutine,
.tp_iternext = &next_LuaCoroutine,
.tp_finalize = &gc_LuaFunc,
.tp_init = (initproc)init_LuaFunc_Wrapper,
.tp_call = (ternaryfunc)call_LuaFunc,
.tp_iter = (getiterfunc)iter_LuaCoroutine,
.tp_iternext = (iternextfunc)next_LuaCoroutine,
.tp_finalize = (destructor)gc_LuaFunc,
};

// TODO: implement convertion to dict, list and iteration for Lua Tables and Instances
Expand All @@ -120,11 +120,11 @@ PyTypeObject pLuaTable_Type = {
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = PyType_GenericNew,
.tp_init = &init_LuaTable_Wrapper,
.tp_init = (initproc)init_LuaTable_Wrapper,
.tp_as_mapping = &pLuaTable_MappingMethods,
.tp_call = &call_LuaTable_Wrapper,
.tp_iter = &iter_LuaTable,
.tp_finalize = &gc_LuaTable,
.tp_call = (ternaryfunc)call_LuaTable_Wrapper,
.tp_iter = (getiterfunc)iter_LuaTable,
.tp_finalize = (destructor)gc_LuaTable,
};

PyTypeObject pLuaInstance_Type = {
Expand All @@ -135,16 +135,16 @@ PyTypeObject pLuaInstance_Type = {
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = PyType_GenericNew,
.tp_init = &init_LuaTable_Wrapper,
.tp_init = (initproc)init_LuaTable_Wrapper,
.tp_as_number = &pLuaInstance_NumberMethods,
.tp_as_mapping = &pLuaInstance_MappingMethods,
.tp_richcompare = &compare_LuaInstance_Wrapper,
.tp_getattr = &getattr_LuaInstance_Wrapper,
.tp_setattr = &setattr_LuaInstance_Wrapper,
.tp_call = &call_LuaInstance_Wrapper,
.tp_iter = &iter_LuaTable,
.tp_str = &string_LuaInstance_Wrapper,
.tp_finalize = &gc_LuaTable,
.tp_richcompare = (richcmpfunc)compare_LuaInstance_Wrapper,
.tp_getattr = (getattrfunc)getattr_LuaInstance_Wrapper,
.tp_setattr = (setattrfunc)setattr_LuaInstance_Wrapper,
.tp_call = (ternaryfunc)call_LuaInstance_Wrapper,
.tp_iter = (getiterfunc)iter_LuaTable,
.tp_str = (reprfunc)string_LuaInstance_Wrapper,
.tp_finalize = (destructor)gc_LuaTable,
};

static PyTypeObject pLuaModule_Type = {
Expand All @@ -155,40 +155,40 @@ static PyTypeObject pLuaModule_Type = {
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = PyType_GenericNew,
.tp_init = &PyLua_LuaInit,
.tp_getattr = &PyLua_LuaGet,
.tp_setattr = &PyLua_LuaSet,
.tp_finalize = &PyLua_LuaGC,
.tp_init = (initproc)PyLua_LuaInit,
.tp_getattr = (getattrfunc)PyLua_LuaGet,
.tp_setattr = (setattrfunc)PyLua_LuaSet,
.tp_finalize = (destructor)PyLua_LuaGC,
};


static PyMappingMethods pLuaTable_MappingMethods = {
.mp_length = &len_LuaInstance_Wrapper,
.mp_subscript = &getelem_LuaTable_Wrapper,
.mp_ass_subscript = &setelem_LuaTable_Wrapper,
.mp_length = (lenfunc)len_LuaInstance_Wrapper,
.mp_subscript = (binaryfunc)getelem_LuaTable_Wrapper,
.mp_ass_subscript = (objobjargproc)setelem_LuaTable_Wrapper,
};


static PyMappingMethods pLuaInstance_MappingMethods = {
.mp_length = &len_LuaInstance_Wrapper,
.mp_length = (lenfunc)len_LuaInstance_Wrapper,
};

static PyNumberMethods pLuaInstance_NumberMethods = {
.nb_add = &add_LuaInstance_Wrapper,
.nb_subtract = &sub_LuaInstance_Wrapper,
.nb_multiply = &mul_LuaInstance_Wrapper,
.nb_true_divide = &div_LuaInstance_Wrapper,
.nb_floor_divide = &floordiv_LuaInstance_Wrapper,
.nb_power = &pow_LuaInstance_Wrapper,
.nb_remainder = &mod_LuaInstance_Wrapper,
.nb_negative = &neg_LuaInstance_Wrapper,
.nb_lshift = &lshift_LuaInstance_Wrapper,
.nb_rshift = &rshift_LuaInstance_Wrapper,
.nb_and = &band_LuaInstance_Wrapper,
.nb_or = &bor_LuaInstance_Wrapper,
.nb_xor = &bxor_LuaInstance_Wrapper,
.nb_invert = &bnot_LuaInstance_Wrapper,
.nb_matrix_multiply = &concat_LuaInstance_Wrapper,
.nb_add = (binaryfunc)add_LuaInstance_Wrapper,
.nb_subtract = (binaryfunc)sub_LuaInstance_Wrapper,
.nb_multiply = (binaryfunc)mul_LuaInstance_Wrapper,
.nb_true_divide = (binaryfunc)div_LuaInstance_Wrapper,
.nb_floor_divide = (binaryfunc)floordiv_LuaInstance_Wrapper,
.nb_power = (ternaryfunc)pow_LuaInstance_Wrapper,
.nb_remainder = (binaryfunc)mod_LuaInstance_Wrapper,
.nb_negative = (unaryfunc)neg_LuaInstance_Wrapper,
.nb_lshift = (binaryfunc)lshift_LuaInstance_Wrapper,
.nb_rshift = (binaryfunc)rshift_LuaInstance_Wrapper,
.nb_and = (binaryfunc)band_LuaInstance_Wrapper,
.nb_or = (binaryfunc)bor_LuaInstance_Wrapper,
.nb_xor = (binaryfunc)bxor_LuaInstance_Wrapper,
.nb_invert = (unaryfunc)bnot_LuaInstance_Wrapper,
.nb_matrix_multiply = (binaryfunc)concat_LuaInstance_Wrapper,
};


Expand All @@ -204,7 +204,7 @@ static struct PyModuleDef LUA_module = {
"pylua",
"pylua doc",
-1,
&PyLua_Methods
PyLua_Methods
};


Expand Down Expand Up @@ -285,7 +285,7 @@ static int PyLua_LuaSet(PyLua_LuaModule* self, char* attr, PyObject* pValue)
static void PyLua_LuaGC(PyLua_LuaModule* self)
{
// TODO: implement
return 0;
return;
}


Expand Down Expand Up @@ -385,7 +385,7 @@ static PyObject* iter_LuaCoroutine(PyLua_LuaFunc* self)
}

Py_INCREF(self);
return self;
return (PyObject*)self;
}

static PyObject* next_LuaCoroutine(PyLua_LuaFunc* self)
Expand Down

0 comments on commit 39a447d

Please sign in to comment.