回答編集履歴
2
C++ライブラリ「Sol2」を使ったプログラムを追加しました。
answer
CHANGED
@@ -111,7 +111,7 @@
|
|
111
111
|
printf("index %3d : type = %s \n", idx, lua_typename(L, type));
|
112
112
|
break;
|
113
113
|
}
|
114
|
-
|
114
|
+
[リンク内容](https://github.com/ThePhD/sol2)
|
115
115
|
}
|
116
116
|
|
117
117
|
// スタックのアイテムの内容を一覧で出力する
|
@@ -130,4 +130,51 @@
|
|
130
130
|
}
|
131
131
|
printf("-------------------\n");
|
132
132
|
}
|
133
|
+
```
|
134
|
+
|
135
|
+
もう一つですが、[Sol2](https://github.com/ThePhD/sol2)というC++用のライブラリを使えば
|
136
|
+
例えば以下の Luaスクリプトで定義されているテーブル、グローバル変数、テーブルをいとも簡単に使えるようになっています。
|
137
|
+
|
138
|
+
``` lua
|
139
|
+
-- test.lua
|
140
|
+
function init()
|
141
|
+
return {
|
142
|
+
width = 640,
|
143
|
+
height = 480
|
144
|
+
};
|
145
|
+
end
|
146
|
+
|
147
|
+
function initmoji()
|
148
|
+
return {
|
149
|
+
returnmoji = "moji"
|
150
|
+
};
|
151
|
+
end
|
152
|
+
```
|
153
|
+
この init関数とinitmoji関数は戻り値としてテーブルを返します。このLuaスクリプトをSol2で書くと以下のように取得できます。
|
154
|
+
```cpp
|
155
|
+
#include <iostream>
|
156
|
+
#include <sol.hpp>
|
157
|
+
using namespace std;
|
158
|
+
|
159
|
+
int main()
|
160
|
+
{
|
161
|
+
sol::state lua;
|
162
|
+
lua.open_libraries(sol::lib::base, sol::lib::coroutine, sol::lib::debug, sol::lib::io, sol::lib::math,
|
163
|
+
sol::lib::os, sol::lib::package, sol::lib::string, sol::lib::table, sol::lib::utf8);
|
164
|
+
|
165
|
+
try {
|
166
|
+
lua.safe_script_file("test.lua");
|
167
|
+
}
|
168
|
+
catch (sol::error& e) {
|
169
|
+
cout << e.what() << endl;
|
170
|
+
}
|
171
|
+
|
172
|
+
// Solを使えば簡単にテーブルから値が取り出せます。
|
173
|
+
sol::table ret = lua["init"]();
|
174
|
+
cout << "width:" << ret["width"].get<int>() << endl;
|
175
|
+
cout << "height:" << ret["height"].get<int>() << endl;
|
176
|
+
|
177
|
+
sol::table ret2 = lua["initmoji"]();
|
178
|
+
cout << ret2["returnmoji"].get<std::string>() << endl;
|
179
|
+
}
|
133
180
|
```
|
1
Luaのスタック状態を把握する関数を追加
answer
CHANGED
@@ -82,4 +82,52 @@
|
|
82
82
|
|
83
83
|
// Luaのスタックを空っぽにしておく
|
84
84
|
lua_settop(L, 0);
|
85
|
+
```
|
86
|
+
|
87
|
+
Luaのスタックを把握するために以下のような関数を作っておくと便利です。
|
88
|
+
```cpp
|
89
|
+
// スタックの指定インデックスのアイテムの内容を表示する
|
90
|
+
static void PrintStackItem(lua_State *L, int idx)
|
91
|
+
{
|
92
|
+
int type = lua_type(L, idx);
|
93
|
+
switch (type) {
|
94
|
+
case LUA_TSTRING:
|
95
|
+
// 文字列アイテムの内容表示
|
96
|
+
printf("index %3d : type = %s\t: \"%s\"\n",
|
97
|
+
idx, lua_typename(L, type), lua_tostring(L, idx));
|
98
|
+
break;
|
99
|
+
case LUA_TNUMBER:
|
100
|
+
// 数値アイテムの内容表示
|
101
|
+
printf("index %3d : type = %s : %f\n",
|
102
|
+
idx, lua_typename(L, type), lua_tonumber(L, idx));
|
103
|
+
break;
|
104
|
+
case LUA_TBOOLEAN:
|
105
|
+
// ブール値アイテムの内容表示
|
106
|
+
printf("index %3d : type = %s : \"%s\"\n",
|
107
|
+
idx, lua_typename(L, type), lua_toboolean(L, idx) ? "true" : "false");
|
108
|
+
break;
|
109
|
+
default:
|
110
|
+
// その他ならば型だけ表示
|
111
|
+
printf("index %3d : type = %s \n", idx, lua_typename(L, type));
|
112
|
+
break;
|
113
|
+
}
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
// スタックのアイテムの内容を一覧で出力する
|
118
|
+
void PrintStack(lua_State *L)
|
119
|
+
{
|
120
|
+
printf("----- stack -----\n");
|
121
|
+
int top = lua_gettop(L);
|
122
|
+
// 正のインデックスで指定
|
123
|
+
for (int i = top; i >= 1; i--) {
|
124
|
+
PrintStackItem(L, i);
|
125
|
+
}
|
126
|
+
printf("-------------------\n");
|
127
|
+
// 負のインデックスで指定
|
128
|
+
for (int i = -1; i >= -top; i--) {
|
129
|
+
PrintStackItem(L, i);
|
130
|
+
}
|
131
|
+
printf("-------------------\n");
|
132
|
+
}
|
85
133
|
```
|