Skip to content

Commit

Permalink
Added bind_libs method and removed default loading of libs
Browse files Browse the repository at this point in the history
  • Loading branch information
Trey2k committed Apr 17, 2022
1 parent f4945c1 commit e205c0b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 11 deletions.
51 changes: 41 additions & 10 deletions lua.cpp
Expand Up @@ -28,16 +28,6 @@ Lua::Lua(){
// threaded is false by default
threaded = false;

// loading base libs
luaL_requiref(state, "", luaopen_base, 1);
lua_pop(state, 1);
luaL_requiref(state, LUA_TABLIBNAME, luaopen_table, 1);
lua_pop(state, 1);
luaL_requiref(state, LUA_STRLIBNAME, luaopen_string, 1);
lua_pop(state, 1);
luaL_requiref(state, LUA_MATHLIBNAME, luaopen_math, 1);
lua_pop(state, 1);

lua_sethook(state, &LineHook, LUA_MASKLINE, 0);
lua_register(state, "print", luaPrint);

Expand All @@ -56,6 +46,7 @@ Lua::Lua(){

}


Lua::~Lua(){
// Warning users about object destruction if code is currently being executed see https://github.com/Trey2k/lua/issues/9
if (executing){
Expand All @@ -71,6 +62,7 @@ static bool shouldKill = false;
void Lua::_bind_methods(){
ClassDB::bind_method(D_METHOD("kill_all"),&Lua::killAll);
ClassDB::bind_method(D_METHOD("set_threaded", "bool"),&Lua::setThreaded);
ClassDB::bind_method(D_METHOD("bind_libs", "Array"),&Lua::bindLibs);
ClassDB::bind_method(D_METHOD("do_file", "File", "ProtectedCall" , "CallbackCaller" , "Callback" ), &Lua::doFile, DEFVAL(true) , DEFVAL(Variant()) , DEFVAL(String()) );
ClassDB::bind_method(D_METHOD("do_string", "Code", "ProtectedCall" , "CallbackCaller" , "Callback" ), &Lua::doString, DEFVAL(true) , DEFVAL(Variant()) , DEFVAL(String()) );
ClassDB::bind_method(D_METHOD("push_variant", "var", "Name"),&Lua::pushGlobalVariant);
Expand All @@ -80,6 +72,45 @@ void Lua::_bind_methods(){
ClassDB::bind_method(D_METHOD("lua_function_exists","LuaFunctionName"), &Lua::luaFunctionExists);
}

void Lua::bindLibs(Array libs) {
for (int i = 0; i < libs.size(); i++) {
String lib = ((String)libs.get(i)).to_lower();
if (lib=="base") {
luaL_requiref(state, "", luaopen_base, 1);
lua_pop(state, 1);
// base will override print, so we take it back. User can still override them selfs
lua_register(state, "print", luaPrint);
} else if (lib=="table") {
luaL_requiref(state, LUA_TABLIBNAME, luaopen_table, 1);
lua_pop(state, 1);
}else if (lib=="string") {
luaL_requiref(state, LUA_STRLIBNAME, luaopen_string, 1);
lua_pop(state, 1);
}else if (lib=="math") {
luaL_requiref(state, LUA_MATHLIBNAME, luaopen_math, 1);
lua_pop(state, 1);
}else if (lib=="os") {
luaL_requiref(state, LUA_OSLIBNAME, luaopen_os, 1);
lua_pop(state, 1);
}else if (lib=="io") {
luaL_requiref(state, LUA_IOLIBNAME, luaopen_io, 1);
lua_pop(state, 1);
}else if (lib=="coroutine") {
luaL_requiref(state, LUA_COLIBNAME, luaopen_coroutine, 1);
lua_pop(state, 1);
}else if (lib=="debug") {
luaL_requiref(state, LUA_DBLIBNAME, luaopen_debug, 1);
lua_pop(state, 1);
}else if (lib=="package") {
luaL_requiref(state, LUA_LOADLIBNAME, luaopen_package, 1);
lua_pop(state, 1);
}else if (lib=="utf8") {
luaL_requiref(state, LUA_UTF8LIBNAME, luaopen_utf8, 1);
lua_pop(state, 1);
}
}
}

// Returns lua state
lua_State* Lua::getState() {
return state;
Expand Down
3 changes: 2 additions & 1 deletion lua.h
Expand Up @@ -19,7 +19,8 @@ class Lua : public RefCounted {
Lua();
~Lua();

void exposeFunction(Callable func, String name);
void bindLibs(Array libs);
void exposeFunction(Callable func, String name);
void doFile( String fileName, bool protected_call = true , Object* CallbackCaller = nullptr , String callback = String() );
void doString( String code, bool protected_call = true , Object* CallbackCaller = nullptr , String callback = String() );
void setThreaded(bool thread);
Expand Down

0 comments on commit e205c0b

Please sign in to comment.