lua c++ (一)
来源:互联网 发布:js延时函数sleep 编辑:程序博客网 时间:2024/06/05 17:16
lua 脚本
function add(a, b)return a+b;end
为了调用一个LUA函数,我们首先要把函数压栈。
这个函数的结果由参数决定,所以,我们要调用函数将需要
lua_call(),调用这个函数之后,返回的结果将在堆栈中存在。
整个步骤:
1.用lua_getglobal()把add函数放入堆栈
2.用lua_pushnumber()把第一个参数压入堆栈
3.用lua_pushnumber()把第二个参数压入堆栈
4.用lua_call()调用函数。
5,现在用lua_tonumber从堆栈头取出结果
6,最后用lua_pop从堆栈中移除结果值。
extern "C"{#include <lua.h>#include <lualib.h>#include <lauxlib.h>}#include <iostream>#include <string>using namespace std;lua_State *pL;int luaAdd(int x, int y){int sum;/*--首先将函数名压入栈*/lua_getglobal(pL, "add");lua_pushnumber(pL, x);lua_pushnumber(pL, y);/*--call the function with 2 args ,return 1 result*/lua_call(pL, 2, 1);/*--get the result*/sum = (int)lua_tonumber(pL, -1);lua_pop(pL, 1);return num;}int main(){pL = lua_open();luaL_openlibs(pL);luaL_dofile(pL, "1029.lua");int sum = luaAdd(10, 19);cout<<"the sum is "<<sum<<endl;lua_close(pL);return 0;}
- 一、C Call Lua
- lua与c交互 一
- Lua与C通信——Lua API(一)
- lua的入门教程-- C++(一)
- objective-c与Lua的交互(一)
- C/C++ Lua交互(一)
- C语言和Lua交互(一)
- lua -->c c-->lua
- c语言与lua交互(capi)介绍, lua调用C语言函数详解(一)
- lua与c (一)Mac下c 和lua环境搭建
- Lua(一)
- lua -->c
- Lua C
- Lua和C语言的交互(一)
- Lua5.1代码阅读(一):lua.c
- Lua和C语言的交互(一)
- 每日一Lua(6)-lua与c的交互及学习总结
- lua源码学习(一)lua的c api外围实现
- MIPS Linux 最初启动流程
- 莫西的伤感情侣日志推荐:分手的那一刻,我们变得陌生
- 英语练习105 Turn right
- asp.net 在网页上签入pdf阅读器,不能打开文件名是中文的文件
- Android onKeyDown
- lua c++ (一)
- matlab 获取当前文件所在路径的上一级路径
- 在UINavigationController下的-(void)viewWillAppear:方法失效的解决办法
- Android ANR问题的解决
- RAD(Rational Application Develop,快速应用开发)模型
- net导入和导出
- jQuery 选择器
- CAS单点登录
- hibernate merge和saveOrUpdate方法