JSBDoc.cpp 7.7 KB

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