Skip to content
This repository has been archived by the owner on Dec 29, 2024. It is now read-only.

Making loadlibraries work on Mac #196

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
33 changes: 21 additions & 12 deletions lua_libraries/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,28 @@ def code_gen(luaJIT=False):
if source_file.endswith(".cpp") or source_file.endswith(".c"):
lib_source_files.append(os.path.join(library, source_file))

luaLibraries_gen_cpp = "#include \"lua_libraries.h\"\n#include <map>\n#include <string>\n\n"
luaLibraries_gen_cpp = "#include \"lua_libraries.h\"\n#include <map>\n#include <string>\n#include <iostream>\n\n"

if len(lib_source_files) > 0:
for source_file in lib_source_files:
luaLibraries_gen_cpp += "#include \"%s\"\n" % source_file
luaLibraries_gen_cpp += "\n"

luaLibraries_gen_cpp += "std::map<std::string, lua_CFunction> luaLibraries = {\n"

luaLibraries_gen_cpp += "std::map<String, lua_CFunction> luaLibraries = {\n"
for library in libraries:
luaLibraries_gen_cpp += "\t{ \"%s\", luaopen_%s },\n" % (library, library)

luaLibraries_gen_cpp += "};\n"

luaLibraries_gen_cpp += "std::map<String, const char*> luaLibraryName = {\n"
for library in libraries:
luaLibraries_gen_cpp += "\t{ \"%s\", \"%s\" },\n" % (library, library)
luaLibraries_gen_cpp += "};\n"

if luaJIT:
luaLibraries_gen_cpp += """
bool loadLuaLibrary(lua_State *L, String libraryName) {
const char *lib_c_str = libraryName.ascii().get_data();
std::string key = lib_c_str;
if (luaLibraries[lib_c_str] == nullptr) {
return false;
}
Expand All @@ -79,17 +84,21 @@ def code_gen(luaJIT=False):
else:
luaLibraries_gen_cpp += """
bool loadLuaLibrary(lua_State *L, String libraryName) {
const char *lib_c_str = libraryName.ascii().get_data();
if (luaLibraries[lib_c_str] == nullptr) {
return false;
}

luaL_requiref(L, lib_c_str, luaLibraries[lib_c_str], 1);
lua_pop(L, 1);
return true;
std::map<String, lua_CFunction>::iterator lib = luaLibraries.find(libraryName);
if( lib == luaLibraries.end() )
return false;

std::map<String, const char*>::iterator lib_name = luaLibraryName.find(libraryName);
if( lib_name == luaLibraryName.end() )
return false;

luaL_requiref(L, (*lib_name).second, (*lib).second, 1);
lua_pop(L, 1);
return true;
}
"""

gen_file = open("lua_libraries.gen.cpp", "w")
gen_file.write(luaLibraries_gen_cpp)
gen_file.close()
gen_file.close()
6 changes: 4 additions & 2 deletions src/luaState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ void LuaState::setState(lua_State *state, LuaAPI *api, bool bindAPI) {
lua_State *LuaState::getState() const {
return L;
}
#include <iostream>

// Binds lua libraries with the lua state
Ref<LuaError> LuaState::bindLibraries(TypedArray<String> libs) {
Ref<LuaError> LuaState::bindLibraries(Array libs) {
for (int i = 0; i < libs.size(); i++) {
if (!loadLuaLibrary(L, libs[i])) {
return LuaError::newError(vformat("Library \"%s\" does not exist.", libs[i]), LuaError::ERR_RUNTIME);
}
if (libs[i] == "base") {
if (libs[i] == String("base")) {
lua_register(L, "print", luaPrint);
}
}

return nullptr;
}

Expand Down
2 changes: 1 addition & 1 deletion src/luaState.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LuaState {
Variant getRegistryValue(String name);

Ref<LuaError> setRegistryValue(String name, Variant var);
Ref<LuaError> bindLibraries(TypedArray<String> libs);
Ref<LuaError> bindLibraries(Array libs);
Ref<LuaError> pushVariant(Variant var) const;
Ref<LuaError> pushGlobalVariant(String name, Variant var);
Ref<LuaError> handleError(int lua_error) const;
Expand Down
Loading