create_lua_library.py 20 KB

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