calling c++ function from Lua, implement sleep function
2010-09-27 13:51
381 查看
You can’t call c function directly from Lua, you have to create a wrapper function that allows calling from Lua. In this post, shows a simple example to implement millisecond sleep function to Lua using nanosleep.
To allow function call from Lua script, your function must obey certain format. As variable passing from Lua to C and vice versa are through Lua Stack, therefore, the wrapper function must pass Lua State as the only parameter.
How about the real function call parameters? We can get it from the stack using lua_tonumber(), lua_tostring() etc. To return result of function call, we use lua_pushnumber(), lua_pushstring() etc. Because Lua function allows return more than 1 results, therefore you need tell Lua by returning an integer value.
We need to setup a library to contain your functions. By doing this, construct a static structure in array. Next, we load up our lib by calling luaL_openlib() and at last, to trigger a dofile().
Lets check out the sample codes on how to implement msleep function to Lua.
From the example above, we implemented msleep() that takes only 1 parameter, and we created a library “misc” in Lua. luaL_optint() is actually a clean version of doing lua_isinteger() and lua_tointeger(). luaL_optint() takes 3 parameter,
If the param specified by lua function call is not integer, the default value will be assigned.
So let see how we can call that from Lua script.
Check out for more details from Programming in Lua Chapter 26 [1].
And more Lua examples here, http://cc.byexamples.com/category/lua/.
Reference:
[1] Programming In Lua, Chapter 26 #Calling C from Lua
To allow function call from Lua script, your function must obey certain format. As variable passing from Lua to C and vice versa are through Lua Stack, therefore, the wrapper function must pass Lua State as the only parameter.
How about the real function call parameters? We can get it from the stack using lua_tonumber(), lua_tostring() etc. To return result of function call, we use lua_pushnumber(), lua_pushstring() etc. Because Lua function allows return more than 1 results, therefore you need tell Lua by returning an integer value.
1 | static int Function_Name (lua_State *L) { |
2 | int i = lua_tointeger(L, 1); /* get argument */ |
3 | /* carry on the procedures here ... */ |
4 | lua_pushinteger(L, sin (d)); /* push result */ |
5 | return 1; /* number of results */ |
6 | } |
Lets check out the sample codes on how to implement msleep function to Lua.
01 | extern "C" { |
02 | #include "lua.h" |
03 | #include "lualib.h" |
04 | #include "lauxlib.h" |
05 | } |
06 |
07 | #include<time.h> |
08 |
09 | int L_MSleep(lua_State* l) |
10 | { |
11 | int milisec=0; |
12 | struct timespec req={0}; |
13 | time_t sec; |
14 |
15 | milisec=luaL_optint(l,1,0); // obtain parameter |
16 |
17 | if (milisec==0) |
18 | return 0; |
19 |
20 | sec=( int )(milisec/1000); |
21 |
22 | milisec=milisec-(sec*1000); |
23 | req.tv_sec=sec; |
24 | req.tv_nsec=milisec*1000000L; |
25 |
26 | while (nanosleep(&req,&req)==-1) |
27 | continue ; |
28 |
29 | return 1; |
30 | } |
31 |
32 | int main() |
33 | { |
34 |
35 | const static struct luaL_reg misc [] = { |
36 | { "msleep" , &L_MSleep}, |
37 | {NULL,NULL} //must! |
38 | }; |
39 |
40 | lua_State *L = lua_open(); |
41 | luaL_openlibs(L); |
42 | //open your lib |
43 | luaL_openlib(L, "misc" , misc, 0); |
44 |
45 | if (luaL_loadfile(L, "callc.lua" ) || lua_pcall(L, 0, 0, 0)) |
46 | printf ( "error: %s" , lua_tostring(L, -1)); |
47 |
48 | lua_close(L); |
49 |
50 | return 0; |
51 | } |
luaL_optint(Lua State, Stack Number, Default Value)
If the param specified by lua function call is not integer, the default value will be assigned.
So let see how we can call that from Lua script.
for i=1,9,1 do io.write(string.format("[%d] Hello/n",i)) misc.msleep(1000) -- sleep 1 sec end
Check out for more details from Programming in Lua Chapter 26 [1].
And more Lua examples here, http://cc.byexamples.com/category/lua/.
Reference:
[1] Programming In Lua, Chapter 26 #Calling C from Lua
相关文章推荐
- Calling Lua function from C++
- AHK Calling an lua-function from ahk
- Linker error calling C-Function from Objective-C++
- Calling Lua function from C#
- Calling C++ Functions From Lua(lua调用c/c++)
- Calling Fortran function and subroutines from a C or C++ function
- Dreamweaver 扩展开发: Calling a C++ function from JavaScript
- LUA Sleep Function
- 今天碰到一个 C++ from C#: C++ function (in a DLL) returning false, but C# thinks it's true! 惊呆了!
- calling a __host__ function("XXX") from a __global__ function is not allowed
- C++ Sleep Function 用法 Sleep(-1)
- Programming Basic--why we need to add ' extern "C" ' in C++ program when calling the function which is complied by C complier?
- Calling lua functions from .lua's using handles?
- Calling a v8 javascript function from c++ with an argument
- lua call c/c++ function
- C++: 为什么我们一般都要将函数的声明和定义分开?(separate the declaration of function from its definition)
- How do I call a C++ function from C?
- Calling Managed .NET C# COM Objects from Unmanaged C++ Code
- Calling C++ DLLs from VC++
- Accessing Lua global variables from c++