forked from WeaselGames/godot_luaAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.py
104 lines (87 loc) · 2.78 KB
/
codegen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
def code_gen(luaJIT=False):
lua_libraries = [
"base",
"coroutine",
"debug",
"io",
"math",
"os",
"package",
"string",
"table",
"utf8"
]
luajit_libraries = [
"base",
"bit",
"debug",
"ffi",
"io",
"jit",
"math",
"os",
"package",
"string",
"string_buffer",
"table"
]
libraries = lua_libraries
lib_source_files = []
if luaJIT:
libraries = luajit_libraries
for library in os.listdir("./"):
if not os.path.isdir(library) or library == "__pycache__" or library == "bin":
continue
libraries.append(library)
for source_file in os.listdir("./%s" % library):
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#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<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;
}
lua_pushcfunction(L, luaLibraries[lib_c_str]);
if (libraryName == "base") {
lua_pushstring(L, "");
} else {
lua_pushstring(L, lib_c_str);
}
lua_call(L, 1, 0);
return true;
}
"""
else:
luaLibraries_gen_cpp += """
bool loadLuaLibrary(lua_State *L, String libraryName) {
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()