Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vscode
.vscode
build
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
* Example: Sleep Lib. [sleep_lib.c](./src/09_sleep_lib.c) -> [test_sleep.lua](./src/test_sleep.lua)
6 changes: 1 addition & 5 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@

SOURCE_DIR=`pwd`

mkdir -p build
&& cd build
&& cmake -D CMAKE_INSTALL_PREFIX=. $SOURCE_DIR
&& make
&& make install
mkdir -p build && cd build && cmake -D CMAKE_INSTALL_PREFIX=. $SOURCE_DIR && make && make install
49 changes: 24 additions & 25 deletions src/04_call_lua_func.c
Original file line number Diff line number Diff line change
@@ -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;
}
31 changes: 14 additions & 17 deletions src/09_sleep_lib.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
#include <unistd.h>

#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;
}
int luaopen_msleep(lua_State *L) {
// lua_register(L, "sleep", s_sleep);
lua_register(L, "msleep", m_sleep);
return 0;
}
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
add_library(split SHARED 07_split_lib.c)
add_library(boolean SHARED 08_boolarray_lib.c)
add_library(sleep SHARED 09_sleep_lib.c)