JSBHaxe.cpp 12 KB

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