JSBTypeScript.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 "JSBindings.h"
  7. #include "JSBModule.h"
  8. #include "JSBFunction.h"
  9. #include "JSBTypeScript.h"
  10. static String GetScriptType(JSBFunctionType* ftype)
  11. {
  12. String scriptType = "number";
  13. if (ftype->type_->asPrimitiveType())
  14. {
  15. JSBPrimitiveType* ptype = ftype->type_->asPrimitiveType();
  16. if (ptype->kind_ == JSBPrimitiveType::Bool)
  17. scriptType = "boolean";
  18. }
  19. if (ftype->type_->asStringHashType() || ftype->type_->asStringType())
  20. scriptType = "string";
  21. if (ftype->type_->asEnumType())
  22. scriptType = ftype->type_->asEnumType()->enum_->name_;
  23. if (ftype->type_->asEnumType())
  24. scriptType = ftype->type_->asEnumType()->enum_->name_;
  25. if (ftype->type_->asClassType())
  26. scriptType = ftype->type_->asClassType()->class_->GetName();
  27. return scriptType;
  28. }
  29. void JSBTypeScript::Begin()
  30. {
  31. source_ += "//Atomic TypeScript Definitions\n\n\n";
  32. source_ += "declare module Atomic {\n\n";
  33. }
  34. void JSBTypeScript::End()
  35. {
  36. source_ += "\n}\n";
  37. }
  38. void JSBTypeScript::ExportFunction(JSBFunction* function)
  39. {
  40. if (function->Skip())
  41. return;
  42. String scriptName = "constructor";
  43. //constructor(width?: number, height?: number, options?: PixiRendererOptions);
  44. if (!function->isConstructor_)
  45. {
  46. scriptName = function->name_;
  47. scriptName[0] = tolower(scriptName[0]);
  48. }
  49. source_ += " " + scriptName + "(";
  50. for (unsigned i = 0; i < function->parameters_.Size(); i++)
  51. {
  52. JSBFunctionType* ftype = function->parameters_.At(i);
  53. String scriptType = GetScriptType(ftype);
  54. if (scriptType == "Context")
  55. continue;
  56. source_ += ftype->name_;
  57. if (ftype->initializer_.Length())
  58. source_ += "?";
  59. source_ += ": " + scriptType;
  60. if (i < function->parameters_.Size() - 1)
  61. source_ += ", ";
  62. }
  63. if (function->isConstructor_)
  64. source_ += ");\n";
  65. else
  66. {
  67. if (!function->returnType_)
  68. source_ += "): void;\n";
  69. else
  70. source_ += "): " + GetScriptType(function->returnType_) + ";\n";
  71. }
  72. }
  73. void JSBTypeScript::ExportModuleClasses(const String& moduleName)
  74. {
  75. JSBModule* module = JSBindings::Instance()->GetModuleByName(moduleName);
  76. if (!module->classes_.Size())
  77. return;
  78. source_ += "\n";
  79. for (unsigned i = 0; i < module->classes_.Size(); i++)
  80. {
  81. JSBClass* klass = module->classes_.At(i);
  82. source_ += " export class " + klass->GetName();
  83. if (klass->GetBaseClass())
  84. source_ += " extends " + klass->GetBaseClass()->GetName();
  85. source_ += " {\n\n";
  86. Vector<String> propertyNames;
  87. klass->GetPropertyNames(propertyNames);
  88. for (unsigned j = 0; j < propertyNames.Size(); j++)
  89. {
  90. JSBProperty* prop = klass->GetProperty(propertyNames[j]);
  91. JSBFunctionType* ftype = NULL;
  92. if (prop->getter_ && !prop->getter_->Skip())
  93. {
  94. ftype = prop->getter_->returnType_;
  95. }
  96. else if (prop->setter_ && !prop->setter_->Skip())
  97. ftype = prop->setter_->parameters_[0];
  98. if (!ftype)
  99. continue;
  100. String scriptType = GetScriptType(ftype);
  101. String scriptName = propertyNames[j];
  102. scriptName[0] = tolower(scriptName[0]);
  103. source_ += " " + scriptName + ": " + scriptType + ";\n";
  104. }
  105. if (propertyNames.Size())
  106. source_ += "\n";
  107. JSBFunction* constructor = klass->GetConstructor();
  108. if (constructor)
  109. {
  110. ExportFunction(constructor);
  111. source_ += "\n";
  112. }
  113. for (unsigned j = 0; j < klass->GetFunctionCount(); j++)
  114. {
  115. JSBFunction* func = klass->GetFunction(j);
  116. if (func->isConstructor_ || func->isDestructor_ || func->Skip())
  117. continue;
  118. ExportFunction(func);
  119. }
  120. source_ += "\n }\n\n";
  121. }
  122. source_ += "\n";
  123. }
  124. void JSBTypeScript::ExportModuleConstants(const String& moduleName)
  125. {
  126. JSBModule* module = JSBindings::Instance()->GetModuleByName(moduleName);
  127. if (!module->constants_.Size())
  128. return;
  129. source_ += "\n";
  130. for (unsigned i = 0; i < module->constants_.Size(); i++)
  131. {
  132. const String& cname = module->constants_.At(i);
  133. source_ += " export var " + cname + ": number;\n";
  134. }
  135. source_ += "\n";
  136. }
  137. void JSBTypeScript::ExportModuleEnums(const String& moduleName)
  138. {
  139. JSBModule* module = JSBindings::Instance()->GetModuleByName(moduleName);
  140. for (unsigned i = 0; i < module->enums_.Size(); i++)
  141. {
  142. JSBEnum* _enum = module->enums_.At(i);
  143. source_ += " export enum " + _enum->name_;
  144. source_ += " {\n\n";
  145. for (unsigned j = 0; j < _enum->values_.Size(); j++)
  146. {
  147. source_ += " " + _enum->values_[j];
  148. if (j != _enum->values_.Size() - 1)
  149. source_ += ",\n";
  150. }
  151. source_ += "\n\n }\n\n";
  152. }
  153. }
  154. void JSBTypeScript::WriteToFile(const String &path)
  155. {
  156. File file(JSBind::context_);
  157. file.Open(path, FILE_WRITE);
  158. file.Write(source_.CString(), source_.Length());
  159. file.Close();
  160. }
  161. void JSBTypeScript::Emit(const String& path)
  162. {
  163. Vector<String> modules;
  164. modules.Push("Container");
  165. modules.Push("Core");
  166. modules.Push("Engine");
  167. modules.Push("Resource");
  168. modules.Push("Scene");
  169. modules.Push("UI");
  170. modules.Push("Atomic2D");
  171. modules.Push("Audio");
  172. modules.Push("Graphics");
  173. modules.Push("Input");
  174. modules.Push("IO");
  175. //modules.Push("Math");
  176. modules.Push("Navigation");
  177. modules.Push("Network");
  178. modules.Push("Physics");
  179. modules.Push("Javascript");
  180. Begin();
  181. for (unsigned i = 0; i < modules.Size(); i++)
  182. {
  183. ExportModuleEnums(modules[i]);
  184. }
  185. for (unsigned i = 0; i < modules.Size(); i++)
  186. {
  187. ExportModuleConstants(modules[i]);
  188. }
  189. for (unsigned i = 0; i < modules.Size(); i++)
  190. {
  191. source_ += "\n//----------------------------------------------------\n";
  192. source_ += "// MODULE: " + modules[i] + "\n";
  193. source_ += "//----------------------------------------------------\n\n";
  194. ExportModuleClasses(modules[i]);
  195. }
  196. End();
  197. WriteToFile(path);
  198. }