LuaBindingsGenerator.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. import ConfigParser
  2. import io
  3. import os
  4. import re
  5. from zipfile import *
  6. import fnmatch
  7. import errno
  8. def mkdir_p(path):
  9. try:
  10. os.makedirs(path)
  11. except OSError as e:
  12. if e.errno == errno.EEXIST:
  13. pass
  14. else: raise
  15. class LuaBindingsGenerator(object):
  16. def __init__(self, config):
  17. self.config = config
  18. self.wrappersHeaderList = ""
  19. self.wrappersHeaderBody = ""
  20. self.cppRegisterOut = ""
  21. self.luaClassBindingOut = ""
  22. self.cppLoaderOut = ""
  23. self.luaIndexOut = ""
  24. self.disableGC = self.config.get('global', 'DisableGarbageCollection').replace(" ", "").split(",")
  25. self.inheritInModule = self.config.get('lua', 'InheritInModule').replace(" ", "").split(",")
  26. self.libName = self.config.get('global', 'LibraryName')
  27. self.cppRegisterOut += "int luaopen_%s(lua_State *L) {\n" % self.libName
  28. if self.libName != "Polycode" and self.libName != "Physics2D" and self.libName != "Physics3D" and self.libName != "UI":
  29. self.cppRegisterOut += "CoreServices *inst = (CoreServices*) *((PolyBase**)lua_touserdata(L, 1));\n"
  30. self.cppRegisterOut += "CoreServices::setInstance(inst);\n"
  31. self.cppRegisterOut += "\tstatic const luaL_Reg %sLib [] = {\n" % (self.libName)
  32. mkdir_p("%s/%s" % (self.config.get('lua', 'LuaApiDirectory'), self.libName))
  33. # ----------------------------------------------------
  34. # Process file
  35. # ----------------------------------------------------
  36. def processTargetFile(self, targetFile):
  37. self.wrappersHeaderList += "#include \"%s/%s\"\n" % (self.config.get('global', 'HeaderIncludeDirectory'), targetFile)
  38. # ----------------------------------------------------
  39. # Process class in file
  40. # ----------------------------------------------------
  41. def processClass(self, c):
  42. inherits = False
  43. parentClass = ""
  44. if "parent" in c:
  45. if c["parent"] in self.inheritInModule: # Parent class is in this module
  46. self.luaClassBindingOut += "require \"%s/%s\"\n\n" % (self.config.get('lua', 'LibraryName'), c["parent"])
  47. else:
  48. self.luaClassBindingOut += "require \"%s/%s\"\n\n" % (self.config.get('lua', 'DefaultModule'), c["parent"])
  49. self.luaClassBindingOut += "class \"%s\" (%s)\n\n" % (c["name"], c["parent"])
  50. parentClass = c["parent"]
  51. inherits = True
  52. if inherits == False:
  53. self.luaClassBindingOut += "class \"%s\"\n\n" % c["name"]
  54. self.generateLuaProperties(c)
  55. self.generateGettersForProperties(c)
  56. self.generateSettersForProperties(c)
  57. self.generateCBindingsForMethods(c)
  58. self.generateLuaMethods(c)
  59. self.cppLoaderOut += "\n\tluaL_newmetatable(L, \"%s.%s\");\n" % (self.libName, c["name"])
  60. if c["name"] not in self.disableGC:
  61. self.cppLoaderOut += "\tlua_pushstring(L, \"__gc\");\n"
  62. self.cppLoaderOut += "\tlua_pushcfunction(L, %s_delete_%s);\n" % (self.libName, c["name"])
  63. self.cppLoaderOut += "\tlua_settable(L, -3);\n"
  64. self.cppLoaderOut +="\tlua_pop(L, 1);\n"
  65. # Delete method (C++ side)
  66. self.cppRegisterOut += "\t\t{\"delete_%s\", %s_delete_%s},\n" % (c["name"], self.libName, c["name"])
  67. self.wrappersHeaderBody += "\tstatic int %s_delete_%s(lua_State *L) {\n" % (self.libName, c["name"])
  68. self.wrappersHeaderBody += "\t\tluaL_checktype(L, 1, LUA_TUSERDATA);\n"
  69. self.wrappersHeaderBody += "\t\tPolyBase **inst = (PolyBase**)lua_touserdata(L, 1);\n"
  70. self.wrappersHeaderBody += "\t\tdelete ((%s*) *inst);\n" % (c["name"])
  71. self.wrappersHeaderBody += "\t\t*inst = NULL;\n"
  72. self.wrappersHeaderBody += "\t\treturn 0;\n"
  73. self.wrappersHeaderBody += "\t}\n\n"
  74. self.writeClass(c)
  75. self.luaIndexOut += "require \"%s/%s\"\n" % (self.libName, c["name"])
  76. # ----------------------------------------------------
  77. # Write out the C bindings for Lua property setters
  78. # ----------------------------------------------------
  79. def generateSettersForProperties(self, c):
  80. for pp in c["properties"]:
  81. if self.isBasicType(pp["type"]):
  82. self.cppRegisterOut += "\t\t{\"%s_set_%s\", %s_%s_set_%s},\n" % (c["name"], pp["name"], self.libName, c["name"], pp["name"])
  83. self.wrappersHeaderBody += "static int %s_%s_set_%s(lua_State *L) {\n" % (self.libName, c["name"], pp["name"])
  84. self.wrappersHeaderBody += "\tluaL_checktype(L, 1, LUA_TUSERDATA);\n"
  85. self.wrappersHeaderBody += "\t%s *inst = (%s*) *((PolyBase**)lua_touserdata(L, 1));\n" % (c["name"], c["name"])
  86. outfunc = "this_shouldnt_happen"
  87. outfuncsuffix = ""
  88. if pp["type"] == "Number":
  89. outfunc = "lua_tonumber"
  90. if pp["type"] == "String":
  91. outfunc = "lua_tostring"
  92. if pp["type"] == "int":
  93. outfunc = "lua_tointeger"
  94. if pp["type"] == "PolyKEY":
  95. outfunc = "(PolyKEY)lua_tointeger"
  96. if pp["type"] == "bool":
  97. outfunc = "lua_toboolean"
  98. outfuncsuffix = " != 0"
  99. self.wrappersHeaderBody += "\t%s param = %s(L, 2)%s;\n" % (pp["type"], outfunc, outfuncsuffix)
  100. self.wrappersHeaderBody += "\tinst->%s = param;\n" % (pp["name"])
  101. self.wrappersHeaderBody += "\treturn 0;\n"
  102. self.wrappersHeaderBody += "}\n\n"
  103. else:
  104. self.cppRegisterOut += "\t\t{\"%s_set_%s\", %s_%s_set_%s},\n" % (c["name"], pp["name"], self.libName, c["name"], pp["name"])
  105. self.wrappersHeaderBody += "static int %s_%s_set_%s(lua_State *L) {\n" % (self.libName, c["name"], pp["name"])
  106. self.wrappersHeaderBody += "\tluaL_checktype(L, 1, LUA_TUSERDATA);\n"
  107. self.wrappersHeaderBody += "\t%s *inst = (%s*) *((PolyBase**)lua_touserdata(L, 1));\n" % (c["name"], c["name"])
  108. self.wrappersHeaderBody += "\tluaL_checktype(L, 2, LUA_TUSERDATA);\n"
  109. self.wrappersHeaderBody += "\t%s *argInst = (%s*) *((PolyBase**)lua_touserdata(L, 2));\n" % (pp["type"], pp["type"])
  110. self.wrappersHeaderBody += "\tinst->%s = *argInst;\n" % (pp["name"])
  111. self.wrappersHeaderBody += "\treturn 0;\n"
  112. self.wrappersHeaderBody += "}\n\n"
  113. # ----------------------------------------------------
  114. # Write out the C bindings for Lua property getters
  115. # ----------------------------------------------------
  116. def generateGettersForProperties(self, c):
  117. for pp in c["properties"]:
  118. self.cppRegisterOut += "\t\t{\"%s_get_%s\", %s_%s_get_%s},\n" % (c["name"], pp["name"], self.libName, c["name"], pp["name"])
  119. self.wrappersHeaderBody += "static int %s_%s_get_%s(lua_State *L) {\n" % (self.libName, c["name"], pp["name"])
  120. self.wrappersHeaderBody += "\tluaL_checktype(L, 1, LUA_TUSERDATA);\n"
  121. self.wrappersHeaderBody += "\t%s *inst = (%s*) *((PolyBase**)lua_touserdata(L, 1));\n" % (c["name"], c["name"])
  122. outfunc = "this_shouldnt_happen"
  123. retFunc = ""
  124. if pp["type"] == "Number":
  125. outfunc = "lua_pushnumber"
  126. if pp["type"] == "String":
  127. outfunc = "lua_pushstring"
  128. retFunc = ".c_str()"
  129. if pp["type"] == "int" or pp["type"] == "PolyKEY":
  130. outfunc = "lua_pushinteger"
  131. if pp["type"] == "bool":
  132. outfunc = "lua_pushboolean"
  133. if pp["type"] == "Number" or pp["type"] == "String" or pp["type"] == "int" or pp["type"] == "bool" or pp["type"] == "PolyKEY":
  134. self.wrappersHeaderBody += "\t%s(L, inst->%s%s);\n" % (outfunc, pp["name"], retFunc)
  135. else:
  136. if pp["type"].find("*") != -1:
  137. self.wrappersHeaderBody += "\tif(!inst->%s%s) {\n" % (pp["name"], retFunc)
  138. self.wrappersHeaderBody += "\t\tlua_pushnil(L);\n"
  139. self.wrappersHeaderBody += "\t} else {\n"
  140. self.wrappersHeaderBody += "\t\tPolyBase **userdataPtr = (PolyBase**)lua_newuserdata(L, sizeof(PolyBase*));\n"
  141. self.wrappersHeaderBody += "\t\t*userdataPtr = (PolyBase*)inst->%s%s;\n" % (pp["name"], retFunc)
  142. self.wrappersHeaderBody += "\t}\n"
  143. else:
  144. self.wrappersHeaderBody += "\tPolyBase **userdataPtr = (PolyBase**)lua_newuserdata(L, sizeof(PolyBase*));\n"
  145. self.wrappersHeaderBody += "\t*userdataPtr = (PolyBase*)&inst->%s%s;\n" % (pp["name"], retFunc)
  146. self.wrappersHeaderBody += "\treturn 1;\n"
  147. self.wrappersHeaderBody += "}\n\n"
  148. # ----------------------------------------------------
  149. # Write out the Lua setters and getters for properties in the Lua class
  150. # ----------------------------------------------------
  151. def generateLuaProperties(self, c):
  152. for pp in c["staticProperties"]:
  153. if "defaultValue" in pp:
  154. defaultValue = pp["defaultValue"]
  155. if re.match(r'\s*[a-zA-Z_][a-zA-Z0-9_]*\s*\+', defaultValue):
  156. defaultValue = "%s.%s" % (c["name"], defaultValue)
  157. self.luaClassBindingOut += "%s.%s = %s\n" % (c["name"], pp["name"], defaultValue)
  158. self.luaClassBindingOut += "\n"
  159. pidx = 0
  160. if len(c["properties"]) > 0:
  161. self.luaClassBindingOut += "function %s:__getvar(name)\n" % c["name"]
  162. for pp in c["properties"]:
  163. if pidx == 0:
  164. self.luaClassBindingOut += "\tif name == \"%s\" then\n" % (pp["name"])
  165. else:
  166. self.luaClassBindingOut += "\telseif name == \"%s\" then\n" % (pp["name"])
  167. if self.isBasicType(pp["type"]):
  168. self.luaClassBindingOut += "\t\treturn %s.%s_get_%s(self.__ptr)\n" % (self.libName, c["name"], pp["name"])
  169. else:
  170. self.luaClassBindingOut += "\t\tlocal retVal = %s.%s_get_%s(self.__ptr)\n" % (self.libName, c["name"], pp["name"])
  171. self.luaClassBindingOut += self.template_returnPtrLookup("\t\t", self.template_quote(pp["type"]), "retVal")
  172. pidx = pidx + 1
  173. self.luaClassBindingOut += "\tend\n"
  174. if "parent" in c:
  175. self.luaClassBindingOut += "\tif %s[\"__getvar\"] ~= nil then\n" % (c["parent"])
  176. self.luaClassBindingOut += "\t\treturn %s.__getvar(self, name)\n" % (c["parent"])
  177. self.luaClassBindingOut += "\tend\n"
  178. self.luaClassBindingOut += "end\n\n"
  179. self.luaClassBindingOut += "function %s:__setvar(name,value)\n" % c["name"]
  180. pidx = 0
  181. for pp in c["properties"]:
  182. if self.isBasicType(pp["type"]) == True:
  183. if pidx == 0:
  184. self.luaClassBindingOut += "\tif name == \"%s\" then\n" % (pp["name"])
  185. else:
  186. self.luaClassBindingOut += "\telseif name == \"%s\" then\n" % (pp["name"])
  187. self.luaClassBindingOut += "\t\t%s.%s_set_%s(self.__ptr, value)\n" % (self.libName, c["name"], pp["name"])
  188. self.luaClassBindingOut += "\t\treturn true\n"
  189. else:
  190. if pidx == 0:
  191. self.luaClassBindingOut += "\tif name == \"%s\" then\n" % (pp["name"])
  192. else:
  193. self.luaClassBindingOut += "\telseif name == \"%s\" then\n" % (pp["name"])
  194. self.luaClassBindingOut += "\t\t%s.%s_set_%s(self.__ptr, value.__ptr)\n" % (self.libName, c["name"], pp["name"])
  195. self.luaClassBindingOut += "\t\treturn true\n"
  196. pidx = pidx + 1
  197. self.luaClassBindingOut += "\tend\n"
  198. if "parent" in c:
  199. self.luaClassBindingOut += "\tif %s[\"__setvar\"] ~= nil then\n" % (c["parent"])
  200. self.luaClassBindingOut += "\t\treturn %s.__setvar(self, name, value)\n" % (c["parent"])
  201. self.luaClassBindingOut += "\telse\n"
  202. self.luaClassBindingOut += "\t\treturn false\n"
  203. self.luaClassBindingOut += "\tend\n"
  204. else:
  205. self.luaClassBindingOut += "\treturn false\n"
  206. self.luaClassBindingOut += "end\n"
  207. # ----------------------------------------------------
  208. # Write out the Lua class file for the class
  209. # ----------------------------------------------------
  210. def writeClass(self, c):
  211. self.luaClassBindingOut += "function %s:__delete()\n" % (c["name"])
  212. self.luaClassBindingOut += "\tif self then %s.delete_%s(self.__ptr) end\n" % (self.libName, c["name"])
  213. self.luaClassBindingOut += "end\n"
  214. if c["name"] != "EventDispatcher":
  215. fout = open("%s/%s/%s.lua" % (self.config.get('lua', 'LuaApiDirectory'), self.libName, c["name"]), "w")
  216. fout.write(self.luaClassBindingOut)
  217. fout.close()
  218. self.luaClassBindingOut = ""
  219. # ----------------------------------------------------
  220. # Create The C binding wrappers for class methods
  221. # ----------------------------------------------------
  222. def generateCBindingsForMethods(self, c):
  223. for method in c["methods"]:
  224. idx = 1
  225. if method["name"] == c["name"]:
  226. self.cppRegisterOut += "\t\t{\"%s\", %s_%s},\n" % (c["name"], self.libName, c["name"])
  227. self.wrappersHeaderBody += "\tstatic int %s_%s(lua_State *L) {\n" % (self.libName, c["name"])
  228. else:
  229. self.cppRegisterOut += "\t\t{\"%s_%s\", %s_%s_%s},\n" % (c["name"], method["name"], self.libName, c["name"], method["name"])
  230. self.wrappersHeaderBody += "\tstatic int %s_%s_%s(lua_State *L) {\n" % (self.libName, c["name"], method["name"])
  231. # if this is not a static method, get the class pointer being passed
  232. if method["isStatic"] == False:
  233. self.wrappersHeaderBody += "\t\tluaL_checktype(L, 1, LUA_TUSERDATA);\n"
  234. self.wrappersHeaderBody += "\t\t%s *inst = (%s*) *((PolyBase**)lua_touserdata(L, 1));\n" % (c["name"], c["name"])
  235. idx = 2
  236. paramlist = []
  237. lparamlist = []
  238. for param in method["parameters"]:
  239. luatype = "LUA_TUSERDATA"
  240. checkfunc = "lua_isuserdata"
  241. if param["type"].find("*") > -1:
  242. luafunc = "(%s) *((PolyBase**)lua_touserdata" % (param["type"].replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle"))
  243. elif param["type"].find("&") > -1:
  244. luafunc = "*(%s*) *((PolyBase**)lua_touserdata" % (param["type"].replace("const", "").replace("&", "").replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle"))
  245. else:
  246. luafunc = "*(%s*) *((PolyBase**)lua_touserdata" % (param["type"].replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle"))
  247. lend = ".__ptr"
  248. luafuncsuffix = ")"
  249. if param["type"] == "int":
  250. luafunc = "lua_tointeger"
  251. luatype = "LUA_TNUMBER"
  252. checkfunc = "lua_isnumber"
  253. luafuncsuffix = ""
  254. lend = ""
  255. if param["type"] == "PolyKEY":
  256. luafunc = "(PolyKEY)lua_tointeger"
  257. luatype = "LUA_TNUMBER"
  258. checkfunc = "lua_isnumber"
  259. luafuncsuffix = ""
  260. lend = ""
  261. if param["type"] == "bool":
  262. luafunc = "lua_toboolean"
  263. luatype = "LUA_TBOOLEAN"
  264. checkfunc = "lua_isboolean"
  265. luafuncsuffix = " != 0"
  266. lend = ""
  267. if param["type"] == "Number":
  268. luatype = "LUA_TNUMBER"
  269. luafunc = "lua_tonumber"
  270. checkfunc = "lua_isnumber"
  271. luafuncsuffix = ""
  272. lend = ""
  273. if param["type"] == "String":
  274. luatype = "LUA_TSTRING"
  275. luafunc = "lua_tostring"
  276. checkfunc = "lua_isstring"
  277. luafuncsuffix = ""
  278. lend = ""
  279. param["type"] = param["type"].replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle")
  280. if "defaultValue" in param:
  281. if checkfunc != "lua_isuserdata" or (checkfunc == "lua_isuserdata" and param["defaultValue"] == "NULL"):
  282. #param["defaultValue"] = param["defaultValue"].replace(" 0f", ".0f")
  283. param["defaultValue"] = param["defaultValue"].replace(": :", "::")
  284. #param["defaultValue"] = param["defaultValue"].replace("0 ", "0.")
  285. param["defaultValue"] = re.sub(r'([0-9]+) ([0-9])+', r'\1.\2', param["defaultValue"])
  286. self.wrappersHeaderBody += "\t\t%s %s;\n" % (param["type"], param["name"])
  287. self.wrappersHeaderBody += "\t\tif(%s(L, %d)) {\n" % (checkfunc, idx)
  288. self.wrappersHeaderBody += "\t\t\t%s = %s(L, %d)%s;\n" % (param["name"], luafunc, idx, luafuncsuffix)
  289. self.wrappersHeaderBody += "\t\t} else {\n"
  290. self.wrappersHeaderBody += "\t\t\t%s = %s;\n" % (param["name"], param["defaultValue"])
  291. self.wrappersHeaderBody += "\t\t}\n"
  292. else:
  293. self.wrappersHeaderBody += "\t\tluaL_checktype(L, %d, %s);\n" % (idx, luatype);
  294. if param["type"] == "String":
  295. self.wrappersHeaderBody += "\t\t%s %s = String(%s(L, %d));\n" % (param["type"], param["name"], luafunc, idx)
  296. else:
  297. self.wrappersHeaderBody += "\t\t%s %s = %s(L, %d)%s;\n" % (param["type"], param["name"], luafunc, idx,luafuncsuffix)
  298. else:
  299. self.wrappersHeaderBody += "\t\tluaL_checktype(L, %d, %s);\n" % (idx, luatype);
  300. if param["type"] == "String":
  301. self.wrappersHeaderBody += "\t\t%s %s = String(%s(L, %d));\n" % (param["type"], param["name"], luafunc, idx)
  302. else:
  303. self.wrappersHeaderBody += "\t\t%s %s = %s(L, %d)%s;\n" % (param["type"], param["name"], luafunc, idx, luafuncsuffix)
  304. paramlist.append(param["name"])
  305. lparamlist.append(param["name"]+lend)
  306. idx = idx +1 # Param parse success-- mark the increased stack
  307. # Generate C++-side method call / generate return value
  308. if method["name"] == c["name"]: # If constructor
  309. if c["name"] == "EventHandler": # See LuaEventHandler above
  310. self.wrappersHeaderBody += "\t\tLuaEventHandler *inst = new LuaEventHandler();\n"
  311. self.wrappersHeaderBody += "\t\tinst->wrapperIndex = luaL_ref(L, LUA_REGISTRYINDEX );\n"
  312. self.wrappersHeaderBody += "\t\tinst->L = L;\n"
  313. else:
  314. self.wrappersHeaderBody += "\t\t%s *inst = new %s(%s);\n" % (c["name"], c["name"], ", ".join(paramlist))
  315. self.wrappersHeaderBody += "\t\tPolyBase **userdataPtr = (PolyBase**)lua_newuserdata(L, sizeof(PolyBase*));\n"
  316. self.wrappersHeaderBody += "\t\t*userdataPtr = (PolyBase*)inst;\n"
  317. self.wrappersHeaderBody += "\t\tluaL_getmetatable(L, \"%s.%s\");\n" % (self.libName, c["name"])
  318. self.wrappersHeaderBody += "\t\tlua_setmetatable(L, -2);\n"
  319. self.wrappersHeaderBody += "\t\treturn 1;\n"
  320. else: #If non-constructor
  321. if method["isStatic"] == False:
  322. call = "inst->%s(%s)" % (method["name"], ", ".join(paramlist))
  323. else:
  324. call = "%s::%s(%s)" % (c["name"], method["name"], ", ".join(paramlist))
  325. if self.isVectorType(method["type"]):
  326. vectorReturnClass = self.getVectorType(method["type"])
  327. if vectorReturnClass.find("&") == -1 and vectorReturnClass.find("*") > -1: #FIXME: return references to std::vectors and basic types
  328. self.wrappersHeaderBody += "\t\tstd::vector<%s> retVector = %s;\n" % (vectorReturnClass,call)
  329. self.wrappersHeaderBody += "\t\tlua_newtable(L);\n"
  330. self.wrappersHeaderBody += "\t\tfor(int i=0; i < retVector.size(); i++) {\n"
  331. self.wrappersHeaderBody += "\t\t\tPolyBase **userdataPtr = (PolyBase**)lua_newuserdata(L, sizeof(PolyBase*));\n"
  332. self.wrappersHeaderBody += "\t\t\t*userdataPtr = (PolyBase*)retVector[i];\n"
  333. self.wrappersHeaderBody += "\t\t\tlua_rawseti(L, -2, i+1);\n"
  334. self.wrappersHeaderBody += "\t\t}\n"
  335. self.wrappersHeaderBody += "\t\treturn 1;\n"
  336. else:
  337. self.wrappersHeaderBody += "\t\treturn 0;\n"
  338. # else If void-typed:
  339. elif method["type"] == "void" or method["type"] == "static void" or method["type"] == "virtual void" or method["type"] == "inline void":
  340. self.wrappersHeaderBody += "\t\t%s;\n" % (call)
  341. self.wrappersHeaderBody += "\t\treturn 0;\n" # 0 arguments returned
  342. else: # If there is a return value:
  343. # What type is the return value? Default to pointer
  344. outfunc = "this_shouldnt_happen"
  345. retFunc = ""
  346. if method["type"] == "Number" or method["type"] == "inline Number":
  347. outfunc = "lua_pushnumber"
  348. if method["type"] == "String" or method["type"] == "static String": # TODO: Path for STL strings?
  349. outfunc = "lua_pushstring"
  350. retFunc = ".c_str()"
  351. if method["type"] == "int" or method["type"] == "unsigned int" or method["type"] == "static int" or method["type"] == "size_t" or method["type"] == "static size_t" or method["type"] == "long" or method["type"] == "unsigned int" or method["type"] == "static long" or method["type"] == "short" or method["type"] == "PolyKEY" or method["type"] == "wchar_t":
  352. outfunc = "lua_pushinteger"
  353. if method["type"] == "bool" or method["type"] == "static bool" or method["type"] == "virtual bool":
  354. outfunc = "lua_pushboolean"
  355. if method["type"].find("*") > -1: # Returned var is definitely a pointer.
  356. self.wrappersHeaderBody += "\t\tPolyBase *ptrRetVal = (PolyBase*)%s%s;\n" % (call, retFunc)
  357. self.wrappersHeaderBody += "\t\tif(ptrRetVal == NULL) {\n"
  358. self.wrappersHeaderBody += "\t\t\tlua_pushnil(L);\n"
  359. self.wrappersHeaderBody += "\t\t} else {\n"
  360. self.wrappersHeaderBody += "\t\t\tPolyBase **userdataPtr = (PolyBase**)lua_newuserdata(L, sizeof(PolyBase*));\n"
  361. self.wrappersHeaderBody += "\t\t\t*userdataPtr = ptrRetVal;\n"
  362. self.wrappersHeaderBody += "\t\t}\n"
  363. elif self.isBasicType(method["type"]) == True:
  364. self.wrappersHeaderBody += "\t\t%s(L, %s%s);\n" % (outfunc, call, retFunc)
  365. else: # Some static object is being returned. Convert it to a pointer, then return that.
  366. className = method["type"].replace("const", "").replace("&", "").replace("inline", "").replace("virtual", "").replace("static", "")
  367. if className == "Polygon": # Deal with potential windows.h conflict
  368. className = "Polycode::Polygon"
  369. if className == "Rectangle":
  370. className = "Polycode::Rectangle"
  371. if className == "Polycode : : Rectangle":
  372. className = "Polycode::Rectangle"
  373. self.wrappersHeaderBody += "\t\t%s *retInst = new %s();\n" % (className, className)
  374. self.wrappersHeaderBody += "\t\t*retInst = %s;\n" % (call)
  375. self.wrappersHeaderBody += "\t\tPolyBase **userdataPtr = (PolyBase**)lua_newuserdata(L, sizeof(PolyBase*));\n"
  376. self.wrappersHeaderBody += "\t\tluaL_getmetatable(L, \"%s.%s\");\n" % (self.libName, className)
  377. self.wrappersHeaderBody += "\t\tlua_setmetatable(L, -2);\n"
  378. self.wrappersHeaderBody += "\t\t*userdataPtr = (PolyBase*)retInst;\n"
  379. self.wrappersHeaderBody += "\t\treturn 1;\n"
  380. self.wrappersHeaderBody += "\t}\n"
  381. # ----------------------------------------------------
  382. # Create Lua API for all class methods
  383. # ----------------------------------------------------
  384. def generateLuaMethods(self, c):
  385. for method in c["methods"]:
  386. paramlist = []
  387. lparamlist = []
  388. for param in method["parameters"]:
  389. paramlist.append(param["name"])
  390. if not self.isBasicType(param["type"]):
  391. lparamlist.append("%s.__ptr" % param["name"])
  392. else:
  393. lparamlist.append(param["name"])
  394. if method["name"] == c["name"]: # Constructor
  395. self.luaClassBindingOut += "function %s:%s(...)\n" % (c["name"], c["name"])
  396. self.luaClassBindingOut += "\tlocal arg = {...}\n"
  397. if "parent" in c:
  398. self.luaClassBindingOut += "\tif type(arg[1]) == \"table\" and count(arg) == 1 then\n"
  399. self.luaClassBindingOut += "\t\tif \"\"..arg[1].__classname == \"%s\" then\n" % (c["parent"])
  400. self.luaClassBindingOut += "\t\t\tself.__ptr = arg[1].__ptr\n"
  401. self.luaClassBindingOut += "\t\t\treturn\n"
  402. self.luaClassBindingOut += "\t\tend\n"
  403. self.luaClassBindingOut += "\tend\n"
  404. self.luaClassBindingOut += "\tfor k,v in pairs(arg) do\n"
  405. self.luaClassBindingOut += "\t\tif type(v) == \"table\" then\n"
  406. self.luaClassBindingOut += "\t\t\tif v.__ptr ~= nil then\n"
  407. self.luaClassBindingOut += "\t\t\t\targ[k] = v.__ptr\n"
  408. self.luaClassBindingOut += "\t\t\tend\n"
  409. self.luaClassBindingOut += "\t\tend\n"
  410. self.luaClassBindingOut += "\tend\n"
  411. self.luaClassBindingOut += "\tif self.__ptr == nil and arg[1] ~= \"__skip_ptr__\" then\n"
  412. if c["name"] == "EventHandler": # See LuaEventHandler above
  413. self.luaClassBindingOut += "\t\tself.__ptr = %s.%s(self)\n" % (self.libName, c["name"])
  414. else:
  415. self.luaClassBindingOut += "\t\tself.__ptr = %s.%s(unpack(arg))\n" % (self.libName, c["name"])
  416. self.luaClassBindingOut += "\tend\n"
  417. self.luaClassBindingOut += "end\n\n"
  418. else: # Non-constructors.
  419. if method["type"].find("static ") == -1: # Non-static method
  420. self.luaClassBindingOut += "function %s:%s(%s)\n" % (c["name"], method["name"], ", ".join(paramlist))
  421. if len(lparamlist):
  422. self.luaClassBindingOut += "\tlocal retVal = %s.%s_%s(self.__ptr, %s)\n" % (self.libName, c["name"], method["name"], ", ".join(lparamlist))
  423. else:
  424. self.luaClassBindingOut += "\tlocal retVal = %s.%s_%s(self.__ptr)\n" % (self.libName, c["name"], method["name"])
  425. else: # Static method
  426. self.luaClassBindingOut += "function %s.%s(%s)\n" % (c["name"], method["name"], ", ".join(paramlist))
  427. if len(lparamlist):
  428. self.luaClassBindingOut += "\tlocal retVal = %s.%s_%s(%s)\n" % (self.libName, c["name"], method["name"], ", ".join(lparamlist))
  429. else:
  430. self.luaClassBindingOut += "\tlocal retVal = %s.%s_%s()\n" % (self.libName, c["name"], method["name"])
  431. if method["type"] != "void":
  432. if self.isBasicType(method["type"]):
  433. self.luaClassBindingOut += "\treturn retVal\n"
  434. else:
  435. if self.isVectorType(method["type"]) == True:
  436. className = self.getVectorType(method["type"]).replace("*", "")
  437. self.luaClassBindingOut += self.template_returnPtrLookupArray("\t",self.template_quote(className),"retVal")
  438. else:
  439. className = method["type"].replace("const", "").replace("&", "").replace("inline", "").replace("virtual", "").replace("static", "").replace("*","").replace(" ", "")
  440. self.luaClassBindingOut += self.template_returnPtrLookup("\t",self.template_quote(className),"retVal")
  441. self.luaClassBindingOut += "end\n\n"
  442. # ----------------------------------------------------
  443. # Write out final files
  444. # ----------------------------------------------------
  445. def finalize(self):
  446. with open(self.config.get('lua', 'WrapperHeaderTemplate'), 'r') as f:
  447. out = f.read().replace("%HEADERS%", self.wrappersHeaderList)
  448. out = out.replace("%BODY%", self.wrappersHeaderBody)
  449. fout = open(self.config.get('lua', 'WrapperHeaderTarget'), "w")
  450. fout.write(out)
  451. fout.close()
  452. with open(self.config.get('lua', 'WrapperMainHeaderTemplate'), 'r') as f:
  453. out = f.read().replace("%BODY%", "int _PolyExport luaopen_%s(lua_State *L);" % self.libName)
  454. fout = open(self.config.get('lua', 'WrapperMainHeaderTarget'), "w")
  455. fout.write(out)
  456. fout.close()
  457. self.cppRegisterOut += "\t\t{NULL, NULL}\n"
  458. self.cppRegisterOut += "\t};\n"
  459. self.cppRegisterOut += "\tlua_newtable(L);\n"
  460. self.cppRegisterOut += "\tluaL_setfuncs (L,%sLib,0);\n" % (self.libName)
  461. self.cppRegisterOut += "\tlua_setglobal(L,\"%s\");" % (self.libName)
  462. self.cppRegisterOut += self.cppLoaderOut
  463. self.cppRegisterOut += "\treturn 1;\n"
  464. self.cppRegisterOut += "}"
  465. with open(self.config.get('lua', 'WrapperSourceTemplate'), 'r') as f:
  466. out = f.read().replace("%BODY%", self.cppRegisterOut)
  467. fout = open(self.config.get('lua', 'WrapperSourceTarget'), "w")
  468. fout.write(out)
  469. fout.close()
  470. fout = open("%s/%s.lua" % (self.config.get('lua', 'LuaApiDirectory'), self.libName), "w")
  471. fout.write(self.luaIndexOut)
  472. fout.close()
  473. pattern = '*.lua'
  474. curDir = os.path.dirname(os.path.realpath(__file__))
  475. os.chdir(self.config.get('lua', 'LuaApiDirectory'))
  476. with ZipFile("lua_%s.pak" % (self.libName), 'w') as myzip:
  477. for root, dirs, files in os.walk("."):
  478. for filename in fnmatch.filter(files, pattern):
  479. myzip.write(os.path.join(root, filename))
  480. os.chdir(curDir)
  481. # ----------------------------------------------------
  482. # Utility methods
  483. # ----------------------------------------------------
  484. def isVectorType(self, t):
  485. if t.find("<") > -1 and t.find("vector") > -1:
  486. return True
  487. else:
  488. return False
  489. def getVectorType(self, t):
  490. return t.replace("std::", "").replace("vector<", "").replace(">","").replace(" ", "")
  491. def isBasicType(self, t):
  492. basicTypes = ["int", "Number", "String", "PolyKEY", "bool"]
  493. if t in basicTypes:
  494. return True
  495. else:
  496. return False
  497. def template_returnPtrLookupArray(self, prefix, className, ptr):
  498. out = "%sif %s == nil then return nil end\n" % (prefix, ptr)
  499. out += "%sfor i=1,count(%s) do\n" % (prefix, ptr)
  500. out += "%s\tlocal __c = _G[%s](\"__skip_ptr__\")\n" % (prefix, className.replace("*", ""))
  501. out += "%s\t__c.__ptr = %s[i]\n" % (prefix, ptr)
  502. out += "%s\t%s[i] = __c\n" % (prefix, ptr)
  503. out += "%send\n" % (prefix)
  504. out += "%sreturn %s\n" % (prefix,ptr)
  505. return out
  506. def template_returnPtrLookup(self, prefix, className, ptr):
  507. out = "%sif %s == nil then return nil end\n" % (prefix, ptr)
  508. out += "%slocal __c = _G[%s](\"__skip_ptr__\")\n" % (prefix, className.replace("*", ""))
  509. out += "%s__c.__ptr = %s\n" % (prefix, ptr)
  510. out += "%sreturn __c\n" % (prefix)
  511. return out
  512. def template_quote(self, str):
  513. return "\"%s\"" % str;