Debian Codegraph @

src/dmlc-core/include/dmlc/lua.h

raw symbols SCIP
1 /*!
2 * Copyright (c) 2016 by Contributors
3 * \file lua.h
4 * \brief C++11 header only interface to easily interact with Lua and Torch.
5 * This code is evolved from torch plugin code for MXNet.
6 *
7 * This header will require Torch and Lua to be presented, do not include.
8 *
9 * \author Junyuan Xie, Min Lin, Tianqi Chen
10 *
11 * \code
12 *
13 * // Example code to use the lua module.
14 * dmlc::LuaState* lua = dmlc::LuaState::ThreadLocalState();
15 * // vectors converts automatically to lua table.
16 * auto tbl = lua->Convert(std::vector<int>{1,2,3});
17 * // use eval to get lua reference, this is a function
18 * auto print = lua->Eval("return function(x) print(x) end");
19 * // lua function can be directly called from c++, arguments are converted.
20 * print(100);
21 *
22 * // set field in the table.
23 * tbl.SetField("square", lua->Eval("return function(x) x*x end"));
24 * // call the function, covert back to C++ values.
25 * int x = tbl["square"](100).Get<int>();
26 *
27 * \endcode
28 */
29 #ifndef DMLC_LUA_H_
30 #define DMLC_LUA_H_
31
32 extern "C" {
33 #include <lua.h>
34 #include <lualib.h>
35 #include <luaT.h>
36 }
37
38 #include <algorithm>
39 #include <memory>
40 #include <mutex>
41 #include <stdexcept>
42 #include <string>
43 #include <tuple>
44 #include <type_traits>
45 #include <unordered_map>
46 #include <utility>
47 #include <vector>
48
49 #include "./base.h"
50 #include "./logging.h"
51 #include "./thread_local.h"
52
53 namespace dmlc {
54
55 // forward declare torch state
56 class LuaState;
57
58 namespace lua_stack {
59 template <typename T>
60 struct Handler;
61 };
62
63 /*! \brief an reference to lua object */
64 class LuaRef {
65 public:
66 /*! \brief construct an nil ref */
67 LuaRef() = default;
68 /*!
69 * \brief move constructor from another LuaRef
70 * \param other The other LuaRef to be moved
71 */
72 inline LuaRef(LuaRef &&other); // NOLINT(*)
73 /*!
74 * \brief copy constructor
75 * \param other The other LuaRef to be copied
76 */
77 inline LuaRef(const LuaRef &other); // NOLINT(*)
78 /*!
79 * \brief assign operator from other
80 * \param other The other LuaRef to be copy or moved.
81 * \return self
82 */
83 inline LuaRef &operator=(LuaRef &&other);
84 /*!
85 * \brief assign operator from other
86 * \param other The other LuaRef to be copy or moved.
87 * \return self
88 */
89 inline LuaRef &operator=(const LuaRef &other);
90 /*! \brief destructor */
91 inline ~LuaRef();
92 /*!
93 * \brief swap content with another ref
94 * \param other another LuaRef to be swaped.
95 */
96 inline void swap(LuaRef &other); // NOLINT(*)
97 /*!
98 * \brief Get content out as type T.
99 *
100 * \tparam T the type to be fetched.
101 * \return the corresponding c type.
102 */
103 template <typename T>
104 inline T Get() const;
105 /*!
106 * \brief Get user data pointer from LuaRef
107 *
108 * CAREFUL when getting userdata(e.g. pointer to Tensor's storage) from LuaRef.
109 * Remember they are managed by Lua, and can get deleted when all the
110 * LuaRef to the userdata destructs. A good practice is always use a LuaRef to keep
111 * the userdata alive when you need them from C++ side.
112 *
113 * \tparam T the type of pointer to be fetched.
114 * \return the corresponding c type.
115 */
116 template <typename T>
117 inline T *GetUDataPtr() const;
118 /*! \return whether the value is nil */
119 inline bool is_nil() const;
120 /*!
121 * \brief invoke the LuaRef as function
122 * \param args Arguments to be passed.
123 * \tparam Args arguments to be passed.
124 * \return The first return value.
125 */
126 template <typename... Args>
127 inline LuaRef operator()(Args &&...args) const;
128 /*!
129 * \brief Get field from the lua table.
130 * The reference must be a table
131 * \param key The key to the table
132 * \return a new ref to the corresponding field.
133 */
134 inline LuaRef operator[](const std::string &key) const;
135 /*!
136 * \brief Get field from the lua array
137 * The reference must be a array
138 * \param index The index to the array,
139 * Note: the index convention follows lua table, starts from 1
140 * \return a new ref to the corresponding field.
141 */
142 inline LuaRef operator[](size_t index) const;
143 /*!
144 * \brief Set field of lua table.
145 * The reference must be a table
146 * \param key The key to the table
147 * \param value Lua convertable value to be setted.
148 * \return self.
149 */
150 template <typename T>
151 inline LuaRef &SetField(const std::string &key, const T &value); // NOLINT(*)
152 /*!
153 * \brief Set LuaRef to the value on top of the stack.
154 * This state must be nil.
155 * This is API used by developer.
156 *
157 * \param s the corresponding lua state.
158 */
159 inline void SetByPopStack_(LuaState *s);
160
161 private:
162 // friend with luastate
163 friend struct lua_stack::Handler<LuaRef>;
164 friend class LuaState;
165 friend std::ostream &operator<<(std::ostream &os, const LuaRef &r);
166 /*! \brief pointer to the state */
167 LuaState *state_{nullptr};
168 /*! \brief reference index */
169 int ref_;
170 };
171
172 /*! \brief A Lua state */
173 class LuaState {
174 public:
175 /*! \brief options to be provided in lua state */
176 enum Option {
177 kNoThreadProtect,
178 kThreadLocal,
179 kLocking,
180 };
181 /*! \brief destructor */
182 inline ~LuaState();
183 /*!
184 * \brief evaluate a piece of lua code, return the first result.
185 * \param lua_code Lua code
186 * \return A LuaRef object of the first returned result,
187 * Can be nil if the code did not return LuaRefthing.
188 */
189 inline LuaRef Eval(const char *lua_code);
190 /*!
191 * \brief evaluate a piece of lua code, return the first result.
192 * \param lua_code Lua code
193 * \return A LuaRef object of the first returned result,
194 * Can be nil if the code did not return anything.
195 */
196 inline LuaRef Eval(const std::string &lua_code) {
197 return this->Eval(lua_code.c_str());
198 }
199 /*!
200 * \brief convert a C++ type to lua type
201 * \param value The data to be converted.
202 * vector, map will be converted to table.
203 * \return a converted value.
204 * \tparam T the type to be converted.
205 */
206 template <typename T>
207 inline LuaRef Convert(const T &value);
208 /*!
209 * \brief get global field from the state
210 * \param key The key to the global field.
211 * \return The global field value.
212 */
213 inline LuaRef operator[](const std::string &key);
214 /*!
215 * \brief Set the value to the global table.
216 * \param key The key of the global field.
217 * \param value The value to the set.
218 */
219 inline void SetGlobalField(const std::string &key, const LuaRef &value);
220 /*!
221 * Get a thread local version of lua state.
222 * The LuaState runs in thread local mode,
223 * all the LuaRef can only be run on the current thread.
224 * This is the recommended behavior when invoking Lua.
225 *
226 * \return a threadlocal version of lua state.
227 */
228 static inline LuaState *ThreadLocalState();
229 /*!
230 * Create a new lua state.
231 * \note It is highly recommended to use ThreadLocalState instead.
232 *
233 * Most Lua program assumes it only runs from the same thread.
234 * Some Lua code that wraps C library(e.g. Torch) could rely
235 * on thread_local storage to store global state such as random number generator.
236 * This means if the code is invoked by another thread, the thread_local
237 * might become inavailable, depending on the implementation.
238 *
239 * If the global state is stored only in Lua's global table, then
240 * it is safe to use kLocking mode and call the code from multiple thread.
241 * Never-the-less, using ThreadLocalState removes the need to lock,
242 * and is the desirable usecase in most times.
243 *
244 * \sa ThreadLocalState
245 * \param option The option to use the state.
246 * \return a newly created lua state
247 */
248 static inline LuaState *Create_(Option option);
249
250 /*!
251 * \brief protected run f, this is used by API developers.
252 * always call this to access lua state
253 * f must not destruct LuaRef, or access the mutex
254 *
255 * \param f the function to be called.
256 * \tparam F the function to be called, signiture (lua_State *L)
257 */
258 template <typename F>
259 inline void PRun_(F f);
260 /*!
261 * \param L the other lua state.
262 * \return if the internal lua state is same as L
263 */
264 inline bool SameLuaState(lua_State *L) const {
265 return L_ == L;
266 }
267
268 protected:
269 struct StackReset;
270 friend class LuaRef;
271 friend struct ThreadLocalStore<LuaState>;
272 /*!
273 * \brief constructor
274 */
275 inline LuaState();
276
277 /*! \brief internal option, default to thread local */
278 Option option_{kThreadLocal};
279 /*! \brief internal lua state */
280 lua_State *L_;
281 /*! \brief internal lock about the state */
282 std::mutex mutex_;
283 };
284
285 // implementations after this line
286 //! \cond Doxygen_Suppress
287 /*! \brief macro to check error during lua call */
288 #define LUA_CALL(x) \
289 if ((x)) { \
290 LOG(FATAL) << "Lua Call Error:" << lua_tostring(L, -1); \
291 }
292
293 /*!
294 * \brief namespace to handle conversions between lua and c++
295 * User can provide an specialization of dmlc::lua_stack::Handler
296 * to allow customized c++ data types to interact with Lua.
297 *
298 * By default basic data types, composition of vector, and unordered_map is supported.
299 * The conversion rules
300 * - basic types(string, int, float) to corresponding lua types.
301 * - unordered_map to Lua table.
302 * - vector to lua indexed table.
303 */
304 namespace lua_stack {
305 inline int lua_abs_index(lua_State *L, int index) {
306 if (index > 0 || index <= LUA_REGISTRYINDEX) {
307 return index;
308 }
309 return lua_gettop(L) + index + 1;
310 }
311
312 template <typename T>
313 struct Handler;
314
315 template <typename T>
316 struct NumberHandler {
317 static inline T Get(lua_State *L, int index, LuaState *s) {
318 CHECK_EQ(lua_type(L, index), LUA_TNUMBER)
319 << "Attempt to get number but type is \'" << lua_typename(L, lua_type(L, index)) << '\'';
320 if (std::is_integral<T>::value) {
321 return static_cast<T>(lua_tointeger(L, index));
322 } else {
323 return static_cast<T>(lua_tonumber(L, index));
324 }
325 }
326 static inline void Push(lua_State *L, const T &v) {
327 if (std::is_integral<T>::value) {
328 lua_pushinteger(L, static_cast<lua_Integer>(v));
329 } else {
330 lua_pushnumber(L, static_cast<lua_Number>(v));
331 }
332 }
333 };
334
335 template <typename ContainerType>
336 struct MapHandler {
337 using K = typename ContainerType::key_type;
338 using V = typename ContainerType::mapped_type;
339 static inline ContainerType Get(lua_State *L, int index, LuaState *s) {
340 ContainerType ret;
341 CHECK(lua_istable(L, index)) << "Expected a table but get "
342 << lua_typename(L, lua_type(L, index)) << '\'';
343 int tid = lua_abs_index(L, index);
344 lua_pushnil(L);
345 while (lua_next(L, -2)) {
346 ret[Handler<K>::Get(L, -2, s)] = Handler<V>::Pop(L, -1, s);
347 lua_pop(L, 1);
348 }
349 lua_settop(L, tid);
350 return ret;
351 }
352 static inline void Push(lua_State *L, const ContainerType &v) {
353 lua_createtable(L, v.size(), 0);
354 for (const auto &kv : v) {
355 Handler<K>::Push(L, kv.first);
356 Handler<V>::Push(L, kv.second);
357 lua_settable(L, -3);
358 }
359 }
360 };
361
362 struct UndefinedHandler {};
363
364 template <typename T>
365 struct Handler :
366 public std::conditional<std::is_arithmetic<T>::value, NumberHandler<T>,
367 UndefinedHandler>::type {};
368
369 template <>
370 struct Handler<std::string> {
371 static inline std::string Get(lua_State *L, int index, LuaState *s) {
372 CHECK_EQ(lua_type(L, index), LUA_TSTRING);
373 return std::string(lua_tostring(L, index));
374 }
375 static inline void Push(lua_State *L, const std::string &v) {
376 lua_pushstring(L, v.c_str());
377 }
378 };
379
380 template <typename T>
381 struct Handler<std::vector<T>> {
382 static inline std::vector<T> Get(lua_State *L, int index, LuaState *s) {
383 std::vector<T> ret;
384 CHECK(lua_istable(L, index)) << "Expected a table but get "
385 << lua_typename(L, lua_type(L, index)) << '\'';
386 int tid = lua_abs_index(L, index);
387 lua_pushnil(L);
388 while (lua_next(L, tid)) {
389 CHECK_EQ(Handler<size_t>::Get(L, -2, s), ret.size() + 1) << "Target table is not an array";
390 ret.push_back(Handler<T>::Get(L, -1, s));
391 lua_pop(L, 1);
392 }
393 lua_settop(L, tid);
394 return ret;
395 }
396 static inline void Push(lua_State *L, const std::vector<T> &v) {
397 lua_createtable(L, v.size(), 0);
398 for (size_t i = 0; i < v.size(); ++i) {
399 Handler<T>::Push(L, v[i]);
400 lua_rawseti(L, -2, i + 1);
401 }
402 }
403 };
404
405 template <typename K, typename V>
406 struct Handler<std::unordered_map<K, V>> : public MapHandler<std::unordered_map<K, V>> {};
407
408 template <>
409 struct Handler<LuaRef> {
410 static inline LuaRef Get(lua_State *L, int index, LuaState *s) {
411 LuaRef ret;
412 lua_pushvalue(L, index);
413 ret.SetByPopStack_(s);
414 return ret;
415 }
416
417 static inline void Push(lua_State *L, const LuaRef &v) {
418 if (v.is_nil()) {
419 lua_pushnil(L);
420 } else {
421 CHECK(v.state_->SameLuaState(L)) << "Cannot pass LuaRef on a different LuaState's function";
422 lua_rawgeti(L, LUA_REGISTRYINDEX, v.ref_);
423 }
424 }
425 };
426
427 template <>
428 struct Handler<std::nullptr_t> {
429 static inline LuaRef Get(lua_State *L, int index, LuaState *s) {
430 LOG(FATAL) << "not supported";
431 return LuaRef();
432 }
433 static inline void Push(lua_State *L, const std::nullptr_t &v) {
434 lua_pushnil(L);
435 }
436 };
437
438 // generic functor to call push the arguments.
439 struct PushArg {
440 lua_State *L;
441 template <typename T>
442 inline void operator()(const T &v) const {
443 Handler<T>::Push(L, v);
444 }
445 };
446
447 } // namespace lua_stack
448
449 inline LuaState::LuaState() {
450 L_ = luaL_newstate();
451 CHECK(L_ != nullptr) << "Failed to create new lua state";
452 luaL_openlibs(L_);
453 }
454
455 inline LuaState::~LuaState() {
456 if (option_ != kThreadLocal && L_ != nullptr) {
457 // never close threadlocal, for save destruction.
458 lua_close(L_);
459 }
460 }
461
462 inline LuaState *LuaState::Create_(Option opt) {
463 LuaState *s = new LuaState();
464 s->option_ = opt;
465 CHECK_NE(opt, kThreadLocal) << "use LuaState::ThreadLocalState() to get the thread local state";
466 return s;
467 }
468
469 inline void LuaRef::SetByPopStack_(LuaState *s) {
470 CHECK(state_ == nullptr);
471 lua_State *L = s->L_;
472 if (!lua_isnil(L, -1)) {
473 ref_ = lua_ref(L, LUA_REGISTRYINDEX);
474 state_ = s;
475 } else {
476 lua_pop(L, 1);
477 }
478 }
479
480 // RAII guard to reset stack
481 struct LuaState::StackReset {
482 lua_State *L;
483 int top;
484 ~StackReset() {
485 lua_settop(L, top);
486 }
487 };
488
489 template <typename F>
490 inline void LuaState::PRun_(F f) {
491 if (option_ != kLocking) {
492 StackReset reset{L_, lua_gettop(L_)};
493 if (option_ == kThreadLocal) {
494 CHECK_EQ(ThreadLocalState(), this)
495 << "Invoke lua from a different thread in ThreadLocal mode.";
496 }
497 f(L_);
498 CHECK_EQ(reset.top, lua_gettop(L_));
499 } else {
500 std::lock_guard<std::mutex> lock(mutex_);
501 StackReset reset{L_, lua_gettop(L_)};
502 f(L_);
503 CHECK_EQ(reset.top, lua_gettop(L_));
504 }
505 }
506
507 inline LuaState *LuaState::ThreadLocalState() {
508 return ThreadLocalStore<LuaState>::Get();
509 }
510
511 inline LuaRef LuaState::Eval(const char *lua_code) {
512 LuaRef ret;
513 this->PRun_([this, lua_code, &ret](lua_State *L) {
514 luaL_loadstring(L, lua_code);
515 CHECK_EQ(lua_pcall(L, 0, 1, 0), 0) << "Lua call error: " << lua_tostring(L, -1) << '\n'
516 << "---------\n"
517 << lua_code << "\n----------";
518 ret.SetByPopStack_(this);
519 });
520 return ret;
521 }
522
523 template <typename T>
524 inline LuaRef LuaState::Convert(const T &value) {
525 LuaRef ret;
526 this->PRun_([this, &value, &ret](lua_State *L) {
527 lua_stack::Handler<T>::Push(L, value);
528 ret.SetByPopStack_(this);
529 });
530 return ret;
531 }
532
533 inline LuaRef LuaState::operator[](const std::string &key) {
534 LuaRef ret;
535 this->PRun_([this, &key, &ret](lua_State *L) {
536 lua_getglobal(L, key.c_str());
537 ret.SetByPopStack_(this);
538 });
539 return ret;
540 }
541
542 inline void LuaState::SetGlobalField(const std::string &key, const LuaRef &value) {
543 this->PRun_([this, &key, &value](lua_State *L) {
544 lua_rawgeti(L, LUA_REGISTRYINDEX, value.ref_);
545 lua_setglobal(L, key.c_str());
546 });
547 }
548
549 inline LuaRef::LuaRef(const LuaRef &other) {
550 if (other.state_ != nullptr) {
551 state_ = other.state_;
552 state_->PRun_([this, &other](lua_State *L) {
553 lua_rawgeti(L, LUA_REGISTRYINDEX, other.ref_);
554 ref_ = luaL_ref(L, LUA_REGISTRYINDEX);
555 });
556 }
557 }
558
559 inline LuaRef::LuaRef(LuaRef &&other) {
560 ref_ = other.ref_;
561 state_ = other.state_;
562 other.state_ = nullptr;
563 }
564
565 inline LuaRef &LuaRef::operator=(LuaRef &&other) {
566 LuaRef(std::move(other)).swap(*this);
567 return *this;
568 }
569
570 inline LuaRef &LuaRef::operator=(const LuaRef &other) {
571 LuaRef(other).swap(*this);
572 return *this;
573 }
574
575 inline void LuaRef::swap(LuaRef &other) { // NOLINT(*)
576 std::swap(state_, other.state_);
577 std::swap(ref_, other.ref_);
578 }
579
580 inline LuaRef::~LuaRef() {
581 if (state_ != nullptr) {
582 state_->PRun_([this](lua_State *L) { luaL_unref(L, LUA_REGISTRYINDEX, ref_); });
583 }
584 }
585
586 inline bool LuaRef::is_nil() const {
587 return state_ == nullptr;
588 }
589
590 std::ostream &operator<<(std::ostream &os, const LuaRef &r) {
591 if (!r.is_nil()) {
592 r.state_->PRun_([&os, &r](lua_State *L) {
593 lua_rawgeti(L, LUA_REGISTRYINDEX, r.ref_);
594 int type = lua_type(L, -1);
595 switch (type) {
596 case LUA_TSTRING:
597 os << "lua_string:'" << lua_tostring(L, -1) << "'";
598 break;
599 case LUA_TBOOLEAN:
600 os << "lua_bool:" << (lua_toboolean(L, -1) ? "true" : "false");
601 break;
602 case LUA_TNUMBER:
603 os << "lua_number:" << lua_tonumber(L, -1);
604 break;
605 default:
606 os << "lua[ref=" << r.ref_ << ']' << lua_typename(L, type);
607 break;
608 }
609 lua_pop(L, 1);
610 });
611 } else {
612 os << "lua_nil";
613 }
614 return os;
615 }
616
617 template <typename T>
618 inline T LuaRef::Get() const {
619 CHECK(state_ != nullptr) << "Get:: LuaRef is nil";
620 T ret;
621 state_->PRun_([&ret, this](lua_State *L) {
622 lua_rawgeti(L, LUA_REGISTRYINDEX, ref_);
623 ret = lua_stack::Handler<T>::Get(L, -1, state_);
624 lua_pop(L, 1);
625 });
626 return ret;
627 }
628
629 template <typename T>
630 inline T *LuaRef::GetUDataPtr() const {
631 CHECK(state_ != nullptr) << "Get:: LuaRef is nil";
632 T *ret;
633 state_->PRun_([&ret, this](lua_State *L) {
634 lua_rawgeti(L, LUA_REGISTRYINDEX, ref_);
635 ret = reinterpret_cast<T *>(lua_touserdata(L, -1));
636 lua_pop(L, 1);
637 });
638 return ret;
639 }
640
641 // helper function to dispatch varg foreach
642 template <bool stop, std::size_t I, typename F, typename... Args>
643 struct for_each_dispatcher_ {
644 static inline void run(const std::tuple<Args...> &args, F f) {
645 f(std::get<I>(args));
646 for_each_dispatcher_<(I + 1) == sizeof...(Args), (I + 1), F, Args...>::run(args, f);
647 }
648 };
649 // helper function to run foreach
650 template <std::size_t I, typename F, typename... Args>
651 struct for_each_dispatcher_<true, I, F, Args...> {
652 static inline void run(const std::tuple<Args...> &args, F f) {}
653 };
654
655 // template function to iterate over tuples
656 template <typename F, typename... Args>
657 inline void for_each(const std::tuple<Args...> &args, F f) {
658 for_each_dispatcher_<sizeof...(Args) == 0, 0, F, Args...>::run(args, f);
659 }
660
661 template <typename... Args>
662 inline LuaRef LuaRef::operator()(Args &&...args) const {
663 CHECK(state_ != nullptr) << "LuaRef is nil";
664 auto targ = std::make_tuple(std::forward<Args>(args)...);
665 size_t nargs = sizeof...(Args);
666 LuaRef ret;
667 state_->PRun_([this, nargs, &targ, &ret](lua_State *L) {
668 lua_rawgeti(L, LUA_REGISTRYINDEX, this->ref_);
669 CHECK(lua_isfunction(L, -1)) << "Expect to invoke a function but type='"
670 << lua_typename(L, lua_type(L, -1)) << '\'';
671 for_each(targ, lua_stack::PushArg{L});
672 LUA_CALL(lua_pcall(L, nargs, 1, 0));
673 ret.SetByPopStack_(state_);
674 });
675 return ret;
676 }
677
678 template <typename T>
679 inline LuaRef &LuaRef::SetField(const std::string &key, const T &value) { // NOLINT(*)
680 CHECK(state_ != nullptr) << "LuaRef is nil";
681 state_->PRun_([this, &key, &value](lua_State *L) {
682 lua_rawgeti(L, LUA_REGISTRYINDEX, this->ref_);
683 CHECK(lua_istable(L, -1)) << "Expect a table but type='" << lua_typename(L, lua_type(L, -1))
684 << '\'';
685 lua_stack::Handler<T>::Push(L, value);
686 lua_setfield(L, -2, key.c_str());
687 lua_pop(L, 1);
688 });
689 return *this;
690 }
691
692 inline LuaRef LuaRef::operator[](const std::string &key) const {
693 CHECK(state_ != nullptr) << "LuaRef is nil";
694 LuaRef ret;
695 state_->PRun_([this, &key, &ret](lua_State *L) {
696 lua_rawgeti(L, LUA_REGISTRYINDEX, this->ref_);
697 CHECK(lua_istable(L, -1)) << "Expect a table but type='" << lua_typename(L, lua_type(L, -1))
698 << '\'';
699 lua_getfield(L, -1, key.c_str());
700 ret.SetByPopStack_(state_);
701 lua_pop(L, 1);
702 });
703 return ret;
704 }
705
706 inline LuaRef LuaRef::operator[](size_t index) const {
707 CHECK(state_ != nullptr) << "LuaRef is nil";
708 LuaRef ret;
709 state_->PRun_([this, index, &ret](lua_State *L) {
710 lua_rawgeti(L, LUA_REGISTRYINDEX, this->ref_);
711 CHECK(lua_istable(L, -1)) << "Expect a table but type='" << lua_typename(L, lua_type(L, -1))
712 << '\'';
713 lua_rawgeti(L, -1, index);
714 ret.SetByPopStack_(state_);
715 lua_pop(L, 1);
716 });
717 return ret;
718 }
719
720 //! \endcond
721 } // namespace dmlc
722
723 #endif // DMLC_LUA_H_