可变参数 和 lua test

来源:互联网 发布:最近淘宝流量出奇的少 编辑:程序博客网 时间:2024/05/23 13:48
#include <stdio.h>#include <stdarg.h>#include <strings.h>#include "lua.h"#include "lauxlib.h"#include "lualib.h" /*!!!only can deal with char * type!!! * *Usage: * 动态函数最后必须输入一个常量字符串 @ */void pushStringsToStack(lua_State *L, ...){char *buff = NULL;va_list arg_ptr;va_start(arg_ptr, L);while(1){buff = va_arg(arg_ptr, char *);if (strcmp(buff, "@") == 0) break;lua_pushstring(L, buff);}va_end(arg_ptr);}  /*这里输出stack中的内容*/ void showStack(lua_State *L, char *pNote){printf("------------------- %s -----------------\n", pNote);       int top = lua_gettop(L);     for (int i = top; i >= 1; i--)      {     printf("[%d] -> ", i);              int t = lua_type(L,i);         switch(t)          {         case LUA_TSTRING:             printf("'%s' \n",lua_tostring(L,i));             break;         case LUA_TBOOLEAN:             printf(lua_toboolean(L,i) ? "true\n" : "false\n");             break;         case LUA_TNUMBER:             printf("%g\n",lua_tonumber(L,i));             break;         default:             printf("%s\n",lua_typename(L,t));             break;         }     }  } int main (void){    lua_State *L = lua_open();  /* opens Lua */luaL_openlibs(L);//    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. */lua_newtable(L);lua_pushnumber(L, 1234);/*push number*/lua_pushboolean(L, true);               /*push boolean*/lua_pushboolean(L, NULL);               /*push NULL*/lua_pushnil(L);                         /*push nil*/lua_pushnumber(L, 20.001);              /*push double*/lua_pushnumber(L, 1999999999999);       /*push number with e*/showStack(L, "push test");pushStringsToStack(L, "This", "is", "a", "simple ","Test", "@");pushStringsToStack(L, "Second", "test", "@");pushStringsToStack(L, "@");showStack(L, "mutiple push");lua_pushvalue(L, 10);//元素个数变化,将指定索引的元素副本压入栈。showStack(L, "pushvalue");lua_remove(L, 12);showStack(L, "remove");lua_insert(L, 12);  //栈中元素个数不变,栈顶替换索引12的元素showStack(L, "insert");lua_settop(L, 10);  //元素个数变化,showStack(L, "settop");lua_replace(L, 8);showStack(L, "replace");    lua_close(L);    return 0;}