JSModuleWriter.cpp 12 KB

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