Calling C + + function from Lua, implement sleep function

Source: Internet
Author: User

http://blog.csdn.net/cnjet/article/details/5909548

You can ' t call C function directly from Lua, and you are having to create a wrapper function, which allows calling from Lua. In this post, the shows a simple example to implement millisecond the 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 is through Lua Stack, therefore, the wrapper function must pass Lua Stat e as the only parameter.
How is the real function call parameters? We can get it from the stack using Lua_tonumber (), lua_tostring () etc. To return result of the 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 staticintFunction_Name (lua_State *L) {
2   inti = lua_tointeger(L, 1);  /* get argument */
3   /* carry on the procedures here ... */
4   lua_pushinteger(L, sin(d));  /* push result */
5   return1;  /* number of results */
6 }

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 the last, to trigger a dofile ().

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 intL_MSleep(lua_State* l)
10 {
11     intmilisec=0;
12     structtimespec req={0};
13     time_tsec;
14
15     milisec=luaL_optint(l,1,0); // obtain parameter
16
17     if(milisec==0)
18        return0;
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     return1;
30 }
31
32 intmain()
33 {
34
35     conststatic structluaL_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     return0;
51 }

From the example above, we implemented Msleep () this 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,

 luaL_optint(Lua StateStack NumberDefault Value)

If the param specified by LUA function call was not integer, the default value would be assigned.

So let's see how we can call this from Lua script.

for i=1,9,1 do    io.write(string.format("[%d] Hello/n",i))    misc.msleep(1000) -- sleep 1 secend

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-#Calling C from Lua

Calling C + + function from Lua, implement sleep function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.