JSBDoc.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Atomic.h>
  23. #include <Atomic/IO/Log.h>
  24. #include <Atomic/IO/FileSystem.h>
  25. #include <Atomic/Core/ProcessUtils.h>
  26. #include <Atomic/Resource/ResourceCache.h>
  27. #include "JSBind.h"
  28. #include "JSBPackage.h"
  29. #include "JSBModule.h"
  30. #include "JSBFunction.h"
  31. #include "JSBDoc.h"
  32. namespace ToolCore
  33. {
  34. static String GetScriptType(JSBFunctionType* ftype)
  35. {
  36. String scriptType = "number";
  37. if (ftype->type_->asPrimitiveType())
  38. {
  39. JSBPrimitiveType* ptype = ftype->type_->asPrimitiveType();
  40. if (ptype->kind_ == JSBPrimitiveType::Bool)
  41. scriptType = "boolean";
  42. }
  43. if (ftype->type_->asStringHashType() || ftype->type_->asStringType())
  44. scriptType = "string";
  45. if (ftype->type_->asEnumType())
  46. scriptType = "Atomic." + ftype->type_->asEnumType()->enum_->GetName();
  47. if (ftype->type_->asEnumType())
  48. scriptType = "Atomic." + ftype->type_->asEnumType()->enum_->GetName();
  49. if (ftype->type_->asClassType())
  50. scriptType = "Atomic." + ftype->type_->asClassType()->class_->GetName();
  51. return scriptType;
  52. }
  53. void JSBDoc::Begin()
  54. {
  55. source_ += "//////////////////////////////////////////////////////////\n";
  56. source_ += "// IMPORTANT: THIS FILE IS GENERATED, CHANGES WILL BE LOST\n";
  57. source_ += "//////////////////////////////////////////////////////////\n\n";
  58. source_ += "//Atomic JSDoc Definitions\n\n";
  59. source_ += "/**\n * Atomic Game Engine\n * @namespace\n*/\n var " + package_->GetName() + " = {}\n\n";
  60. }
  61. void JSBDoc::End()
  62. {
  63. }
  64. String JSBDoc::GenFunctionDoc(JSBFunction* function)
  65. {
  66. if (function->Skip())
  67. return "";
  68. String params;
  69. Vector<JSBFunctionType*>& parameters = function->GetParameters();
  70. for (unsigned i = 0; i < parameters.Size(); i++)
  71. {
  72. JSBFunctionType* ftype = parameters.At(i);
  73. String scriptType = GetScriptType(ftype);
  74. if (scriptType == "Atomic.Context")
  75. continue;
  76. // mark as optional
  77. if (ftype->initializer_.Length())
  78. scriptType += "=";
  79. params += " * @param {" + scriptType + "} " + ftype->name_ + "\n";
  80. }
  81. String returns;
  82. if (function->GetReturnType())
  83. returns = " * @returns { " + GetScriptType(function->GetReturnType()) + "}\n";
  84. String docString;
  85. if (function->IsConstructor())
  86. {
  87. docString.AppendWithFormat("%s", params.CString());
  88. }
  89. else
  90. {
  91. docString.AppendWithFormat(" * %s\n * @memberof Atomic.%s.prototype\n%s%s\n",
  92. function->GetDocString().CString(),
  93. function->GetClass()->GetName().CString(),
  94. params.CString(),
  95. returns.CString());
  96. }
  97. return docString;
  98. }
  99. void JSBDoc::ExportModuleClasses(JSBModule* module)
  100. {
  101. Vector<SharedPtr<JSBClass>> classes = module->GetClasses();
  102. if (!classes.Size())
  103. return;
  104. source_ += "\n";
  105. for (unsigned i = 0; i < classes.Size(); i++)
  106. {
  107. JSBClass* klass = classes.At(i);
  108. source_ += "/**\n * @class\n* @memberof Atomic\n";
  109. if (klass->GetBaseClass())
  110. source_ += " * @augments Atomic." + klass->GetBaseClass()->GetName()+ "\n";
  111. // PROPERTIES
  112. Vector<String> propertyNames;
  113. klass->GetPropertyNames(propertyNames);
  114. for (unsigned j = 0; j < propertyNames.Size(); j++)
  115. {
  116. JSBProperty* prop = klass->GetProperty(propertyNames[j]);
  117. JSBFunctionType* ftype = NULL;
  118. String desc;
  119. if (prop->getter_ && !prop->getter_->Skip())
  120. {
  121. desc = prop->getter_->GetDocString();
  122. ftype = prop->getter_->GetReturnType();
  123. }
  124. else if (prop->setter_ && !prop->setter_->Skip())
  125. {
  126. ftype = prop->setter_->GetParameters()[0];
  127. }
  128. if (prop->setter_ && prop->setter_->GetDocString().Length())
  129. {
  130. // overwrite getter docstring if it exsists
  131. desc = prop->setter_->GetDocString();
  132. }
  133. if (!ftype)
  134. continue;
  135. String scriptType = GetScriptType(ftype);
  136. String scriptName = prop->GetCasePropertyName();
  137. if (desc.Length())
  138. {
  139. source_ += " * @property {" + scriptType + "} " + scriptName + " - " + desc + "\n";
  140. }
  141. else
  142. {
  143. source_ += " * @property {" + scriptType + "} " + scriptName + "\n";
  144. }
  145. }
  146. JSBFunction* constructor = klass->GetConstructor(BINDINGLANGUAGE_JAVASCRIPT);
  147. if (constructor)
  148. {
  149. String docs = GenFunctionDoc(constructor);
  150. source_ += docs;
  151. }
  152. source_ += "*/ \nfunction " + klass->GetName() + "() {};\n\n";
  153. // FUNCTIONS
  154. PODVector<JSBFunction*>& functions = klass->GetFunctions();
  155. for (unsigned j = 0; j < functions.Size(); j++)
  156. {
  157. JSBFunction* func = functions[j];
  158. if (func->IsConstructor() || func->IsDestructor() || func->Skip())
  159. continue;
  160. String docs = GenFunctionDoc(func);
  161. String scriptName = func->GetName();
  162. scriptName[0] = tolower(scriptName[0]);
  163. if (scriptName == "delete")
  164. scriptName = "__delete";
  165. String docString;
  166. docString.AppendWithFormat("/**\n %s */\n function %s() {};\n\n",
  167. docs.CString(),
  168. scriptName.CString());
  169. source_ += docString;
  170. }
  171. }
  172. }
  173. void JSBDoc::ExportModuleConstants(JSBModule* module)
  174. {
  175. const Vector<String>& constants = module->GetConstants().Keys();
  176. if (!constants.Size())
  177. return;
  178. source_ += "\n";
  179. for (unsigned i = 0; i < constants.Size(); i++)
  180. {
  181. const String& cname = constants.At(i);
  182. source_ += "/**\n * @memberof Atomic\n * @type {number}\n */\nvar " + cname + ";\n";
  183. }
  184. source_ += "\n";
  185. }
  186. void JSBDoc::ExportModuleEnums(JSBModule* module)
  187. {
  188. Vector<SharedPtr<JSBEnum>> enums = module->GetEnums();
  189. for (unsigned i = 0; i < enums.Size(); i++)
  190. {
  191. JSBEnum* _enum = enums.At(i);
  192. source_ += "/**\n * @memberof Atomic\n * @readonly\n * @enum {number}\n */\n";
  193. source_ += " var " + _enum->GetName() + " = {\n";
  194. HashMap<String, String>& values = _enum->GetValues();
  195. HashMap<String, String>::ConstIterator itr = values.Begin();
  196. while (itr != values.End())
  197. {
  198. String name = (*itr).first_;
  199. source_ += " " + name + " : undefined";
  200. itr++;
  201. if (itr != values.End())
  202. source_ += ",\n";
  203. }
  204. source_ += "\n\n};\n\n";
  205. }
  206. }
  207. void JSBDoc::WriteToFile(const String &path)
  208. {
  209. FileSystem* fs = package_->GetSubsystem<FileSystem>();
  210. String jsDocPath = GetPath(path);
  211. if (!fs->DirExists(jsDocPath))
  212. fs->CreateDir(jsDocPath);
  213. File file(package_->GetContext());
  214. file.Open(path, FILE_WRITE);
  215. file.Write(source_.CString(), source_.Length());
  216. file.Close();
  217. }
  218. void JSBDoc::Emit(JSBPackage* package, const String& path)
  219. {
  220. package_ = package;
  221. Vector<SharedPtr<JSBModule>>& modules = package->GetModules();
  222. Begin();
  223. for (unsigned i = 0; i < modules.Size(); i++)
  224. {
  225. ExportModuleEnums(modules[i]);
  226. }
  227. for (unsigned i = 0; i < modules.Size(); i++)
  228. {
  229. ExportModuleConstants(modules[i]);
  230. }
  231. for (unsigned i = 0; i < modules.Size(); i++)
  232. {
  233. source_ += "\n//----------------------------------------------------\n";
  234. source_ += "// MODULE: " + modules[i]->GetName() + "\n";
  235. source_ += "//----------------------------------------------------\n\n";
  236. ExportModuleClasses(modules[i]);
  237. }
  238. End();
  239. WriteToFile(path);
  240. }
  241. }