和我一起写lua - C和lua的参数传递与返回值
来源:互联网 发布:智多星软件教学视频 编辑:程序博客网 时间:2024/06/02 09:07
lua通过一个运行时栈来维护参数传递及返回,使用lua_to*等函数获取lua传递到C函数的参数,使用lua_push*从C函数返回值到lua脚本。此外也可以使用lua_getglobal从C函数获取lua脚本定义的全局变量。具体参看例子(test_lua.c):
lua脚本(my.lua)如下所示(在lua脚本中,如果变量不用local明确声明为局部变量,则默认为全局变量):
执行结果:
http://blog.chinaunix.net/uid-16175292-id-3472702.html
点击(此处)折叠或打开
- #include <lua.h>
- #include <lauxlib.h>
- #include <stdlib.h> /* For function exit() */
- #include <stdio.h> /* For input/output */
- void bail(lua_State *L, char *msg){
- fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",
- msg, lua_tostring(L, -1));
- exit(1);
- }
- int lua_func_from_c(lua_State *L)
- {
- printf("This is C\n");
- int argc = lua_gettop(L); /* number of arguments */
- const char * str = lua_tostring(L,1); /* the first argument: string */
- int num = lua_tonumber(L,2); /* the second argument: number */
-
- printf("The first argument: %s\n", str);
- printf("The second argument: %d\n", num);
- lua_getglobal(L,"global_var");
- const char * global_str = lua_tostring(L,-1);
- printf("global_var is %s\n", global_str);
- int the_second_ret = 2*num;
- lua_pushstring(L, "the first return");
- lua_pushnumber(L, the_second_ret);
- return 2; /* tell lua how many variables are returned */
- }
- int main(int argc, const char *argv[])
- {
- if(argc != 2)
- {
- return 1;
- }
- lua_State *L = luaL_newstate(); /* Create new lua state variable */
- /* Load Lua libraries, otherwise, the lua function in *.lua will be nil */
- luaL_openlibs(L);
-
- /* register new lua function in C */
- lua_register(L, "lua_func_from_c", lua_func_from_c);
- if( luaL_loadfile(L,argv[1]) ) /* Only load the lua script file */
- bail(L, "luaL_loadfile() failed");
- if( lua_pcall(L,0,0,0) ) /* Run the loaded lua file */
- bail(L, "lua_pcall() failed");
- lua_close(L); /* Close the lua state variable */
- return 0;
- }
点击(此处)折叠或打开
- print("Hello world")
- global_var = "this is a global string"
- first, second = lua_func_from_c("the first one", 2)
- print("the first returned", first)
- print("the second returned", second)
$ ./a.out my.lua
Hello world
This is C
The first argument: the first one
The second argument: 2
global_var is this is a global string
the first returnedthe first return
the second returned4
0 0
- 和我一起写lua - C和lua的参数传递与返回值
- 和我一起写lua - C和lua的参数传递与返回值
- 和我一起写lua - 使用C扩展lua
- 和我一起写lua - C调用lua函数
- 和我一起写lua - lua模块管理
- 和我一起写lua - Hello world
- 和我一起写lua - 确认操作系统
- 和我一起写lua - Mac OS X环境编译C模块
- lua和c互调 参数传递
- C++获取Lua全局变量和执行Lua多参数多返回值函数
- lua和c的交互
- lua和c的交互
- lua和c的交互
- lua和c的交互
- lua和c的交互
- lua和c的交互
- lua和c的交互
- lua和c的交互
- hdu 1048 用string
- async whilst使用注意
- 用Dialog实现单选对话框
- poj3450Corporate Identity-后缀数组
- js中获取上下文路径的方法
- 和我一起写lua - C和lua的参数传递与返回值
- ROS学习之cpp教程
- pthread_exit
- STM32串口使用IDLE中断接收不定长数据原理与源程序
- python unicode
- OC中的内存管理
- NSOperationQueue没有必要反复创建临时变量,可以使用一个强引用的NSOperationQueue
- Android学习笔记--AIDL
- CentOS6.7 下安装JDK