JSBModule.cpp 11 KB

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