JSBDoc.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 %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. if (prop->getter_ && !prop->getter_->Skip())
  91. {
  92. ftype = prop->getter_->returnType_;
  93. }
  94. else if (prop->setter_ && !prop->setter_->Skip())
  95. ftype = prop->setter_->parameters_[0];
  96. if (!ftype)
  97. continue;
  98. String scriptType = GetScriptType(ftype);
  99. String scriptName = propertyNames[j];
  100. scriptName[0] = tolower(scriptName[0]);
  101. source_ += " * @property {" + scriptType + "} " + scriptName + "\n";
  102. }
  103. JSBFunction* constructor = klass->GetConstructor();
  104. if (constructor)
  105. {
  106. String docs = GenFunctionDoc(constructor);
  107. source_ += docs;
  108. }
  109. source_ += "*/ \nfunction " + klass->GetName() + "() {};\n\n";
  110. // FUNCTIONS
  111. for (unsigned j = 0; j < klass->GetFunctionCount(); j++)
  112. {
  113. JSBFunction* func = klass->GetFunction(j);
  114. if (func->isConstructor_ || func->isDestructor_ || func->Skip())
  115. continue;
  116. String docs = GenFunctionDoc(func);
  117. String scriptName = func->name_;
  118. scriptName[0] = tolower(scriptName[0]);
  119. if (scriptName == "delete")
  120. scriptName = "__delete";
  121. String docString;
  122. docString.AppendWithFormat("/**\n %s */\n function %s() {};\n\n",
  123. docs.CString(),
  124. scriptName.CString());
  125. source_ += docString;
  126. }
  127. }
  128. }
  129. void JSBDoc::ExportModuleConstants(const String& moduleName)
  130. {
  131. JSBModule* module = JSBindings::Instance()->GetModuleByName(moduleName);
  132. if (!module->constants_.Size())
  133. return;
  134. source_ += "\n";
  135. for (unsigned i = 0; i < module->constants_.Size(); i++)
  136. {
  137. const String& cname = module->constants_.At(i);
  138. source_ += "/**\n * @memberof Atomic\n * @type {number}\n */\nvar " + cname + ";\n";
  139. }
  140. source_ += "\n";
  141. }
  142. void JSBDoc::ExportModuleEnums(const String& moduleName)
  143. {
  144. JSBModule* module = JSBindings::Instance()->GetModuleByName(moduleName);
  145. for (unsigned i = 0; i < module->enums_.Size(); i++)
  146. {
  147. JSBEnum* _enum = module->enums_.At(i);
  148. source_ += "/**\n * @memberof Atomic\n * @readonly\n * @enum {number}\n */\n";
  149. source_ += " var " + _enum->name_ + " = {\n";
  150. for (unsigned j = 0; j < _enum->values_.Size(); j++)
  151. {
  152. source_ += " " + _enum->values_[j] + " : undefined";
  153. if (j != _enum->values_.Size() - 1)
  154. source_ += ",\n";
  155. }
  156. source_ += "\n\n};\n\n";
  157. }
  158. }
  159. void JSBDoc::WriteToFile(const String &path)
  160. {
  161. File file(JSBind::context_);
  162. file.Open(path, FILE_WRITE);
  163. file.Write(source_.CString(), source_.Length());
  164. file.Close();
  165. }
  166. void JSBDoc::Emit(const String& path)
  167. {
  168. Vector<String> modules;
  169. modules.Push("Container");
  170. modules.Push("Core");
  171. modules.Push("Engine");
  172. modules.Push("Resource");
  173. modules.Push("Scene");
  174. modules.Push("UI");
  175. modules.Push("Atomic2D");
  176. modules.Push("Audio");
  177. modules.Push("Graphics");
  178. modules.Push("Input");
  179. modules.Push("IO");
  180. //modules.Push("Math");
  181. modules.Push("Navigation");
  182. modules.Push("Network");
  183. modules.Push("Physics");
  184. modules.Push("Javascript");
  185. Begin();
  186. for (unsigned i = 0; i < modules.Size(); i++)
  187. {
  188. ExportModuleEnums(modules[i]);
  189. }
  190. for (unsigned i = 0; i < modules.Size(); i++)
  191. {
  192. ExportModuleConstants(modules[i]);
  193. }
  194. for (unsigned i = 0; i < modules.Size(); i++)
  195. {
  196. source_ += "\n//----------------------------------------------------\n";
  197. source_ += "// MODULE: " + modules[i] + "\n";
  198. source_ += "//----------------------------------------------------\n\n";
  199. ExportModuleClasses(modules[i]);
  200. }
  201. End();
  202. WriteToFile(path);
  203. }