create_lua_library.py 20 KB

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