JSBModule.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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/Log.h>
  23. #include <Atomic/IO/File.h>
  24. #include <Atomic/IO/FileSystem.h>
  25. #include <Atomic/Resource/JSONFile.h>
  26. #include "Atomic/Core/ProcessUtils.h"
  27. #include "JSBind.h"
  28. #include "JSBPackage.h"
  29. #include "JSBModule.h"
  30. #include "JSBHeader.h"
  31. #include "JSBClass.h"
  32. #include "JSBEnum.h"
  33. #include "JSBModuleWriter.h"
  34. #include "JSBType.h"
  35. #include "JavaScript/JSModuleWriter.h"
  36. #include "CSharp/CSModuleWriter.h"
  37. namespace ToolCore
  38. {
  39. JSBModule::JSBModule(Context* context, JSBPackage* package) : Object(context),
  40. package_(package),
  41. dotNetModule_(false)
  42. {
  43. }
  44. JSBModule::~JSBModule()
  45. {
  46. }
  47. Vector<SharedPtr<JSBClass>> JSBModule::GetClasses()
  48. {
  49. return classes_.Values();
  50. }
  51. Vector<SharedPtr<JSBEnum>> JSBModule::GetEnums()
  52. {
  53. return enums_.Values();
  54. }
  55. void JSBModule::PreprocessHeaders()
  56. {
  57. for (unsigned i = 0; i < headers_.Size(); i++)
  58. {
  59. headers_[i]->VisitPreprocess();
  60. }
  61. }
  62. void JSBModule::VisitHeaders()
  63. {
  64. for (unsigned i = 0; i < headers_.Size(); i++)
  65. {
  66. headers_[i]->VisitHeader();
  67. }
  68. // validate that all classes found
  69. for (unsigned i = 0; i < classnames_.Size(); i++)
  70. {
  71. JSBClass* cls = GetClass(classnames_[i]);
  72. if (!cls)
  73. {
  74. ErrorExit(ToString("Module class not found %s", classnames_[i].CString()));
  75. }
  76. }
  77. ProcessOverloads();
  78. ProcessExcludes();
  79. ProcessClassExcludes();
  80. ProcessTypeScriptDecl();
  81. ProcessHaxeDecl();
  82. }
  83. void JSBModule::PreprocessClasses()
  84. {
  85. HashMap<StringHash, SharedPtr<JSBClass> >::Iterator itr;
  86. for (itr = classes_.Begin(); itr != classes_.End(); itr++)
  87. {
  88. itr->second_->Preprocess();
  89. }
  90. }
  91. void JSBModule::ProcessClasses()
  92. {
  93. HashMap<StringHash, SharedPtr<JSBClass> >::Iterator itr;
  94. for (itr = classes_.Begin(); itr != classes_.End(); itr++)
  95. {
  96. itr->second_->Process();
  97. }
  98. }
  99. void JSBModule::PostProcessClasses()
  100. {
  101. HashMap<StringHash, SharedPtr<JSBClass> >::Iterator itr;
  102. for (itr = classes_.Begin(); itr != classes_.End(); itr++)
  103. {
  104. itr->second_->PostProcess();
  105. }
  106. }
  107. void JSBModule::ProcessOverloads()
  108. {
  109. // overloads
  110. JSONValue root = moduleJSON_->GetRoot();
  111. JSONValue overloads = root.Get("overloads");
  112. if (overloads.IsObject())
  113. {
  114. Vector<String> childNames = overloads.GetObject().Keys();
  115. for (unsigned j = 0; j < childNames.Size(); j++)
  116. {
  117. String classname = childNames.At(j);
  118. JSBClass* klass = GetClass(classname);
  119. if (!klass)
  120. {
  121. ErrorExit("Bad overload klass");
  122. }
  123. JSONValue classoverloads = overloads.Get(classname);
  124. Vector<String> functionNames = classoverloads.GetObject().Keys();
  125. for (unsigned k = 0; k < functionNames.Size(); k++)
  126. {
  127. JSONValue _sig = classoverloads.Get(functionNames[k]);
  128. if (!_sig.IsArray())
  129. {
  130. ErrorExit("Bad overload defintion");
  131. }
  132. JSONArray sig = _sig.GetArray();
  133. Vector<String> values;
  134. for (unsigned x = 0; x < sig.Size(); x++)
  135. {
  136. values.Push(sig[x].GetString());
  137. }
  138. JSBFunctionSignature* fo = new JSBFunctionSignature(functionNames[k], values);
  139. klass->AddFunctionOverride(fo);
  140. }
  141. }
  142. }
  143. }
  144. void JSBModule::ProcessExcludes()
  145. {
  146. // excludes
  147. JSONValue root = moduleJSON_->GetRoot();
  148. JSONValue excludes = root.Get("excludes");
  149. if (excludes.IsObject())
  150. {
  151. Vector<String> childNames = excludes.GetObject().Keys();
  152. for (unsigned j = 0; j < childNames.Size(); j++)
  153. {
  154. String classname = childNames.At(j);
  155. JSBClass* klass = GetClass(classname);
  156. if (!klass)
  157. {
  158. ErrorExit("Bad exclude klass");
  159. }
  160. JSONValue classexcludes = excludes.Get(classname);
  161. Vector<String> functionNames = classexcludes.GetObject().Keys();
  162. for (unsigned k = 0; k < functionNames.Size(); k++)
  163. {
  164. JSONValue _sig = classexcludes.Get(functionNames[k]);
  165. if (!_sig.IsArray())
  166. {
  167. ErrorExit("Bad exclude defintion");
  168. }
  169. JSONArray sig = _sig.GetArray();
  170. Vector<String> values;
  171. for (unsigned x = 0; x < sig.Size(); x++)
  172. {
  173. values.Push(sig[x].GetString());
  174. }
  175. JSBFunctionSignature* fe = new JSBFunctionSignature(functionNames[k], values);
  176. klass->AddFunctionExclude(fe);
  177. }
  178. }
  179. }
  180. }
  181. void JSBModule::ProcessClassExcludes()
  182. {
  183. JSONValue root = moduleJSON_->GetRoot();
  184. JSONValue excludes = root.Get("classExcludes");
  185. if (excludes.IsObject())
  186. {
  187. Vector<String> classes = excludes.GetObject().Keys();
  188. for (unsigned i = 0; i < classes.Size(); i++)
  189. {
  190. const String& classname = classes[i];
  191. if (!classExcludes_.Contains(classname))
  192. {
  193. classExcludes_[classname] = Vector<String>();
  194. }
  195. JSONArray platforms = excludes[classname].GetArray();
  196. for (unsigned j = 0; j < platforms.Size(); j++)
  197. {
  198. classExcludes_[classname].Push(platforms[j].GetString());
  199. }
  200. }
  201. }
  202. }
  203. void JSBModule::ProcessTypeScriptDecl()
  204. {
  205. // TypeScript declarations
  206. JSONValue root = moduleJSON_->GetRoot();
  207. JSONValue decl = root.Get("typescript_decl");
  208. if (decl.IsObject())
  209. {
  210. Vector<String> childNames = decl.GetObject().Keys();
  211. for (unsigned j = 0; j < childNames.Size(); j++)
  212. {
  213. String classname = childNames.At(j);
  214. JSBClass* klass = GetClass(classname);
  215. if (!klass)
  216. {
  217. ErrorExit("Bad TypeScript decl klass");
  218. }
  219. JSONArray classdecl = decl.Get(classname).GetArray();
  220. for (unsigned k = 0; k < classdecl.Size(); k++)
  221. {
  222. klass->AddTypeScriptDecl(classdecl[k].GetString());
  223. }
  224. }
  225. }
  226. }
  227. void JSBModule::ProcessHaxeDecl()
  228. {
  229. // Haxe declarations
  230. JSONValue root = moduleJSON_->GetRoot();
  231. JSONValue decl = root.Get("haxe_decl");
  232. if (decl.IsObject())
  233. {
  234. Vector<String> childNames = decl.GetObject().Keys();
  235. for (unsigned j = 0; j < childNames.Size(); j++)
  236. {
  237. String classname = childNames.At(j);
  238. JSBClass* klass = GetClass(classname);
  239. if (!klass)
  240. {
  241. ErrorExit("Bad Haxe decl class");
  242. }
  243. JSONArray classdecl = decl.Get(classname).GetArray();
  244. for (unsigned k = 0; k < classdecl.Size(); k++)
  245. {
  246. klass->AddHaxeDecl(classdecl[k].GetString());
  247. }
  248. }
  249. }
  250. }
  251. void JSBModule::ScanHeaders()
  252. {
  253. JSBind* jsbind = GetSubsystem<JSBind>();
  254. FileSystem* fs = GetSubsystem<FileSystem>();
  255. const String& sourceRoot = jsbind->GetSourceRootFolder();
  256. for (unsigned i = 0; i < sourceDirs_.Size(); i++)
  257. {
  258. const String& dir = sourceRoot + sourceDirs_[i] + "/";
  259. Vector<String> fileNames;
  260. fs->ScanDir(fileNames, dir, "*.h", SCAN_FILES, false);
  261. for (unsigned k = 0; k < fileNames.Size(); k++)
  262. {
  263. String filepath = dir + fileNames[k];
  264. SharedPtr<JSBHeader> header(new JSBHeader(context_, this, filepath));
  265. // Parse the C++ header
  266. header->Parse();
  267. headers_.Push(header);
  268. }
  269. }
  270. }
  271. JSBClass* JSBModule::GetClass(const String& name)
  272. {
  273. if (classes_.Contains(name))
  274. return classes_[name];
  275. return 0;
  276. }
  277. void JSBModule::RegisterClass(String name)
  278. {
  279. String nativeName = name;
  280. if (classnames_.Contains(name))
  281. {
  282. if (classRenames_.Contains(name))
  283. {
  284. name = classRenames_[name];
  285. }
  286. if (JSBPackage::GetClassAllPackages(nativeName))
  287. {
  288. ErrorExit(ToString("Class collision: %s", name.CString()));
  289. }
  290. JSBClass* cls = new JSBClass(context_, this, name, nativeName);
  291. if (genericClassnames_.Contains(name))
  292. {
  293. cls->SetGeneric();
  294. }
  295. classes_[nativeName] = cls;
  296. package_->RegisterClass(cls);
  297. }
  298. }
  299. void JSBModule::RegisterEnum(JSBEnum* jenum)
  300. {
  301. if (JSBPackage::GetClassAllPackages(jenum->GetName()))
  302. {
  303. ErrorExit(ToString("Enum collision: %s", jenum->GetName().CString()));
  304. }
  305. enums_[jenum->GetName()] = jenum;
  306. }
  307. JSBEnum* JSBModule::GetEnum(const String& name)
  308. {
  309. if (enums_.Contains(name))
  310. {
  311. return enums_[name];
  312. }
  313. return 0;
  314. }
  315. String JSBModule::GetClassDefineGuard(const String& name, const String& language) const
  316. {
  317. StringVector platforms;
  318. if (!classExcludes_.TryGetValue(name, platforms) || !platforms.Size())
  319. return String::EMPTY;
  320. Vector<String> defines;
  321. for (unsigned i = 0; i < platforms.Size(); i++)
  322. {
  323. String platform = platforms[i].ToLower();
  324. if (platform == "windows")
  325. {
  326. if (language == "csharp")
  327. {
  328. if (!defines.Contains("!ATOMIC_DESKTOP"))
  329. defines.Push("!ATOMIC_DESKTOP");
  330. }
  331. else
  332. {
  333. defines.Push("!defined(ATOMIC_PLATFORM_WINDOWS)");
  334. }
  335. }
  336. else if (platform == "macosx")
  337. {
  338. if (language == "csharp")
  339. {
  340. if (!defines.Contains("!ATOMIC_DESKTOP"))
  341. defines.Push("!ATOMIC_DESKTOP");
  342. }
  343. else
  344. {
  345. defines.Push("!defined(ATOMIC_PLATFORM_OSX)");
  346. }
  347. }
  348. else if (platform == "linux")
  349. {
  350. if (language == "csharp")
  351. {
  352. if (!defines.Contains("!ATOMIC_DESKTOP"))
  353. defines.Push("!ATOMIC_DESKTOP");
  354. }
  355. else
  356. {
  357. defines.Push("!defined(ATOMIC_PLATFORM_LINUX)");
  358. }
  359. }
  360. else if (platform == "android")
  361. {
  362. if (language == "csharp")
  363. {
  364. defines.Push("!ATOMIC_ANDROID");
  365. }
  366. else
  367. {
  368. defines.Push("!defined(ATOMIC_PLATFORM_ANDROID)");
  369. }
  370. }
  371. else if (platform == "ios")
  372. {
  373. if (language == "csharp")
  374. {
  375. defines.Push("!ATOMIC_IOS");
  376. }
  377. else
  378. {
  379. defines.Push("!defined(ATOMIC_PLATFORM_IOS)");
  380. }
  381. }
  382. else if (platform == "web")
  383. {
  384. if (language == "csharp")
  385. {
  386. defines.Push("!ATOMIC_WEB");
  387. }
  388. else
  389. {
  390. defines.Push("!defined(ATOMIC_PLATFORM_WEB)");
  391. }
  392. }
  393. else
  394. {
  395. ATOMIC_LOGERRORF("Unknown package platform: %s", platform.CString());
  396. }
  397. }
  398. if (!defines.Size())
  399. return String::EMPTY;
  400. String defineString = "#if " + String::Joined(defines, " && ");
  401. return defineString;
  402. }
  403. String JSBModule::GetModuleDefineGuard() const
  404. {
  405. // platform -> vector of modules
  406. const HashMap<String, Vector<String>>& platformExcludes = package_->GetModuleExcludes();
  407. HashMap<String, Vector<String>>::ConstIterator itr = platformExcludes.Begin();
  408. Vector<String> defines;
  409. while (itr != platformExcludes.End())
  410. {
  411. const String& platform = itr->first_;
  412. const Vector<String>& modules = itr->second_;
  413. for (unsigned i = 0; i < modules.Size(); i++)
  414. {
  415. if (modules[i].ToLower() == name_.ToLower())
  416. {
  417. if (platform.ToLower() == "windows")
  418. defines.Push("!defined(ATOMIC_PLATFORM_WINDOWS)");
  419. else if (platform.ToLower() == "macosx")
  420. defines.Push("!defined(ATOMIC_PLATFORM_OSX)");
  421. else if (platform.ToLower() == "linux")
  422. defines.Push("!defined(ATOMIC_PLATFORM_LINUX)");
  423. else if (platform.ToLower() == "android")
  424. defines.Push("!defined(ATOMIC_PLATFORM_ANDROID)");
  425. else if (platform.ToLower() == "ios")
  426. defines.Push("!defined(ATOMIC_PLATFORM_IOS)");
  427. else if (platform.ToLower() == "web")
  428. defines.Push("!defined(ATOMIC_PLATFORM_WEB)");
  429. else
  430. {
  431. ATOMIC_LOGERRORF("Unknown package platform: %s", platform.CString());
  432. }
  433. break;
  434. }
  435. }
  436. itr++;
  437. }
  438. if (!defines.Size())
  439. return String::EMPTY;
  440. String defineString = "#if " + String::Joined(defines, " && ");
  441. return defineString;
  442. }
  443. bool JSBModule::ContainsConstant(const String& constantName)
  444. {
  445. return constants_.Contains(constantName);
  446. }
  447. void JSBModule::RegisterConstant(const String& constantName, const String& value, unsigned type, bool isUnsigned)
  448. {
  449. // MAX_CASCADE_SPLITS is defined differently for desktop/mobile
  450. if (constantName == "MAX_CASCADE_SPLITS" && JSBPackage::ContainsConstantAllPackages(constantName))
  451. {
  452. return;
  453. }
  454. if (JSBPackage::ContainsConstantAllPackages(constantName))
  455. {
  456. ErrorExit(ToString("Constant collision: %s", constantName.CString()));
  457. }
  458. Constant c;
  459. c.type = new JSBPrimitiveType(type, isUnsigned);
  460. c.value = value;
  461. constants_[constantName] = c;
  462. }
  463. bool JSBModule::Load(const String& jsonFilename)
  464. {
  465. ATOMIC_LOGINFOF("Loading Module: %s", jsonFilename.CString());
  466. JSBind* jsbind = GetSubsystem<JSBind>();
  467. SharedPtr<File> jsonFile(new File(context_, jsonFilename));
  468. if (!jsonFile->IsOpen())
  469. {
  470. ATOMIC_LOGERRORF("Unable to open module json: %s", jsonFilename.CString());
  471. return false;
  472. }
  473. moduleJSON_ = new JSONFile(context_);
  474. if (!moduleJSON_->BeginLoad(*jsonFile))
  475. {
  476. ATOMIC_LOGERRORF("Unable to parse module json: %s", jsonFilename.CString());
  477. return false;
  478. }
  479. JSONValue root = moduleJSON_->GetRoot();
  480. name_ = root.Get("name").GetString();
  481. JSONValue requires = root.Get("requires");
  482. if (requires.IsArray())
  483. {
  484. for (unsigned j = 0; j < requires.GetArray().Size(); j++)
  485. {
  486. requirements_.Push(requires[j].GetString());
  487. }
  488. }
  489. JSONArray classes = root.Get("classes").GetArray();
  490. for (unsigned i = 0; i < classes.Size(); i++)
  491. {
  492. classnames_.Push(classes[i].GetString());
  493. }
  494. JSONArray classesGeneric = root.Get("classes_generic").GetArray();
  495. for (unsigned i = 0; i < classesGeneric.Size(); i++)
  496. {
  497. genericClassnames_.Push(classesGeneric[i].GetString());
  498. }
  499. JSONValue classes_rename = root.Get("classes_rename");
  500. if (classes_rename.IsObject())
  501. {
  502. Vector<String> childNames = classes_rename.GetObject().Keys();
  503. for (unsigned j = 0; j < childNames.Size(); j++)
  504. {
  505. String classname = childNames.At(j);
  506. String crename = classes_rename.Get(classname).GetString();
  507. classRenames_[classname] = crename;
  508. }
  509. }
  510. JSONValue includes = root.Get("includes");
  511. if (includes.IsArray())
  512. {
  513. for (unsigned j = 0; j < includes.GetArray().Size(); j++)
  514. {
  515. includes_.Push(includes.GetArray()[j].GetString());
  516. }
  517. }
  518. JSONValue jsmodulepreamble = root.Get("jsmodulepreamble");
  519. if (jsmodulepreamble.IsArray())
  520. {
  521. for (unsigned j = 0; j < jsmodulepreamble.GetArray().Size(); j++)
  522. {
  523. jsmodulePreamble_.Push(jsmodulepreamble.GetArray()[j].GetString());
  524. }
  525. }
  526. JSONValue sources = root.Get("sources");
  527. for (unsigned i = 0; i < sources.GetArray().Size(); i++)
  528. {
  529. sourceDirs_.Push(sources.GetArray()[i].GetString());
  530. }
  531. ScanHeaders();
  532. return true;
  533. }
  534. }