JSBindings.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include <Atomic/Atomic.h>
  5. #include <Atomic/IO/Log.h>
  6. #include <Atomic/Core/ProcessUtils.h>
  7. #include <Atomic/Resource/ResourceCache.h>
  8. #include <Atomic/Resource/JSONFile.h>
  9. #include "JSBind.h"
  10. #include "JSBModule.h"
  11. #include "JSBindings.h"
  12. #include "JSBClass.h"
  13. #include "JSBTypeScript.h"
  14. #include "JSBDoc.h"
  15. JSBindings* JSBindings::instance_ = NULL;
  16. void JSBindings::ParseHeaders()
  17. {
  18. for (unsigned i = 0; i < modules_.Size(); i++)
  19. {
  20. modules_[i]->ParseHeaders();
  21. }
  22. for (unsigned i = 0; i < modules_.Size(); i++)
  23. {
  24. modules_[i]->PreprocessHeaders();
  25. }
  26. for (unsigned i = 0; i < modules_.Size(); i++)
  27. {
  28. modules_[i]->VisitHeaders();
  29. }
  30. JSBClass::Preprocess();
  31. JSBClass::Process();
  32. String modulesFolder = "Build/Source/Generated/" + JSBind::PLATFORM + "/Javascript/Modules";
  33. String outputFolder = JSBind::ROOT_FOLDER + "/" + modulesFolder;
  34. if (!JSBind::fileSystem_->CreateDirs(JSBind::ROOT_FOLDER, modulesFolder)
  35. || !JSBind::fileSystem_->DirExists(outputFolder))
  36. {
  37. String error = "Unable to create bindings output folder: " + outputFolder;
  38. ErrorExit(error.CString());
  39. }
  40. EmitJSModules(outputFolder);
  41. if (JSBind::PLATFORM == "MACOSX")
  42. {
  43. JSBTypeScript* ts = new JSBTypeScript();
  44. ts->Emit(JSBind::ROOT_FOLDER + "/Bin/Atomic.d.ts");
  45. JSBDoc* jsdoc = new JSBDoc();
  46. jsdoc->Emit(JSBind::ROOT_FOLDER + "/Bin/Atomic.js");
  47. }
  48. }
  49. void JSBindings::EmitJSModules(const String& rootpath)
  50. {
  51. File file(JSBind::context_);
  52. file.Open(rootpath + "/JSModules.cpp", FILE_WRITE);
  53. String source = "// This file was autogenerated by JSBind, changes will be lost\n\n";
  54. source += "#include <Duktape/duktape.h>\n";
  55. source += "#include <AtomicJS/Javascript/JSVM.h>\n";
  56. source += "#include <AtomicJS/Javascript/JSAPI.h>\n";
  57. source += "\n\nnamespace Atomic\n{\n";
  58. for (unsigned i = 0; i < modules_.Size(); i++)
  59. {
  60. JSBModule* module = modules_.At(i);
  61. source.AppendWithFormat("\nextern void jsb_preinit_%s (JSVM* vm);", module->name_.ToLower().CString());
  62. source.AppendWithFormat("\nextern void jsb_init_%s (JSVM* vm);", module->name_.ToLower().CString());
  63. }
  64. source += "\n\nstatic void jsb_modules_setup_prototypes(JSVM* vm)\n{\n";
  65. source += " // It is important that these are in order so the prototypes are created properly\n";
  66. source += " // This isn't trivial as modules can have dependencies, so do it here\n\n";
  67. JSBClass::WriteProtoTypeSetup(source);
  68. source += "\n}\n";
  69. source += "\n\nvoid jsb_modules_preinit(JSVM* vm)\n{";
  70. for (unsigned i = 0; i < modules_.Size(); i++)
  71. {
  72. JSBModule* module = modules_.At(i);
  73. if (module->Requires("3D"))
  74. source += "\n#ifdef ATOMIC_3D";
  75. source.AppendWithFormat("\n jsb_preinit_%s(vm);", module->name_.ToLower().CString());
  76. if (module->Requires("3D"))
  77. source += "\n#endif //ATOMIC_3D\n";
  78. }
  79. source += "\n}\n\n";
  80. source += "\n\nvoid jsb_modules_init(JSVM* vm)\n{";
  81. source += "\n\n jsb_modules_preinit(vm);\n";
  82. source += "\n\n jsb_modules_setup_prototypes(vm);\n";
  83. for (unsigned i = 0; i < modules_.Size(); i++)
  84. {
  85. JSBModule* module = modules_.At(i);
  86. if (module->Requires("3D"))
  87. source += "\n#ifdef ATOMIC_3D";
  88. source.AppendWithFormat("\n jsb_init_%s(vm);", module->name_.ToLower().CString());
  89. if (module->Requires("3D"))
  90. source += "\n#endif //ATOMIC_3D\n";
  91. }
  92. source += "\n}\n\n";
  93. // end Atomic namespace
  94. source += "\n}\n";
  95. file.Write(source.CString(), source.Length());
  96. file.Close();
  97. for (unsigned i = 0; i < modules_.Size(); i++)
  98. {
  99. String filepath = rootpath + "/JSModule" + modules_.At(i)->name_ + ".cpp";
  100. modules_.At(i)->EmitSource(filepath);
  101. }
  102. }
  103. void JSBindings::RegisterEnum(JSBEnum* jenum)
  104. {
  105. if (enums_.Contains(jenum->name_))
  106. {
  107. LOGERRORF("Enum name collision: %s", jenum->name_.CString());
  108. ErrorExit();
  109. }
  110. for (unsigned i = 0; i < jenum->values_.Size(); i++)
  111. {
  112. if (enumNames_.Contains(jenum->values_[i]))
  113. {
  114. LOGERRORF("Enum value collision: %s", jenum->values_[i].CString());
  115. ErrorExit();
  116. }
  117. else
  118. enumNames_[jenum->values_[i]] = jenum->values_[i];
  119. }
  120. enums_[jenum->name_] = jenum;
  121. }
  122. void JSBindings::RegisterClass(const String &classname, const String &_rename)
  123. {
  124. String rename = _rename;
  125. if (!rename.Length())
  126. rename = classname;
  127. if (classes_.Contains(rename))
  128. {
  129. LOGERRORF("Class name collision: %s", classname.CString());
  130. ErrorExit();
  131. }
  132. classes_[classname] = new JSBClass(rename, classname);
  133. }
  134. void JSBindings::Initialize()
  135. {
  136. ResourceCache* cache = JSBind::context_->GetSubsystem<ResourceCache>();
  137. JSONFile* jsonFile = cache->GetResource<JSONFile>("modules/Modules.json");
  138. JSONValue json = jsonFile->GetRoot();
  139. JSONValue modules = json.GetChild("modules");
  140. List<String> moduleExclusions;
  141. if (JSBind::PLATFORM == "WEB")
  142. {
  143. JSONValue jmodulesExclude = json.GetChild("moduleExclude");
  144. JSONValue jexcludes = jmodulesExclude.GetChild("WEB");
  145. for (unsigned i = 0; i < jexcludes.GetSize(); i++)
  146. {
  147. moduleExclusions.Push(jexcludes.GetString(i));
  148. }
  149. }
  150. for (unsigned i = 0; i < modules.GetSize(); i++)
  151. {
  152. if (moduleExclusions.Contains(modules.GetString(i)))
  153. continue;
  154. String moduleName = "modules/" + modules.GetString(i) + ".json";
  155. JSBModule* jsbModule = new JSBModule(this);
  156. jsbModule->Load(moduleName);
  157. modules_.Push(jsbModule);
  158. }
  159. }
  160. JSBModule* JSBindings::GetModuleByName(const String& name)
  161. {
  162. for (unsigned i = 0; i < modules_.Size(); i++)
  163. if (modules_[i]->name_ == name)
  164. return modules_[i];
  165. return NULL;
  166. }