create_lua_library.py 20 KB

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