JSBTypeScript.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. if (function->docString_.Length())
  50. source_ += " " + function->docString_ + "\n";
  51. source_ += " " + scriptName + "(";
  52. for (unsigned i = 0; i < function->parameters_.Size(); i++)
  53. {
  54. JSBFunctionType* ftype = function->parameters_.At(i);
  55. String scriptType = GetScriptType(ftype);
  56. if (scriptType == "Context")
  57. continue;
  58. source_ += ftype->name_;
  59. if (ftype->initializer_.Length())
  60. source_ += "?";
  61. source_ += ": " + scriptType;
  62. if (i < function->parameters_.Size() - 1)
  63. source_ += ", ";
  64. }
  65. if (function->isConstructor_)
  66. source_ += ");\n";
  67. else
  68. {
  69. if (!function->returnType_)
  70. source_ += "): void;\n";
  71. else
  72. source_ += "): " + GetScriptType(function->returnType_) + ";\n";
  73. }
  74. }
  75. void JSBTypeScript::ExportModuleClasses(const String& moduleName)
  76. {
  77. JSBModule* module = JSBindings::Instance()->GetModuleByName(moduleName);
  78. if (!module->classes_.Size())
  79. return;
  80. source_ += "\n";
  81. for (unsigned i = 0; i < module->classes_.Size(); i++)
  82. {
  83. JSBClass* klass = module->classes_.At(i);
  84. source_ += " export class " + klass->GetName();
  85. if (klass->GetBaseClass())
  86. source_ += " extends " + klass->GetBaseClass()->GetName();
  87. source_ += " {\n\n";
  88. Vector<String> propertyNames;
  89. klass->GetPropertyNames(propertyNames);
  90. for (unsigned j = 0; j < propertyNames.Size(); j++)
  91. {
  92. JSBProperty* prop = klass->GetProperty(propertyNames[j]);
  93. JSBFunctionType* ftype = NULL;
  94. if (prop->getter_ && !prop->getter_->Skip())
  95. {
  96. ftype = prop->getter_->returnType_;
  97. }
  98. else if (prop->setter_ && !prop->setter_->Skip())
  99. ftype = prop->setter_->parameters_[0];
  100. if (!ftype)
  101. continue;
  102. String scriptType = GetScriptType(ftype);
  103. String scriptName = propertyNames[j];
  104. scriptName[0] = tolower(scriptName[0]);
  105. source_ += " " + scriptName + ": " + scriptType + ";\n";
  106. }
  107. if (propertyNames.Size())
  108. source_ += "\n";
  109. JSBFunction* constructor = klass->GetConstructor();
  110. if (constructor)
  111. {
  112. ExportFunction(constructor);
  113. source_ += "\n";
  114. }
  115. for (unsigned j = 0; j < klass->GetFunctionCount(); j++)
  116. {
  117. JSBFunction* func = klass->GetFunction(j);
  118. if (func->isConstructor_ || func->isDestructor_ || func->Skip())
  119. continue;
  120. ExportFunction(func);
  121. }
  122. source_ += "\n }\n\n";
  123. }
  124. source_ += "\n";
  125. }
  126. void JSBTypeScript::ExportModuleConstants(const String& moduleName)
  127. {
  128. JSBModule* module = JSBindings::Instance()->GetModuleByName(moduleName);
  129. if (!module->constants_.Size())
  130. return;
  131. source_ += "\n";
  132. for (unsigned i = 0; i < module->constants_.Size(); i++)
  133. {
  134. const String& cname = module->constants_.At(i);
  135. source_ += " export var " + cname + ": number;\n";
  136. }
  137. source_ += "\n";
  138. }
  139. void JSBTypeScript::ExportModuleEnums(const String& moduleName)
  140. {
  141. JSBModule* module = JSBindings::Instance()->GetModuleByName(moduleName);
  142. for (unsigned i = 0; i < module->enums_.Size(); i++)
  143. {
  144. JSBEnum* _enum = module->enums_.At(i);
  145. source_ += " export enum " + _enum->name_;
  146. source_ += " {\n\n";
  147. for (unsigned j = 0; j < _enum->values_.Size(); j++)
  148. {
  149. source_ += " " + _enum->values_[j];
  150. if (j != _enum->values_.Size() - 1)
  151. source_ += ",\n";
  152. }
  153. source_ += "\n\n }\n\n";
  154. }
  155. }
  156. void JSBTypeScript::WriteToFile(const String &path)
  157. {
  158. File file(JSBind::context_);
  159. file.Open(path, FILE_WRITE);
  160. file.Write(source_.CString(), source_.Length());
  161. file.Close();
  162. }
  163. void JSBTypeScript::Emit(const String& path)
  164. {
  165. Vector<String> modules;
  166. modules.Push("Container");
  167. modules.Push("Core");
  168. modules.Push("Engine");
  169. modules.Push("Resource");
  170. modules.Push("Scene");
  171. modules.Push("UI");
  172. modules.Push("Atomic2D");
  173. modules.Push("Audio");
  174. modules.Push("Graphics");
  175. modules.Push("Input");
  176. modules.Push("IO");
  177. //modules.Push("Math");
  178. modules.Push("Navigation");
  179. modules.Push("Network");
  180. modules.Push("Physics");
  181. modules.Push("Javascript");
  182. Begin();
  183. for (unsigned i = 0; i < modules.Size(); i++)
  184. {
  185. ExportModuleEnums(modules[i]);
  186. }
  187. for (unsigned i = 0; i < modules.Size(); i++)
  188. {
  189. ExportModuleConstants(modules[i]);
  190. }
  191. for (unsigned i = 0; i < modules.Size(); i++)
  192. {
  193. source_ += "\n//----------------------------------------------------\n";
  194. source_ += "// MODULE: " + modules[i] + "\n";
  195. source_ += "//----------------------------------------------------\n\n";
  196. ExportModuleClasses(modules[i]);
  197. }
  198. End();
  199. WriteToFile(path);
  200. }