JSBModule.cpp 18 KB

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