From e2af3c2670b7e6ebf5c28b501cf911ec76f9271f Mon Sep 17 00:00:00 2001 From: DownToTwo <63496597+DownToTwo@users.noreply.github.com> Date: Thu, 30 Mar 2023 09:52:55 +0300 Subject: [PATCH 1/2] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 85c477c..447b18e 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ## Contents * [Get Started](./doc/01_get_started.md) - * [Hello World](./src/01.hello.c) + * [Hello World](./src/01_hello.c) * [Lua Stack](./src/02_stack.c) * [Calling Lua From a C Program](./doc/02_calling_lua_from_a_c_program.md) @@ -15,4 +15,4 @@ * Lua to C Function Call. [call_c_func.c](./src/06_call_lua_script.c) -> [call_c_func.lua](./src/call_c_func.lua) * Example: Split Lib. [split_lib.c](./src/07_split_lib.c) -> [test_split.lua](./src/test_split.lua) * Example: Boolean Array Lib. [boolarray_lib.c](./src/08_boolarray_lib.c) -> [test_boolarray.lua](./src/test_boolarray.lua) - * Example: Sleep Lib. [sleep_lib.c](./src/09_sleep_lib.c) -> [test_sleep.lua](./src/test_sleep.lua) \ No newline at end of file + * Example: Sleep Lib. [sleep_lib.c](./src/09_sleep_lib.c) -> [test_sleep.lua](./src/test_sleep.lua) From 6bdf7d91f52e4aa8daa27cb40aa7ce9a0f20a897 Mon Sep 17 00:00:00 2001 From: DownToTwo Date: Thu, 30 Mar 2023 13:06:50 +0300 Subject: [PATCH 2/2] some fixes --- .gitignore | 3 ++- build.sh | 6 +----- src/04_call_lua_func.c | 49 +++++++++++++++++++++--------------------- src/09_sleep_lib.c | 31 ++++++++++++-------------- src/CMakeLists.txt | 4 +++- 5 files changed, 44 insertions(+), 49 deletions(-) diff --git a/.gitignore b/.gitignore index 600d2d3..af96791 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.vscode \ No newline at end of file +.vscode +build diff --git a/build.sh b/build.sh index 9e20509..4d73dfe 100755 --- a/build.sh +++ b/build.sh @@ -2,8 +2,4 @@ SOURCE_DIR=`pwd` -mkdir -p build - && cd build - && cmake -D CMAKE_INSTALL_PREFIX=. $SOURCE_DIR - && make - && make install \ No newline at end of file +mkdir -p build && cd build && cmake -D CMAKE_INSTALL_PREFIX=. $SOURCE_DIR && make && make install diff --git a/src/04_call_lua_func.c b/src/04_call_lua_func.c index d51385f..e601f03 100644 --- a/src/04_call_lua_func.c +++ b/src/04_call_lua_func.c @@ -1,40 +1,39 @@ #include "errors.h" -#include "lua.h" #include "lauxlib.h" +#include "lua.h" #include "lualib.h" -int main(void) -{ - lua_State *L = luaL_newstate(); - luaL_openlibs(L); +int main(void) { + lua_State *L = luaL_newstate(); + luaL_openlibs(L); - if(luaL_loadfile(L, "call_lua_func.lua")) - err_exit(L, "luaL_loadfile failed.\n"); + if (luaL_loadfile(L, "../src/call_lua_func.lua")) + err_exit(L, "luaL_loadfile failed.\n"); - if(lua_pcall(L, 0, 0, 0)) - err_exit(L, "lua_pcall failed.\n"); + if (lua_pcall(L, 0, 0, 0)) + err_exit(L, "lua_pcall failed.\n"); - printf("In C, calling Lua->sayHello()\n"); + printf("In C, calling Lua->sayHello()\n"); - lua_getglobal(L, "sayHello"); //Tell it to run test2.lua -> sayHello() - if(lua_pcall(L, 0, 0, 0)) - err_exit(L, "lua_pcall failed.\n"); + lua_getglobal(L, "sayHello"); // Tell it to run test2.lua -> sayHello() + if (lua_pcall(L, 0, 0, 0)) + err_exit(L, "lua_pcall failed.\n"); - printf("Back in C again\n"); - printf("\nIn C, calling Lua->add()\n"); + printf("Back in C again\n"); + printf("\nIn C, calling Lua->add()\n"); - lua_getglobal(L, "add"); //Tell it to run test2.lua -> add() - lua_pushnumber(L, 1); - lua_pushnumber(L, 5); - if(lua_pcall(L, 2, 1, 0)) - err_exit(L, "lua_pcall failed.\n"); + lua_getglobal(L, "add"); // Tell it to run test2.lua -> add() + lua_pushnumber(L, 1); + lua_pushnumber(L, 5); + if (lua_pcall(L, 2, 1, 0)) + err_exit(L, "lua_pcall failed.\n"); - printf("Back in C again\n"); - int returnNum = lua_tonumber(L, -1); - printf("Returned number : %d\n", returnNum); + printf("Back in C again\n"); + int returnNum = lua_tonumber(L, -1); + printf("Returned number : %d\n", returnNum); - lua_close(L); + lua_close(L); - return 0; + return 0; } diff --git a/src/09_sleep_lib.c b/src/09_sleep_lib.c index 9c2d938..b0396df 100644 --- a/src/09_sleep_lib.c +++ b/src/09_sleep_lib.c @@ -1,26 +1,23 @@ #include -#include "lua.h" #include "lauxlib.h" +#include "lua.h" #include "lualib.h" -static int s_sleep(lua_State *L) -{ - long seconds = lua_tointeger(L, -1); - sleep(seconds); - return 0; +static int s_sleep(lua_State *L) { + long seconds = lua_tointeger(L, -1); + sleep(seconds); + return 0; } -static int u_sleep(lua_State *L) -{ - long usec = lua_tointeger(L, -1); - usleep(usec); - return 0; +static int m_sleep(lua_State *L) { + long usec = lua_tointeger(L, -1); + usleep(usec); + return 0; } -int luaopen_msleep(lua_State *L) -{ - lua_register(L, "sleep", s_sleep); - lua_register(L, "msleep", m_sleep); - return 0; -} \ No newline at end of file +int luaopen_msleep(lua_State *L) { + // lua_register(L, "sleep", s_sleep); + lua_register(L, "msleep", m_sleep); + return 0; +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5cf8d87..2ac2984 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,4 +5,6 @@ add_executable(call_lua_func 04_call_lua_func.c) add_executable(call_lua_table 05_call_lua_table.c) add_library(mylib SHARED 06_call_c_func.c) -add_library(split SHARED 08_split_lib.c) \ No newline at end of file +add_library(split SHARED 07_split_lib.c) +add_library(boolean SHARED 08_boolarray_lib.c) +add_library(sleep SHARED 09_sleep_lib.c)