JSBModule.cpp 19 KB

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