JSBHaxe.cpp 12 KB

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