create_lua_library.py 32 KB

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