JSBHaxe.cpp 12 KB

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