added jtest
git-svn-id: svn://anubis/misc/lua@131 bd8a9e45-a331-0410-811e-c64571078777
This commit is contained in:
parent
e74bc7d391
commit
d5e2895c2a
9
jtest/Makefile
Normal file
9
jtest/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
|
||||
TARGET := tut
|
||||
CPPFLAGS := -I/usr/include/lua5.1
|
||||
LDFLAGS := -llua5.1
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): $(TARGET).cc
|
||||
$(CXX) -o $@ $(CPPFLAGS) $(CXXFLAGS) $^ $(LDFLAGS)
|
7
jtest/test.lua
Normal file
7
jtest/test.lua
Normal file
@ -0,0 +1,7 @@
|
||||
|
||||
io.write("Running test.lua\n")
|
||||
a = my_function(1, 2, 3, "abc", "def")
|
||||
io.write("my_function() returned ", a.val, "\n")
|
||||
|
||||
a:func()
|
||||
|
97
jtest/tut.cc
Normal file
97
jtest/tut.cc
Normal file
@ -0,0 +1,97 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <lua.hpp>
|
||||
using namespace std;
|
||||
|
||||
int mywrite(lua_State * L)
|
||||
{
|
||||
int argc = lua_gettop(L);
|
||||
|
||||
for ( int n = 1; n <= argc; ++n )
|
||||
{
|
||||
cout << lua_tostring(L, n);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int val_handler(lua_State * L)
|
||||
{
|
||||
int argc = lua_gettop(L);
|
||||
|
||||
cout << "val_handler(): argc = " << argc << endl;
|
||||
lua_getfield(L, -1, "val");
|
||||
int the_val = lua_tointeger(L, -1);
|
||||
lua_pop(L, 1);
|
||||
|
||||
cout << "val_handler(): the_val = " << the_val << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int my_function(lua_State * L)
|
||||
{
|
||||
int argc = lua_gettop(L);
|
||||
|
||||
cerr << "-- my_function() called with " << argc
|
||||
<< " arguments:" << endl;
|
||||
|
||||
for ( int n = 1; n <= argc; ++n )
|
||||
{
|
||||
cerr << "-- argument " << n << ": "
|
||||
<< lua_tostring(L, n) << endl;
|
||||
}
|
||||
|
||||
/* construct return value */
|
||||
lua_newtable(L);
|
||||
lua_pushinteger(L, 42);
|
||||
lua_setfield(L, -2, "val");
|
||||
lua_pushcfunction(L, val_handler);
|
||||
lua_setfield(L, -2, "func");
|
||||
return 1; // number of return values
|
||||
}
|
||||
|
||||
void report_errors(lua_State *L, int status)
|
||||
{
|
||||
if ( status!=0 )
|
||||
{
|
||||
cerr << "-- " << lua_tostring(L, -1) << endl;
|
||||
lua_pop(L, 1); // remove error message
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
for ( int n = 1; n < argc; ++n )
|
||||
{
|
||||
const char * file = argv[n];
|
||||
|
||||
lua_State * L = lua_open();
|
||||
|
||||
// luaL_openlibs(L);
|
||||
luaL_Reg io_functions[] = {
|
||||
{ "write", mywrite },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
luaL_register(L, "io", io_functions);
|
||||
|
||||
// make my_function() available to Lua programs
|
||||
lua_register(L, "my_function", my_function);
|
||||
|
||||
cerr << "-- Loading file: " << file << endl;
|
||||
|
||||
int s = luaL_loadfile(L, file);
|
||||
|
||||
if ( s == 0 )
|
||||
{
|
||||
// execute Lua program
|
||||
s = lua_pcall(L, 0, LUA_MULTRET, 0);
|
||||
}
|
||||
|
||||
report_errors(L, s);
|
||||
lua_close(L);
|
||||
cerr << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user