JSBDoc.cpp 7.6 KB

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