JSPackageWriter.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 <AtomicJS/Javascript/JSVM.h>\n";
  98. source += "#include <AtomicJS/Javascript/JSAPI.h>\n";
  99. source += "\n\nnamespace Atomic\n{\n";
  100. String packageLower = package_->GetName().ToLower();
  101. for (unsigned i = 0; i < package_->modules_.Size(); i++)
  102. {
  103. JSBModule* module = package_->modules_.At(i);
  104. if (module->GetDotNetModule())
  105. continue;
  106. String moduleGuard = module->GetModuleDefineGuard();
  107. if (moduleGuard.Length())
  108. {
  109. source += ToString("\n%s\n", moduleGuard.CString());
  110. }
  111. String moduleLower = module->GetName().ToLower();
  112. source.AppendWithFormat("\nextern void jsb_package_%s_preinit_%s (JSVM* vm);", packageLower.CString(), moduleLower.CString());
  113. source.AppendWithFormat("\nextern void jsb_package_%s_init_%s (JSVM* vm);", packageLower.CString(), moduleLower.CString());
  114. if (moduleGuard.Length())
  115. {
  116. source += ToString("\n#endif\n", moduleGuard.CString());
  117. }
  118. }
  119. source += "\n\nstatic void jsb_modules_setup_prototypes(JSVM* vm)\n{\n";
  120. source += " // It is important that these are in order so the prototypes are created properly\n";
  121. source += " // This isn't trivial as modules can have dependencies, so do it here\n\n";
  122. WriteProtoTypeSetup(source);
  123. source += "\n}\n";
  124. source.AppendWithFormat("\n\nstatic void jsb_package_%s_preinit(JSVM* vm)\n{", packageLower.CString());
  125. source.Append("\n // Create the global package object\n");
  126. source.Append(" duk_context* ctx = vm->GetJSContext();\n");
  127. source.Append(" duk_push_object(ctx);\n");
  128. source.AppendWithFormat(" duk_put_global_string(ctx, \"%s\");\n", package_->GetName().CString());
  129. for (unsigned i = 0; i < package_->modules_.Size(); i++)
  130. {
  131. JSBModule* module = package_->modules_.At(i);
  132. if (module->GetDotNetModule())
  133. continue;
  134. String moduleGuard = module->GetModuleDefineGuard();
  135. if (moduleGuard.Length())
  136. {
  137. source += ToString("\n%s\n", moduleGuard.CString());
  138. }
  139. if (module->Requires("3D"))
  140. source += "\n#ifdef ATOMIC_3D";
  141. String moduleLower = module->GetName().ToLower();
  142. source.AppendWithFormat("\n jsb_package_%s_preinit_%s(vm);", packageLower.CString(), moduleLower.CString());
  143. if (module->Requires("3D"))
  144. source += "\n#endif //ATOMIC_3D\n";
  145. if (moduleGuard.Length())
  146. {
  147. source += ToString("\n#endif\n", moduleGuard.CString());
  148. }
  149. }
  150. source += "\n}\n\n";
  151. source.AppendWithFormat("\n\nvoid jsb_package_%s_init(JSVM* vm)\n{", packageLower.CString());
  152. source.AppendWithFormat("\n\n jsb_package_%s_preinit(vm);\n", packageLower.CString());
  153. source += "\n\n jsb_modules_setup_prototypes(vm);\n";
  154. for (unsigned i = 0; i < package_->modules_.Size(); i++)
  155. {
  156. JSBModule* module = package_->modules_.At(i);
  157. if (module->GetDotNetModule())
  158. continue;
  159. String moduleLower = module->GetName().ToLower();
  160. String moduleGuard = module->GetModuleDefineGuard();
  161. if (moduleGuard.Length())
  162. {
  163. source += ToString("\n%s\n", moduleGuard.CString());
  164. }
  165. if (module->Requires("3D"))
  166. source += "\n#ifdef ATOMIC_3D";
  167. source.AppendWithFormat("\n jsb_package_%s_init_%s(vm);", packageLower.CString(), moduleLower.CString());
  168. if (module->Requires("3D"))
  169. {
  170. source += "\n#endif //ATOMIC_3D\n";
  171. }
  172. if (moduleGuard.Length())
  173. {
  174. source += ToString("\n#endif\n", moduleGuard.CString());
  175. }
  176. }
  177. source += "\n}\n\n";
  178. // end Atomic namespace
  179. source += "\n}\n";
  180. if (defineGuard.Length())
  181. {
  182. source += "\n#endif\n";
  183. }
  184. JSBind* jsbind = package_->GetSubsystem<JSBind>();
  185. String filepath = jsbind->GetDestNativeFolder() + "/JSPackage" + package_->name_ + ".cpp";
  186. File file(package_->GetContext());
  187. file.Open(filepath, FILE_WRITE);
  188. file.Write(source.CString(), source.Length());
  189. file.Close();
  190. for (unsigned i = 0; i < package_->modules_.Size(); i++)
  191. {
  192. if (package_->modules_[i]->GetDotNetModule())
  193. continue;
  194. JSModuleWriter writer(package_->modules_[i]);
  195. writer.GenerateSource();
  196. }
  197. }
  198. void JSPackageWriter::PostProcess()
  199. {
  200. JSBind* jsbind = package_->GetSubsystem<JSBind>();
  201. JSBDoc jdoc;
  202. jdoc.Emit(package_, jsbind->GetSourceRootFolder() + "Artifacts/Build/JSDoc/" + package_->GetName() + ".js");
  203. JSBTypeScript ts;
  204. ts.Emit(package_, jsbind->GetSourceRootFolder() + "Script/TypeScript/" + package_->GetName() + ".d.ts");
  205. JSBHaxe hx;
  206. hx.Emit(package_, jsbind->GetSourceRootFolder() + "Script/Haxe/" + package_->GetName() + ".hx");
  207. }
  208. }