create_lua_library.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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):
  16. out = ""
  17. sout = ""
  18. lfout = ""
  19. sout += "#include \"%sLUA.h\"\n" % (prefix)
  20. sout += "#include \"%sLUAWrappers.h\"\n" % (prefix)
  21. sout += "#include \"PolyCoreServices.h\"\n\n"
  22. sout += "using namespace Polycode;\n\n"
  23. sout += "int luaopen_%s(lua_State *L) {\n" % (prefix)
  24. if prefix != "Polycode":
  25. sout += "CoreServices *inst = (CoreServices*)lua_topointer(L, 1);\n"
  26. sout += "CoreServices::setInstance(inst);\n"
  27. sout += "\tstatic const struct luaL_reg %sLib [] = {" % (libSmallName)
  28. out += "#pragma once\n\n"
  29. out += "extern \"C\" {\n\n"
  30. out += "#include <stdio.h>\n"
  31. out += "#include \"lua.h\"\n"
  32. out += "#include \"lualib.h\"\n"
  33. out += "#include \"lauxlib.h\"\n"
  34. out += "} // extern \"C\" \n\n"
  35. files = os.listdir(inputPath)
  36. filteredFiles = []
  37. for fileName in files:
  38. ignore = ["PolyGLSLProgram", "PolyGLSLShader", "PolyGLSLShaderModule", "PolyWinCore", "PolyCocoaCore", "PolyAGLCore", "PolySDLCore", "Poly_iPhone", "PolyGLES1Renderer", "PolyGLRenderer", "tinyxml", "tinystr", "OpenGLCubemap", "PolyiPhoneCore", "PolyGLES1Texture", "PolyGLTexture", "PolyGLVertexBuffer", "PolyThreaded"]
  39. if fileName.split(".")[1] == "h" and fileName.split(".")[0] not in ignore:
  40. filteredFiles.append(fileName)
  41. out += "#include \"%s\"\n" % (fileName)
  42. out += "\nusing namespace std;\n\n"
  43. out += "\nnamespace Polycode {\n\n"
  44. if prefix == "Polycode":
  45. out += "class LuaEventHandler : public EventHandler {\n"
  46. out += "public:\n"
  47. out += " LuaEventHandler() : EventHandler() {}\n"
  48. out += " ~LuaEventHandler();\n"
  49. out += " void handleEvent(Event *e) {\n"
  50. out += " lua_rawgeti( L, LUA_REGISTRYINDEX, wrapperIndex );\n"
  51. out += " lua_getfield(L, -1, \"__handleEvent\");\n"
  52. out += " lua_rawgeti( L, LUA_REGISTRYINDEX, wrapperIndex );\n"
  53. out += " lua_pushlightuserdata(L, e);\n"
  54. out += " lua_call(L, 2, 0);\n"
  55. out += " }\n"
  56. out += " int wrapperIndex;\n"
  57. out += " lua_State *L;\n"
  58. out += "};\n\n"
  59. for fileName in filteredFiles:
  60. inheritInModule = ["PhysicsSceneEntity", "CollisionScene", "CollisionSceneEntity"]
  61. headerFile = "%s/%s" % (inputPath, fileName)
  62. print "Parsing %s" % fileName
  63. try:
  64. f = open(headerFile)
  65. contents = f.read().replace("_PolyExport", "")
  66. cppHeader = CppHeaderParser.CppHeader(contents, "string")
  67. ignore_classes = ["PolycodeShaderModule", "Object", "Threaded", "OpenGLCubemap"]
  68. for ckey in cppHeader.classes:
  69. print ">> Parsing class %s" % ckey
  70. c = cppHeader.classes[ckey]
  71. # if ckey == "ParticleEmitter":
  72. # print c
  73. lout = ""
  74. inherits = False
  75. if len(c["inherits"]) > 0:
  76. if c["inherits"][0]["class"] not in ignore_classes:
  77. if c["inherits"][0]["class"] in inheritInModule:
  78. lout += "require \"%s/%s\"\n\n" % (prefix, c["inherits"][0]["class"])
  79. else:
  80. lout += "require \"Polycode/%s\"\n\n" % (c["inherits"][0]["class"])
  81. lout += "class \"%s\" (%s)\n\n" % (ckey, c["inherits"][0]["class"])
  82. inherits = True
  83. if inherits == False:
  84. lout += "class \"%s\"\n\n" % ckey
  85. if len(c["methods"]["public"]) < 2 or ckey in ignore_classes:
  86. continue
  87. if ckey == "OSFileEntry":
  88. print c["methods"]["public"]
  89. parsed_methods = []
  90. ignore_methods = ["readByte32", "readByte16", "getCustomEntitiesByType", "Core", "Renderer", "Shader", "Texture", "handleEvent", "secondaryHandler", "getSTLString"]
  91. lout += "\n\n"
  92. pps = []
  93. for pp in c["properties"]["public"]:
  94. pp["type"] = pp["type"].replace("Polycode::", "")
  95. pp["type"] = pp["type"].replace("std::", "")
  96. if pp["type"].find("static ") != -1:
  97. if "defaltValue" in pp:
  98. lout += "%s = %s\n" % (pp["name"], pp["defaltValue"])
  99. else:
  100. #there are some bugs in the class parser that cause it to return junk
  101. if pp["type"].find("*") == -1 and pp["type"].find("vector") == -1 and pp["name"] != "16" and pp["name"] != "setScale" and pp["name"] != "setPosition" and pp["name"] != "BUFFER_CACHE_PRECISION":
  102. pps.append(pp)
  103. #if pp["type"] == "Number" or pp["type"] == "String" or pp["type"] == "int" or pp["type"] == "bool":
  104. # pps.append(pp)
  105. #else:
  106. # print(">>> Skipping %s[%s %s]" % (ckey, pp["type"], pp["name"]))
  107. pidx = 0
  108. # hack to fix the lack of multiple inheritance
  109. #if ckey == "ScreenParticleEmitter" or ckey == "SceneParticleEmitter":
  110. # pps.append({"name": "emitter", "type": "ParticleEmitter"})
  111. if len(pps) > 0:
  112. lout += "function %s:__index__(name)\n" % ckey
  113. for pp in pps:
  114. pp["type"] = pp["type"].replace("Polycode::", "")
  115. pp["type"] = pp["type"].replace("std::", "")
  116. if pidx == 0:
  117. lout += "\tif name == \"%s\" then\n" % (pp["name"])
  118. else:
  119. lout += "\telseif name == \"%s\" then\n" % (pp["name"])
  120. if pp["type"] == "Number" or pp["type"] == "String" or pp["type"] == "int" or pp["type"] == "bool":
  121. lout += "\t\treturn %s.%s_get_%s(self.__ptr)\n" % (libName, ckey, pp["name"])
  122. elif (ckey == "ScreenParticleEmitter" or ckey == "SceneParticleEmitter") and pp["name"] == "emitter":
  123. lout += "\t\tlocal ret = %s(\"__skip_ptr__\")\n" % (pp["type"])
  124. lout += "\t\tret.__ptr = self.__ptr\n"
  125. lout += "\t\treturn ret\n"
  126. else:
  127. lout += "\t\tretVal = %s.%s_get_%s(self.__ptr)\n" % (libName, ckey, pp["name"])
  128. lout += "\t\tif Polycore.__ptr_lookup[retVal] ~= nil then\n"
  129. lout += "\t\t\treturn Polycore.__ptr_lookup[retVal]\n"
  130. lout += "\t\telse\n"
  131. lout += "\t\t\tPolycore.__ptr_lookup[retVal] = %s(\"__skip_ptr__\")\n" % (pp["type"])
  132. lout += "\t\t\tPolycore.__ptr_lookup[retVal].__ptr = retVal\n"
  133. lout += "\t\t\treturn Polycore.__ptr_lookup[retVal]\n"
  134. lout += "\t\tend\n"
  135. if not ((ckey == "ScreenParticleEmitter" or ckey == "SceneParticleEmitter") and pp["name"] == "emitter"):
  136. sout += "\t\t{\"%s_get_%s\", %s_%s_get_%s},\n" % (ckey, pp["name"], libName, ckey, pp["name"])
  137. out += "static int %s_%s_get_%s(lua_State *L) {\n" % (libName, ckey, pp["name"])
  138. out += "\tluaL_checktype(L, 1, LUA_TLIGHTUSERDATA);\n"
  139. out += "\t%s *inst = (%s*)lua_topointer(L, 1);\n" % (ckey, ckey)
  140. outfunc = "lua_pushlightuserdata"
  141. retFunc = ""
  142. if pp["type"] == "Number":
  143. outfunc = "lua_pushnumber"
  144. if pp["type"] == "String":
  145. outfunc = "lua_pushstring"
  146. retFunc = ".c_str()"
  147. if pp["type"] == "int":
  148. outfunc = "lua_pushinteger"
  149. if pp["type"] == "bool":
  150. outfunc = "lua_pushboolean"
  151. if pp["type"] == "Number" or pp["type"] == "String" or pp["type"] == "int" or pp["type"] == "bool":
  152. out += "\t%s(L, inst->%s%s);\n" % (outfunc, pp["name"], retFunc)
  153. else:
  154. out += "\t%s(L, &inst->%s%s);\n" % (outfunc, pp["name"], retFunc)
  155. out += "\treturn 1;\n"
  156. out += "}\n\n"
  157. pidx = pidx + 1
  158. lout += "\tend\n"
  159. lout += "end\n"
  160. lout += "\n\n"
  161. pidx = 0
  162. if len(pps) > 0:
  163. lout += "function %s:__set_callback(name,value)\n" % ckey
  164. for pp in pps:
  165. pp["type"] = pp["type"].replace("Polycode::", "")
  166. pp["type"] = pp["type"].replace("std::", "")
  167. if pp["type"] == "Number" or pp["type"] == "String" or pp["type"] == "int" or pp["type"] == "bool":
  168. if pidx == 0:
  169. lout += "\tif name == \"%s\" then\n" % (pp["name"])
  170. else:
  171. lout += "\telseif name == \"%s\" then\n" % (pp["name"])
  172. lout += "\t\t%s.%s_set_%s(self.__ptr, value)\n" % (libName, ckey, pp["name"])
  173. lout += "\t\treturn true\n"
  174. sout += "\t\t{\"%s_set_%s\", %s_%s_set_%s},\n" % (ckey, pp["name"], libName, ckey, pp["name"])
  175. out += "static int %s_%s_set_%s(lua_State *L) {\n" % (libName, ckey, pp["name"])
  176. out += "\tluaL_checktype(L, 1, LUA_TLIGHTUSERDATA);\n"
  177. out += "\t%s *inst = (%s*)lua_topointer(L, 1);\n" % (ckey, ckey)
  178. outfunc = "lua_topointer"
  179. if pp["type"] == "Number":
  180. outfunc = "lua_tonumber"
  181. if pp["type"] == "String":
  182. outfunc = "lua_tostring"
  183. if pp["type"] == "int":
  184. outfunc = "lua_tointeger"
  185. if pp["type"] == "bool":
  186. outfunc = "lua_toboolean"
  187. out += "\t%s param = %s(L, 2);\n" % (pp["type"], outfunc)
  188. out += "\tinst->%s = param;\n" % (pp["name"])
  189. out += "\treturn 0;\n"
  190. out += "}\n\n"
  191. pidx = pidx + 1
  192. if pidx != 0:
  193. lout += "\tend\n"
  194. lout += "\treturn false\n"
  195. lout += "end\n"
  196. lout += "\n\n"
  197. for pm in c["methods"]["public"]:
  198. if pm["name"] in parsed_methods or pm["name"].find("operator") > -1 or pm["name"] in ignore_methods:
  199. continue
  200. if pm["name"] == "~"+ckey or pm["rtnType"].find("<") > -1:
  201. out += ""
  202. else:
  203. basicType = False
  204. voidRet = False
  205. if pm["name"] == ckey:
  206. sout += "\t\t{\"%s\", %s_%s},\n" % (ckey, libName, ckey)
  207. out += "static int %s_%s(lua_State *L) {\n" % (libName, ckey)
  208. idx = 1
  209. else:
  210. sout += "\t\t{\"%s_%s\", %s_%s_%s},\n" % (ckey, pm["name"], libName, ckey, pm["name"])
  211. out += "static int %s_%s_%s(lua_State *L) {\n" % (libName, ckey, pm["name"])
  212. if pm["rtnType"].find("static ") == -1:
  213. out += "\tluaL_checktype(L, 1, LUA_TLIGHTUSERDATA);\n"
  214. out += "\t%s *inst = (%s*)lua_topointer(L, 1);\n" % (ckey, ckey)
  215. idx = 2
  216. paramlist = []
  217. lparamlist = []
  218. for param in pm["parameters"]:
  219. if not param.has_key("type"):
  220. continue
  221. if param["type"] == "0":
  222. continue
  223. param["type"] = param["type"].replace("Polycode::", "")
  224. param["type"] = param["type"].replace("std::", "")
  225. param["type"] = param["type"].replace("const", "")
  226. param["type"] = param["type"].replace("&", "")
  227. param["type"] = param["type"].replace(" ", "")
  228. param["type"] = param["type"].replace("long", "long ")
  229. param["type"] = param["type"].replace("unsigned", "unsigned ")
  230. param["name"] = param["name"].replace("end", "_end").replace("repeat", "_repeat")
  231. if"type" in param:
  232. luatype = "LUA_TLIGHTUSERDATA"
  233. checkfunc = "lua_islightuserdata"
  234. if param["type"].find("*") > -1:
  235. luafunc = "(%s)lua_topointer" % (param["type"].replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle"))
  236. elif param["type"].find("&") > -1:
  237. luafunc = "*(%s*)lua_topointer" % (param["type"].replace("const", "").replace("&", "").replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle"))
  238. else:
  239. luafunc = "*(%s*)lua_topointer" % (param["type"].replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle"))
  240. lend = ".__ptr"
  241. if param["type"] == "int" or param["type"] == "unsigned int":
  242. luafunc = "lua_tointeger"
  243. luatype = "LUA_TNUMBER"
  244. checkfunc = "lua_isnumber"
  245. lend = ""
  246. if param["type"] == "bool":
  247. luafunc = "lua_toboolean"
  248. luatype = "LUA_TBOOLEAN"
  249. checkfunc = "lua_isboolean"
  250. lend = ""
  251. if param["type"] == "Number" or param["type"] == "float" or param["type"] == "double":
  252. luatype = "LUA_TNUMBER"
  253. luafunc = "lua_tonumber"
  254. checkfunc = "lua_isnumber"
  255. lend = ""
  256. if param["type"] == "String":
  257. luatype = "LUA_TSTRING"
  258. luafunc = "lua_tostring"
  259. checkfunc = "lua_isstring"
  260. lend = ""
  261. param["type"] = param["type"].replace("Polygon", "Polycode::Polygon").replace("Rectangle", "Polycode::Rectangle")
  262. if "defaltValue" in param:
  263. if checkfunc != "lua_islightuserdata" or (checkfunc == "lua_islightuserdata" and param["defaltValue"] == "NULL"):
  264. #param["defaltValue"] = param["defaltValue"].replace(" 0f", ".0f")
  265. param["defaltValue"] = param["defaltValue"].replace(": :", "::")
  266. #param["defaltValue"] = param["defaltValue"].replace("0 ", "0.")
  267. param["defaltValue"] = re.sub(r'([0-9]+) ([0-9])+', r'\1.\2', param["defaltValue"])
  268. out += "\t%s %s;\n" % (param["type"], param["name"])
  269. out += "\tif(%s(L, %d)) {\n" % (checkfunc, idx)
  270. out += "\t\t%s = %s(L, %d);\n" % (param["name"], luafunc, idx)
  271. out += "\t} else {\n"
  272. out += "\t\t%s = %s;\n" % (param["name"], param["defaltValue"])
  273. out += "\t}\n"
  274. else:
  275. out += "\tluaL_checktype(L, %d, %s);\n" % (idx, luatype);
  276. if param["type"] == "String":
  277. out += "\t%s %s = String(%s(L, %d));\n" % (param["type"], param["name"], luafunc, idx)
  278. else:
  279. out += "\t%s %s = %s(L, %d);\n" % (param["type"], param["name"], luafunc, idx)
  280. else:
  281. out += "\tluaL_checktype(L, %d, %s);\n" % (idx, luatype);
  282. if param["type"] == "String":
  283. out += "\t%s %s = String(%s(L, %d));\n" % (param["type"], param["name"], luafunc, idx)
  284. else:
  285. out += "\t%s %s = %s(L, %d);\n" % (param["type"], param["name"], luafunc, idx)
  286. paramlist.append(param["name"])
  287. lparamlist.append(param["name"]+lend)
  288. idx = idx +1
  289. if pm["name"] == ckey:
  290. if ckey == "EventHandler":
  291. out += "\tLuaEventHandler *inst = new LuaEventHandler();\n"
  292. out += "\tinst->wrapperIndex = luaL_ref(L, LUA_REGISTRYINDEX );\n"
  293. out += "\tinst->L = L;\n"
  294. else:
  295. out += "\t%s *inst = new %s(%s);\n" % (ckey, ckey, ", ".join(paramlist))
  296. out += "\tlua_pushlightuserdata(L, (void*)inst);\n"
  297. out += "\treturn 1;\n"
  298. else:
  299. if pm["rtnType"].find("static ") == -1:
  300. call = "inst->%s(%s)" % (pm["name"], ", ".join(paramlist))
  301. else:
  302. call = "%s::%s(%s)" % (ckey, pm["name"], ", ".join(paramlist))
  303. if pm["rtnType"] == "void" or pm["rtnType"] == "static void" or pm["rtnType"] == "virtual void" or pm["rtnType"] == "inline void":
  304. out += "\t%s;\n" % (call)
  305. basicType = True
  306. voidRet = True
  307. out += "\treturn 0;\n"
  308. else:
  309. outfunc = "lua_pushlightuserdata"
  310. retFunc = ""
  311. basicType = False
  312. if pm["rtnType"] == "Number" or pm["rtnType"] == "inline Number":
  313. outfunc = "lua_pushnumber"
  314. basicType = True
  315. if pm["rtnType"] == "String" or pm["rtnType"] == "static String":
  316. outfunc = "lua_pushstring"
  317. basicType = True
  318. retFunc = ".c_str()"
  319. 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":
  320. outfunc = "lua_pushinteger"
  321. basicType = True
  322. if pm["rtnType"] == "bool" or pm["rtnType"] == "static bool" or pm["rtnType"] == "virtual bool":
  323. outfunc = "lua_pushboolean"
  324. basicType = True
  325. if pm["rtnType"].find("*") > -1:
  326. out += "\tvoid *ptrRetVal = (void*)%s%s;\n" % (call, retFunc)
  327. out += "\tif(ptrRetVal == NULL) {\n"
  328. out += "\t\tlua_pushnil(L);\n"
  329. out += "\t} else {\n"
  330. out += "\t\t%s(L, ptrRetVal);\n" % (outfunc)
  331. out += "\t}\n"
  332. elif basicType == True:
  333. out += "\t%s(L, %s%s);\n" % (outfunc, call, retFunc)
  334. else:
  335. className = pm["rtnType"].replace("const", "").replace("&", "").replace("inline", "").replace("virtual", "").replace("static", "")
  336. if className == "Polygon":
  337. className = "Polycode::Polygon"
  338. if className == "Rectangle":
  339. className = "Polycode::Rectangle"
  340. out += "\t%s *retInst = new %s();\n" % (className, className)
  341. out += "\t*retInst = %s;\n" % (call)
  342. out += "\t%s(L, retInst);\n" % (outfunc)
  343. out += "\treturn 1;\n"
  344. out += "}\n\n"
  345. if pm["name"] == ckey:
  346. lout += "function %s:%s(...)\n" % (ckey, ckey)
  347. if inherits:
  348. lout += "\tif type(arg[1]) == \"table\" and count(arg) == 1 then\n"
  349. lout += "\t\tif \"\"..arg[1]:class() == \"%s\" then\n" % (c["inherits"][0]["class"])
  350. lout += "\t\t\tself.__ptr = arg[1].__ptr\n"
  351. lout += "\t\t\treturn\n"
  352. lout += "\t\tend\n"
  353. lout += "\tend\n"
  354. lout += "\tfor k,v in pairs(arg) do\n"
  355. lout += "\t\tif type(v) == \"table\" then\n"
  356. lout += "\t\t\tif v.__ptr ~= nil then\n"
  357. lout += "\t\t\t\targ[k] = v.__ptr\n"
  358. lout += "\t\t\tend\n"
  359. lout += "\t\tend\n"
  360. lout += "\tend\n"
  361. lout += "\tif self.__ptr == nil and arg[1] ~= \"__skip_ptr__\" then\n"
  362. if ckey == "EventHandler":
  363. lout += "\t\tself.__ptr = %s.%s(self)\n" % (libName, ckey)
  364. else:
  365. lout += "\t\tself.__ptr = %s.%s(unpack(arg))\n" % (libName, ckey)
  366. lout += "\t\tPolycore.__ptr_lookup[self.__ptr] = self\n"
  367. lout += "\tend\n"
  368. lout += "end\n\n"
  369. else:
  370. lout += "function %s:%s(%s)\n" % (ckey, pm["name"], ", ".join(paramlist))
  371. if pm["rtnType"].find("static ") == -1:
  372. if len(lparamlist):
  373. lout += "\tlocal retVal = %s.%s_%s(self.__ptr, %s)\n" % (libName, ckey, pm["name"], ", ".join(lparamlist))
  374. else:
  375. lout += "\tlocal retVal = %s.%s_%s(self.__ptr)\n" % (libName, ckey, pm["name"])
  376. else:
  377. if len(lparamlist):
  378. lout += "\tlocal retVal = %s.%s_%s(%s)\n" % (libName, ckey, pm["name"], ", ".join(lparamlist))
  379. else:
  380. lout += "\tlocal retVal = %s.%s_%s()\n" % (libName, ckey, pm["name"])
  381. if not voidRet:
  382. if basicType == True:
  383. lout += "\treturn retVal\n"
  384. else:
  385. className = pm["rtnType"].replace("const", "").replace("&", "").replace("inline", "").replace("virtual", "").replace("static", "").replace("*","").replace(" ", "")
  386. lout += "\tif retVal == nil then return nil end\n"
  387. lout += "\tif Polycore.__ptr_lookup[retVal] ~= nil then\n"
  388. lout += "\t\treturn Polycore.__ptr_lookup[retVal]\n"
  389. lout += "\telse\n"
  390. lout += "\t\tPolycore.__ptr_lookup[retVal] = %s(\"__skip_ptr__\")\n" % (className)
  391. lout += "\t\tPolycore.__ptr_lookup[retVal].__ptr = retVal\n"
  392. lout += "\t\treturn Polycore.__ptr_lookup[retVal]\n"
  393. lout += "\tend\n"
  394. lout += "end\n\n"
  395. parsed_methods.append(pm["name"])
  396. #cleanup
  397. sout += "\t\t{\"delete_%s\", %s_delete_%s},\n" % (ckey, libName, ckey)
  398. out += "static int %s_delete_%s(lua_State *L) {\n" % (libName, ckey)
  399. out += "\tluaL_checktype(L, 1, LUA_TLIGHTUSERDATA);\n"
  400. out += "\t%s *inst = (%s*)lua_topointer(L, 1);\n" % (ckey, ckey)
  401. out += "\tdelete inst;\n"
  402. out += "\treturn 0;\n"
  403. out += "}\n\n"
  404. lout += "\n\n"
  405. lout += "function %s:__delete()\n" % (ckey)
  406. lout += "\tPolycore.__ptr_lookup[self.__ptr] = nil\n"
  407. lout += "\t%s.delete_%s(self.__ptr)\n" % (libName, ckey)
  408. lout += "end\n"
  409. if ckey == "EventHandler":
  410. lout += "\n\n"
  411. lout += "function EventHandler:__handleEvent(event)\n"
  412. lout += "\tevt = Event(\"__skip_ptr__\")\n"
  413. lout += "\tevt.__ptr = event\n"
  414. lout += "\tself:handleEvent(evt)\n"
  415. #lout += "\tself:handleEvent(event)\n"
  416. lout += "end\n"
  417. lfout += "require \"%s/%s\"\n" % (prefix, ckey)
  418. mkdir_p(apiClassPath)
  419. fout = open("%s/%s.lua" % (apiClassPath, ckey), "w")
  420. fout.write(lout)
  421. except CppHeaderParser.CppParseError, e:
  422. print e
  423. sys.exit(1)
  424. out += "} // namespace Polycode\n"
  425. sout += "\t\t{NULL, NULL}\n"
  426. sout += "\t};\n"
  427. sout += "\tluaL_openlib(L, \"%s\", %sLib, 0);\n" % (libName, libSmallName)
  428. sout += "\treturn 1;\n"
  429. sout += "}"
  430. shout = ""
  431. shout += "#pragma once\n"
  432. shout += "#include <%s>\n" % (mainInclude)
  433. shout += "extern \"C\" {\n"
  434. shout += "#include <stdio.h>\n"
  435. shout += "#include \"lua.h\"\n"
  436. shout += "#include \"lualib.h\"\n"
  437. shout += "#include \"lauxlib.h\"\n"
  438. shout += "int _PolyExport luaopen_%s(lua_State *L);\n" % (prefix)
  439. shout += "}\n"
  440. mkdir_p(includePath)
  441. mkdir_p(apiPath)
  442. mkdir_p(sourcePath)
  443. fout = open("%s/%sLUA.h" % (includePath, prefix), "w")
  444. fout.write(shout)
  445. fout = open("%s/%s.lua" % (apiPath, prefix), "w")
  446. fout.write(lfout)
  447. fout = open("%s/%sLUAWrappers.h" % (includePath, prefix), "w")
  448. fout.write(out)
  449. fout = open("%s/%sLUA.cpp" % (sourcePath, prefix), "w")
  450. fout.write(sout)
  451. pattern = '*.lua'
  452. os.chdir(apiPath)
  453. if libName == "Polycore":
  454. with ZipFile("api.pak", 'w') as myzip:
  455. for root, dirs, files in os.walk("."):
  456. for filename in fnmatch.filter(files, pattern):
  457. myzip.write(os.path.join(root, filename))
  458. #print cppHeader
  459. 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])