Skip to content

Commit

Permalink
add set global field (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
tqchen authored Sep 27, 2016
1 parent a1d38d4 commit f35f14f
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions include/dmlc/lua.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ class LuaState {
* \return The global field value.
*/
inline LuaRef operator[](const std::string& key);
/*!
* \brief Set the value to the global table.
* \param key The key of the global field.
* \param value The value to the set.
*/
inline void SetGlobalField(const std::string& key, const LuaRef& value);
/*!
* Get a thread local version of lua state.
* The LuaState runs in thread local mode,
Expand Down Expand Up @@ -544,6 +550,14 @@ inline LuaRef LuaState::operator[](const std::string& key) {
return ret;
}

inline void LuaState::SetGlobalField(
const std::string& key, const LuaRef& value) {
this->PRun_([this, &key, &value](lua_State* L) {
lua_rawgeti(L, LUA_REGISTRYINDEX, value.ref_);
lua_setglobal(L, key.c_str());
});
}

inline LuaRef::LuaRef(const LuaRef& other) {
if (other.state_ != nullptr) {
state_ = other.state_;
Expand Down

0 comments on commit f35f14f

Please sign in to comment.