您的位置:首页 > 编程语言 > Lua

c++ 与 lua 简单交互1 (LUA CAPI)

2010-04-22 20:43 549 查看
一、第一个例子 Hello World !

#include <stdio.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
int main (void)
{
char buff[256];
int error;
lua_State *L = lua_open(); /* opens Lua */
// 5.1.4 版本加载库 方法
luaL_openlibs(L);
// 5.1.4 版本之前 加载库 方式
//luaopen_base(L); /* opens the basic library */
//luaopen_table(L); /* opens the table library */
//luaopen_io(L); /* opens the I/O library */
//luaopen_string(L); /* opens the string lib. */
//luaopen_math(L); /* opens the math lib. */
while (fgets(buff, sizeof(buff), stdin) != NULL) {
error = luaL_loadbuffer(L, buff, strlen(buff),
"line") || lua_pcall(L, 0, 0, 0);
if (error) {
fprintf(stderr, "%s", lua_tostring(L, -1));
lua_pop(L, 1);/* pop error message from the stack */
}
}
lua_close(L);
return 0;
}


注意一下几点:

1.需要lua库的 .dll 和 .lib 文件

2.在include “lua.h lauxlib.h lualib.h” 时,注意区分是否需要将这些include用 extern "C" { ... } 包含起来

3.初始化lua虚拟机函数已改成 luaL_openlibs(L);

二、堆栈

2.1:压入元素

将每种可以用C 来描述的Lua 类型压栈

void lua_pushnil (lua_State *L);
void lua_pushboolean (lua_State *L, int bool);
void lua_pushnumber (lua_State *L, double n);
void lua_pushlstring (lua_State *L, const char *s, size_t length);
void lua_pushstring (lua_State *L, const char *s);


将字符串压入串的正式函数是lua_pushlstring,它要求一个明确的长度作为参数。对于以零结束的字符串,你可以用lua_pushstring

2.2:查询元素

各个类型的这些函数都有同一个原型: int lua_is... (lua_State *L, int index);

这些函数中使用 lua_type, 并对结果(几种宏)进行判断,返回0 or 1

#define LUA_TNIL		0
#define LUA_TBOOLEAN		1
#define LUA_TLIGHTUSERDATA	2
#define LUA_TNUMBER		3
#define LUA_TSTRING		4
#define LUA_TTABLE		5
#define LUA_TFUNCTION		6
#define LUA_TUSERDATA		7
#define LUA_TTHREAD		8


2.3:从栈中获取值,lua_to... ()函数:

int lua_toboolean (lua_State *L, int index);
double lua_tonumber (lua_State *L, int index);
const char * lua_tostring (lua_State *L, int index);
size_t lua_strlen (lua_State *L, int index);


即使给定的元素的类型不正确,调用上面这些函数也没有什么问题。在这种情况下,lua_toboolean、lua_tonumber 和lua_strlen 返回0,其他函数返回NULL。

lua 允许 string 中包含'/0',所以下面的语句总是有效的:

const char *s = lua_tostring(L, -1); /* any Lua string */
size_t l = lua_strlen(L, -1); /* its length */
assert(s[l] == '/0');
assert(strlen(s) <= l);


2.4:其他堆栈操作

int lua_gettop (lua_State *L);
void lua_settop (lua_State *L, int index);
void lua_pushvalue (lua_State *L, int index);
void lua_remove (lua_State *L, int index);
void lua_insert (lua_State *L, int index);
void lua_replace (lua_State *L, int index);


lua_gettop:返回堆栈中的元素个数,它也是栈顶元素的索引(注意一个负数索引-x 对应于正数索引gettop-x+1)

lua_settop:设置栈顶(也就是堆栈中的元素个数)为一个指定的值

如果开始的栈顶高于新的栈顶,顶部的值被丢弃。否则,为了得到指定的大小这个函数压入相应个数的空值(nil)到栈上

lua_settop(L,0); 清空堆栈

也可以用负数索引作为调用lua_settop 的参数,那将会设置栈顶到指定的索引。利用这种技巧,API 提供了下面这个宏,它从堆栈中弹出n 个元素:

#define lua_pop(L,n) lua_settop(L, -(n)-1)

lua_pushvalue:压入堆栈上指定索引的一个抟贝到栈顶

lua_remove:移除指定索引位置的元素,并将其上面所有的元素下移来填补这个位置的空白

lua_insert:移动栈顶元素到指定索引的位置,并将这个索引位置上面的元素全部上移至栈顶被移动留下的空隔;

lua_replace 从栈顶弹出元素值并将其设置到指定索引位置,没有任何移动操作。

2.5:表操作

lua_getglobal:其中一参数为变量名称,每调用一次就把相应的变量值压入栈顶

lua_gettable:他接受table在栈中的位置为参数,调用前需要先将要取的key(string)压入栈,并位于栈顶,

调用lua_gettable 后对应key 值出栈,返回与key 对应的value(栈顶)

lua_newtable:创建一个新的空table 然后将其入栈

lua_settable:以table 在栈中的索引作为参数(key先入栈,value后(顶)),并将栈中的key 和value出栈,用这两个值修改table的相应key值。

lua_setglobal:将栈顶元素出栈,并将其赋给一个全局变量名

void setfield (const char *index, int value) {
lua_pushstring(L, index);
lua_pushnumber(L, (double)value/MAX_COLOR);
lua_settable(L, -3);
}
void setcolor (struct ColorTable *ct) {
lua_newtable(L); /* creates a table */
setfield("r", ct->red); /* table.r = ct->r */
setfield("g", ct->green); /* table.g = ct->g */
setfield("b", ct->blue); /* table.b = ct->b */
lua_setglobal(ct->name); /* 'name' = table */
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: