JSBHaxe.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Atomic.h>
  23. #include <Atomic/IO/Log.h>
  24. #include <Atomic/Core/ProcessUtils.h>
  25. #include <Atomic/Resource/ResourceCache.h>
  26. #include "JSBind.h"
  27. #include "JSBPackage.h"
  28. #include "JSBModule.h"
  29. #include "JSBFunction.h"
  30. #include "JSBHaxe.h"
  31. namespace ToolCore
  32. {
  33. String JSBHaxe::GetScriptType(JSBFunctionType* ftype)
  34. {
  35. String scriptType = "Dynamic";
  36. if (ftype->type_->asPrimitiveType())
  37. {
  38. JSBPrimitiveType* ptype = ftype->type_->asPrimitiveType();
  39. scriptType = GetPrimitiveType(ptype);
  40. return scriptType;
  41. }
  42. if (ftype->type_->asStringHashType() || ftype->type_->asStringType())
  43. scriptType = "String";
  44. if (ftype->type_->asEnumType())
  45. scriptType = ftype->type_->asEnumType()->enum_->GetName();
  46. if (ftype->type_->asClassType())
  47. {
  48. JSBClass* klass = ftype->type_->asClassType()->class_;
  49. scriptType = klass->GetName();
  50. if (klass->GetPackage()->GetName() != package_->GetName())
  51. {
  52. scriptType = klass->GetPackage()->GetName() + "." + klass->GetName();
  53. }
  54. }
  55. if (ftype->type_->asVectorType())
  56. {
  57. JSBVectorType* vectorType = ftype->type_->asVectorType();
  58. scriptType = "Array<String>";
  59. }
  60. return scriptType;
  61. }
  62. String JSBHaxe::GetPrimitiveType(JSBPrimitiveType* ptype)
  63. {
  64. if (ptype->kind_ == JSBPrimitiveType::Bool)
  65. return "Bool";
  66. if (ptype->kind_ == JSBPrimitiveType::Int && ptype->isUnsigned_)
  67. return "UInt";
  68. else if (ptype->kind_ == JSBPrimitiveType::Int)
  69. return "Int";
  70. if (ptype->kind_ == JSBPrimitiveType::Float)
  71. return "Float";
  72. return "Dynamic";
  73. }
  74. void JSBHaxe::Begin()
  75. {
  76. source_ += "//Atomic Haxe Definitions\n\n";
  77. source_ += "extern class " + package_->GetName() + " {\n\n";
  78. if (package_->GetName() == "Atomic") {
  79. //hand written data
  80. source_ += " public static var engine : Engine;\n";
  81. source_ += " public static var graphics: Graphics;\n";
  82. source_ += " public static var renderer: Renderer;\n";
  83. source_ += " public static var cache: ResourceCache;\n";
  84. source_ += " public static var input: Input;\n";
  85. source_ += " public static var fileSystem: FileSystem;\n";
  86. source_ += " public static var network: Network;\n";
  87. source_ += " public static var ui: UI;\n";
  88. source_ += " public static var audio: Audio;\n";
  89. }
  90. }
  91. void JSBHaxe::End()
  92. {
  93. source_ += "\n}\n";
  94. }
  95. bool JSBHaxe::IsOverride(JSBFunction* function) {
  96. return findFunctionInBase(function) != NULL;
  97. }
  98. JSBFunction* JSBHaxe::findFunctionInBase(JSBFunction* function) {
  99. PODVector<JSBClass*>& base = function->GetClass()->GetBaseClasses();
  100. for (unsigned j = 0; j < base.Size(); j++)
  101. {
  102. JSBClass* klass = base[j];
  103. PODVector<JSBFunction*>& functions = klass->GetFunctions();
  104. for (unsigned j = 0; j < functions.Size(); j++)
  105. {
  106. JSBFunction* func = functions[j];
  107. if (func->IsConstructor() || func->IsDestructor() )
  108. continue;
  109. if (func->GetName() == function->GetName()) {
  110. return func;
  111. }
  112. }
  113. }
  114. return NULL;
  115. }
  116. void JSBHaxe::ExportFunction(JSBFunction* function)
  117. {
  118. //probably no need to check
  119. if (function->Skip())
  120. return;
  121. String scriptName = "new";
  122. if (!function->IsConstructor())
  123. {
  124. scriptName = function->GetName();
  125. scriptName[0] = tolower(scriptName[0]);
  126. }
  127. if (function->GetDocString().Length())
  128. source_ += " //" + function->GetDocString() + "\n";
  129. //Add @:overload annotation
  130. if (IsOverride(function))
  131. {
  132. //if (function->IsOverload())
  133. {
  134. source_ += " @:overload(function(";
  135. Vector<JSBFunctionType*>& parameters = function->GetParameters();
  136. for (unsigned i = 0; i < parameters.Size(); i++)
  137. {
  138. JSBFunctionType* ftype = parameters.At(i);
  139. String scriptType = GetScriptType(ftype);
  140. if (scriptType == "Context" || scriptType == "Atomic.Context")
  141. continue;
  142. String name = ftype->name_;
  143. if (ftype->initializer_.Length())
  144. source_ += "?";
  145. source_ += name;
  146. source_ += ": " + scriptType;
  147. if (i < parameters.Size() - 1)
  148. source_ += ", ";
  149. }
  150. if (!function->GetReturnType())
  151. source_ += "): Void{})\n";
  152. else
  153. source_ += "): " + GetScriptType(function->GetReturnType()) + "{})\n";
  154. }
  155. source_ += " override function ";
  156. }
  157. else
  158. {
  159. source_ += " function ";
  160. }
  161. source_ += scriptName + "(";
  162. Vector<JSBFunctionType*>& parameters = function->GetParameters();
  163. if (IsOverride(function))
  164. parameters = findFunctionInBase(function)->GetParameters();
  165. for (unsigned i = 0; i < parameters.Size(); i++)
  166. {
  167. JSBFunctionType* ftype = parameters.At(i);
  168. String scriptType = GetScriptType(ftype);
  169. if (scriptType == "Context" || scriptType == "Atomic.Context")
  170. continue;
  171. String name = ftype->name_;
  172. if (ftype->initializer_.Length())
  173. source_ += "?";
  174. source_ += name;
  175. source_ += ": " + scriptType;
  176. if (i < parameters.Size() - 1)
  177. source_ += ", ";
  178. }
  179. if (function->IsConstructor())
  180. source_ += ");\n";
  181. else
  182. {
  183. if (!function->GetReturnType())
  184. source_ += "): Void;\n";
  185. else
  186. source_ += "): " + GetScriptType(function->GetReturnType()) + ";\n";
  187. }
  188. }
  189. bool JSBHaxe::checkV(JSBClass* clazz, const String name, const String type) {
  190. PODVector<JSBClass*>& base = clazz->GetBaseClasses();
  191. for (unsigned j = 0; j < base.Size(); j++)
  192. {
  193. JSBClass* baseClass = base[j];
  194. Vector<String> propertyNames;
  195. baseClass->GetPropertyNames(propertyNames);
  196. for (unsigned i = 0; i < propertyNames.Size(); i++)
  197. {
  198. JSBProperty* prop = baseClass->GetProperty(propertyNames[i]);
  199. JSBFunctionType* ftype = NULL;
  200. if (prop->getter_ && !prop->getter_->Skip())
  201. {
  202. ftype = prop->getter_->GetReturnType();
  203. }
  204. else if (prop->setter_ && !prop->setter_->Skip())
  205. ftype = prop->setter_->GetParameters()[0];
  206. if (!ftype)
  207. continue;
  208. String scriptName = prop->GetCasePropertyName();
  209. if (scriptName == name)
  210. {
  211. return true;
  212. }
  213. }
  214. }
  215. return false;
  216. }
  217. void JSBHaxe::ExportModuleClasses(JSBModule* module)
  218. {
  219. Vector<SharedPtr<JSBClass>> classes = module->GetClasses();
  220. if (!classes.Size())
  221. return;
  222. source_ += "\n";
  223. for (unsigned i = 0; i < classes.Size(); i++)
  224. {
  225. JSBClass* klass = classes.At(i);
  226. if (klass->IsNumberArray()) {
  227. source_ += "typedef " + klass->GetName() + " = Array<Float>;\n";
  228. continue;
  229. }
  230. source_ += "@:native(\"Atomic." + klass->GetName() + "\")\n";
  231. source_ += "extern class " + klass->GetName();
  232. JSBClass* base = klass->GetBaseClass();
  233. if (base)
  234. {
  235. source_ += " extends " + base->GetName();
  236. }
  237. source_ += " {\n\n";
  238. Vector<String> propertyNames;
  239. klass->GetPropertyNames(propertyNames);
  240. for (unsigned j = 0; j < propertyNames.Size(); j++)
  241. {
  242. JSBProperty* prop = klass->GetProperty(propertyNames[j]);
  243. JSBFunctionType* ftype = NULL;
  244. if (prop->getter_ && !prop->getter_->Skip())
  245. {
  246. ftype = prop->getter_->GetReturnType();
  247. }
  248. else if (prop->setter_ && !prop->setter_->Skip())
  249. ftype = prop->setter_->GetParameters()[0];
  250. if (!ftype)
  251. continue;
  252. String scriptType = GetScriptType(ftype);
  253. String scriptName = prop->GetCasePropertyName();
  254. if (!checkV(klass, scriptName, scriptType)) {
  255. //rename haxe reserved words
  256. if (scriptName == "override") {
  257. scriptName = "overide";
  258. }
  259. if (scriptName == "dynamic") {
  260. scriptName = "dynamik";
  261. }
  262. source_ += " var " + scriptName + ": " + scriptType + ";\n";
  263. }
  264. }
  265. if (propertyNames.Size())
  266. source_ += "\n";
  267. JSBFunction* constructor = klass->GetConstructor(BINDINGLANGUAGE_JAVASCRIPT);
  268. if (constructor)
  269. {
  270. ExportFunction(constructor);
  271. source_ += "\n";
  272. }
  273. PODVector<JSBFunction*>& functions = klass->GetFunctions();
  274. for (unsigned j = 0; j < functions.Size(); j++)
  275. {
  276. JSBFunction* func = functions[j];
  277. if (func->IsConstructor() || func->IsDestructor() || func->Skip())
  278. continue;
  279. ExportFunction(func);
  280. }
  281. for (unsigned j = 0; j < klass->GetNumHaxeDecl(); j++)
  282. {
  283. source_ += " " + klass->GetHaxeDecl(j) + "\n";
  284. }
  285. source_ += "\n}\n\n";
  286. }
  287. source_ += "\n";
  288. }
  289. void JSBHaxe::ExportModuleConstants(JSBModule* module)
  290. {
  291. HashMap<String, JSBModule::Constant>& constants = module->GetConstants();
  292. const Vector<String>& constantsName = constants.Keys();
  293. if (!constants.Size())
  294. return;
  295. source_ += "\n";
  296. for (unsigned i = 0; i < constantsName.Size(); i++)
  297. {
  298. const String& cname = constantsName.At(i);
  299. JSBModule::Constant& constant = constants[cname];
  300. source_ += " public static var " + cname + ": " + GetPrimitiveType(constant.type) + ";\n";
  301. }
  302. source_ += "\n";
  303. }
  304. void JSBHaxe::ExportEnums(JSBModule* module)
  305. {
  306. Vector<SharedPtr<JSBEnum>> enums = module->GetEnums();
  307. for (unsigned i = 0; i <enums.Size(); i++)
  308. {
  309. JSBEnum* _enum = enums[i];
  310. source_ += "@:native(\"Atomic\")\n";
  311. source_ += "extern enum " + _enum->GetName() + " {\n";
  312. HashMap<String, String>& values = _enum->GetValues();
  313. HashMap<String, String>::ConstIterator itr = values.Begin();
  314. while (itr != values.End())
  315. {
  316. String name = (*itr).first_;
  317. source_ += " " + name + ";\n";
  318. itr++;
  319. }
  320. source_ += "}\n";
  321. }
  322. }
  323. void JSBHaxe::WriteToFile(const String &path)
  324. {
  325. File file(package_->GetContext());
  326. file.Open(path, FILE_WRITE);
  327. file.Write(source_.CString(), source_.Length());
  328. file.Close();
  329. }
  330. void JSBHaxe::Emit(JSBPackage* package, const String& path)
  331. {
  332. package_ = package;
  333. Vector<SharedPtr<JSBModule>>& modules = package->GetModules();
  334. source_ += "package atomic;\n\n";
  335. for (unsigned i = 0; i < modules.Size(); i++)
  336. {
  337. ExportEnums(modules[i]);
  338. }
  339. Begin();
  340. for (unsigned i = 0; i < modules.Size(); i++)
  341. {
  342. ExportModuleConstants(modules[i]);
  343. }
  344. End();
  345. for (unsigned i = 0; i < modules.Size(); i++)
  346. {
  347. source_ += "\n//----------------------------------------------------\n";
  348. source_ += "// MODULE: " + modules[i]->GetName() + "\n";
  349. source_ += "//----------------------------------------------------\n\n";
  350. ExportModuleClasses(modules[i]);
  351. }
  352. //End();
  353. WriteToFile(path);
  354. }
  355. }