JSBDoc.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/Atomic.h>
  8. #include <Atomic/IO/Log.h>
  9. #include <Atomic/IO/FileSystem.h>
  10. #include <Atomic/Core/ProcessUtils.h>
  11. #include <Atomic/Resource/ResourceCache.h>
  12. #include "JSBind.h"
  13. #include "JSBPackage.h"
  14. #include "JSBModule.h"
  15. #include "JSBFunction.h"
  16. #include "JSBDoc.h"
  17. namespace ToolCore
  18. {
  19. static String GetScriptType(JSBFunctionType* ftype)
  20. {
  21. String scriptType = "number";
  22. if (ftype->type_->asPrimitiveType())
  23. {
  24. JSBPrimitiveType* ptype = ftype->type_->asPrimitiveType();
  25. if (ptype->kind_ == JSBPrimitiveType::Bool)
  26. scriptType = "boolean";
  27. }
  28. if (ftype->type_->asStringHashType() || ftype->type_->asStringType())
  29. scriptType = "string";
  30. if (ftype->type_->asEnumType())
  31. scriptType = "Atomic." + ftype->type_->asEnumType()->enum_->GetName();
  32. if (ftype->type_->asEnumType())
  33. scriptType = "Atomic." + ftype->type_->asEnumType()->enum_->GetName();
  34. if (ftype->type_->asClassType())
  35. scriptType = "Atomic." + ftype->type_->asClassType()->class_->GetName();
  36. return scriptType;
  37. }
  38. void JSBDoc::Begin()
  39. {
  40. source_ += "//Atomic JSDoc Definitions\n\n\n";
  41. source_ += "/**\n * Atomic Game Engine\n * @namespace\n*/\n var " + package_->GetName() + " = {}\n\n";
  42. }
  43. void JSBDoc::End()
  44. {
  45. }
  46. String JSBDoc::GenFunctionDoc(JSBFunction* function)
  47. {
  48. if (function->Skip())
  49. return "";
  50. String params;
  51. Vector<JSBFunctionType*>& parameters = function->GetParameters();
  52. for (unsigned i = 0; i < parameters.Size(); i++)
  53. {
  54. JSBFunctionType* ftype = parameters.At(i);
  55. String scriptType = GetScriptType(ftype);
  56. if (scriptType == "Atomic.Context")
  57. continue;
  58. // mark as optional
  59. if (ftype->initializer_.Length())
  60. scriptType += "=";
  61. params += " * @param {" + scriptType + "} " + ftype->name_ + "\n";
  62. }
  63. String returns;
  64. if (function->GetReturnType())
  65. returns = " * @returns { " + GetScriptType(function->GetReturnType()) + "}\n";
  66. String docString;
  67. if (function->IsConstructor())
  68. {
  69. docString.AppendWithFormat("%s", params.CString());
  70. }
  71. else
  72. {
  73. docString.AppendWithFormat(" * %s\n * @memberof Atomic.%s.prototype\n%s%s\n",
  74. function->GetDocString().CString(),
  75. function->GetClass()->GetName().CString(),
  76. params.CString(),
  77. returns.CString());
  78. }
  79. return docString;
  80. }
  81. void JSBDoc::ExportModuleClasses(JSBModule* module)
  82. {
  83. Vector<SharedPtr<JSBClass>> classes = module->GetClasses();
  84. if (!classes.Size())
  85. return;
  86. source_ += "\n";
  87. for (unsigned i = 0; i < classes.Size(); i++)
  88. {
  89. JSBClass* klass = classes.At(i);
  90. source_ += "/**\n * @class\n* @memberof Atomic\n";
  91. if (klass->GetBaseClass())
  92. source_ += " * @augments Atomic." + klass->GetBaseClass()->GetName()+ "\n";
  93. // PROPERTIES
  94. Vector<String> propertyNames;
  95. klass->GetPropertyNames(propertyNames);
  96. for (unsigned j = 0; j < propertyNames.Size(); j++)
  97. {
  98. JSBProperty* prop = klass->GetProperty(propertyNames[j]);
  99. JSBFunctionType* ftype = NULL;
  100. String desc;
  101. if (prop->getter_ && !prop->getter_->Skip())
  102. {
  103. desc = prop->getter_->GetDocString();
  104. ftype = prop->getter_->GetReturnType();
  105. }
  106. else if (prop->setter_ && !prop->setter_->Skip())
  107. {
  108. ftype = prop->setter_->GetParameters()[0];
  109. }
  110. if (prop->setter_ && prop->setter_->GetDocString().Length())
  111. {
  112. // overwrite getter docstring if it exsists
  113. desc = prop->setter_->GetDocString();
  114. }
  115. if (!ftype)
  116. continue;
  117. String scriptType = GetScriptType(ftype);
  118. String scriptName = prop->GetCasePropertyName();
  119. if (desc.Length())
  120. {
  121. source_ += " * @property {" + scriptType + "} " + scriptName + " - " + desc + "\n";
  122. }
  123. else
  124. {
  125. source_ += " * @property {" + scriptType + "} " + scriptName + "\n";
  126. }
  127. }
  128. JSBFunction* constructor = klass->GetConstructor();
  129. if (constructor)
  130. {
  131. String docs = GenFunctionDoc(constructor);
  132. source_ += docs;
  133. }
  134. source_ += "*/ \nfunction " + klass->GetName() + "() {};\n\n";
  135. // FUNCTIONS
  136. PODVector<JSBFunction*>& functions = klass->GetFunctions();
  137. for (unsigned j = 0; j < functions.Size(); j++)
  138. {
  139. JSBFunction* func = functions[j];
  140. if (func->IsConstructor() || func->IsDestructor() || func->Skip())
  141. continue;
  142. String docs = GenFunctionDoc(func);
  143. String scriptName = func->GetName();
  144. scriptName[0] = tolower(scriptName[0]);
  145. if (scriptName == "delete")
  146. scriptName = "__delete";
  147. String docString;
  148. docString.AppendWithFormat("/**\n %s */\n function %s() {};\n\n",
  149. docs.CString(),
  150. scriptName.CString());
  151. source_ += docString;
  152. }
  153. }
  154. }
  155. void JSBDoc::ExportModuleConstants(JSBModule* module)
  156. {
  157. const Vector<String>& constants = module->GetConstants().Keys();
  158. if (!constants.Size())
  159. return;
  160. source_ += "\n";
  161. for (unsigned i = 0; i < constants.Size(); i++)
  162. {
  163. const String& cname = constants.At(i);
  164. source_ += "/**\n * @memberof Atomic\n * @type {number}\n */\nvar " + cname + ";\n";
  165. }
  166. source_ += "\n";
  167. }
  168. void JSBDoc::ExportModuleEnums(JSBModule* module)
  169. {
  170. Vector<SharedPtr<JSBEnum>> enums = module->GetEnums();
  171. for (unsigned i = 0; i < enums.Size(); i++)
  172. {
  173. JSBEnum* _enum = enums.At(i);
  174. source_ += "/**\n * @memberof Atomic\n * @readonly\n * @enum {number}\n */\n";
  175. source_ += " var " + _enum->GetName() + " = {\n";
  176. HashMap<String, String>& values = _enum->GetValues();
  177. HashMap<String, String>::ConstIterator itr = values.Begin();
  178. while (itr != values.End())
  179. {
  180. String name = (*itr).first_;
  181. source_ += " " + name + " : undefined";
  182. itr++;
  183. if (itr != values.End())
  184. source_ += ",\n";
  185. }
  186. source_ += "\n\n};\n\n";
  187. }
  188. }
  189. void JSBDoc::WriteToFile(const String &path)
  190. {
  191. FileSystem* fs = package_->GetSubsystem<FileSystem>();
  192. String jsDocPath = GetPath(path);
  193. if (!fs->DirExists(jsDocPath))
  194. fs->CreateDir(jsDocPath);
  195. File file(package_->GetContext());
  196. file.Open(path, FILE_WRITE);
  197. file.Write(source_.CString(), source_.Length());
  198. file.Close();
  199. }
  200. void JSBDoc::Emit(JSBPackage* package, const String& path)
  201. {
  202. package_ = package;
  203. Vector<SharedPtr<JSBModule>>& modules = package->GetModules();
  204. Begin();
  205. for (unsigned i = 0; i < modules.Size(); i++)
  206. {
  207. ExportModuleEnums(modules[i]);
  208. }
  209. for (unsigned i = 0; i < modules.Size(); i++)
  210. {
  211. ExportModuleConstants(modules[i]);
  212. }
  213. for (unsigned i = 0; i < modules.Size(); i++)
  214. {
  215. source_ += "\n//----------------------------------------------------\n";
  216. source_ += "// MODULE: " + modules[i]->GetName() + "\n";
  217. source_ += "//----------------------------------------------------\n\n";
  218. ExportModuleClasses(modules[i]);
  219. }
  220. End();
  221. WriteToFile(path);
  222. }
  223. }