JSBHaxe.cpp 12 KB

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