/*
lua 的各种操作
压入元素
lua_pushnil
lua_pushboolean
lua_pushnumber
lua_pushlstring(要求一个明确的长度作为参数)
lua_pushstring(对于以零结束的字符串可以使用这个函数,它用strlen来计算字符串长度)
-----------------------------------------------------
查询元素
lua_isnumber 注:lua_isnumber和lua_isstring函数不检查这个值是否是指定的类型,而是看它是否能被转换成指定的那种类型。例如,任何数字类型都满足lua_isstring。
lua_isstring
lua_istable
这些都是有同样原型的
int lua_is... (lua_State *L, int index);
lua_type它返回栈中元素的类型。(lua_is*中的有些函数实际上是用了这个函数定义的宏)
为了从栈中获得值,这里有lua_to*函数:
lua_toboolean
lua_tonumber
lua_tostring
lua_strlen
-----------------------------------------------------
其他操作
lua_gettop
lua_settop
lua_pushvalue
lua_remove
lua_insert
lua_replace
配置需包含文件
E:\game\devenv\include\lua5
E:\game\devenv\include
需将lua5d.dll拷贝到exe目录
*/
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include <iostream>
#include <stdio.h>
#include <conio.h>
using namespace std;
#pragma comment(lib,"lua5d.lib")
static void stackDump (lua_State *L) //输出所有lua栈内函数
{
int i;
int top = lua_gettop(L);
if( top == 0 )
{
printf("栈内没有任何元素\n");
return;
}
for (i = 1; i <= top; i++) { /* repeat for each level */
int t = lua_type(L, i);
switch (t) {
case LUA_TSTRING: /* strings */
printf("`%s'", lua_tostring(L, i));
break;
case LUA_TBOOLEAN: /* booleans */
printf(lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TNUMBER: /* numbers */
printf("%g", lua_tonumber(L, i));
break;
default: /* other values */
printf("%s", lua_typename(L, t));
break;
}
printf(" "); /* put a separator */
}
printf("\n"); /* end the listing */
}
int main()
{
lua_State *L = lua_open(); //初始化lua
luaL_openlibs(L); //载入所有lua标准库
printf( "随意按一个按键 运行Lua程序.\n");
getch();
luaL_dofile(L, "add.lua"); //执行add.lua,将add函数加入lua_State栈中
lua_getglobal(L, "add"); //通过函数名取出函数地址压入栈
lua_pushnumber(L, 10); //将第一个参数压入栈
lua_pushnumber(L, 15); //将第二个参数压入栈
stackDump(L); // 打印结果:function 10 15
printf("-------------------------------------\n");
lua_call(L, 2, 1); //调用add函数
stackDump(L); // 打印结果:25
int sum = (int)lua_tonumber(L, -1); //从lua_State栈中取出函数执行结果
lua_pop(L, 1); //将结果弹出栈 从栈顶删除
printf( "The sum is %d\n", sum ); // 打印结果:The sum is 25
stackDump(L); // 打印结果:栈内没有任何元素
printf("2 -------------------------------------\n");
lua_getglobal(L, "del");
lua_pushnumber(L, 18); //将第一个参数压入栈
lua_pushnumber(L, 15); //将第二个参数压入栈
stackDump(L); // 打印结果:function 18 15
lua_call(L, 2, 1); //调用del函数
int sum2 = (int)lua_tonumber(L, -1); //从lua_State栈中取出函数执行结果
printf( "The sum is %d\n", sum2 ); // 打印结果:The sum is 3
printf("3 -------------------------------------\n");
//栈内第一个元素是刚刚上面del函数的返回值
lua_pushnumber(L, 22); //将第二个参数压入栈
lua_pushnumber(L, 32); //将第三个参数压入栈
lua_pushstring(L, "hello"); //讲第四个参数压入栈
lua_settop(L,6); //将栈顶设为6 其余全部填nil
stackDump(L); // 打印结果:3 22 32 `hello' nil nil
printf("移除从栈底开始数的第4个元素hello\n");
lua_remove(L,4); //移除从栈底开始数的第4个元素"hello"
stackDump(L); // 打印结果:3 22 32 nil nil
printf("移除从栈顶开始数的第4个元素22\n");
lua_remove(L,-4); //移除从栈底开始数的第4个元素22
stackDump(L); // 打印结果:3 32 nil nil
lua_pushstring(L, "FF"); //压栈 注意:这里压栈的时候不会覆盖空值 而是往空值后面添加 平时写程序的时候 一定要注意维护实时清空栈
stackDump(L); // 打印结果:3 32 nil nil `FF'
lua_insert(L,2); //将栈顶的"FF"放到从下往上索引为2的位置
stackDump(L); // 打印结果:3 `FF' 32 nil nil
lua_replace(L,2); //将栈顶的nil 强行覆盖从下往上索引为2的位置
stackDump(L); // 打印结果:3 nil 32 nil
lua_pushvalue(L,3); //将从下往上索引为3的位置 复制一份拷贝到栈顶
stackDump(L); // 打印结果:3 nil 32 nil 32
getch();
lua_close(L);
return 1;
}
/*add.lua*/
function add ( x, y )
return x + y
end
function del ( x, y )
return x - y
end