1.lua访问c中的栈变量
void lua_setglobal (lua_State *L, const char *name);
将虚拟栈中,将栈顶元素弹出,作为全局 lua 变量 name 的值。 Pops a value from the stack
and sets it as the new value of global name。
#include <stdio.h>
#include "lua.hpp"
extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
int main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_pushinteger(L,18);
lua_setglobal(L,"age");
lua_pushstring(L,"nzsoft");
lua_setglobal(L,"org");
luaL_dofile(L,"yy.lua");
lua_close(L);
return 0;
}
//yy.lua
print("age",age)
print("org",org)
测试结果:
2.lua访问c中入栈的表
void lua_setfield (lua_State *L, int index, const char *k);
上式,等价于 t[k] = v, t 是栈上索引为 index 的表, v 是栈顶的值。函数结束,栈顶值 v
会被弹出。
#include <stdio.h>
#include "lua.hpp"
extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
int main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_newtable(L);
lua_pushstring(L,"age");
lua_pushinteger(L,18);
printf("Stack size = %d\n",lua_gettop(L));
lua_settable(L,-3);
lua_pushstring(L,"nzsoft");
lua_setfield(L,-2,"org");
printf("Stack size = %d\n",lua_gettop(L));
lua_setglobal(L,"table")
printf("Stack size = %d\n",lua_gettop(L));
luaL_dofile(L,"yy.lua");
lua_close(L);
return 0;
}
//yy.lua
print("age",tab.age)
print("org",tab.org)
运行结果:
3.lua 调用 C 函数(无参无返回)
#include <stdio.h>
#include "lua.hpp"
extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
int func(lua_State *L)
{
printf("hello lua");
return 0;
}
int main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_pushcfunction(L,func);
lua_setglobal(L,"funn");
luaL_dofile(L,"yy.lua");
lua_close(L);
return 0;
}
//yy.lua
func()
测试结果:
4.lua 调用 C 函数(有参无返回)
#include <stdio.h>
#include "lua.hpp"
extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
int func(lua_State *L)
{
char *str = lua_tostring(L,-1);
int num2 = lua_tointeger(L,-2);
printf("%s %d\n",str,num2);
return 0;
}
int main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_pushcfunction(L,func);
lua_setglobal(L,"funn");
luaL_dofile(L,"yy.lua");
lua_close(L);
return 0;
}
//yy.lua
func(999,"nzsoft") //入参会被传到栈上
测试结果:
5.lua 调用 C 函数(有参有返回)
#include <stdio.h>
#include "lua.hpp"
extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
int func(lua_State *L)
{
int n = lua_gettop(L);
int sum = 0;
for(int i =0; i < n;++i)
sum += lua_tointeger(L,i);
printf("size = %d\n",lua_gettop(L));
lua_pushinteger(L,sum);
lua_pushinteger(L,sum/n);
printf("size = %d\n",lua_gettop(L));
return 1;
}
int main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
lua_pushcfunction(L,add);
lua_setglobal(L,"add");
luaL_dofile(L,"yy.lua");
lua_close(L);
return 0;
}
//yy.lua
sum,avg= add(1,2,3,4,5)
print(sum,avg)
测试结果:
6.Lua 加载 dll 或 so(第一种方法)
加入以后C中有100个函数要供lua调用,那么可以把这些函数丢在一个表中,再把表丢到table去
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
int printHello(lua_State * l)
{
lua_pushstring(l, "hello lua");
return 1;
}
int foo(lua_State * l)
{
int n = lua_gettop(l);
if(n != 0)
{
int i = lua_tonumber(l, 1);
lua_pushnumber(l, i+1);
return 1;
}
return 0;
}
int add(lua_State * l)
{
int n = lua_gettop(l);
int sum = 0;
for (int i=0;i<n;i++)
{
sum += lua_tonumber(l, i+1);
}
if(n!=0)
{
lua_pushnumber(l, sum);
return 1;
}
return 0;
}
static const luaL_Reg mylib[] =
{
{"printHello",printHello},
{"foo",foo),
{"add",add},
{NULL,NULL}
};
int main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
const luaL_Reg *libf = mylib;
for(;libf->func;libf++)
{
lua_register(L,libf->name,libf->func);
lua_settop(L,0);
}
luaL_dofile(L,"yy.lua");
lua_close(L);
}
//yy.lua
sum = add(1,2,3,4,5)
print(sum)
print(printhello())
测试结果:
7.6.Lua 加载 dll 或 so(第二种方法:批量绑定)
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
int printHello(lua_State * l)
{
lua_pushstring(l, "hello lua");
return 1;
}
int foo(lua_State * l)
{
int n = lua_gettop(l);
if(n != 0)
{
int i = lua_tonumber(l, 1);
lua_pushnumber(l, i+1);
return 1;
}
return 0;
}
int add(lua_State * l)
{
int n = lua_gettop(l);
int sum = 0;
for (int i=0;i<n;i++)
{
sum += lua_tonumber(l, i+1);
}
if(n!=0)
{
lua_pushnumber(l, sum);
return 1;
}
return 0;
}
static const luaL_Reg mylib[] =
{
{"printHello",printHello},
{"foo",foo),
{"add",add},
{NULL,NULL}
};
int luaopen_my(lua_State * L)
{
luaL_newlib(L,mylib);
return 1;
}
int main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
const luaL_Reg *libf = mylib;
luaL_requiref(L,"my",luaopen_my,1);
luaL_dofile(L,"yy.lua");
lua_close(L);
}
//yy.lua
sum = my.add(1,2,3,4,5)
print(sum)
print(my.printhello())
|
请发表评论