create_lua_library.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. import sys
  2. import CppHeaderParser
  3. import os
  4. import errno
  5. import re
  6. from zipfile import *
  7. import fnmatch
  8. def mkdir_p(path): # Same effect as mkdir -p, create dir and all necessary parent dirs
  9. try:
  10. os.makedirs(path)
  11. except OSError as e:
  12. if e.errno == errno.EEXIST: # Dir already exists; not really an error
  13. pass
  14. else: raise
  15. def createLUABindings(inputPath, prefix, mainInclude, libSmallName, libName, apiPath, apiClassPath, includePath, sourcePath, inheritInModuleFiles):
  16. wrappersHeaderOut = "" # Def: Global C++ *LUAWrappers.h
  17. cppRegisterOut = "" # Def: Global C++ *LUA.cpp
  18. luaIndexOut = "" # Def: Global Lua everything-gets-required-from-this-file file
  19. # Header boilerplate for wrappersHeaderOut and cppRegisterOut
  20. cppRegisterOut += "#include \"%sLUA.h\"\n" % (prefix)
  21. cppRegisterOut += "#include \"%sLUAWrappers.h\"\n" % (prefix)
  22. cppRegisterOut += "#include \"PolyCoreServices.h\"\n\n"
  23. cppRegisterOut += "using namespace Polycode;\n\n"
  24. cppRegisterOut += "int luaopen_%s(lua_State *L) {\n" % (prefix)
  25. if prefix != "Polycode":
  26. cppRegisterOut += "CoreServices *inst = (CoreServices*)lua_topointer(L, 1);\n"
  27. cppRegisterOut += "CoreServices::setInstance(inst);\n"
  28. cppRegisterOut += "\tstatic const struct luaL_reg %sLib [] = {" % (libSmallName)
  29. wrappersHeaderOut += "#pragma once\n\n"
  30. wrappersHeaderOut += "extern \"C\" {\n\n"
  31. wrappersHeaderOut += "#include <stdio.h>\n"
  32. wrappersHeaderOut += "#include \"lua.h\"\n"
  33. wrappersHeaderOut += "#include \"lualib.h\"\n"
  34. wrappersHeaderOut += "#include \"lauxlib.h\"\n"
  35. wrappersHeaderOut += "} // extern \"C\" \n\n"
  36. # Get list of headers to create bindings from
  37. files = os.listdir(inputPath)
  38. filteredFiles = []
  39. for fileName in files:
  40. ignore = ["PolyGLSLProgram", "PolyGLSLShader", "PolyGLSLShaderModule", "PolyWinCore", "PolyCocoaCore", "PolyAGLCore", "PolySDLCore", "Poly_iPhone", "PolyGLES1Renderer", "PolyGLRenderer", "tinyxml", "tinystr", "OpenGLCubemap", "PolyiPhoneCore", "PolyGLES1Texture", "PolyGLTexture", "PolyGLVertexBuffer", "PolyThreaded", "PolyGLHeaders", "GLee"]
  41. if fileName.split(".")[1] == "h" and fileName.split(".")[0] not in ignore:
  42. filteredFiles.append(fileName)
  43. wrappersHeaderOut += "#include \"%s\"\n" % (fileName)
  44. wrappersHeaderOut += "\nusing namespace std;\n\n"
  45. wrappersHeaderOut += "\nnamespace Polycode {\n\n"
  46. # Special case: If we are building the Polycode library itself, inject the LuaEventHandler class
  47. if prefix == "Polycode":
  48. wrappersHeaderOut += "class LuaEventHandler : public EventHandler {\n"
  49. wrappersHeaderOut += "public:\n"
  50. wrappersHeaderOut += " LuaEventHandler() : EventHandler() {}\n"
  51. wrappersHeaderOut += " void handleEvent(Event *e) {\n"
  52. wrappersHeaderOut += " lua_rawgeti( L, LUA_REGISTRYINDEX, wrapperIndex );\n"
  53. wrappersHeaderOut += " lua_getfield(L, -1, \"__handleEvent\");\n"
  54. wrappersHeaderOut += " lua_rawgeti( L, LUA_REGISTRYINDEX, wrapperIndex );\n"
  55. wrappersHeaderOut += " lua_pushlightuserdata(L, e);\n"
  56. wrappersHeaderOut += " lua_call(L, 2, 0);\n"
  57. wrappersHeaderOut += " }\n"
  58. wrappersHeaderOut += " int wrapperIndex;\n"
  59. wrappersHeaderOut += " lua_State *L;\n"
  60. wrappersHeaderOut += "};\n\n"
  61. # Iterate, process each input file
  62. for fileName in filteredFiles:
  63. # "Package owned" classes that ship with Polycode
  64. inheritInModule = ["PhysicsSceneEntity", "CollisionScene", "CollisionSceneEntity"]
  65. # A file or comma-separated list of files can be given to specify classes which are "package owned"
  66. # and should not be inherited out of Polycode/. The files should contain one class name per line,
  67. # and the class name may be prefixed with a path (which will be ignored).
  68. if inheritInModuleFiles:
  69. for filename in inheritInModuleFiles.split(","):
  70. with open(filename) as f:
  71. for line in f.readlines():
  72. inheritInModule.append(line.strip().split("/",1)[-1]) # Strip whitespace, path/
  73. headerFile = "%s/%s" % (inputPath, fileName) # Def: Full path to input file
  74. print "Parsing %s" % fileName
  75. try: # One input file parse.
  76. f = open(headerFile) # Def: Input file handle
  77. contents = f.read().replace("_PolyExport", "") # Def: Input file contents, strip out "_PolyExport"
  78. cppHeader = CppHeaderParser.CppHeader(contents, "string") # Def: Input file contents, parsed structure
  79. ignore_classes = ["PolycodeShaderModule", "Object", "Threaded", "OpenGLCubemap", "ParticleEmitter"]
  80. # Iterate, check each class in this file.
  81. for ckey in cppHeader.classes:
  82. print ">> Parsing class %s" % ckey
  83. c = cppHeader.classes[ckey] # Def: The class structure
  84. luaClassBindingOut = "" # Def: The local lua file to generate for this class.
  85. inherits = False
  86. if len(c["inherits"]) > 0: # Does this class have parents?
  87. if c["inherits"][0]["class"] not in ignore_classes:
  88. if c["inherits"][0]["class"] in inheritInModule: # Parent class is in this module
  89. luaClassBindingOut += "require \"%s/%s\"\n\n" % (prefix, c["inherits"][0]["class"])
  90. else: # Parent class is in Polycore
  91. luaClassBindingOut += "require \"Polycode/%s\"\n\n" % (c["inherits"][0]["class"])
  92. luaClassBindingOut += "class \"%s\" (%s)\n\n" % (ckey, c["inherits"][0]["class"])
  93. inherits = True
  94. if inherits == False: # Class does not have parents
  95. luaClassBindingOut += "class \"%s\"\n\n" % ckey
  96. if ckey in ignore_classes:
  97. continue
  98. if len(c["methods"]["public"]) < 2: # Used to, this was a continue.
  99. print("Warning: Lua-binding class with less than two methods")
  100. continue # FIXME: Remove this, move any non-compileable classes into ignore_classes
  101. parsed_methods = [] # Def: List of discovered methods
  102. ignore_methods = ["readByte32", "readByte16", "getCustomEntitiesByType", "Core", "Renderer", "Shader", "Texture", "handleEvent", "secondaryHandler", "getSTLString"]
  103. luaClassBindingOut += "\n\n"
  104. classProperties = [] # Def: List of found property structures ("properties" meaning "data members")
  105. for pp in c["properties"]["public"]:
  106. pp["type"] = pp["type"].replace("Polycode::", "")
  107. pp["type"] = pp["type"].replace("std::", "")
  108. if pp["type"].find("static ") != -1: # If static. FIXME: Static doesn't work?
  109. if "defaltValue" in pp: # FIXME: defaltValue is misspelled.
  110. luaClassBindingOut += "%s = %s\n" % (pp["name"], pp["defaltValue"])
  111. else: # FIXME: Nonstatic method ? variable ?? found.
  112. #there are some bugs in the class parser that cause it to return junk
  113. if pp["type"].find("*") == -1 and pp["type"].find("vector") == -1 and pp["name"] != "setScale" and pp["name"] != "setPosition" and pp["name"] != "BUFFER_CACHE_PRECISION" and not pp["name"].isdigit():
  114. classProperties.append(pp)
  115. # Iterate over properties, creating getters
  116. pidx = 0 # Def: Count of properties processed so far
  117. # TODO: Remove or generalize ParticleEmitter special casing. These lines are marked with #SPEC
  118. if len(classProperties) > 0: # If there are properties, add index lookup to the metatable
  119. luaClassBindingOut += "function %s:__index__(name)\n" % ckey
  120. # Iterate over property structures, creating if/else clauses for each.
  121. # TODO: Could a table be more appropriate for
  122. for pp in classProperties:
  123. pp["type"] = pp["type"].replace("Polycode::", "")
  124. pp["type"] = pp["type"].replace("std::", "")
  125. if pidx == 0:
  126. luaClassBindingOut += "\tif name == \"%s\" then\n" % (pp["name"])
  127. else:
  128. luaClassBindingOut += "\telseif name == \"%s\" then\n" % (pp["name"])
  129. # Generate Lua side of binding:
  130. # If type is a primitive such as Number/String/int/bool
  131. if pp["type"] == "Number" or pp["type"] == "String" or pp["type"] == "int" or pp["type"] == "bool":
  132. luaClassBindingOut += "\t\treturn %s.%s_get_%s(self.__ptr)\n" % (libName, ckey, pp["name"])
  133. # If type is a particle emitter, specifically #SPEC
  134. elif (ckey == "ScreenParticleEmitter" or ckey == "SceneParticleEmitter") and pp["name"] == "emitter":
  135. luaClassBindingOut += "\t\tlocal ret = %s(\"__skip_ptr__\")\n" % (pp["type"])
  136. luaClassBindingOut += "\t\tret.__ptr = self.__ptr\n"
  137. luaClassBindingOut += "\t\treturn ret\n"
  138. # If type is a class
  139. else:
  140. luaClassBindingOut += "\t\tretVal = %s.%s_get_%s(self.__ptr)\n" % (libName, ckey, pp["name"])
  141. luaClassBindingOut += "\t\tif __ptr_lookup.%s[retVal] ~= nil then\n" % (ckey)
  142. luaClassBindingOut += "\t\t\treturn __ptr_lookup.%s[retVal]\n" % (ckey)
  143. luaClassBindingOut += "\t\telse\n"
  144. luaClassBindingOut += "\t\t\t__ptr_lookup.%s[retVal] = %s(\"__skip_ptr__\")\n" % (ckey, pp["type"])
  145. luaClassBindingOut += "\t\t\t__ptr_lookup.%s[retVal].__ptr = retVal\n" % (ckey)
  146. luaClassBindingOut += "\t\t\treturn __ptr_lookup.%s[retVal]\n" % (ckey)
  147. luaClassBindingOut += "\t\tend\n"
  148. # Generate C++ side of binding:
  149. if not ((ckey == "ScreenParticleEmitter" or ckey == "SceneParticleEmitter") and pp["name"] == "emitter"): #SPEC
  150. cppRegisterOut += "\t\t{\"%s_get_%s\", %s_%s_get_%s},\n" % (ckey, pp["name"], libName, ckey, pp["name"])
  151. wrappersHeaderOut += "static int %s_%s_get_%s(lua_State *L) {\n" % (libName, ckey, pp["name"])
  152. wrappersHeaderOut += "\tluaL_checktype(L, 1, LUA_TLIGHTUSERDATA);\n"
  153. wrappersHeaderOut += "\t%s *inst = (%s*)lua_topointer(L, 1);\n" % (ckey, ckey)
  154. outfunc = "lua_pushlightuserdata"
  155. retFunc = ""
  156. if pp["type"] == "Number":
  157. outfunc = "lua_pushnumber"
  158. if pp["type"] == "String":
  159. outfunc = "lua_pushstring"
  160. retFunc = ".c_str()"
  161. if pp["type"] == "int":
  162. outfunc = "lua_pushinteger"
  163. if pp["type"] == "bool":
  164. outfunc = "lua_pushboolean"
  165. if pp["type"] == "Number" or pp["type"] == "String" or pp["type"] == "int" or pp["type"] == "bool":
  166. wrappersHeaderOut += "\t%s(L, inst->%s%s);\n" % (outfunc, pp["name"], retFunc)
  167. else:
  168. wrappersHeaderOut += "\t%s(L, &inst->%s%s);\n" % (outfunc, pp["name"], retFunc)
  169. wrappersHeaderOut += "\treturn 1;\n"
  170. wrappersHeaderOut += "}\n\n"
  171. # Success
  172. pidx = pidx + 1
  173. luaClassBindingOut += "\tend\n"
  174. luaClassBindingOut += "end\n"
  175. luaClassBindingOut += "\n\n"
  176. # Iterate over properties again, creating setters
  177. pidx = 0 # Def: Count of
  178. if len(classProperties) > 0: # If there are properties, add index setter to the metatable
  179. luaClassBindingOut += "function %s:__set_callback(name,value)\n" % ckey
  180. for pp in classProperties:
  181. pp["type"] = pp["type"].replace("Polycode::", "")
  182. pp["type"] = pp["type"].replace("std::", "")
  183. # If type is a primitive: Create lua and C++ sides at the same time.
  184. if pp["type"] == "Number" or pp["type"] == "String" or pp["type"] == "int" or pp["type"] == "bool":
  185. if pidx == 0:
  186. luaClassBindingOut += "\tif name == \"%s\" then\n" % (pp["name"])
  187. else:
  188. luaClassBindingOut += "\telseif name == \"%s\" then\n" % (pp["name"])
  189. luaClassBindingOut += "\t\t%s.%s_set_%s(self.__ptr, value)\n" % (libName, ckey, pp["name"])
  190. luaClassBindingOut += "\t\treturn true\n"
  191. cppRegisterOut += "\t\t{\"%s_set_%s\", %s_%s_set_%s},\n" % (ckey, pp["name"], libName, ckey, pp["name"])
  192. wrappersHeaderOut += "static int %s_%s_set_%s(lua_State *L) {\n" % (libName, ckey, pp["name"])
  193. wrappersHeaderOut += "\tluaL_checktype(L, 1, LUA_TLIGHTUSERDATA);\n"
  194. wrappersHeaderOut += "\t%s *inst = (%s*)lua_topointer(L, 1);\n" % (ckey, ckey)
  195. outfunc = "lua_topointer"
  196. if pp["type"] == "Number":
  197. outfunc = "lua_tonumber"
  198. if pp["type"] == "String":
  199. outfunc = "lua_tostring"
  200. if pp["type"] == "int":
  201. outfunc = "lua_tointeger"
  202. if pp["type"] == "bool":
  203. outfunc = "lua_toboolean"
  204. wrappersHeaderOut += "\t%s param = %s(L, 2);\n" % (pp["type"], outfunc)
  205. wrappersHeaderOut += "\tinst->%s = param;\n" % (pp["name"])
  206. wrappersHeaderOut += "\treturn 0;\n"
  207. wrappersHeaderOut += "}\n\n"
  208. pidx = pidx + 1 # Success
  209. # Notice: Setters for object types are not created.
  210. if pidx != 0:
  211. luaClassBindingOut += "\tend\n"
  212. luaClassBindingOut += "\treturn false\n"
  213. luaClassBindingOut += "end\n"
  214. # Iterate over methods
  215. luaClassBindingOut += "\n\n"
  216. for pm in c["methods"]["public"]:
  217. # Skip argument-overloaded methods and operators.
  218. # TODO: Instead of skipping arguemnt overloads, have special behavior.
  219. # TODO: Instead of skipping operators, add to metatable.
  220. if pm["name"] in parsed_methods or pm["name"].find("operator") > -1 or pm["name"] in ignore_methods:
  221. continue
  222. # Skip destructors and methods which return templates.
  223. # TODO: Special-case certain kind of vector<>s?
  224. if pm["name"] == "~"+ckey or pm["rtnType"].find("<") > -1:
  225. continue
  226. basicType = False
  227. voidRet = False
  228. # Basic setup, C++ side: Add function to registry and start building wrapper function.
  229. if pm["name"] == ckey: # It's a constructor
  230. cppRegisterOut += "\t\t{\"%s\", %s_%s},\n" % (ckey, libName, ckey)
  231. wrappersHeaderOut += "static int %s_%s(lua_State *L) {\n" % (libName, ckey)
  232. idx = 1 # Def: Current stack depth (TODO: Figure out, is this correct?)
  233. else: # It's not a constructor
  234. cppRegisterOut += "\t\t{\"%s_%s\", %s_%s_%s},\n" % (ckey, pm["name"], libName, ckey, pm["name"])
  235. wrappersHeaderOut += "static int %s_%s_%s(lua_State *L) {\n" % (libName, ckey, pm["name"])
  236. # Skip static methods (TODO: Figure out, why is this being done here?). # FIXME
  237. if pm["rtnType"].find("static ") == -1:
  238. wrappersHeaderOut += "\tluaL_checktype(L, 1, LUA_TLIGHTUSERDATA);\n"
  239. wrappersHeaderOut += "\t%s *inst = (%s*)lua_topointer(L, 1);\n" % (ckey, ckey)
  240. idx = 2
  241. # Generate C++ side parameter pushing
  242. paramlist = []
  243. lparamlist = []
  244. for param in pm["parameters"]:
  245. if not param.has_key("type"):
  246. continue
  247. if param["type"] == "0":
  248. continue
  249. param["type"] = param["type"].replace("Polycode::", "")
  250. param["type"] = param["type"].replace("std::", "")
  251. param["type"] = param["type"].replace("const", "")
  252. param["type"] = param["type"].replace("&", "")
  253. param["type"] = param["type"].replace(" ", "")
  254. param["type"] = param["type"].replace("long", "long ")
  255. param["type"] = param["type"].replace("unsigned", "unsigned ")
  256. param["name"] = param["name"].replace("end", "_end").replace("repeat", "_repeat")
  257. if"type" in param:
  258. luatype = "LUA_TLIGHTUSERDATA"
  259. checkfunc = "lua_islightuserdata"
  260. if param["type"].find("*") > -1:
  261. luafunc = "(%s)lua_topointer" % (param["type"].replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle"))
  262. elif param["type"].find("&") > -1:
  263. luafunc = "*(%s*)lua_topointer" % (param["type"].replace("const", "").replace("&", "").replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle"))
  264. else:
  265. luafunc = "*(%s*)lua_topointer" % (param["type"].replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle"))
  266. lend = ".__ptr"
  267. if param["type"] == "int" or param["type"] == "unsigned int":
  268. luafunc = "lua_tointeger"
  269. luatype = "LUA_TNUMBER"
  270. checkfunc = "lua_isnumber"
  271. lend = ""
  272. if param["type"] == "bool":
  273. luafunc = "lua_toboolean"
  274. luatype = "LUA_TBOOLEAN"
  275. checkfunc = "lua_isboolean"
  276. lend = ""
  277. if param["type"] == "Number" or param["type"] == "float" or param["type"] == "double":
  278. luatype = "LUA_TNUMBER"
  279. luafunc = "lua_tonumber"
  280. checkfunc = "lua_isnumber"
  281. lend = ""
  282. if param["type"] == "String":
  283. luatype = "LUA_TSTRING"
  284. luafunc = "lua_tostring"
  285. checkfunc = "lua_isstring"
  286. lend = ""
  287. param["type"] = param["type"].replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle")
  288. if "defaltValue" in param:
  289. if checkfunc != "lua_islightuserdata" or (checkfunc == "lua_islightuserdata" and param["defaltValue"] == "NULL"):
  290. #param["defaltValue"] = param["defaltValue"].replace(" 0f", ".0f")
  291. param["defaltValue"] = param["defaltValue"].replace(": :", "::")
  292. #param["defaltValue"] = param["defaltValue"].replace("0 ", "0.")
  293. param["defaltValue"] = re.sub(r'([0-9]+) ([0-9])+', r'\1.\2', param["defaltValue"])
  294. wrappersHeaderOut += "\t%s %s;\n" % (param["type"], param["name"])
  295. wrappersHeaderOut += "\tif(%s(L, %d)) {\n" % (checkfunc, idx)
  296. wrappersHeaderOut += "\t\t%s = %s(L, %d);\n" % (param["name"], luafunc, idx)
  297. wrappersHeaderOut += "\t} else {\n"
  298. wrappersHeaderOut += "\t\t%s = %s;\n" % (param["name"], param["defaltValue"])
  299. wrappersHeaderOut += "\t}\n"
  300. else:
  301. wrappersHeaderOut += "\tluaL_checktype(L, %d, %s);\n" % (idx, luatype);
  302. if param["type"] == "String":
  303. wrappersHeaderOut += "\t%s %s = String(%s(L, %d));\n" % (param["type"], param["name"], luafunc, idx)
  304. else:
  305. wrappersHeaderOut += "\t%s %s = %s(L, %d);\n" % (param["type"], param["name"], luafunc, idx)
  306. else:
  307. wrappersHeaderOut += "\tluaL_checktype(L, %d, %s);\n" % (idx, luatype);
  308. if param["type"] == "String":
  309. wrappersHeaderOut += "\t%s %s = String(%s(L, %d));\n" % (param["type"], param["name"], luafunc, idx)
  310. else:
  311. wrappersHeaderOut += "\t%s %s = %s(L, %d);\n" % (param["type"], param["name"], luafunc, idx)
  312. paramlist.append(param["name"])
  313. lparamlist.append(param["name"]+lend)
  314. idx = idx +1 # Param parse success-- mark the increased stack
  315. # Generate C++-side method call / generate return value
  316. if pm["name"] == ckey: # If constructor
  317. if ckey == "EventHandler": # Special case EventHandler base class (TODO: Figure out why)
  318. wrappersHeaderOut += "\tLuaEventHandler *inst = new LuaEventHandler();\n"
  319. wrappersHeaderOut += "\tinst->wrapperIndex = luaL_ref(L, LUA_REGISTRYINDEX );\n"
  320. wrappersHeaderOut += "\tinst->L = L;\n"
  321. else:
  322. wrappersHeaderOut += "\t%s *inst = new %s(%s);\n" % (ckey, ckey, ", ".join(paramlist))
  323. wrappersHeaderOut += "\tlua_pushlightuserdata(L, (void*)inst);\n"
  324. wrappersHeaderOut += "\treturn 1;\n"
  325. else: #If non-constructor
  326. if pm["rtnType"].find("static ") == -1: # If non-static
  327. call = "inst->%s(%s)" % (pm["name"], ", ".join(paramlist))
  328. else: # If static (FIXME: Why doesn't this work?)
  329. call = "%s::%s(%s)" % (ckey, pm["name"], ", ".join(paramlist))
  330. # If void-typed:
  331. if pm["rtnType"] == "void" or pm["rtnType"] == "static void" or pm["rtnType"] == "virtual void" or pm["rtnType"] == "inline void":
  332. wrappersHeaderOut += "\t%s;\n" % (call)
  333. basicType = True
  334. voidRet = True
  335. wrappersHeaderOut += "\treturn 0;\n" # 0 arguments returned
  336. else: # If there is a return value:
  337. # What type is the return value? Default to pointer
  338. outfunc = "lua_pushlightuserdata"
  339. retFunc = ""
  340. basicType = False
  341. if pm["rtnType"] == "Number" or pm["rtnType"] == "inline Number":
  342. outfunc = "lua_pushnumber"
  343. basicType = True
  344. if pm["rtnType"] == "String" or pm["rtnType"] == "static String": # TODO: Path for STL strings?
  345. outfunc = "lua_pushstring"
  346. basicType = True
  347. retFunc = ".c_str()"
  348. if pm["rtnType"] == "int" or pm["rtnType"] == "static int" or pm["rtnType"] == "size_t" or pm["rtnType"] == "static size_t" or pm["rtnType"] == "long" or pm["rtnType"] == "unsigned int" or pm["rtnType"] == "static long":
  349. outfunc = "lua_pushinteger"
  350. basicType = True
  351. if pm["rtnType"] == "bool" or pm["rtnType"] == "static bool" or pm["rtnType"] == "virtual bool":
  352. outfunc = "lua_pushboolean"
  353. basicType = True
  354. if pm["rtnType"].find("*") > -1: # Returned var is definitely a pointer.
  355. wrappersHeaderOut += "\tvoid *ptrRetVal = (void*)%s%s;\n" % (call, retFunc)
  356. wrappersHeaderOut += "\tif(ptrRetVal == NULL) {\n"
  357. wrappersHeaderOut += "\t\tlua_pushnil(L);\n"
  358. wrappersHeaderOut += "\t} else {\n"
  359. wrappersHeaderOut += "\t\t%s(L, ptrRetVal);\n" % (outfunc)
  360. wrappersHeaderOut += "\t}\n"
  361. elif basicType == True: # Returned var has been flagged as a recognized primitive type
  362. wrappersHeaderOut += "\t%s(L, %s%s);\n" % (outfunc, call, retFunc)
  363. else: # Some static object is being returned. Convert it to a pointer, then return that.
  364. className = pm["rtnType"].replace("const", "").replace("&", "").replace("inline", "").replace("virtual", "").replace("static", "")
  365. if className == "Polygon": # Deal with potential windows.h conflict
  366. className = "Polycode::Polygon"
  367. if className == "Rectangle":
  368. className = "Polycode::Rectangle"
  369. wrappersHeaderOut += "\t%s *retInst = new %s();\n" % (className, className)
  370. wrappersHeaderOut += "\t*retInst = %s;\n" % (call)
  371. wrappersHeaderOut += "\t%s(L, retInst);\n" % (outfunc)
  372. wrappersHeaderOut += "\treturn 1;\n"
  373. wrappersHeaderOut += "}\n\n" # Close out C++ generation
  374. # Now generate the Lua side method.
  375. if pm["name"] == ckey: # Constructors
  376. luaClassBindingOut += "function %s:%s(...)\n" % (ckey, ckey)
  377. if inherits:
  378. luaClassBindingOut += "\tif type(arg[1]) == \"table\" and count(arg) == 1 then\n"
  379. luaClassBindingOut += "\t\tif \"\"..arg[1]:class() == \"%s\" then\n" % (c["inherits"][0]["class"])
  380. luaClassBindingOut += "\t\t\tself.__ptr = arg[1].__ptr\n"
  381. luaClassBindingOut += "\t\t\treturn\n"
  382. luaClassBindingOut += "\t\tend\n"
  383. luaClassBindingOut += "\tend\n"
  384. luaClassBindingOut += "\tfor k,v in pairs(arg) do\n"
  385. luaClassBindingOut += "\t\tif type(v) == \"table\" then\n"
  386. luaClassBindingOut += "\t\t\tif v.__ptr ~= nil then\n"
  387. luaClassBindingOut += "\t\t\t\targ[k] = v.__ptr\n"
  388. luaClassBindingOut += "\t\t\tend\n"
  389. luaClassBindingOut += "\t\tend\n"
  390. luaClassBindingOut += "\tend\n"
  391. luaClassBindingOut += "\tif self.__ptr == nil and arg[1] ~= \"__skip_ptr__\" then\n"
  392. if ckey == "EventHandler": # As above: Special case behavior for EventHandler. As above, TODO figure out why.
  393. luaClassBindingOut += "\t\tself.__ptr = %s.%s(self)\n" % (libName, ckey)
  394. else:
  395. luaClassBindingOut += "\t\tself.__ptr = %s.%s(unpack(arg))\n" % (libName, ckey)
  396. luaClassBindingOut += "\t\t__ptr_lookup.%s[self.__ptr] = self\n" % (ckey)
  397. luaClassBindingOut += "\tend\n"
  398. luaClassBindingOut += "end\n\n"
  399. else: # Non-constructors.
  400. luaClassBindingOut += "function %s:%s(%s)\n" % (ckey, pm["name"], ", ".join(paramlist))
  401. if pm["rtnType"].find("static ") == -1: # Non-static method
  402. if len(lparamlist):
  403. luaClassBindingOut += "\tlocal retVal = %s.%s_%s(self.__ptr, %s)\n" % (libName, ckey, pm["name"], ", ".join(lparamlist))
  404. else:
  405. luaClassBindingOut += "\tlocal retVal = %s.%s_%s(self.__ptr)\n" % (libName, ckey, pm["name"])
  406. else: # Static method
  407. if len(lparamlist):
  408. luaClassBindingOut += "\tlocal retVal = %s.%s_%s(%s)\n" % (libName, ckey, pm["name"], ", ".join(lparamlist))
  409. else:
  410. luaClassBindingOut += "\tlocal retVal = %s.%s_%s()\n" % (libName, ckey, pm["name"])
  411. if not voidRet: # Was there a return value?
  412. if basicType == True: # Yes, a primitive
  413. luaClassBindingOut += "\treturn retVal\n"
  414. else: # Yes, a pointer was returned
  415. className = pm["rtnType"].replace("const", "").replace("&", "").replace("inline", "").replace("virtual", "").replace("static", "").replace("*","").replace(" ", "")
  416. luaClassBindingOut += "\tif retVal == nil then return nil end\n"
  417. luaClassBindingOut += "\tif __ptr_lookup.%s[retVal] ~= nil then\n" % (className)
  418. luaClassBindingOut += "\t\treturn __ptr_lookup.%s[retVal]\n" % (className)
  419. luaClassBindingOut += "\telse\n"
  420. luaClassBindingOut += "\t\t__ptr_lookup.%s[retVal] = %s(\"__skip_ptr__\")\n" % (className, className)
  421. luaClassBindingOut += "\t\t__ptr_lookup.%s[retVal].__ptr = retVal\n" % (className)
  422. luaClassBindingOut += "\t\treturn __ptr_lookup.%s[retVal]\n" % (className)
  423. luaClassBindingOut += "\tend\n"
  424. luaClassBindingOut += "end\n\n" # Close out Lua generation
  425. parsed_methods.append(pm["name"]) # Method parse success
  426. # With methods out of the way, do some final cleanup:
  427. # Delete method (C++ side)
  428. cppRegisterOut += "\t\t{\"delete_%s\", %s_delete_%s},\n" % (ckey, libName, ckey)
  429. wrappersHeaderOut += "static int %s_delete_%s(lua_State *L) {\n" % (libName, ckey)
  430. wrappersHeaderOut += "\tluaL_checktype(L, 1, LUA_TLIGHTUSERDATA);\n"
  431. wrappersHeaderOut += "\t%s *inst = (%s*)lua_topointer(L, 1);\n" % (ckey, ckey)
  432. wrappersHeaderOut += "\tdelete inst;\n"
  433. wrappersHeaderOut += "\treturn 0;\n"
  434. wrappersHeaderOut += "}\n\n"
  435. # Delete method (Lua side)
  436. luaClassBindingOut += "\n\n"
  437. luaClassBindingOut += "if not __ptr_lookup then __ptr_lookup = {} end\n"
  438. luaClassBindingOut += "__ptr_lookup.%s = {}\n\n" % (ckey)
  439. luaClassBindingOut += "function %s:__delete()\n" % (ckey)
  440. luaClassBindingOut += "\t__ptr_lookup.%s[self.__ptr] = nil\n" % (ckey)
  441. luaClassBindingOut += "\t%s.delete_%s(self.__ptr)\n" % (libName, ckey)
  442. luaClassBindingOut += "end\n"
  443. if ckey == "EventHandler": # TODO Why the eventhandler special case?
  444. luaClassBindingOut += "\n\n"
  445. luaClassBindingOut += "function EventHandler:__handleEvent(event)\n"
  446. luaClassBindingOut += "\tevt = Event(\"__skip_ptr__\")\n"
  447. luaClassBindingOut += "\tevt.__ptr = event\n"
  448. luaClassBindingOut += "\tself:handleEvent(evt)\n"
  449. #luaClassBindingOut += "\tself:handleEvent(event)\n"
  450. luaClassBindingOut += "end\n"
  451. # Add class to lua index file
  452. luaIndexOut += "require \"%s/%s\"\n" % (prefix, ckey)
  453. # Write lua file
  454. mkdir_p(apiClassPath)
  455. fout = open("%s/%s.lua" % (apiClassPath, ckey), "w")
  456. fout.write(luaClassBindingOut)
  457. except CppHeaderParser.CppParseError, e: # One input file parse; failed.
  458. print e
  459. sys.exit(1)
  460. # Footer boilerplate for wrappersHeaderOut and cppRegisterOut.
  461. wrappersHeaderOut += "} // namespace Polycode\n"
  462. cppRegisterOut += "\t\t{NULL, NULL}\n"
  463. cppRegisterOut += "\t};\n"
  464. cppRegisterOut += "\tluaL_openlib(L, \"%s\", %sLib, 0);\n" % (libName, libSmallName)
  465. cppRegisterOut += "\treturn 1;\n"
  466. cppRegisterOut += "}"
  467. cppRegisterHeaderOut = "" # Def: Global C++ *LUA.h
  468. cppRegisterHeaderOut += "#pragma once\n"
  469. cppRegisterHeaderOut += "#include <%s>\n" % (mainInclude)
  470. cppRegisterHeaderOut += "extern \"C\" {\n"
  471. cppRegisterHeaderOut += "#include <stdio.h>\n"
  472. cppRegisterHeaderOut += "#include \"lua.h\"\n"
  473. cppRegisterHeaderOut += "#include \"lualib.h\"\n"
  474. cppRegisterHeaderOut += "#include \"lauxlib.h\"\n"
  475. cppRegisterHeaderOut += "int _PolyExport luaopen_%s(lua_State *L);\n" % (prefix)
  476. cppRegisterHeaderOut += "}\n"
  477. # Write out global files
  478. mkdir_p(includePath)
  479. mkdir_p(apiPath)
  480. mkdir_p(sourcePath)
  481. fout = open("%s/%sLUA.h" % (includePath, prefix), "w")
  482. fout.write(cppRegisterHeaderOut)
  483. fout = open("%s/%s.lua" % (apiPath, prefix), "w")
  484. fout.write(luaIndexOut)
  485. fout = open("%s/%sLUAWrappers.h" % (includePath, prefix), "w")
  486. fout.write(wrappersHeaderOut)
  487. fout = open("%s/%sLUA.cpp" % (sourcePath, prefix), "w")
  488. fout.write(cppRegisterOut)
  489. # Create .pak zip archive
  490. pattern = '*.lua'
  491. os.chdir(apiPath)
  492. if libName == "Polycore":
  493. with ZipFile("api.pak", 'w') as myzip:
  494. for root, dirs, files in os.walk("."):
  495. for filename in fnmatch.filter(files, pattern):
  496. myzip.write(os.path.join(root, filename))
  497. if len(sys.argv) < 10:
  498. print ("Usage:\n%s [input path] [prefix] [main include] [lib small name] [lib name] [api path] [api class-path] [include path] [source path] [inherit-in-module-file path (optional)]" % (sys.argv[0]))
  499. sys.exit(1)
  500. else:
  501. createLUABindings(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6], sys.argv[7], sys.argv[8], sys.argv[9], sys.argv[10] if len(sys.argv)>10 else None)