用LUA扩展cpp程序的几个测试
来源:互联网 发布:校园网络拓扑结构设计 编辑:程序博客网 时间:2024/05/22 03:26
Lua提供的很多特性(垃圾收集,高效的字符串处理,便捷的开发)都是很吸引人的,能够把C中复杂的业务逻辑剥离到LUA脚本中处理的话会方便不少。下面做的几组测试示例分别展示了C语言如何调用Lua脚本,调用Lua函数以及Lua脚本如何调用C中自定义函数。
测试一:最经典的Hello World
main.cpp
extern "C" { #include "lua.h"#include "lualib.h"#include "luaconf.h"#include "lauxlib.h"}int main(void){//create a new lua statelua_State *L = luaL_newstate();//open all standard lua libraries into given stateluaL_openlibs(L);//load and run the given fileluaL_dofile(L, "test.lua");//destroy all objects in the given lua statelua_close(L);return 0;}test.lua
print("Hello world")
测试二:从Lua脚本获取width和height的值
main.cpp
extern "C" { #include "lua.h"#include "lualib.h"#include "luaconf.h"#include "lauxlib.h"}int main(void){int width,height;lua_State *L = luaL_newstate();//to have access to certain librariesluaopen_base(L);luaopen_io(L);luaopen_string(L);//load the given file and run it as a functionif (luaL_loadfile(L, "test.lua") || lua_pcall(L, 0, 0, 0)){luaL_error(L, "cannot run configuration file: %s",lua_tostring(L, -1));}//pushes onto the stack the value of the global variableslua_getglobal(L, "width");lua_getglobal(L, "height");if(!lua_isnumber(L, -2))luaL_error(L, "'width' should be a number\n");if(!lua_isnumber(L, -1))luaL_error(L, "'height' should be a number\n");width = (int)lua_tonumber(L, -2);height = (int)lua_tonumber(L, -1);printf("width is %d , height is %d\n", width, height);lua_close(L);return 0;}
test.lua
width = 200height = 300
测试三:调用Lua函数实现两个数相加
main.cpp
extern "C" { #include "lua.h"#include "lualib.h"#include "luaconf.h"#include "lauxlib.h"}int main(void){int x = 3, y = 4, z;lua_State *L = luaL_newstate();luaopen_base(L);luaopen_io(L);luaopen_string(L);if (luaL_loadfile(L, "test.lua") || lua_pcall(L, 0, 0, 0)){luaL_error(L, "cannot load lua file: %s",lua_tostring(L, -1));}//push function and argumentslua_getglobal(L, "add");lua_pushnumber(L, x);lua_pushnumber(L, y);//call functionif (lua_pcall(L, 2, 1, 0) != 0)luaL_error(L, "error running function add\n");//get resultif(!lua_isnumber(L, -1))luaL_error(L, "function should return a number\n");z = lua_tonumber(L, -1);printf("x + y = %d\n", z);lua_close(L);return 0;}
test.lua
function add(x, y)return x + yend
测试四:调用Lua函数实现字符串截取前六位
main.cpp
extern "C" { #include "lua.h"#include "lualib.h"#include "luaconf.h"#include "lauxlib.h"}int main(void){char *str = "Hello World";char *substr;lua_State *L = luaL_newstate();luaL_openlibs(L);if (luaL_loadfile(L, "test.lua") || lua_pcall(L, 0, 0, 0)){luaL_error(L, "cannot load lua file: %s",lua_tostring(L, -1));}//push function and argumentslua_getglobal(L, "substr5");lua_pushstring(L, str);//call functionif (lua_pcall(L, 1, 1, 0) != 0)luaL_error(L, "error running function substr5. %s\n", lua_tostring(L, -1));substr = (char *)lua_tostring(L, -1);printf("substring of '%s' is %s\n", str, substr);lua_close(L);return 0;}
test.lua
function substr5(str)return string.sub(str,0,5)end测试五:Lua中调用自定义C函数实现计算sin值
main.cpp
extern "C" { #include "lua.h"#include "lualib.h"#include "luaconf.h"#include "lauxlib.h"}#include<math.h>static int l_sin (lua_State *L){//get argumentsdouble d = lua_tonumber(L, 1);//push resultslua_pushnumber(L, sin(d));//number of resultsreturn 1;}int main(void){char *str = "Hello World";char *substr;lua_State *L = luaL_newstate();luaL_openlibs(L);//push function and set it to global varaiblelua_pushcfunction(L, l_sin);lua_setglobal(L, "mysin");if (luaL_loadfile(L, "test.lua") || lua_pcall(L, 0, 0, 0)){luaL_error(L, "cannot load lua file: %s",lua_tostring(L, -1));}lua_close(L);return 0;}
test.lua
print(mysin(1))
Lua和C两者互相的调用是通过相同类型的栈来传递函数名,参数,返回值的,简单易用。
项目的环境和示例程序可在http://dl.dbank.com/c0djezo1b6这里下载得到
- 用LUA扩展cpp程序的几个测试
- 几个cpp程序
- 探索LUA用于API级的测试中(用C对Lua进行扩展)
- 初步的测试程序--main.cpp
- javamail的几个测试程序!
- 几个chrome扩展程序
- 用extern对外部变量作提前引用声明,以扩展程序文件的作用域.Cpp
- 用lua扩展你的Nginx
- 用lua扩展你的Nginx
- 用lua扩展你的Nginx
- vs2012 cpp与lua的hello world
- lua建的项目用于CPP注意事项:
- 单链表结构测试程序(Linktest.cpp)
- Lua相关的扩展库
- Lua String 的扩展函数
- lua 的几个常识例子
- 扩展 JUnit 测试并行程序
- 扩展 JUnit 测试并行程序
- glReadPixels
- oracle flash query
- VC6.0工程设置介绍
- Python_API_os.path_relpath_待填充
- startup_LPC11xx.s启动代码分析
- 用LUA扩展cpp程序的几个测试
- VC2010 代码字体及颜色设置
- 堆(heap)和栈(stack)、内存泄漏(memory leak)和内存溢出
- //8----14//指向数组的指针作函数参数
- c++中vector增长策略详解(很有用的哦)
- oracle Flashback DROP
- Python_API_ZipFile_write_待填充
- 8----14 不完整的
- Python_API_ZipFile_close_待填充