回答編集履歴

2

C++ライブラリ「Sol2」を使ったプログラムを追加しました。

2017/12/28 05:30

投稿

naohiro19_
naohiro19_

スコア178

test CHANGED
@@ -224,42 +224,136 @@
224
224
 
225
225
  }
226
226
 
227
+ [リンク内容](https://github.com/ThePhD/sol2)
228
+
229
+ }
230
+
231
+
232
+
233
+ // スタックのアイテムの内容を一覧で出力する
234
+
235
+ void PrintStack(lua_State *L)
236
+
237
+ {
238
+
239
+ printf("----- stack -----\n");
240
+
241
+ int top = lua_gettop(L);
242
+
243
+ // 正のインデックスで指定
244
+
245
+ for (int i = top; i >= 1; i--) {
246
+
247
+ PrintStackItem(L, i);
248
+
249
+ }
250
+
251
+ printf("-------------------\n");
252
+
253
+ // 負のインデックスで指定
254
+
255
+ for (int i = -1; i >= -top; i--) {
256
+
257
+ PrintStackItem(L, i);
258
+
259
+ }
260
+
261
+ printf("-------------------\n");
262
+
263
+ }
264
+
265
+ ```
266
+
267
+
268
+
269
+ もう一つですが、[Sol2](https://github.com/ThePhD/sol2)というC++用のライブラリを使えば
270
+
271
+ 例えば以下の Luaスクリプトで定義されているテーブル、グローバル変数、テーブルをいとも簡単に使えるようになっています。
272
+
273
+
274
+
275
+ ``` lua
276
+
277
+ -- test.lua
278
+
279
+ function init()
280
+
281
+ return {
282
+
283
+ width = 640,
284
+
285
+ height = 480
286
+
287
+ };
288
+
289
+ end
290
+
291
+
292
+
293
+ function initmoji()
294
+
295
+ return {
296
+
297
+ returnmoji = "moji"
298
+
299
+ };
300
+
301
+ end
302
+
303
+ ```
304
+
305
+ この init関数とinitmoji関数は戻り値としてテーブルを返します。このLuaスクリプトをSol2で書くと以下のように取得できます。
306
+
307
+ ```cpp
308
+
309
+ #include <iostream>
310
+
311
+ #include <sol.hpp>
312
+
313
+ using namespace std;
314
+
315
+
316
+
317
+ int main()
318
+
319
+ {
320
+
321
+ sol::state lua;
322
+
323
+ lua.open_libraries(sol::lib::base, sol::lib::coroutine, sol::lib::debug, sol::lib::io, sol::lib::math,
324
+
325
+ sol::lib::os, sol::lib::package, sol::lib::string, sol::lib::table, sol::lib::utf8);
326
+
227
327
 
228
328
 
329
+ try {
330
+
331
+ lua.safe_script_file("test.lua");
332
+
229
- }
333
+ }
230
-
231
-
232
-
233
- // スタックのアイテムの内容を一覧で出力する
334
+
234
-
235
- void PrintStack(lua_State *L)
236
-
237
- {
238
-
239
- printf("----- stack -----\n");
240
-
241
- int top = lua_gettop(L);
335
+ catch (sol::error& e) {
242
-
243
- // 正のインデックスで指定
336
+
244
-
245
- for (int i = top; i >= 1; i--) {
337
+ cout << e.what() << endl;
246
-
247
- PrintStackItem(L, i);
338
+
248
-
249
- }
339
+ }
250
-
251
- printf("-------------------\n");
340
+
252
-
341
+
342
+
253
- // 負のインデックスで指定
343
+ // Solを使えば簡単にテーブルから値が取り出せます。
254
-
344
+
255
- for (int i = -1; i >= -top; i--) {
345
+ sol::table ret = lua["init"]();
346
+
256
-
347
+ cout << "width:" << ret["width"].get<int>() << endl;
348
+
349
+ cout << "height:" << ret["height"].get<int>() << endl;
350
+
351
+
352
+
257
- PrintStackItem(L, i);
353
+ sol::table ret2 = lua["initmoji"]();
354
+
258
-
355
+ cout << ret2["returnmoji"].get<std::string>() << endl;
356
+
259
- }
357
+ }
260
-
261
- printf("-------------------\n");
358
+
262
-
263
- }
264
-
265
- ```
359
+ ```

1

Luaのスタック状態を把握する関数を追加

2017/12/28 05:30

投稿

naohiro19_
naohiro19_

スコア178

test CHANGED
@@ -167,3 +167,99 @@
167
167
  lua_settop(L, 0);
168
168
 
169
169
  ```
170
+
171
+
172
+
173
+ Luaのスタックを把握するために以下のような関数を作っておくと便利です。
174
+
175
+ ```cpp
176
+
177
+ // スタックの指定インデックスのアイテムの内容を表示する
178
+
179
+ static void PrintStackItem(lua_State *L, int idx)
180
+
181
+ {
182
+
183
+ int type = lua_type(L, idx);
184
+
185
+ switch (type) {
186
+
187
+ case LUA_TSTRING:
188
+
189
+ // 文字列アイテムの内容表示
190
+
191
+ printf("index %3d : type = %s\t: \"%s\"\n",
192
+
193
+ idx, lua_typename(L, type), lua_tostring(L, idx));
194
+
195
+ break;
196
+
197
+ case LUA_TNUMBER:
198
+
199
+ // 数値アイテムの内容表示
200
+
201
+ printf("index %3d : type = %s : %f\n",
202
+
203
+ idx, lua_typename(L, type), lua_tonumber(L, idx));
204
+
205
+ break;
206
+
207
+ case LUA_TBOOLEAN:
208
+
209
+ // ブール値アイテムの内容表示
210
+
211
+ printf("index %3d : type = %s : \"%s\"\n",
212
+
213
+ idx, lua_typename(L, type), lua_toboolean(L, idx) ? "true" : "false");
214
+
215
+ break;
216
+
217
+ default:
218
+
219
+ // その他ならば型だけ表示
220
+
221
+ printf("index %3d : type = %s \n", idx, lua_typename(L, type));
222
+
223
+ break;
224
+
225
+ }
226
+
227
+
228
+
229
+ }
230
+
231
+
232
+
233
+ // スタックのアイテムの内容を一覧で出力する
234
+
235
+ void PrintStack(lua_State *L)
236
+
237
+ {
238
+
239
+ printf("----- stack -----\n");
240
+
241
+ int top = lua_gettop(L);
242
+
243
+ // 正のインデックスで指定
244
+
245
+ for (int i = top; i >= 1; i--) {
246
+
247
+ PrintStackItem(L, i);
248
+
249
+ }
250
+
251
+ printf("-------------------\n");
252
+
253
+ // 負のインデックスで指定
254
+
255
+ for (int i = -1; i >= -top; i--) {
256
+
257
+ PrintStackItem(L, i);
258
+
259
+ }
260
+
261
+ printf("-------------------\n");
262
+
263
+ }
264
+
265
+ ```