簡體   English   中英

luaL_dostring什么都不放在堆棧上?

[英]luaL_dostring puts nothing on the stack?

我正在嘗試學習Lua與C ++接口的基礎知識,但我遇到了一個問題。 我想調用一個返回字符串的函數,然后使用C ++端的字符串,但是luaL_dostring似乎沒有在Lua堆棧上放任何東西。

即使是簡單的測試似乎也不能正常工作:

lua_State* lua = lua_open();
luaL_openlibs(lua);

//Test dostring.
luaL_dostring(lua, "return 'derp'");

int top = lua_gettop(lua);
cout << "stack top is " <<top << endl;

//Next, test pushstring.
lua_pushstring(lua, "derp");

top = lua_gettop(lua);
cout << "stack top is " << top << endl;

輸出:

stack top is 0
stack top is 1

有任何想法嗎?

啊哈,發現了問題。 根據這個頁面 ,在Lua 5.1中luaL_dostring忽略了返回。 我的代碼可能適用於Lua 5.2。

要更改功能,您應該使用:

#undef luaL_dostring
#define luaL_dostring(L,s)  \
    (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM