create_lua_library.py 20 KB

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