JSBHaxe.cpp 13 KB

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