JSBHaxe.cpp 12 KB

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