JSBindingsGenerator.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import ConfigParser
  2. import io
  3. import os
  4. import re
  5. from zipfile import *
  6. import fnmatch
  7. import errno
  8. def mkdir_p(path):
  9. try:
  10. os.makedirs(path)
  11. except OSError as e:
  12. if e.errno == errno.EEXIST:
  13. pass
  14. else: raise
  15. class JSBindingsGenerator(object):
  16. def __init__(self, config):
  17. self.config = config
  18. self.libName = self.config.get('global', 'LibraryName')
  19. self.jsClassOut = ""
  20. self.jsIndexOut = ""
  21. self.wrappersHeaderList = ""
  22. self.wrappersHeaderBody = ""
  23. self.cppRegisterOut = ""
  24. self.cppRegisterOut += "int jsopen_%s(duk_context *ctx) {\n" % (self.libName)
  25. self.cppRegisterOut += "\tconst duk_function_list_entry %s_funcs[] = {\n" % (self.libName)
  26. mkdir_p("%s/%s" % (self.config.get('js', 'JSApiDirectory'), self.libName))
  27. def processTargetFile(self, targetFile):
  28. self.wrappersHeaderList += "#include \"%s/%s\"\n" % (self.config.get('global', 'HeaderIncludeDirectory'), targetFile)
  29. def processClass(self, c):
  30. self.jsClassOut += "function %s() {\n" % c["name"]
  31. self.makeJSProperties(c)
  32. self.jsClassOut += "}\n"
  33. self.makeJSPropAccessors(c)
  34. self.makeJSMethods(c)
  35. self.writeClass(c)
  36. self.jsIndexOut += "require('%s/%s')\n" % (self.libName, c["name"])
  37. def makeJSPropAccessors(self, c):
  38. if len(c["properties"]) > 0:
  39. for pp in c["properties"]:
  40. self.jsClassOut += "%s.prototype.__get_%s = function() {\n" % (c["name"], pp["name"])
  41. self.cppRegisterOut += "\t\t\t{\"%s__get_%s\", %s_%s__get_%s, 1},\n" % (c["name"], pp["name"], self.libName, c["name"], pp["name"])
  42. self.wrappersHeaderBody += "\tduk_ret_t %s_%s__get_%s(duk_context *context) {\n" % (self.libName, c["name"], pp["name"])
  43. self.wrappersHeaderBody += "\t\t%s *inst = (%s*)duk_to_pointer(context, 0);\n" % (c["name"], c["name"])
  44. outfunc = "this_shouldnt_happen"
  45. retFunc = ""
  46. if pp["type"] == "Number":
  47. outfunc = "duk_push_number"
  48. if pp["type"] == "String":
  49. outfunc = "duk_push_string"
  50. retFunc = ".c_str()"
  51. if pp["type"] == "int" or pp["type"] == "PolyKEY":
  52. outfunc = "duk_push_int"
  53. if pp["type"] == "bool":
  54. outfunc = "duk_push_boolean"
  55. if pp["type"].find("*") > -1: # Returned var is definitely a pointer.
  56. self.wrappersHeaderBody += "\t\tPolyBase *ptrRetVal = (PolyBase*)inst->%s%s;\n" % (pp["name"], retFunc)
  57. self.wrappersHeaderBody += "\t\tduk_push_pointer(context, (void*)ptrRetVal);\n"
  58. self.jsClassOut += "\tvar retVal = new %s()\n\tretVal.__ptr = " % (pp["type"].replace("*", ""))
  59. self.jsClassOut += "\t%s.%s__get_%s(this.__ptr)\n" % (self.libName, c["name"], pp["name"])
  60. self.jsClassOut += "\treturn retVal\n"
  61. self.jsClassOut += "}\n\n"
  62. elif self.isBasicType(pp["type"]) == True:
  63. self.wrappersHeaderBody += "\t\t%s(context, inst->%s%s);\n" % (outfunc, pp["name"], retFunc);
  64. self.jsClassOut += "\treturn %s.%s__get_%s(this.__ptr)\n" % (self.libName, c["name"], pp["name"])
  65. self.jsClassOut += "}\n\n"
  66. else:
  67. className = pp["type"].replace("const", "").replace("&", "").replace("inline", "").replace("virtual", "").replace("static", "")
  68. if className == "Polygon": # Deal with potential windows.h conflict
  69. className = "Polycode::Polygon"
  70. if className == "Rectangle":
  71. className = "Polycode::Rectangle"
  72. if className == "Polycode : : Rectangle":
  73. className = "Polycode::Rectangle"
  74. self.wrappersHeaderBody += "\t\t%s *retInst = new %s();\n" % (className, className)
  75. self.wrappersHeaderBody += "\t\t*retInst = inst->%s%s;\n" % (pp["name"], retFunc)
  76. self.wrappersHeaderBody += "\t\tduk_push_pointer(context, (void*)retInst);\n"
  77. self.jsClassOut += "\tvar retVal = new %s()\n\tretVal.__ptr = " % (pp["type"].replace("*", ""))
  78. self.jsClassOut += "\t%s.%s__get_%s(this.__ptr)\n" % (self.libName, c["name"], pp["name"])
  79. self.jsClassOut += "\treturn retVal\n"
  80. self.jsClassOut += "}\n\n"
  81. self.wrappersHeaderBody += "\t\treturn 1;\n"
  82. self.wrappersHeaderBody += "\t}\n\n"
  83. self.jsClassOut += "%s.prototype.__set_%s = function(val) {\n" % (c["name"], pp["name"])
  84. if self.isBasicType(pp["type"]):
  85. self.jsClassOut += "\t%s.%s__set_%s(this.__ptr, val)\n" % (self.libName, c["name"], pp["name"])
  86. else:
  87. self.jsClassOut += "\t%s.%s__set_%s(this.__ptr, val.__ptr)\n" % (self.libName, c["name"], pp["name"])
  88. self.jsClassOut += "}\n\n"
  89. self.cppRegisterOut += "\t\t\t{\"%s__set_%s\", %s_%s__set_%s, 2},\n" % (c["name"], pp["name"], self.libName, c["name"], pp["name"])
  90. self.wrappersHeaderBody += "\tduk_ret_t %s_%s__set_%s(duk_context *context) {\n" % (self.libName, c["name"], pp["name"])
  91. self.wrappersHeaderBody += "\t\t%s *inst = (%s*)duk_to_pointer(context, 0);\n" % (c["name"], c["name"])
  92. outfunc = "this_shouldnt_happen"
  93. if "*" in pp["type"]:
  94. outfunc = "(%s)duk_to_pointer" % (pp["type"])
  95. else:
  96. outfunc = "*(%s*)duk_to_pointer" % (pp["type"])
  97. if pp["type"] == "int":
  98. outfunc = "duk_to_int"
  99. if pp["type"] == "PolyKEY":
  100. outfunc = "(PolyKEY)duk_to_int"
  101. if pp["type"] == "Number":
  102. outfunc = "duk_to_number"
  103. if pp["type"] == "bool":
  104. outfunc = "duk_to_boolean"
  105. if pp["type"] == "String":
  106. outfunc = "duk_to_string"
  107. self.wrappersHeaderBody += "\t\tinst->%s = %s(context, 1);\n" % (pp["name"], outfunc)
  108. self.wrappersHeaderBody += "\t\treturn 0;\n"
  109. self.wrappersHeaderBody += "\t}\n\n"
  110. def makeJSProperties(self, c):
  111. if len(c["properties"]) > 0:
  112. self.jsClassOut += "\tObject.defineProperties(this, {\n"
  113. idx = 0
  114. for pp in c["properties"]:
  115. self.jsClassOut += "\t\t'%s': { enumerable: true, configurable: true, get: %s.prototype.__get_%s, set: %s.prototype.__set_%s}" % (pp["name"], c["name"], pp["name"], c["name"], pp["name"])
  116. if idx < len(c["properties"])-1:
  117. self.jsClassOut += ","
  118. self.jsClassOut += "\n"
  119. idx += 1
  120. self.jsClassOut += "\t})\n"
  121. def makeJSMethods(self, c):
  122. for method in c["methods"]:
  123. if method["isStatic"] == False and method["name"] != c["name"]:
  124. self.jsClassOut += "\n%s.prototype.%s = function(" % (c["name"], method["name"])
  125. params = ",".join(str(p["name"]) for p in method["parameters"])
  126. self.jsClassOut += params
  127. self.jsClassOut += ") {\n"
  128. self.cppRegisterOut += "\t\t\t{\"%s_%s\", %s_%s_%s, %d},\n" % (c["name"], method["name"], self.libName, c["name"], method["name"], len(method["parameters"]) + 1)
  129. self.wrappersHeaderBody += "\tduk_ret_t %s_%s_%s(duk_context *context) {\n" % (self.libName, c["name"], method["name"])
  130. self.wrappersHeaderBody += "\t\t%s *inst = (%s*)duk_to_pointer(context, 0);\n" % (c["name"], c["name"])
  131. idx = 1
  132. for p in method["parameters"]:
  133. if "*" in p["type"]:
  134. outfunc = "(%s)duk_to_pointer" % (p["type"])
  135. outtype = "%s" % p["type"]
  136. else:
  137. outfunc = "*(%s*)duk_to_pointer" % (p["type"])
  138. outtype = "%s" % p["type"]
  139. if p["type"] == "int":
  140. outfunc = "duk_to_int"
  141. outtype = "int"
  142. if p["type"] == "PolyKEY":
  143. outfunc = "(PolyKEY)duk_to_int"
  144. outtype = "PolyKEY"
  145. if p["type"] == "Number":
  146. outfunc = "duk_to_number"
  147. outtype = "Number"
  148. if p["type"] == "bool":
  149. outfunc = "duk_to_boolean"
  150. outtype = "bool"
  151. if p["type"] == "String":
  152. outfunc = "duk_to_string"
  153. outtype = "String"
  154. self.wrappersHeaderBody += "\t\t%s %s = %s(context, %d);\n" % (outtype, p["name"], outfunc, idx)
  155. idx += 1
  156. jsRet = ""
  157. finalOut = ""
  158. if self.isVectorType(method["type"]):
  159. self.wrappersHeaderBody += "\t\treturn 0;\n"
  160. elif method["type"] == "void" or method["type"] == "static void" or method["type"] == "virtual void" or method["type"] == "inline void" or method["type"] == "void*":
  161. self.wrappersHeaderBody += "\t\tinst->%s(%s);\n" % (method["name"], params)
  162. self.wrappersHeaderBody += "\t\treturn 0;\n"
  163. else:
  164. outfunc = "this_shouldnt_happen"
  165. retFunc = ""
  166. if method["type"] == "Number" or method["type"] == "inline Number":
  167. outfunc = "duk_push_number"
  168. if method["type"] == "String" or method["type"] == "static String": # TODO: Path for STL strings?
  169. outfunc = "duk_push_string"
  170. retFunc = ".c_str()"
  171. if method["type"] == "int" or method["type"] == "unsigned int" or method["type"] == "static int" or method["type"] == "size_t" or method["type"] == "static size_t" or method["type"] == "long" or method["type"] == "unsigned int" or method["type"] == "static long" or method["type"] == "short" or method["type"] == "PolyKEY" or method["type"] == "wchar_t":
  172. outfunc = "duk_push_int"
  173. if method["type"] == "bool" or method["type"] == "static bool" or method["type"] == "virtual bool":
  174. outfunc = "duk_push_boolean"
  175. if method["type"].find("*") > -1: # Returned var is definitely a pointer.
  176. self.wrappersHeaderBody += "\t\tPolyBase *ptrRetVal = (PolyBase*)inst->%s(%s)%s;\n" % (method["name"], params, retFunc)
  177. self.wrappersHeaderBody += "\t\tduk_push_pointer(context, (void*)ptrRetVal);\n"
  178. jsRet = "var retVal = new %s()\n\tretVal.__ptr = " % (method["type"].replace("*", ""))
  179. finalOut = "\treturn retVal\n"
  180. elif self.isBasicType(method["type"]) == True:
  181. self.wrappersHeaderBody += "\t\t%s(context, inst->%s(%s)%s);\n" % (outfunc, method["name"], params, retFunc);
  182. jsRet = "return "
  183. else:
  184. className = method["type"].replace("const", "").replace("&", "").replace("inline", "").replace("virtual", "").replace("static", "")
  185. if className == "Polygon": # Deal with potential windows.h conflict
  186. className = "Polycode::Polygon"
  187. if className == "Rectangle":
  188. className = "Polycode::Rectangle"
  189. if className == "Polycode : : Rectangle":
  190. className = "Polycode::Rectangle"
  191. self.wrappersHeaderBody += "\t\t%s *retInst = new %s();\n" % (className, className)
  192. self.wrappersHeaderBody += "\t\t*retInst = inst->%s(%s)%s;\n" % (method["name"], params, retFunc)
  193. self.wrappersHeaderBody += "\t\tduk_push_pointer(context, (void*)retInst);\n"
  194. jsRet = "var retVal = new %s()\n\tretVal.__ptr = " % (method["type"].replace("*", ""))
  195. finalOut = "\treturn retVal\n"
  196. self.wrappersHeaderBody += "\t\treturn 1;\n"
  197. self.wrappersHeaderBody += "\t}\n\n"
  198. if len(params) > 0:
  199. self.jsClassOut += "\t%s%s.%s_%s(this.__ptr, %s)\n" % (jsRet, self.libName, c["name"], method["name"], params)
  200. else:
  201. self.jsClassOut += "\t%s%s.%s_%s(this.__ptr)\n" % (jsRet, self.libName, c["name"], method["name"])
  202. self.jsClassOut += finalOut
  203. self.jsClassOut += "}\n"
  204. def isBasicType(self, t):
  205. basicTypes = ["int", "Number", "String", "PolyKEY", "bool"]
  206. if t in basicTypes:
  207. return True
  208. else:
  209. return False
  210. def isVectorType(self, t):
  211. if t.find("<") > -1 and t.find("vector") > -1:
  212. return True
  213. else:
  214. return False
  215. def finalize(self):
  216. fout = open("%s/%s.js" % (self.config.get('js', 'JSApiDirectory'), self.libName), "w")
  217. fout.write(self.jsIndexOut)
  218. self.jsClassOut = ""
  219. with open(self.config.get('js', 'WrapperHeaderTemplate'), 'r') as f:
  220. out = f.read().replace("%HEADERS%", self.wrappersHeaderList)
  221. out = out.replace("%BODY%", self.wrappersHeaderBody)
  222. fout = open(self.config.get('js', 'WrapperHeaderTarget'), "w")
  223. fout.write(out)
  224. fout.close()
  225. with open(self.config.get('js', 'WrapperMainHeaderTemplate'), 'r') as f:
  226. out = f.read().replace("%BODY%", "int _PolyExport jsopen_%s(duk_context *ctx);" % self.libName)
  227. fout = open(self.config.get('js', 'WrapperMainHeaderTarget'), "w")
  228. fout.write(out)
  229. fout.close()
  230. self.cppRegisterOut += "\t\t\t{NULL, NULL, 0}\n"
  231. self.cppRegisterOut += "\t};\n"
  232. self.cppRegisterOut += "\tduk_push_global_object(ctx);\n"
  233. self.cppRegisterOut += "\tduk_push_object(ctx);\n"
  234. self.cppRegisterOut += "\tduk_put_function_list(ctx, -1, %s_funcs);\n" % (self.libName)
  235. self.cppRegisterOut += "\tduk_put_prop_string(ctx, -2, \"%s\");\n" % (self.libName)
  236. self.cppRegisterOut += "\tduk_pop(ctx);\n"
  237. self.cppRegisterOut += "\treturn 1;\n"
  238. self.cppRegisterOut += "}\n"
  239. with open(self.config.get('js', 'WrapperSourceTemplate'), 'r') as f:
  240. out = f.read().replace("%BODY%", self.cppRegisterOut)
  241. fout = open(self.config.get('js', 'WrapperSourceTarget'), "w")
  242. fout.write(out)
  243. fout.close()
  244. curDir = os.path.dirname(os.path.realpath(__file__))
  245. pattern = '*.js'
  246. os.chdir(self.config.get('js', 'JSApiDirectory'))
  247. with ZipFile("js_%s.pak" % (self.libName), 'w') as myzip:
  248. for root, dirs, files in os.walk("."):
  249. for filename in fnmatch.filter(files, pattern):
  250. myzip.write(os.path.join(root, filename))
  251. os.chdir(curDir)
  252. def writeClass(self, c):
  253. fout = open("%s/%s/%s.js" % (self.config.get('js', 'JSApiDirectory'), self.libName, c["name"]), "w")
  254. fout.write(self.jsClassOut)
  255. self.jsClassOut = ""