原文http://blog.csdn.net/johnice/article/details/5517431

一、第一个例子 Hello World !

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "lua.h"
  4. #include "lauxlib.h"
  5. #include "lualib.h"
  6. int main (void)
  7. {
  8. char buff[256];
  9. int error;
  10. lua_State *L = lua_open(); /* opens Lua */
  11. // 5.1.4 版本加载库 方法
  12. luaL_openlibs(L);
  13. // 5.1.4 版本之前 加载库 方式
  14. //luaopen_base(L); /* opens the basic library */
  15. //luaopen_table(L); /* opens the table library */
  16. //luaopen_io(L); /* opens the I/O library */
  17. //luaopen_string(L); /* opens the string lib. */
  18. //luaopen_math(L); /* opens the math lib. */
  19. while (fgets(buff, sizeof(buff), stdin) != NULL) {
  20. error = luaL_loadbuffer(L, buff, strlen(buff),
  21. "line") || lua_pcall(L, 0, 0, 0);
  22. if (error) {
  23. fprintf(stderr, "%s", lua_tostring(L, -1));
  24. lua_pop(L, 1);/* pop error message from the stack */
  25. }
  26. }
  27. lua_close(L);
  28. return 0;
  29. }

注意一下几点:

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

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

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

二、堆栈

2.1:压入元素

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

  1. void lua_pushnil (lua_State *L);
  2. void lua_pushboolean (lua_State *L, int bool);
  3. void lua_pushnumber (lua_State *L, double n);
  4. void lua_pushlstring (lua_State *L, const char *s, size_t length);
  5. 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

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

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

  1. int lua_toboolean (lua_State *L, int index);
  2. double lua_tonumber (lua_State *L, int index);
  3. const char * lua_tostring (lua_State *L, int index);
  4. size_t lua_strlen (lua_State *L, int index);

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

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

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

2.4:其他堆栈操作

  1. int lua_gettop (lua_State *L);
  2. void lua_settop (lua_State *L, int index);
  3. void lua_pushvalue (lua_State *L, int index);
  4. void lua_remove (lua_State *L, int index);
  5. void lua_insert (lua_State *L, int index);
  6. 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:将栈顶元素出栈,并将其赋给一个全局变量名

  1. void setfield (const char *index, int value) {
  2. lua_pushstring(L, index);
  3. lua_pushnumber(L, (double)value/MAX_COLOR);
  4. lua_settable(L, -3);
  5. }
  6. void setcolor (struct ColorTable *ct) {
  7. lua_newtable(L); /* creates a table */
  8. setfield("r", ct->red); /* table.r = ct->r */
  9. setfield("g", ct->green); /* table.g = ct->g */
  10. setfield("b", ct->blue); /* table.b = ct->b */
  11. lua_setglobal(ct->name); /* 'name' = table */
  12. }

c++ 与 lua 简单交互参数介绍的更多相关文章

  1. cocos2d-x lua与c++简单交互

    cocos2d-x lua与c++简单交互 version: cocos2d-x 3.6 本文讲述lua与c++的一些简单交互: lua通过消息方式调用c++无参接口 c++调用lua带参接口 1.通 ...

  2. Linux系统IO分析工具之iotop常用参数介绍

      Linux系统IO分析工具之iotop常用参数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在一般运维工作中经常会遇到这么一个场景,服务器的IO负载很高(iostat中的 ...

  3. nc命令的常用参数介绍

    nc命令的常用参数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 想必做运维的应该在网络安全上都对一些开源软件都应该是相当的了解吧,比如tcpdump,namp等神奇,今天要给 ...

  4. Linux操作系统的文件查找工具locate和find命令常用参数介绍

    Linux操作系统的文件查找工具locate和find命令常用参数介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.非实时查找(数据库查找)locate工具  locate命 ...

  5. 2020年的UWP(3)——UWP和desktop extension的简单交互

    上一篇<2020年的UWP(2)--In Process App Service>中我们了解了UWP和Desktop Extension可以通过AppService进行数据交互.本篇我们就 ...

  6. Lua C++交互 应用实例步骤(UserData使用)

    一.配置Lua C++交互环境 1.下载Lua 包环境 地址: https://www.lua.org/download.html ,我们这里用的是5.4.2版本. 2.新建C++ 控制台应用程序 3 ...

  7. SQLMAP参数介绍

    转自:http://zhan.renren.com/bugpower?gid=3602888498044629629&checked=true SQLMAP参数介绍 sqlmap的使用方式:p ...

  8. G++ 参数介绍(转载)

    g++参数介绍 From: http://www.cnblogs.com/lidan/archive/2011/05/25/2239517.html gcc and g++分别是gnu的c & ...

  9. pentaho cde 画图参数介绍

    初步接触pentaho,由于在国内的资料很少,唯有看英文文档,做了N次反复尝试,挖掘了pentaho CDE中画图的一些基本参数. 下面就列出来了一些常用参数介绍: crosstabMode:表明如果 ...

随机推荐

  1. hdu 1907 John(anti nim)

    John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submis ...

  2. windows下安装virtualenvwrapper之后workon不是内部或外部指令

    virtualenvwrapper是虚拟环境的操作,在windows下需要使用以下命令安装: pip install virtualenvwrapper-win 安装win下的环境 相关操作:work ...

  3. window上脚本到linux上不能用

    window上的脚本一定要用dos2unix 文件名处理一下

  4. 分布式事务_01_2PC框架raincat快速体验

    一.前言 关于2PC的理论知识请见:分布式_理论_03_2PC 这一节我们来看下github上一个优秀的2PC分布式事务开源框架的快速体验. 二.源码 源码请见: https://github.com ...

  5. C/C++ 字符串与数字相互转换

    一.利用stringstream类 1. 字符串到整数     stringstream sstr(str);     int x;     sstr >> x;(即从sstr中提取数据) ...

  6. 重构代码 —— 函数即变量(Replace temp with Query)

    函数即变量,这里的函数指的是返回值为某一对象的函数.Replace temp with query,query 是一种查询函数. example 1 double price() { return t ...

  7. THUPC2017 小 L 的计算题

    求 $k=1,2,\cdots,n \space \space \sum\limits_{i=1}^n a_i^k$ $n \leq 2 \times 10^5$ sol: 时隔多年终于卡过去了 之前 ...

  8. HihoCoder1105 题外话·堆(基础二叉搜索树)

    第1行为1个整数N,表示需要处理的事件数目. 接下来的M行,每行描述一个事件,且事件类型由该行的第一个字符表示,如果为'A',表示小Ho将一粒糖果放进了盒子,且接下来为一个整数W,表示这颗糖果的重量: ...

  9. LeetCode Max Consecutive Ones II

    原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-ii/ 题目: Given a binary array, find the ma ...

  10. LoadRunner常见错误代码解决方案

    一.LoadRunner26377错误 通常不是长度的问题,如果长度有问题就都出错了.应该是服务器返回的数据问题,导致没有找到这个参数. 26377错误的成因很多,总结最直接的结果就是No match ...