JSModuleWriter.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 "../JSBPackage.h"
  26. #include "../JSBModule.h"
  27. #include "../JSBEnum.h"
  28. #include "../JSBClass.h"
  29. #include "../JSBFunction.h"
  30. #include "JSModuleWriter.h"
  31. #include "JSClassWriter.h"
  32. namespace ToolCore
  33. {
  34. JSModuleWriter::JSModuleWriter(JSBModule *module) : JSBModuleWriter(module)
  35. {
  36. }
  37. void JSModuleWriter::WriteForwardDeclarations(String& source)
  38. {
  39. Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
  40. for (unsigned i = 0; i < classes.Size(); i++)
  41. {
  42. JSBClass* cls = classes.At(i);
  43. if (cls->IsNumberArray())
  44. continue;
  45. source.AppendWithFormat("static duk_ret_t jsb_constructor_%s(duk_context* ctx);\n", cls->GetName().CString());
  46. source.AppendWithFormat("static void jsb_class_define_%s(JSVM* vm);\n", cls->GetName().CString());
  47. }
  48. }
  49. void JSModuleWriter::WriteClassDeclaration(String& source)
  50. {
  51. Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
  52. source += "static void jsb_declare_classes(JSVM* vm)\n{\n";
  53. source += "duk_context* ctx = vm->GetJSContext();\n";
  54. String packageName = module_->GetPackage()->GetName();
  55. for (unsigned i = 0; i < classes.Size(); i++)
  56. {
  57. JSBClass* klass = classes.At(i);
  58. if (klass->IsNumberArray())
  59. continue;
  60. source.AppendWithFormat(" js_class_declare<%s>(vm, \"%s\", \"%s\", jsb_constructor_%s);\n", klass->GetNativeName().CString(), packageName.CString(), klass->GetName().CString(), klass->GetName().CString());
  61. if (klass->HasProperties())
  62. {
  63. source.AppendWithFormat("js_class_push_propertyobject(vm, \"%s\", \"%s\");\n", packageName.CString(), klass->GetName().CString());
  64. Vector<String> pnames;
  65. klass->GetPropertyNames(pnames);
  66. for (unsigned j = 0; j < pnames.Size(); j++)
  67. {
  68. JSBProperty* prop = klass->GetProperty(pnames[j]);
  69. source.Append("duk_push_object(ctx);\n");
  70. if (prop->getter_ && !prop->getter_->Skip(BINDINGLANGUAGE_JAVASCRIPT))
  71. {
  72. source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, 0);\n",
  73. klass->GetName().CString(), prop->getter_->GetName().CString());
  74. source.Append("duk_put_prop_string(ctx, -2, \"get\");\n");
  75. }
  76. if (prop->setter_ && !prop->setter_->Skip(BINDINGLANGUAGE_JAVASCRIPT))
  77. {
  78. source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, 1);\n",
  79. klass->GetName().CString(), prop->setter_->GetName().CString());
  80. source.Append("duk_put_prop_string(ctx, -2, \"set\");\n");
  81. }
  82. String propertyName = prop->GetCasePropertyName();
  83. source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", propertyName.CString());
  84. }
  85. source.Append("duk_pop(ctx);\n");
  86. }
  87. }
  88. source += "\n}\n\n";
  89. }
  90. void JSModuleWriter::WriteIncludes(String& source)
  91. {
  92. Vector<String>& includes = module_->includes_;
  93. for (unsigned i = 0; i < includes.Size(); i++)
  94. {
  95. if (includes[i].StartsWith("<"))
  96. source.AppendWithFormat("#include %s\n", includes[i].CString());
  97. else
  98. source.AppendWithFormat("#include \"%s\"\n", includes[i].CString());
  99. }
  100. Vector<JSBHeader*> allheaders;
  101. HashMap<StringHash, SharedPtr<JSBEnum> >::Iterator eitr = module_->enums_.Begin();
  102. while (eitr != module_->enums_.End())
  103. {
  104. allheaders.Push(eitr->second_->GetHeader());
  105. eitr++;
  106. }
  107. HashMap<StringHash, SharedPtr<JSBClass> >::Iterator citr = module_->classes_.Begin();
  108. while (citr != module_->classes_.End())
  109. {
  110. allheaders.Push(citr->second_->GetHeader());
  111. citr++;
  112. }
  113. Vector<JSBHeader*> included;
  114. for (unsigned i = 0; i < allheaders.Size(); i++)
  115. {
  116. JSBHeader* header = allheaders.At(i);
  117. if (included.Contains(header))
  118. continue;
  119. String headerPath = GetPath(header->GetFilePath());
  120. String headerfile = GetFileNameAndExtension(header->GetFilePath());
  121. JSBind* jsbind = header->GetSubsystem<JSBind>();
  122. headerPath.Replace(jsbind->GetSourceRootFolder() + "Source/", "");
  123. source.AppendWithFormat("#include <%s%s>\n", headerPath.CString(), headerfile.CString());
  124. included.Push(header);
  125. }
  126. }
  127. void JSModuleWriter::WritePreamble(String& source)
  128. {
  129. if (!module_->jsmodulePreamble_.Size())
  130. return;
  131. source += "\n// Begin Module Preamble\n\n";
  132. for (unsigned i = 0; i < module_->jsmodulePreamble_.Size(); i++)
  133. {
  134. source += module_->jsmodulePreamble_[i] + "\n";
  135. }
  136. source += "\n// End Module Preamble\n\n";
  137. }
  138. void JSModuleWriter::WriteClassDefine(String& source)
  139. {
  140. Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
  141. source += "static void jsb_init_classes(JSVM* vm)\n{\n";
  142. for (unsigned i = 0; i < classes.Size(); i++)
  143. {
  144. JSBClass* klass = classes.At(i);
  145. if (klass->IsNumberArray())
  146. continue;
  147. source.AppendWithFormat(" jsb_class_define_%s(vm);\n", klass->GetName().CString());
  148. }
  149. source += "\n}\n\n";
  150. }
  151. void JSModuleWriter::WriteModulePreInit(String& source)
  152. {
  153. source.AppendWithFormat("\nvoid jsb_package_%s_preinit_%s (JSVM* vm)\n{\n\njsb_declare_classes(vm);\n",
  154. module_->package_->GetName().ToLower().CString(), module_->GetName().ToLower().CString());
  155. // register enums and constants
  156. source += "// enums and constants\n";
  157. source += "duk_context* ctx = vm->GetJSContext();\n";
  158. source.AppendWithFormat("duk_get_global_string(ctx, \"%s\");\n", module_->package_->GetName().CString());
  159. source += "// enums\n";
  160. Vector<SharedPtr<JSBEnum>> enums = module_->enums_.Values();
  161. for (unsigned i = 0; i < enums.Size(); i++)
  162. {
  163. JSBEnum* jenum = enums[i];
  164. HashMap<String, String>& values = jenum->GetValues();
  165. HashMap<String, String>::ConstIterator itr = values.Begin();
  166. while (itr != values.End())
  167. {
  168. String name = (*itr).first_;
  169. source.AppendWithFormat("duk_push_number(ctx, (double) %s);\n", name.CString());
  170. source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n",name.CString());
  171. itr++;
  172. }
  173. }
  174. source += "// constants\n";
  175. Vector<String> constants = module_->constants_.Keys();
  176. for (unsigned i = 0; i < constants.Size(); i++)
  177. {
  178. source.AppendWithFormat("duk_push_number(ctx, (double) %s);\n", constants.At(i).CString());
  179. source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", constants.At(i).CString());
  180. }
  181. source += "duk_pop(ctx);\n";
  182. source += "// end enums and constants\n";
  183. source += "\n}\n";
  184. }
  185. void JSModuleWriter::WriteModuleInit(String& source)
  186. {
  187. source.AppendWithFormat("\nvoid jsb_package_%s_init_%s (JSVM* vm)\n{\n\n jsb_init_classes(vm);\n\n}\n\n",
  188. module_->package_->GetName().ToLower().CString(), module_->name_.ToLower().CString());
  189. }
  190. void JSModuleWriter::GenerateSource()
  191. {
  192. String source = "// This file was autogenerated by JSBind, changes will be lost\n";
  193. String moduleGuard = module_->GetModuleDefineGuard();
  194. if (moduleGuard.Length())
  195. {
  196. source += ToString("\n%s\n", moduleGuard.CString());
  197. }
  198. source += "#ifdef ATOMIC_PLATFORM_WINDOWS\n";
  199. source += "#pragma warning(disable: 4244) // possible loss of data\n";
  200. source += "#endif\n";
  201. if (module_->Requires("3D"))
  202. {
  203. source += "#ifdef ATOMIC_3D\n";
  204. }
  205. source += "#include <Duktape/duktape.h>\n";
  206. source += "#include <AtomicJS/Javascript/JSVM.h>\n";
  207. source += "#include <AtomicJS/Javascript/JSAPI.h>\n";
  208. WriteIncludes(source);
  209. WritePreamble(source);
  210. String ns = module_->GetPackage()->GetNamespace();
  211. if (ns != "Atomic")
  212. {
  213. source += "\n\nusing namespace " + ns + ";\n\n";
  214. }
  215. source += "\n\nnamespace Atomic\n{\n \n";
  216. source += "// Begin Class Declarations\n";
  217. WriteForwardDeclarations(source);
  218. source += "// End Class Declarations\n\n";
  219. source += "// Begin Classes\n";
  220. Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
  221. for (unsigned i = 0; i < classes.Size(); i++)
  222. {
  223. JSClassWriter clsWriter(classes[i]);
  224. clsWriter.GenerateSource(source);
  225. }
  226. source += "// End Classes\n\n";
  227. WriteClassDeclaration(source);
  228. WriteClassDefine(source);
  229. WriteModulePreInit(source);
  230. WriteModuleInit(source);
  231. // end Atomic namespace
  232. source += "\n}\n";
  233. if (module_->Requires("3D"))
  234. {
  235. source += "#endif //ATOMIC_3D\n";
  236. }
  237. if (moduleGuard.Length())
  238. {
  239. source += ToString("\n#endif\n", moduleGuard.CString());
  240. }
  241. JSBind* jsbind = module_->GetSubsystem<JSBind>();
  242. String filepath = jsbind->GetDestNativeFolder() + "/JSModule" + module_->name_ + ".cpp";
  243. File file(module_->GetContext());
  244. file.Open(filepath, FILE_WRITE);
  245. file.Write(source.CString(), source.Length());
  246. file.Close();
  247. }
  248. }