JSBTypeScript.cpp 6.8 KB

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