JSPackageWriter.cpp 7.3 KB

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