Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constructor to support pre-exising Lua state. #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion include/LuaContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class LuaContext {
/**
* @param openDefaultLibs True if luaL_openlibs should be called
*/
explicit LuaContext(bool openDefaultLibs = true)
explicit LuaContext(bool openDefaultLibs = true) : mOwnsLua(true)
{
// luaL_newstate can return null if allocation failed
mState = luaL_newstate();
Expand All @@ -102,6 +102,15 @@ class LuaContext {
luaL_openlibs(mState);
}

/**
* Constructor to hook into an existing Lua state.
*
* @param L exisitng Lua state
*/
explicit LuaContext(lua_State* L) : mState(L), mOwnsLua(false)
{
}

/**
* Move constructor
*/
Expand Down Expand Up @@ -135,6 +144,8 @@ class LuaContext {
*/
~LuaContext() noexcept
{
if (!mOwnsLua)
return;
assert(mState);
lua_close(mState);
}
Expand Down Expand Up @@ -674,6 +685,8 @@ class LuaContext {
// each table has its getter functions at offset 0, getter members at offset 1, default getter at offset 2
// offset 3 is unused, setter members at offset 4, default setter at offset 5
lua_State* mState;
/// Flag if we should do the cleanup, or if Lua is owned by another system.
bool mOwnsLua;


/**************************************************/
Expand Down