Accessing Lua global variables from c++

來源:互聯網
上載者:User

標籤:

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

 

Calling Lua scripts from c++’s example was written in post How to embed Lua 5.1 in C++. Now, let us look at how to access Lua’s global variables from c++.

Value passing between c++ and Lua rely on Lua stack. Stack is a data structure based on the principle of Last In First Out (LIFO). This is very important keep in mind while coding with C API of Lua.

P.S: I am using Lua 5.1.3’s C API.

 

01 //aconf.cpp
02 extern "C" {
03 #include "lua.h"
04 #include "lualib.h"
05 #include "lauxlib.h"
06 }
07  
08 int main()
09 {
10     int var1=0,var2=0;
11  
12     lua_State *L = lua_open();
13     luaL_openlibs(L);
14     if (luaL_loadfile(L, "config.lua") || lua_pcall(L, 0, 0, 0))
15     {
16         printf("error: %s", lua_tostring(L, -1));
17         return -1;
18     }
19  
20     lua_getglobal(L, "var1");
21     lua_getglobal(L, "var2");
22     if (!lua_isnumber(L, -2)) {
23         printf ("`var1‘ should be a number/n");
24         return -1;
25     }
26     if (!lua_isnumber(L, -1))
27     {
28         printf("`var2 should be a number/n");
29         return -1;
30     }
31     var1 = (int)lua_tonumber(L, -2);
32     var2 = (int)lua_tonumber(L, -1);
33     printf("var1: %d/nvar2: %d/n",var1, var2);
34     lua_close(L);
35  
36     return 0;
37 }

The Lua script “config.lua”:

var1=12var2=34

Compilation line:

g++ -o aconf{,.cpp} -llua -ldl

if (luaL_loadfile(L, "config.lua") || lua_pcall(L, 0, 0, 0)) can be written asluaL_dofile(L,"config.lua"), it does the same thing. By calling lua_getglobal(L, "var1");, Lua will push the value of var1 to the stack. When execute getglobal for “var2″, again it will push var2’s value to the stack, var2 will now stack on top of var1.

The most top of the stack will be assign as logical address -1. Stack address -2 will be at below the stack -1. Therefore to access var1, you have to point to -2. For more info regarding Lua stack, can readHERE.

To check the value type in the stack -2 (var1) is it number, we can use this:
lua_isnumber(L, -2)

To access the value to number, we do this:
lua_tonumber(L, -2)

As lua_tonumber(L, -2) will return as double, therefore we must add (int) in front of the function call. Well, we should use lua_tointeger(L, -2) in this case.

Again, I can access var1 and var2 one by one, therefore you access both variables from the top stack.

1 lua_getglobal(L, "var1");
2 var1=lua_tointeger(L,-1);
3 lua_getglobal(L, "var2");
4 var2=lua_tointeger(L,-1);

When accessing Lua script and trigger exception, the error message will always push to the stack, that why We can print out the message when errors detected like this:

printf("error: %s", lua_tostring(L, -1));

Accessing simple variables is simple, but for accessing table data type, you need more steps, the Lua online ebook does cover that.

Accessing Lua global variables from c++

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.