JSBDoc.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 "JSBPackage.h"
  10. #include "JSBModule.h"
  11. #include "JSBFunction.h"
  12. #include "JSBDoc.h"
  13. namespace ToolCore
  14. {
  15. static String GetScriptType(JSBFunctionType* ftype)
  16. {
  17. String scriptType = "number";
  18. if (ftype->type_->asPrimitiveType())
  19. {
  20. JSBPrimitiveType* ptype = ftype->type_->asPrimitiveType();
  21. if (ptype->kind_ == JSBPrimitiveType::Bool)
  22. scriptType = "boolean";
  23. }
  24. if (ftype->type_->asStringHashType() || ftype->type_->asStringType())
  25. scriptType = "string";
  26. if (ftype->type_->asEnumType())
  27. scriptType = "Atomic." + ftype->type_->asEnumType()->enum_->GetName();
  28. if (ftype->type_->asEnumType())
  29. scriptType = "Atomic." + ftype->type_->asEnumType()->enum_->GetName();
  30. if (ftype->type_->asClassType())
  31. scriptType = "Atomic." + ftype->type_->asClassType()->class_->GetName();
  32. return scriptType;
  33. }
  34. void JSBDoc::Begin()
  35. {
  36. source_ += "//Atomic JSDoc Definitions\n\n\n";
  37. source_ += "/**\n * Atomic Game Engine\n * @namespace\n*/\n var " + package_->GetName() + " = {}\n\n";
  38. }
  39. void JSBDoc::End()
  40. {
  41. }
  42. String JSBDoc::GenFunctionDoc(JSBFunction* function)
  43. {
  44. if (function->Skip())
  45. return "";
  46. String params;
  47. Vector<JSBFunctionType*>& parameters = function->GetParameters();
  48. for (unsigned i = 0; i < parameters.Size(); i++)
  49. {
  50. JSBFunctionType* ftype = parameters.At(i);
  51. String scriptType = GetScriptType(ftype);
  52. if (scriptType == "Atomic.Context")
  53. continue;
  54. // mark as optional
  55. if (ftype->initializer_.Length())
  56. scriptType += "=";
  57. params += " * @param {" + scriptType + "} " + ftype->name_ + "\n";
  58. }
  59. String returns;
  60. if (function->GetReturnType())
  61. returns = " * @returns { " + GetScriptType(function->GetReturnType()) + "}\n";
  62. String docString;
  63. if (function->IsConstructor())
  64. {
  65. docString.AppendWithFormat("%s", params.CString());
  66. }
  67. else
  68. {
  69. docString.AppendWithFormat(" * %s\n * @memberof Atomic.%s.prototype\n%s%s\n",
  70. function->GetDocString().CString(),
  71. function->GetClass()->GetName().CString(),
  72. params.CString(),
  73. returns.CString());
  74. }
  75. return docString;
  76. }
  77. void JSBDoc::ExportModuleClasses(JSBModule* module)
  78. {
  79. Vector<SharedPtr<JSBClass>> classes = module->GetClasses();
  80. if (!classes.Size())
  81. return;
  82. source_ += "\n";
  83. for (unsigned i = 0; i < classes.Size(); i++)
  84. {
  85. JSBClass* klass = classes.At(i);
  86. source_ += "/**\n * @class\n* @memberof Atomic\n";
  87. if (klass->GetBaseClass())
  88. source_ += " * @augments Atomic." + klass->GetBaseClass()->GetName()+ "\n";
  89. // PROPERTIES
  90. Vector<String> propertyNames;
  91. klass->GetPropertyNames(propertyNames);
  92. for (unsigned j = 0; j < propertyNames.Size(); j++)
  93. {
  94. JSBProperty* prop = klass->GetProperty(propertyNames[j]);
  95. JSBFunctionType* ftype = NULL;
  96. String desc;
  97. if (prop->getter_ && !prop->getter_->Skip())
  98. {
  99. desc = prop->getter_->GetDocString();
  100. ftype = prop->getter_->GetReturnType();
  101. }
  102. else if (prop->setter_ && !prop->setter_->Skip())
  103. {
  104. ftype = prop->setter_->GetParameters()[0];
  105. }
  106. if (prop->setter_ && prop->setter_->GetDocString().Length())
  107. {
  108. // overwrite getter docstring if it exsists
  109. desc = prop->setter_->GetDocString();
  110. }
  111. if (!ftype)
  112. continue;
  113. String scriptType = GetScriptType(ftype);
  114. String scriptName = propertyNames[j];
  115. scriptName[0] = tolower(scriptName[0]);
  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. Vector<String>& constants = module->GetConstants();
  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. File file(package_->GetContext());
  186. file.Open(path, FILE_WRITE);
  187. file.Write(source_.CString(), source_.Length());
  188. file.Close();
  189. }
  190. void JSBDoc::Emit(JSBPackage* package, const String& path)
  191. {
  192. package_ = package;
  193. Vector<SharedPtr<JSBModule>>& modules = package->GetModules();
  194. Begin();
  195. for (unsigned i = 0; i < modules.Size(); i++)
  196. {
  197. ExportModuleEnums(modules[i]);
  198. }
  199. for (unsigned i = 0; i < modules.Size(); i++)
  200. {
  201. ExportModuleConstants(modules[i]);
  202. }
  203. for (unsigned i = 0; i < modules.Size(); i++)
  204. {
  205. source_ += "\n//----------------------------------------------------\n";
  206. source_ += "// MODULE: " + modules[i]->GetName() + "\n";
  207. source_ += "//----------------------------------------------------\n\n";
  208. ExportModuleClasses(modules[i]);
  209. }
  210. End();
  211. WriteToFile(path);
  212. }
  213. }