JSPackageWriter.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/IO/File.h>
  23. #include <Atomic/IO/FileSystem.h>
  24. #include <Atomic/Core/StringUtils.h>
  25. #include "../JSBind.h"
  26. #include "../JSBModule.h"
  27. #include "../JSBPackage.h"
  28. #include "../JSBEnum.h"
  29. #include "../JSBClass.h"
  30. #include "../JSBDoc.h"
  31. #include "../JSBTypeScript.h"
  32. #include "../JSBHaxe.h"
  33. #include "JSPackageWriter.h"
  34. #include "JSModuleWriter.h"
  35. namespace ToolCore
  36. {
  37. JSPackageWriter::JSPackageWriter(JSBPackage *package) : JSBPackageWriter(package)
  38. {
  39. }
  40. void JSPackageWriter::WriteProtoTypeRecursive(String &source, JSBClass* klass, Vector<JSBClass*>& written)
  41. {
  42. if (written.Contains(klass))
  43. return;
  44. if (klass->GetModule()->GetDotNetModule())
  45. return;
  46. PODVector<JSBClass*>& baseClasses = klass->GetBaseClasses();
  47. Vector<JSBClass*>::Iterator itr = baseClasses.End() - 1 ;
  48. while (itr != baseClasses.Begin() - 1)
  49. {
  50. WriteProtoTypeRecursive(source, (*itr), written);
  51. itr--;
  52. }
  53. JSBClass* base = baseClasses.Size() ? baseClasses[0] : NULL;
  54. if (!klass->IsNumberArray() && klass->GetPackage() == package_)
  55. {
  56. JSBModule* module = klass->GetModule();
  57. String moduleGuard = module->GetModuleDefineGuard();
  58. if (moduleGuard.Length())
  59. {
  60. source += ToString("\n%s\n", moduleGuard.CString());
  61. }
  62. if (module->Requires("3D"))
  63. source += "\n#ifdef ATOMIC_3D\n";
  64. String packageName = klass->GetModule()->GetPackage()->GetName();
  65. String basePackage = base ? base->GetModule()->GetPackage()->GetName() : "";
  66. source.AppendWithFormat(" js_setup_prototype(vm, \"%s\", \"%s\", \"%s\", \"%s\", %s);\n",
  67. packageName.CString(), klass->GetName().CString(),
  68. base ? basePackage.CString() : "", base ? base->GetName().CString() : "",
  69. klass->HasProperties() ? "true" : "false");
  70. if (module->Requires("3D"))
  71. source += "#endif\n\n";
  72. if (moduleGuard.Length())
  73. {
  74. source += ToString("\n#endif\n", moduleGuard.CString());
  75. }
  76. }
  77. written.Push(klass);
  78. }
  79. void JSPackageWriter::WriteProtoTypeSetup(String& source)
  80. {
  81. Vector<JSBClass*> written;
  82. PODVector<JSBClass*> allClasses = package_->GetAllClasses();
  83. for (unsigned i = 0; i < allClasses.Size(); i++)
  84. {
  85. WriteProtoTypeRecursive(source, allClasses[i], written);
  86. }
  87. }
  88. void JSPackageWriter::GenerateSource()
  89. {
  90. String source = "// This file was autogenerated by JSBind, changes will be lost\n\n";
  91. String defineGuard = package_->GetPlatformDefineGuard();
  92. if (defineGuard.Length())
  93. {
  94. source += ToString("%s\n\n", defineGuard.CString());
  95. }
  96. source += "#include <Duktape/duktape.h>\n";
  97. source += "#include <Atomic/Script/ScriptVector.h>\n";
  98. source += "#include <AtomicJS/Javascript/JSVM.h>\n";
  99. source += "#include <AtomicJS/Javascript/JSAPI.h>\n";
  100. source += "\n\nnamespace Atomic\n{\n";
  101. String packageLower = package_->GetName().ToLower();
  102. for (unsigned i = 0; i < package_->modules_.Size(); i++)
  103. {
  104. JSBModule* module = package_->modules_.At(i);
  105. if (module->GetDotNetModule())
  106. continue;
  107. String moduleGuard = module->GetModuleDefineGuard();
  108. if (moduleGuard.Length())
  109. {
  110. source += ToString("\n%s\n", moduleGuard.CString());
  111. }
  112. String moduleLower = module->GetName().ToLower();
  113. source.AppendWithFormat("\nextern void jsb_package_%s_preinit_%s (JSVM* vm);", packageLower.CString(), moduleLower.CString());
  114. source.AppendWithFormat("\nextern void jsb_package_%s_init_%s (JSVM* vm);", packageLower.CString(), moduleLower.CString());
  115. if (moduleGuard.Length())
  116. {
  117. source += ToString("\n#endif\n", moduleGuard.CString());
  118. }
  119. }
  120. source += "\n\nstatic void jsb_modules_setup_prototypes(JSVM* vm)\n{\n";
  121. source += " // It is important that these are in order so the prototypes are created properly\n";
  122. source += " // This isn't trivial as modules can have dependencies, so do it here\n\n";
  123. WriteProtoTypeSetup(source);
  124. source += "\n}\n";
  125. source.AppendWithFormat("\n\nstatic void jsb_package_%s_preinit(JSVM* vm)\n{", packageLower.CString());
  126. source.Append("\n // Create the global package object\n");
  127. source.Append(" duk_context* ctx = vm->GetJSContext();\n");
  128. source.Append(" duk_push_object(ctx);\n");
  129. source.AppendWithFormat(" duk_put_global_string(ctx, \"%s\");\n", package_->GetName().CString());
  130. for (unsigned i = 0; i < package_->modules_.Size(); i++)
  131. {
  132. JSBModule* module = package_->modules_.At(i);
  133. if (module->GetDotNetModule())
  134. continue;
  135. String moduleGuard = module->GetModuleDefineGuard();
  136. if (moduleGuard.Length())
  137. {
  138. source += ToString("\n%s\n", moduleGuard.CString());
  139. }
  140. if (module->Requires("3D"))
  141. source += "\n#ifdef ATOMIC_3D";
  142. String moduleLower = module->GetName().ToLower();
  143. source.AppendWithFormat("\n jsb_package_%s_preinit_%s(vm);", packageLower.CString(), moduleLower.CString());
  144. if (module->Requires("3D"))
  145. source += "\n#endif //ATOMIC_3D\n";
  146. if (moduleGuard.Length())
  147. {
  148. source += ToString("\n#endif\n", moduleGuard.CString());
  149. }
  150. }
  151. source += "\n}\n\n";
  152. source.AppendWithFormat("\n\nvoid jsb_package_%s_init(JSVM* vm)\n{", packageLower.CString());
  153. source.AppendWithFormat("\n\n jsb_package_%s_preinit(vm);\n", packageLower.CString());
  154. source += "\n\n jsb_modules_setup_prototypes(vm);\n";
  155. for (unsigned i = 0; i < package_->modules_.Size(); i++)
  156. {
  157. JSBModule* module = package_->modules_.At(i);
  158. if (module->GetDotNetModule())
  159. continue;
  160. String moduleLower = module->GetName().ToLower();
  161. String moduleGuard = module->GetModuleDefineGuard();
  162. if (moduleGuard.Length())
  163. {
  164. source += ToString("\n%s\n", moduleGuard.CString());
  165. }
  166. if (module->Requires("3D"))
  167. source += "\n#ifdef ATOMIC_3D";
  168. source.AppendWithFormat("\n jsb_package_%s_init_%s(vm);", packageLower.CString(), moduleLower.CString());
  169. if (module->Requires("3D"))
  170. {
  171. source += "\n#endif //ATOMIC_3D\n";
  172. }
  173. if (moduleGuard.Length())
  174. {
  175. source += ToString("\n#endif\n", moduleGuard.CString());
  176. }
  177. }
  178. source += "\n}\n\n";
  179. // end Atomic namespace
  180. source += "\n}\n";
  181. if (defineGuard.Length())
  182. {
  183. source += "\n#endif\n";
  184. }
  185. JSBind* jsbind = package_->GetSubsystem<JSBind>();
  186. String filepath = jsbind->GetDestNativeFolder() + "/JSPackage" + package_->name_ + ".cpp";
  187. File file(package_->GetContext());
  188. file.Open(filepath, FILE_WRITE);
  189. file.Write(source.CString(), source.Length());
  190. file.Close();
  191. for (unsigned i = 0; i < package_->modules_.Size(); i++)
  192. {
  193. if (package_->modules_[i]->GetDotNetModule())
  194. continue;
  195. JSModuleWriter writer(package_->modules_[i]);
  196. writer.GenerateSource();
  197. }
  198. }
  199. void JSPackageWriter::PostProcess()
  200. {
  201. JSBind* jsbind = package_->GetSubsystem<JSBind>();
  202. JSBDoc jdoc;
  203. jdoc.Emit(package_, jsbind->GetSourceRootFolder() + "Artifacts/Build/JSDoc/" + package_->GetName() + ".js");
  204. JSBTypeScript ts;
  205. ts.Emit(package_, jsbind->GetSourceRootFolder() + "Script/TypeScript/" + package_->GetName() + ".d.ts");
  206. JSBHaxe hx;
  207. hx.Emit(package_, jsbind->GetSourceRootFolder() + "Script/Haxe/" + package_->GetName() + ".hx");
  208. }
  209. }