JSBModule.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/IO/Log.h>
  8. #include <Atomic/IO/File.h>
  9. #include <Atomic/IO/FileSystem.h>
  10. #include <Atomic/Resource/JSONFile.h>
  11. #include "Atomic/Core/ProcessUtils.h"
  12. #include "JSBind.h"
  13. #include "JSBPackage.h"
  14. #include "JSBModule.h"
  15. #include "JSBHeader.h"
  16. #include "JSBClass.h"
  17. #include "JSBEnum.h"
  18. #include "JSBModuleWriter.h"
  19. #include "JSBType.h"
  20. namespace ToolCore
  21. {
  22. JSBModule::JSBModule(Context* context, JSBPackage* package) : Object(context),
  23. package_(package)
  24. {
  25. }
  26. JSBModule::~JSBModule()
  27. {
  28. }
  29. Vector<SharedPtr<JSBClass>> JSBModule::GetClasses()
  30. {
  31. return classes_.Values();
  32. }
  33. Vector<SharedPtr<JSBEnum>> JSBModule::GetEnums()
  34. {
  35. return enums_.Values();
  36. }
  37. void JSBModule::PreprocessHeaders()
  38. {
  39. for (unsigned i = 0; i < headers_.Size(); i++)
  40. {
  41. headers_[i]->VisitPreprocess();
  42. }
  43. }
  44. void JSBModule::VisitHeaders()
  45. {
  46. for (unsigned i = 0; i < headers_.Size(); i++)
  47. {
  48. headers_[i]->VisitHeader();
  49. }
  50. // validate that all classes found
  51. for (unsigned i = 0; i < classnames_.Size(); i++)
  52. {
  53. JSBClass* cls = GetClass(classnames_[i]);
  54. if (!cls)
  55. {
  56. ErrorExit(ToString("Module class not found %s", classnames_[i].CString()));
  57. }
  58. }
  59. ProcessOverloads();
  60. ProcessExcludes();
  61. ProcessTypeScriptDecl();
  62. ProcessHaxeDecl();
  63. }
  64. void JSBModule::PreprocessClasses()
  65. {
  66. HashMap<StringHash, SharedPtr<JSBClass> >::Iterator itr;
  67. for (itr = classes_.Begin(); itr != classes_.End(); itr++)
  68. {
  69. itr->second_->Preprocess();
  70. }
  71. }
  72. void JSBModule::ProcessClasses()
  73. {
  74. HashMap<StringHash, SharedPtr<JSBClass> >::Iterator itr;
  75. for (itr = classes_.Begin(); itr != classes_.End(); itr++)
  76. {
  77. itr->second_->Process();
  78. }
  79. }
  80. void JSBModule::PostProcessClasses()
  81. {
  82. HashMap<StringHash, SharedPtr<JSBClass> >::Iterator itr;
  83. for (itr = classes_.Begin(); itr != classes_.End(); itr++)
  84. {
  85. itr->second_->PostProcess();
  86. }
  87. }
  88. void JSBModule::ProcessOverloads()
  89. {
  90. // overloads
  91. JSONValue root = moduleJSON_->GetRoot();
  92. JSONValue overloads = root.Get("overloads");
  93. if (overloads.IsObject())
  94. {
  95. Vector<String> childNames = overloads.GetObject().Keys();
  96. for (unsigned j = 0; j < childNames.Size(); j++)
  97. {
  98. String classname = childNames.At(j);
  99. JSBClass* klass = GetClass(classname);
  100. if (!klass)
  101. {
  102. ErrorExit("Bad overload klass");
  103. }
  104. JSONValue classoverloads = overloads.Get(classname);
  105. Vector<String> functionNames = classoverloads.GetObject().Keys();
  106. for (unsigned k = 0; k < functionNames.Size(); k++)
  107. {
  108. JSONValue _sig = classoverloads.Get(functionNames[k]);
  109. if (!_sig.IsArray())
  110. {
  111. ErrorExit("Bad overload defintion");
  112. }
  113. JSONArray sig = _sig.GetArray();
  114. Vector<String> values;
  115. for (unsigned x = 0; x < sig.Size(); x++)
  116. {
  117. values.Push(sig[x].GetString());
  118. }
  119. JSBFunctionSignature* fo = new JSBFunctionSignature(functionNames[k], values);
  120. klass->AddFunctionOverride(fo);
  121. }
  122. }
  123. }
  124. }
  125. void JSBModule::ProcessExcludes()
  126. {
  127. // excludes
  128. JSONValue root = moduleJSON_->GetRoot();
  129. JSONValue excludes = root.Get("excludes");
  130. if (excludes.IsObject())
  131. {
  132. Vector<String> childNames = excludes.GetObject().Keys();
  133. for (unsigned j = 0; j < childNames.Size(); j++)
  134. {
  135. String classname = childNames.At(j);
  136. JSBClass* klass = GetClass(classname);
  137. if (!klass)
  138. {
  139. ErrorExit("Bad exclude klass");
  140. }
  141. JSONValue classexcludes = excludes.Get(classname);
  142. Vector<String> functionNames = classexcludes.GetObject().Keys();
  143. for (unsigned k = 0; k < functionNames.Size(); k++)
  144. {
  145. JSONValue _sig = classexcludes.Get(functionNames[k]);
  146. if (!_sig.IsArray())
  147. {
  148. ErrorExit("Bad exclude defintion");
  149. }
  150. JSONArray sig = _sig.GetArray();
  151. Vector<String> values;
  152. for (unsigned x = 0; x < sig.Size(); x++)
  153. {
  154. values.Push(sig[x].GetString());
  155. }
  156. JSBFunctionSignature* fe = new JSBFunctionSignature(functionNames[k], values);
  157. klass->AddFunctionExclude(fe);
  158. }
  159. }
  160. }
  161. }
  162. void JSBModule::ProcessTypeScriptDecl()
  163. {
  164. // TypeScript declarations
  165. JSONValue root = moduleJSON_->GetRoot();
  166. JSONValue decl = root.Get("typescript_decl");
  167. if (decl.IsObject())
  168. {
  169. Vector<String> childNames = decl.GetObject().Keys();
  170. for (unsigned j = 0; j < childNames.Size(); j++)
  171. {
  172. String classname = childNames.At(j);
  173. JSBClass* klass = GetClass(classname);
  174. if (!klass)
  175. {
  176. ErrorExit("Bad TypeScript decl klass");
  177. }
  178. JSONArray classdecl = decl.Get(classname).GetArray();
  179. for (unsigned k = 0; k < classdecl.Size(); k++)
  180. {
  181. klass->AddTypeScriptDecl(classdecl[k].GetString());
  182. }
  183. }
  184. }
  185. }
  186. void JSBModule::ProcessHaxeDecl()
  187. {
  188. // Haxe declarations
  189. JSONValue root = moduleJSON_->GetRoot();
  190. JSONValue decl = root.Get("haxe_decl");
  191. if (decl.IsObject())
  192. {
  193. Vector<String> childNames = decl.GetObject().Keys();
  194. for (unsigned j = 0; j < childNames.Size(); j++)
  195. {
  196. String classname = childNames.At(j);
  197. JSBClass* klass = GetClass(classname);
  198. if (!klass)
  199. {
  200. ErrorExit("Bad Haxe decl class");
  201. }
  202. JSONArray classdecl = decl.Get(classname).GetArray();
  203. for (unsigned k = 0; k < classdecl.Size(); k++)
  204. {
  205. klass->AddHaxeDecl(classdecl[k].GetString());
  206. }
  207. }
  208. }
  209. }
  210. void JSBModule::ScanHeaders()
  211. {
  212. JSBind* jsbind = GetSubsystem<JSBind>();
  213. FileSystem* fs = GetSubsystem<FileSystem>();
  214. const String& sourceRoot = jsbind->GetSourceRootFolder();
  215. for (unsigned i = 0; i < sourceDirs_.Size(); i++)
  216. {
  217. const String& dir = sourceRoot + sourceDirs_[i] + "/";
  218. Vector<String> fileNames;
  219. fs->ScanDir(fileNames, dir, "*.h", SCAN_FILES, false);
  220. for (unsigned k = 0; k < fileNames.Size(); k++)
  221. {
  222. String filepath = dir + fileNames[k];
  223. SharedPtr<JSBHeader> header(new JSBHeader(context_, this, filepath));
  224. // Parse the C++ header
  225. header->Parse();
  226. headers_.Push(header);
  227. }
  228. }
  229. }
  230. JSBClass* JSBModule::GetClass(const String& name)
  231. {
  232. if (classes_.Contains(name))
  233. return classes_[name];
  234. return 0;
  235. }
  236. void JSBModule::RegisterClass(String name)
  237. {
  238. String nativeName = name;
  239. if (classnames_.Contains(name))
  240. {
  241. if (classRenames_.Contains(name))
  242. {
  243. name = classRenames_[name];
  244. }
  245. if (JSBPackage::GetClassAllPackages(nativeName))
  246. {
  247. ErrorExit(ToString("Class collision: %s", name.CString()));
  248. }
  249. JSBClass* cls = new JSBClass(context_, this, name, nativeName);
  250. classes_[nativeName] = cls;
  251. package_->RegisterClass(cls);
  252. }
  253. }
  254. void JSBModule::RegisterEnum(JSBEnum* jenum)
  255. {
  256. if (JSBPackage::GetClassAllPackages(jenum->GetName()))
  257. {
  258. ErrorExit(ToString("Enum collision: %s", jenum->GetName().CString()));
  259. }
  260. enums_[jenum->GetName()] = jenum;
  261. }
  262. JSBEnum* JSBModule::GetEnum(const String& name)
  263. {
  264. if (enums_.Contains(name))
  265. {
  266. return enums_[name];
  267. }
  268. return 0;
  269. }
  270. bool JSBModule::ContainsConstant(const String& constantName)
  271. {
  272. return constants_.Contains(constantName);
  273. }
  274. void JSBModule::RegisterConstant(const String& constantName, unsigned type)
  275. {
  276. // MAX_CASCADE_SPLITS is defined differently for desktop/mobile
  277. if (constantName == "MAX_CASCADE_SPLITS" && JSBPackage::ContainsConstantAllPackages(constantName))
  278. {
  279. return;
  280. }
  281. if (JSBPackage::ContainsConstantAllPackages(constantName))
  282. {
  283. ErrorExit(ToString("Constant collision: %s", constantName.CString()));
  284. }
  285. constants_[constantName] = new JSBPrimitiveType(type);
  286. }
  287. bool JSBModule::Load(const String& jsonFilename)
  288. {
  289. JSBind* jsbind = GetSubsystem<JSBind>();
  290. LOGINFOF("Loading Module: %s", jsonFilename.CString());
  291. SharedPtr<File> jsonFile(new File(context_, jsonFilename));
  292. if (!jsonFile->IsOpen())
  293. {
  294. LOGERRORF("Unable to open module json: %s", jsonFilename.CString());
  295. return false;
  296. }
  297. moduleJSON_ = new JSONFile(context_);
  298. if (!moduleJSON_->BeginLoad(*jsonFile))
  299. {
  300. LOGERRORF("Unable to parse module json: %s", jsonFilename.CString());
  301. return false;
  302. }
  303. JSONValue root = moduleJSON_->GetRoot();
  304. name_ = root.Get("name").GetString();
  305. JSONValue requires = root.Get("requires");
  306. if (requires.IsArray())
  307. {
  308. for (unsigned j = 0; j < requires.GetArray().Size(); j++)
  309. {
  310. requirements_.Push(requires[j].GetString());
  311. }
  312. }
  313. JSONArray classes = root.Get("classes").GetArray();
  314. for (unsigned i = 0; i < classes.Size(); i++)
  315. {
  316. classnames_.Push(classes[i].GetString());
  317. }
  318. JSONValue classes_rename = root.Get("classes_rename");
  319. if (classes_rename.IsObject())
  320. {
  321. Vector<String> childNames = classes_rename.GetObject().Keys();
  322. for (unsigned j = 0; j < childNames.Size(); j++)
  323. {
  324. String classname = childNames.At(j);
  325. String crename = classes_rename.Get(classname).GetString();
  326. classRenames_[classname] = crename;
  327. }
  328. }
  329. JSONValue includes = root.Get("includes");
  330. if (includes.IsArray())
  331. {
  332. for (unsigned j = 0; j < includes.GetArray().Size(); j++)
  333. {
  334. includes_.Push(includes.GetArray()[j].GetString());
  335. }
  336. }
  337. JSONValue sources = root.Get("sources");
  338. for (unsigned i = 0; i < sources.GetArray().Size(); i++)
  339. {
  340. sourceDirs_.Push(sources.GetArray()[i].GetString());
  341. }
  342. if (name_ == "Graphics")
  343. {
  344. #ifdef _MSC_VER
  345. if (jsbind->GetPlatform() == "ANDROID" || jsbind->GetPlatform() == "WEB")
  346. {
  347. sourceDirs_.Push("Source/Atomic/Graphics/OpenGL");
  348. }
  349. else
  350. {
  351. #ifdef ATOMIC_D3D11
  352. sourceDirs_.Push("Source/Atomic/Graphics/Direct3D11");
  353. #else
  354. sourceDirs_.Push("Source/Atomic/Graphics/Direct3D9");
  355. #endif
  356. }
  357. #else
  358. sourceDirs_.Push("Source/Atomic/Graphics/OpenGL");
  359. #endif
  360. }
  361. ScanHeaders();
  362. return true;
  363. }
  364. void JSBModule::GenerateSource(const String& outPath)
  365. {
  366. JSBModuleWriter writer(this);
  367. writer.GenerateSource(source_);
  368. String filepath = outPath + "/JSModule" + name_ + ".cpp";
  369. File file(context_);
  370. file.Open(filepath, FILE_WRITE);
  371. file.Write(source_.CString(), source_.Length());
  372. file.Close();
  373. }
  374. }