JSBDoc.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "JSBDoc.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 = "Atomic." + ftype->type_->asEnumType()->enum_->name_;
  26. if (ftype->type_->asEnumType())
  27. scriptType = "Atomic." + ftype->type_->asEnumType()->enum_->name_;
  28. if (ftype->type_->asClassType())
  29. scriptType = "Atomic." + ftype->type_->asClassType()->class_->GetName();
  30. return scriptType;
  31. }
  32. void JSBDoc::Begin()
  33. {
  34. source_ += "//Atomic JSDoc Definitions\n\n\n";
  35. source_ += "/**\n * Atomic Game Engine\n * @namespace\n*/\n var Atomic = {}\n\n";
  36. }
  37. void JSBDoc::End()
  38. {
  39. }
  40. String JSBDoc::GenFunctionDoc(JSBFunction* function)
  41. {
  42. if (function->Skip())
  43. return "";
  44. String params;
  45. for (unsigned i = 0; i < function->parameters_.Size(); i++)
  46. {
  47. JSBFunctionType* ftype = function->parameters_.At(i);
  48. String scriptType = GetScriptType(ftype);
  49. if (scriptType == "Atomic.Context")
  50. continue;
  51. // mark as optional
  52. if (ftype->initializer_.Length())
  53. scriptType += "=";
  54. params += " * @param {" + scriptType + "} " + ftype->name_ + "\n";
  55. }
  56. String returns;
  57. if (function->returnType_)
  58. returns = " * @returns { " + GetScriptType(function->returnType_) + "}\n";
  59. String docString;
  60. if (function->isConstructor_)
  61. {
  62. docString.AppendWithFormat("%s", params.CString());
  63. }
  64. else
  65. {
  66. docString.AppendWithFormat(" * %s\n * @memberof Atomic.%s.prototype\n%s%s\n",
  67. function->docString_.CString(),
  68. function->class_->GetName().CString(),
  69. params.CString(),
  70. returns.CString());
  71. }
  72. return docString;
  73. }
  74. void JSBDoc::ExportModuleClasses(const String& moduleName)
  75. {
  76. JSBModule* module = JSBindings::Instance()->GetModuleByName(moduleName);
  77. if (!module->classes_.Size())
  78. return;
  79. source_ += "\n";
  80. for (unsigned i = 0; i < module->classes_.Size(); i++)
  81. {
  82. JSBClass* klass = module->classes_.At(i);
  83. source_ += "/**\n * @class\n* @memberof Atomic\n";
  84. if (klass->GetBaseClass())
  85. source_ += " * @augments Atomic." + klass->GetBaseClass()->GetName()+ "\n";
  86. // PROPERTIES
  87. Vector<String> propertyNames;
  88. klass->GetPropertyNames(propertyNames);
  89. for (unsigned j = 0; j < propertyNames.Size(); j++)
  90. {
  91. JSBProperty* prop = klass->GetProperty(propertyNames[j]);
  92. JSBFunctionType* ftype = NULL;
  93. String desc;
  94. if (prop->getter_ && !prop->getter_->Skip())
  95. {
  96. desc = prop->getter_->docString_;
  97. ftype = prop->getter_->returnType_;
  98. }
  99. else if (prop->setter_ && !prop->setter_->Skip())
  100. {
  101. ftype = prop->setter_->parameters_[0];
  102. }
  103. if (prop->setter_ && prop->setter_->docString_.Length())
  104. {
  105. // overwrite getter docstring if it exsists
  106. desc = prop->setter_->docString_;
  107. }
  108. if (!ftype)
  109. continue;
  110. String scriptType = GetScriptType(ftype);
  111. String scriptName = propertyNames[j];
  112. scriptName[0] = tolower(scriptName[0]);
  113. if (desc.Length())
  114. {
  115. source_ += " * @property {" + scriptType + "} " + scriptName + " - " + desc + "\n";
  116. }
  117. else
  118. {
  119. source_ += " * @property {" + scriptType + "} " + scriptName + "\n";
  120. }
  121. }
  122. JSBFunction* constructor = klass->GetConstructor();
  123. if (constructor)
  124. {
  125. String docs = GenFunctionDoc(constructor);
  126. source_ += docs;
  127. }
  128. source_ += "*/ \nfunction " + klass->GetName() + "() {};\n\n";
  129. // FUNCTIONS
  130. for (unsigned j = 0; j < klass->GetFunctionCount(); j++)
  131. {
  132. JSBFunction* func = klass->GetFunction(j);
  133. if (func->isConstructor_ || func->isDestructor_ || func->Skip())
  134. continue;
  135. String docs = GenFunctionDoc(func);
  136. String scriptName = func->name_;
  137. scriptName[0] = tolower(scriptName[0]);
  138. if (scriptName == "delete")
  139. scriptName = "__delete";
  140. String docString;
  141. docString.AppendWithFormat("/**\n %s */\n function %s() {};\n\n",
  142. docs.CString(),
  143. scriptName.CString());
  144. source_ += docString;
  145. }
  146. }
  147. }
  148. void JSBDoc::ExportModuleConstants(const String& moduleName)
  149. {
  150. JSBModule* module = JSBindings::Instance()->GetModuleByName(moduleName);
  151. if (!module->constants_.Size())
  152. return;
  153. source_ += "\n";
  154. for (unsigned i = 0; i < module->constants_.Size(); i++)
  155. {
  156. const String& cname = module->constants_.At(i);
  157. source_ += "/**\n * @memberof Atomic\n * @type {number}\n */\nvar " + cname + ";\n";
  158. }
  159. source_ += "\n";
  160. }
  161. void JSBDoc::ExportModuleEnums(const String& moduleName)
  162. {
  163. JSBModule* module = JSBindings::Instance()->GetModuleByName(moduleName);
  164. for (unsigned i = 0; i < module->enums_.Size(); i++)
  165. {
  166. JSBEnum* _enum = module->enums_.At(i);
  167. source_ += "/**\n * @memberof Atomic\n * @readonly\n * @enum {number}\n */\n";
  168. source_ += " var " + _enum->name_ + " = {\n";
  169. for (unsigned j = 0; j < _enum->values_.Size(); j++)
  170. {
  171. source_ += " " + _enum->values_[j] + " : undefined";
  172. if (j != _enum->values_.Size() - 1)
  173. source_ += ",\n";
  174. }
  175. source_ += "\n\n};\n\n";
  176. }
  177. }
  178. void JSBDoc::WriteToFile(const String &path)
  179. {
  180. File file(JSBind::context_);
  181. file.Open(path, FILE_WRITE);
  182. file.Write(source_.CString(), source_.Length());
  183. file.Close();
  184. }
  185. void JSBDoc::Emit(const String& path)
  186. {
  187. Vector<String> modules;
  188. modules.Push("Container");
  189. modules.Push("Core");
  190. modules.Push("Engine");
  191. modules.Push("Resource");
  192. modules.Push("Scene");
  193. modules.Push("UI");
  194. modules.Push("Atomic2D");
  195. modules.Push("Atomic3D");
  196. modules.Push("Audio");
  197. modules.Push("Graphics");
  198. modules.Push("Input");
  199. modules.Push("IO");
  200. //modules.Push("Math");
  201. modules.Push("Navigation");
  202. modules.Push("Network");
  203. modules.Push("Physics");
  204. modules.Push("Javascript");
  205. Begin();
  206. for (unsigned i = 0; i < modules.Size(); i++)
  207. {
  208. ExportModuleEnums(modules[i]);
  209. }
  210. for (unsigned i = 0; i < modules.Size(); i++)
  211. {
  212. ExportModuleConstants(modules[i]);
  213. }
  214. for (unsigned i = 0; i < modules.Size(); i++)
  215. {
  216. source_ += "\n//----------------------------------------------------\n";
  217. source_ += "// MODULE: " + modules[i] + "\n";
  218. source_ += "//----------------------------------------------------\n\n";
  219. ExportModuleClasses(modules[i]);
  220. }
  221. End();
  222. WriteToFile(path);
  223. }