JSBModule.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #ifdef WIN32
  139. headerPath.Replace(JSBind::ROOT_FOLDER + "/Source/Atomic/", "");
  140. #else
  141. headerPath.Replace(JSBind::ROOT_FOLDER + "/Source/Atomic/", "Atomic/");
  142. #endif
  143. source.AppendWithFormat("#include <%s%s>\n", headerPath.CString(), headerfile.CString());
  144. included.Push(header);
  145. }
  146. for (unsigned i = 0; i < includes_.Size(); i++)
  147. {
  148. if (includes_[i].StartsWith("<"))
  149. source.AppendWithFormat("#include %s\n", includes_[i].CString());
  150. else
  151. source.AppendWithFormat("#include \"%s\"\n", includes_[i].CString());
  152. }
  153. }
  154. void JSBModule::EmitSource(const String& filepath)
  155. {
  156. File file(JSBind::context_);
  157. file.Open(filepath, FILE_WRITE);
  158. source_ = "// This file was autogenerated by JSBind, changes will be lost\n";
  159. source_ += "#include <Duktape/duktape.h>\n";
  160. source_ += "#include <AtomicJS/Javascript/JSVM.h>\n";
  161. source_ += "#include <AtomicJS/Javascript/JSAPI.h>\n";
  162. WriteIncludes(source_);
  163. source_ += "\n\nnamespace Atomic\n{\n \n";
  164. source_ += "// Begin Class Declarations\n";
  165. for (unsigned i = 0; i < classes_.Size(); i++)
  166. {
  167. classes_[i]->WriteForwardDeclarations(source_);
  168. }
  169. source_ += "// End Class Declarations\n\n";
  170. source_ += "// Begin Classes\n";
  171. for (unsigned i = 0; i < classes_.Size(); i++)
  172. {
  173. classes_[i]->Write(source_);
  174. }
  175. source_ += "// End Classes\n\n";
  176. WriteClassDeclaration(source_);
  177. WriteClassDefine(source_);
  178. WriteModulePreInit(source_);
  179. WriteModuleInit(source_);
  180. // end Atomic namespace
  181. source_ += "\n}\n";
  182. file.Write(source_.CString(), source_.Length());
  183. file.Close();
  184. }
  185. void JSBModule::Load(const String &moduleJSONFilename)
  186. {
  187. ResourceCache* cache = JSBind::context_->GetSubsystem<ResourceCache>();
  188. JSONFile* moduleJSONFile = cache->GetResource<JSONFile>(moduleJSONFilename);
  189. if (!moduleJSONFile)
  190. {
  191. LOGERRORF("Couldn't load module json: %s", moduleJSONFilename.CString());
  192. ErrorExit("Couldn't load module json");
  193. }
  194. JSONValue moduleJSON = moduleJSONFile->GetRoot();
  195. JSONValue sources = moduleJSON.GetChild("sources");
  196. JSONValue classes = moduleJSON.GetChild("classes");
  197. JSONValue includes = moduleJSON.GetChild("includes");
  198. JSONValue classes_rename = moduleJSON.GetChild("classes_rename");
  199. JSONValue overloads = moduleJSON.GetChild("overloads");
  200. HashMap<String, String> rename;
  201. if (classes_rename.IsObject())
  202. {
  203. Vector<String> childNames = classes_rename.GetValueNames();
  204. for (unsigned j = 0; j < childNames.Size(); j++)
  205. {
  206. String classname = childNames.At(j);
  207. String crename = classes_rename.GetString(classname);
  208. rename[classname] = crename;
  209. }
  210. }
  211. if (includes.IsArray())
  212. {
  213. for (unsigned j = 0; j < includes.GetSize(); j++)
  214. {
  215. includes_.Push(includes.GetString(j));
  216. }
  217. }
  218. if (classes.IsArray())
  219. {
  220. for (unsigned j = 0; j < classes.GetSize(); j++)
  221. {
  222. String classname = classes.GetString(j);
  223. if (rename.Contains(classname))
  224. bindings_->RegisterClass(classname, rename[classname]);
  225. else
  226. bindings_->RegisterClass(classname);
  227. }
  228. }
  229. if (overloads.IsObject())
  230. {
  231. Vector<String> childNames = overloads.GetChildNames();
  232. for (unsigned j = 0; j < childNames.Size(); j++)
  233. {
  234. String classname = childNames.At(j);
  235. JSBClass* klass = bindings_->GetClass(classname);
  236. if (!klass)
  237. {
  238. ErrorExit("Bad overload klass");
  239. }
  240. JSONValue classoverloads = overloads.GetChild(classname);
  241. Vector<String> functionNames = classoverloads.GetChildNames();
  242. for (unsigned k = 0; k < functionNames.Size(); k++)
  243. {
  244. JSONValue sig = classoverloads.GetChild(functionNames[k]);
  245. if (!sig.IsArray())
  246. {
  247. ErrorExit("Bad overload defintion");
  248. }
  249. Vector<String> values;
  250. for (unsigned x = 0; x < sig.GetSize(); x++)
  251. {
  252. values.Push(sig.GetString(x));
  253. }
  254. JSBFunctionOverride* fo = new JSBFunctionOverride(functionNames[k], values);
  255. klass->AddFunctionOverride(fo);
  256. }
  257. }
  258. }
  259. this->name_ = moduleJSON.GetString("name");
  260. if (this->name_ == "Graphics")
  261. {
  262. #ifdef _MSC_VER
  263. sources.AddString("Graphics/Direct3D9");
  264. #else
  265. sources.AddString("Graphics/OpenGL");
  266. #endif
  267. }
  268. for (unsigned j = 0; j < sources.GetSize(); j++)
  269. {
  270. String sourceFolder = sources.GetString(j);
  271. Vector<String> fileNames;
  272. JSBind::fileSystem_->ScanDir(fileNames, JSBind::ROOT_FOLDER + "/Source/Atomic/" + sourceFolder, "*.h", SCAN_FILES, false);
  273. for (unsigned k = 0; k < fileNames.Size(); k++)
  274. {
  275. // TODO: filter
  276. String filepath = JSBind::ROOT_FOLDER + "/Source/Atomic/" + sourceFolder + "/" + fileNames[k];
  277. this->headerFiles_.Push(filepath);
  278. }
  279. }
  280. }