JSBModule.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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/Resource/JSONFile.h>
  7. #include <Atomic/Resource/ResourceCache.h>
  8. #include <Atomic/Core/ProcessUtils.h>
  9. #include "JSBind.h"
  10. #include "JSBindings.h"
  11. #include "JSBHeader.h"
  12. #include "JSBModule.h"
  13. #include "JSBFunction.h"
  14. void JSBModule::ParseHeaders()
  15. {
  16. for (unsigned i = 0; i < headerFiles_.Size(); i++)
  17. {
  18. JSBHeader* header = new JSBHeader(this, headerFiles_.At(i));
  19. headers_.Push(header);
  20. header->Parse();
  21. }
  22. }
  23. void JSBModule::PreprocessHeaders()
  24. {
  25. for (unsigned i = 0; i < headers_.Size(); i++)
  26. {
  27. headers_[i]->VisitPreprocess();
  28. }
  29. }
  30. void JSBModule::VisitHeaders()
  31. {
  32. for (unsigned i = 0; i < headers_.Size(); i++)
  33. {
  34. headers_[i]->VisitHeader();
  35. }
  36. }
  37. void JSBModule::WriteClassDeclaration(String& source)
  38. {
  39. source += "static void jsb_declare_classes(JSVM* vm)\n{\n";
  40. source += "duk_context* ctx = vm->GetJSContext();\n";
  41. for (unsigned i = 0; i < classes_.Size(); i++)
  42. {
  43. JSBClass* klass = classes_.At(i);
  44. if (klass->isNumberArray())
  45. continue;
  46. source.AppendWithFormat(" js_class_declare(vm, \"%s\", jsb_constructor_%s);\n", klass->GetName().CString(), klass->GetName().CString());
  47. if (klass->hasProperties())
  48. {
  49. source.AppendWithFormat("js_class_push_propertyobject(vm, \"%s\");\n", klass->GetName().CString());
  50. Vector<String> pnames;
  51. klass->GetPropertyNames(pnames);
  52. for (unsigned j = 0; j < pnames.Size(); j++)
  53. {
  54. JSBProperty* prop = klass->GetProperty(pnames[j]);
  55. source.Append("duk_push_object(ctx);\n");
  56. if (prop->getter_ && !prop->getter_->Skip())
  57. {
  58. source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, 0);\n",
  59. klass->GetName().CString(), prop->getter_->name_.CString());
  60. source.Append("duk_put_prop_string(ctx, -2, \"get\");\n");
  61. }
  62. if (prop->setter_ && !prop->setter_->Skip())
  63. {
  64. source.AppendWithFormat("duk_push_c_function(ctx, jsb_class_%s_%s, 1);\n",
  65. klass->GetName().CString(), prop->setter_->name_.CString());
  66. source.Append("duk_put_prop_string(ctx, -2, \"set\");\n");
  67. }
  68. pnames[j][0] = tolower(pnames[j][0]);
  69. source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", pnames[j].CString());
  70. }
  71. source.Append("duk_pop(ctx);\n");
  72. }
  73. }
  74. source += "\n}\n\n";
  75. }
  76. void JSBModule::WriteClassDefine(String& source)
  77. {
  78. source += "static void jsb_init_classes(JSVM* vm)\n{\n";
  79. for (unsigned i = 0; i < classes_.Size(); i++)
  80. {
  81. JSBClass* klass = classes_.At(i);
  82. if (klass->isNumberArray())
  83. continue;
  84. source.AppendWithFormat(" jsb_class_define_%s(vm);\n", klass->GetName().CString());
  85. }
  86. source += "\n}\n\n";
  87. }
  88. void JSBModule::WriteModulePreInit(String& source)
  89. {
  90. source.AppendWithFormat("\nvoid jsb_preinit_%s (JSVM* vm)\n{\n\njsb_declare_classes(vm);\n", name_.ToLower().CString());
  91. // register enums and constants
  92. source += "// enums and constants\n";
  93. source += "duk_context* ctx = vm->GetJSContext();\n";
  94. source += "duk_get_global_string(ctx, \"Atomic\");\n";
  95. source += "// enums\n";
  96. for (unsigned i = 0; i < enums_.Size(); i++)
  97. {
  98. JSBEnum* jenum = enums_[i];
  99. for (unsigned k = 0; k < jenum->values_.Size(); k++)
  100. {
  101. source.AppendWithFormat("duk_push_number(ctx, (double) %s);\n", jenum->values_.At(k).CString());
  102. source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", jenum->values_.At(k).CString());
  103. }
  104. }
  105. source += "// constants\n";
  106. for (unsigned i = 0; i < constants_.Size(); i++)
  107. {
  108. source.AppendWithFormat("duk_push_number(ctx, (double) %s);\n", constants_.At(i).CString());
  109. source.AppendWithFormat("duk_put_prop_string(ctx, -2, \"%s\");\n", constants_.At(i).CString());
  110. }
  111. source += "duk_pop(ctx);\n";
  112. source += "// end enums and constants\n";
  113. source += "\n}\n";
  114. }
  115. void JSBModule::WriteModuleInit(String& source)
  116. {
  117. source.AppendWithFormat("\nvoid jsb_init_%s (JSVM* vm)\n{\n\n jsb_init_classes(vm);\n\n}\n\n", name_.ToLower().CString());
  118. }
  119. void JSBModule::WriteIncludes(String& source)
  120. {
  121. Vector<JSBHeader*> allheaders;
  122. for (unsigned i = 0; i < enums_.Size(); i++)
  123. {
  124. allheaders.Push(enums_.At(i)->header_);
  125. }
  126. for (unsigned i = 0; i < classes_.Size(); i++)
  127. {
  128. allheaders.Push(classes_.At(i)->GetHeader());
  129. }
  130. Vector<JSBHeader*> included;
  131. for (unsigned i = 0; i < allheaders.Size(); i++)
  132. {
  133. JSBHeader* header = allheaders.At(i);
  134. if (included.Contains(header))
  135. continue;
  136. String headerPath = GetPath(header->filepath_);
  137. String headerfile = GetFileNameAndExtension(header->filepath_);
  138. headerPath.Replace(JSBind::ROOT_FOLDER + "/Source/Atomic/", "Atomic/");
  139. source.AppendWithFormat("#include <%s%s>\n", headerPath.CString(), headerfile.CString());
  140. included.Push(header);
  141. }
  142. for (unsigned i = 0; i < includes_.Size(); i++)
  143. {
  144. if (includes_[i].StartsWith("<"))
  145. source.AppendWithFormat("#include %s\n", includes_[i].CString());
  146. else
  147. source.AppendWithFormat("#include \"%s\"\n", includes_[i].CString());
  148. }
  149. }
  150. void JSBModule::EmitSource(const String& filepath)
  151. {
  152. File file(JSBind::context_);
  153. file.Open(filepath, FILE_WRITE);
  154. source_ = "// This file was autogenerated by JSBind, changes will be lost\n";
  155. source_ += "#ifdef ATOMIC_PLATFORM_WINDOWS\n";
  156. source_ += "#pragma warning(disable: 4244) // possible loss of data\n";
  157. source_ += "#endif\n";
  158. if (Requires("3D"))
  159. {
  160. source_ += "#ifdef ATOMIC_3D\n";
  161. }
  162. source_ += "#include <Duktape/duktape.h>\n";
  163. source_ += "#include <AtomicJS/Javascript/JSVM.h>\n";
  164. source_ += "#include <AtomicJS/Javascript/JSAPI.h>\n";
  165. WriteIncludes(source_);
  166. source_ += "\n\nnamespace Atomic\n{\n \n";
  167. source_ += "// Begin Class Declarations\n";
  168. for (unsigned i = 0; i < classes_.Size(); i++)
  169. {
  170. classes_[i]->WriteForwardDeclarations(source_);
  171. }
  172. source_ += "// End Class Declarations\n\n";
  173. source_ += "// Begin Classes\n";
  174. for (unsigned i = 0; i < classes_.Size(); i++)
  175. {
  176. classes_[i]->Write(source_);
  177. }
  178. source_ += "// End Classes\n\n";
  179. WriteClassDeclaration(source_);
  180. WriteClassDefine(source_);
  181. WriteModulePreInit(source_);
  182. WriteModuleInit(source_);
  183. // end Atomic namespace
  184. source_ += "\n}\n";
  185. if (Requires("3D"))
  186. {
  187. source_ += "#endif //ATOMIC_3D\n";
  188. }
  189. file.Write(source_.CString(), source_.Length());
  190. file.Close();
  191. }
  192. void JSBModule::Load(const String &moduleJSONFilename)
  193. {
  194. ResourceCache* cache = JSBind::context_->GetSubsystem<ResourceCache>();
  195. JSONFile* moduleJSONFile = cache->GetResource<JSONFile>(moduleJSONFilename);
  196. if (!moduleJSONFile)
  197. {
  198. LOGERRORF("Couldn't load module json: %s", moduleJSONFilename.CString());
  199. ErrorExit("Couldn't load module json");
  200. }
  201. JSONValue moduleJSON = moduleJSONFile->GetRoot();
  202. JSONValue sources = moduleJSON.GetChild("sources");
  203. JSONValue classes = moduleJSON.GetChild("classes");
  204. JSONValue includes = moduleJSON.GetChild("includes");
  205. JSONValue classes_rename = moduleJSON.GetChild("classes_rename");
  206. JSONValue overloads = moduleJSON.GetChild("overloads");
  207. JSONValue requires = moduleJSON.GetChild("requires");
  208. HashMap<String, String> rename;
  209. if (requires.IsArray())
  210. {
  211. for (unsigned j = 0; j < requires.GetSize(); j++)
  212. {
  213. requirements_.Push(requires.GetString(j));
  214. }
  215. }
  216. if (classes_rename.IsObject())
  217. {
  218. Vector<String> childNames = classes_rename.GetValueNames();
  219. for (unsigned j = 0; j < childNames.Size(); j++)
  220. {
  221. String classname = childNames.At(j);
  222. String crename = classes_rename.GetString(classname);
  223. rename[classname] = crename;
  224. }
  225. }
  226. if (includes.IsArray())
  227. {
  228. for (unsigned j = 0; j < includes.GetSize(); j++)
  229. {
  230. includes_.Push(includes.GetString(j));
  231. }
  232. }
  233. if (classes.IsArray())
  234. {
  235. for (unsigned j = 0; j < classes.GetSize(); j++)
  236. {
  237. String classname = classes.GetString(j);
  238. if (rename.Contains(classname))
  239. bindings_->RegisterClass(classname, rename[classname]);
  240. else
  241. bindings_->RegisterClass(classname);
  242. }
  243. }
  244. if (overloads.IsObject())
  245. {
  246. Vector<String> childNames = overloads.GetChildNames();
  247. for (unsigned j = 0; j < childNames.Size(); j++)
  248. {
  249. String classname = childNames.At(j);
  250. JSBClass* klass = bindings_->GetClass(classname);
  251. if (!klass)
  252. {
  253. ErrorExit("Bad overload klass");
  254. }
  255. JSONValue classoverloads = overloads.GetChild(classname);
  256. Vector<String> functionNames = classoverloads.GetChildNames();
  257. for (unsigned k = 0; k < functionNames.Size(); k++)
  258. {
  259. JSONValue sig = classoverloads.GetChild(functionNames[k]);
  260. if (!sig.IsArray())
  261. {
  262. ErrorExit("Bad overload defintion");
  263. }
  264. Vector<String> values;
  265. for (unsigned x = 0; x < sig.GetSize(); x++)
  266. {
  267. values.Push(sig.GetString(x));
  268. }
  269. JSBFunctionOverride* fo = new JSBFunctionOverride(functionNames[k], values);
  270. klass->AddFunctionOverride(fo);
  271. }
  272. }
  273. }
  274. this->name_ = moduleJSON.GetString("name");
  275. if (this->name_ == "Graphics")
  276. {
  277. #ifdef _MSC_VER
  278. if (JSBind::PLATFORM == "ANDROID" || JSBind::PLATFORM == "WEB")
  279. {
  280. sources.AddString("Graphics/OpenGL");
  281. }
  282. else
  283. {
  284. #ifdef ATOMIC_D3D11
  285. sources.AddString("Graphics/Direct3D11");
  286. #else
  287. sources.AddString("Graphics/Direct3D9");
  288. #endif
  289. }
  290. #else
  291. sources.AddString("Graphics/OpenGL");
  292. #endif
  293. }
  294. for (unsigned j = 0; j < sources.GetSize(); j++)
  295. {
  296. String sourceFolder = sources.GetString(j);
  297. Vector<String> fileNames;
  298. String sourceRoot = "Atomic";
  299. if (sourceFolder == "Javascript")
  300. sourceRoot = "AtomicJS";
  301. JSBind::fileSystem_->ScanDir(fileNames, JSBind::ROOT_FOLDER + "/Source/" + sourceRoot + "/" + sourceFolder, "*.h", SCAN_FILES, false);
  302. for (unsigned k = 0; k < fileNames.Size(); k++)
  303. {
  304. // TODO: filter
  305. String filepath = JSBind::ROOT_FOLDER + "/Source/" + sourceRoot + "/" + sourceFolder + "/" + fileNames[k];
  306. this->headerFiles_.Push(filepath);
  307. }
  308. }
  309. }