diff options
Diffstat (limited to 'Examples/lua/embed2')
-rw-r--r-- | Examples/lua/embed2/Makefile | 6 | ||||
-rw-r--r-- | Examples/lua/embed2/embed2.c | 19 | ||||
-rw-r--r-- | Examples/lua/embed2/runme.lua | 30 |
3 files changed, 30 insertions, 25 deletions
diff --git a/Examples/lua/embed2/Makefile b/Examples/lua/embed2/Makefile index ec22bdcae..d30ba0942 100644 --- a/Examples/lua/embed2/Makefile +++ b/Examples/lua/embed2/Makefile @@ -9,12 +9,12 @@ LIBS = -lm # this is a little different to normal as we have our own special interpreter # which we want to static link check: build - $(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' lua_embed_run + $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' lua_embed_run build: - $(MAKE) -f $(TOP)/Makefile $(SWIGLIB) SRCS='$(SRCS)' SWIG='$(SWIG)' \ + $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' $(SWIGLIB) SRCS='$(SRCS)' SWIG='$(SWIG)' \ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' LIBS='$(LIBS)' INTERFACE='example.i' LUA_INTERP='$(LUA_INTERP)' lua_static clean: - $(MAKE) -f $(TOP)/Makefile lua_clean + $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' lua_clean rm -f $(TARGET) diff --git a/Examples/lua/embed2/embed2.c b/Examples/lua/embed2/embed2.c index 100a1fb33..0ce9f8f7f 100644 --- a/Examples/lua/embed2/embed2.c +++ b/Examples/lua/embed2/embed2.c @@ -24,6 +24,7 @@ We will be using the luaL_dostring()/lua_dostring() function to call into lua #include <stdlib.h> #include <stdio.h> +#include <string.h> #include <lua.h> #include <lauxlib.h> @@ -52,7 +53,7 @@ int call_add(lua_State *L,int a,int b,int* res) { lua_getglobal(L, "add"); /* function to be called */ if (!lua_isfunction(L,-1)) { printf("[C] error: cannot find function 'add'\n"); - lua_settop(L,top); // reset + lua_settop(L,top); return 0; } lua_pushnumber(L,a); @@ -60,18 +61,18 @@ int call_add(lua_State *L,int a,int b,int* res) { if (lua_pcall(L, 2, 1, 0) != 0) /* call function with 2 arguments and 1 result */ { printf("[C] error running function `add': %s\n",lua_tostring(L, -1)); - lua_settop(L,top); // reset + lua_settop(L,top); return 0; } - // check results + /* check results */ if (!lua_isnumber(L,-1)) { printf("[C] error: returned value is not a number\n"); - lua_settop(L,top); // reset + lua_settop(L,top); return 0; } *res=(int)lua_tonumber(L,-1); lua_settop(L,top); /* reset stack */ - return 1; // ok + return 1; } /* This is a variargs call function for calling from C into Lua. @@ -189,9 +190,13 @@ int main(int argc,char* argv[]) { luaopen_example(L); printf("[C] all looks ok\n"); printf("\n"); - printf("[C] let's load the file 'runme.lua'\n"); + if (argc != 2 || argv[1] == NULL || strlen(argv[1]) == 0) { + printf("[C] ERROR: no lua file given on command line\n"); + exit(3); + } + printf("[C] let's load the file '%s'\n", argv[1]); printf("[C] any lua code in this file will be executed\n"); - if (luaL_loadfile(L, "runme.lua") || lua_pcall(L, 0, 0, 0)) { + if (luaL_loadfile(L, argv[1]) || lua_pcall(L, 0, 0, 0)) { printf("[C] ERROR: cannot run lua file: %s",lua_tostring(L, -1)); exit(3); } diff --git a/Examples/lua/embed2/runme.lua b/Examples/lua/embed2/runme.lua index 9e7ab616c..6e93164c0 100644 --- a/Examples/lua/embed2/runme.lua +++ b/Examples/lua/embed2/runme.lua @@ -3,25 +3,25 @@ print "[lua] This is runme.lua" -- we do not need to load the library, as it was already in the interpreter -- but let's check anyway assert(type(example)=='table',"Don't appear to have loaded the example module") -
--- note: we will copy the functions from example table into global
--- this will help us later
-for k,v in pairs(example) do _G[k]=v end
--- our add function
+-- note: we will copy the functions from example table into global +-- this will help us later +for k,v in pairs(example) do _G[k]=v end + +-- our add function -- we will be calling this from C -function add(a,b)
- print("[lua] this is function add(",a,b,")")
- c=a+b
- print("[lua] returning",c)
- return c
+function add(a,b) + print("[lua] this is function add(",a,b,")") + c=a+b + print("[lua] returning",c) + return c end -function append(a,b)
- print("[lua] this is function append(",a,b,")")
- c=a..b
- print("[lua] returning",c)
- return c
+function append(a,b) + print("[lua] this is function append(",a,b,")") + c=a..b + print("[lua] returning",c) + return c end |