JSBModule.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. ProcessTypeScriptDecl();
  80. ProcessHaxeDecl();
  81. }
  82. void JSBModule::PreprocessClasses()
  83. {
  84. HashMap<StringHash, SharedPtr<JSBClass> >::Iterator itr;
  85. for (itr = classes_.Begin(); itr != classes_.End(); itr++)
  86. {
  87. itr->second_->Preprocess();
  88. }
  89. }
  90. void JSBModule::ProcessClasses()
  91. {
  92. HashMap<StringHash, SharedPtr<JSBClass> >::Iterator itr;
  93. for (itr = classes_.Begin(); itr != classes_.End(); itr++)
  94. {
  95. itr->second_->Process();
  96. }
  97. }
  98. void JSBModule::PostProcessClasses()
  99. {
  100. HashMap<StringHash, SharedPtr<JSBClass> >::Iterator itr;
  101. for (itr = classes_.Begin(); itr != classes_.End(); itr++)
  102. {
  103. itr->second_->PostProcess();
  104. }
  105. }
  106. void JSBModule::ProcessOverloads()
  107. {
  108. // overloads
  109. JSONValue root = moduleJSON_->GetRoot();
  110. JSONValue overloads = root.Get("overloads");
  111. if (overloads.IsObject())
  112. {
  113. Vector<String> childNames = overloads.GetObject().Keys();
  114. for (unsigned j = 0; j < childNames.Size(); j++)
  115. {
  116. String classname = childNames.At(j);
  117. JSBClass* klass = GetClass(classname);
  118. if (!klass)
  119. {
  120. ErrorExit("Bad overload klass");
  121. }
  122. JSONValue classoverloads = overloads.Get(classname);
  123. Vector<String> functionNames = classoverloads.GetObject().Keys();
  124. for (unsigned k = 0; k < functionNames.Size(); k++)
  125. {
  126. JSONValue _sig = classoverloads.Get(functionNames[k]);
  127. if (!_sig.IsArray())
  128. {
  129. ErrorExit("Bad overload defintion");
  130. }
  131. JSONArray sig = _sig.GetArray();
  132. Vector<String> values;
  133. for (unsigned x = 0; x < sig.Size(); x++)
  134. {
  135. values.Push(sig[x].GetString());
  136. }
  137. JSBFunctionSignature* fo = new JSBFunctionSignature(functionNames[k], values);
  138. klass->AddFunctionOverride(fo);
  139. }
  140. }
  141. }
  142. }
  143. void JSBModule::ProcessExcludes()
  144. {
  145. // excludes
  146. JSONValue root = moduleJSON_->GetRoot();
  147. JSONValue excludes = root.Get("excludes");
  148. if (excludes.IsObject())
  149. {
  150. Vector<String> childNames = excludes.GetObject().Keys();
  151. for (unsigned j = 0; j < childNames.Size(); j++)
  152. {
  153. String classname = childNames.At(j);
  154. JSBClass* klass = GetClass(classname);
  155. if (!klass)
  156. {
  157. ErrorExit("Bad exclude klass");
  158. }
  159. JSONValue classexcludes = excludes.Get(classname);
  160. Vector<String> functionNames = classexcludes.GetObject().Keys();
  161. for (unsigned k = 0; k < functionNames.Size(); k++)
  162. {
  163. JSONValue _sig = classexcludes.Get(functionNames[k]);
  164. if (!_sig.IsArray())
  165. {
  166. ErrorExit("Bad exclude defintion");
  167. }
  168. JSONArray sig = _sig.GetArray();
  169. Vector<String> values;
  170. for (unsigned x = 0; x < sig.Size(); x++)
  171. {
  172. values.Push(sig[x].GetString());
  173. }
  174. JSBFunctionSignature* fe = new JSBFunctionSignature(functionNames[k], values);
  175. klass->AddFunctionExclude(fe);
  176. }
  177. }
  178. }
  179. }
  180. void JSBModule::ProcessTypeScriptDecl()
  181. {
  182. // TypeScript declarations
  183. JSONValue root = moduleJSON_->GetRoot();
  184. JSONValue decl = root.Get("typescript_decl");
  185. if (decl.IsObject())
  186. {
  187. Vector<String> childNames = decl.GetObject().Keys();
  188. for (unsigned j = 0; j < childNames.Size(); j++)
  189. {
  190. String classname = childNames.At(j);
  191. JSBClass* klass = GetClass(classname);
  192. if (!klass)
  193. {
  194. ErrorExit("Bad TypeScript decl klass");
  195. }
  196. JSONArray classdecl = decl.Get(classname).GetArray();
  197. for (unsigned k = 0; k < classdecl.Size(); k++)
  198. {
  199. klass->AddTypeScriptDecl(classdecl[k].GetString());
  200. }
  201. }
  202. }
  203. }
  204. void JSBModule::ProcessHaxeDecl()
  205. {
  206. // Haxe declarations
  207. JSONValue root = moduleJSON_->GetRoot();
  208. JSONValue decl = root.Get("haxe_decl");
  209. if (decl.IsObject())
  210. {
  211. Vector<String> childNames = decl.GetObject().Keys();
  212. for (unsigned j = 0; j < childNames.Size(); j++)
  213. {
  214. String classname = childNames.At(j);
  215. JSBClass* klass = GetClass(classname);
  216. if (!klass)
  217. {
  218. ErrorExit("Bad Haxe decl class");
  219. }
  220. JSONArray classdecl = decl.Get(classname).GetArray();
  221. for (unsigned k = 0; k < classdecl.Size(); k++)
  222. {
  223. klass->AddHaxeDecl(classdecl[k].GetString());
  224. }
  225. }
  226. }
  227. }
  228. void JSBModule::ScanHeaders()
  229. {
  230. JSBind* jsbind = GetSubsystem<JSBind>();
  231. FileSystem* fs = GetSubsystem<FileSystem>();
  232. const String& sourceRoot = jsbind->GetSourceRootFolder();
  233. for (unsigned i = 0; i < sourceDirs_.Size(); i++)
  234. {
  235. const String& dir = sourceRoot + sourceDirs_[i] + "/";
  236. Vector<String> fileNames;
  237. fs->ScanDir(fileNames, dir, "*.h", SCAN_FILES, false);
  238. for (unsigned k = 0; k < fileNames.Size(); k++)
  239. {
  240. String filepath = dir + fileNames[k];
  241. SharedPtr<JSBHeader> header(new JSBHeader(context_, this, filepath));
  242. // Parse the C++ header
  243. header->Parse();
  244. headers_.Push(header);
  245. }
  246. }
  247. }
  248. JSBClass* JSBModule::GetClass(const String& name)
  249. {
  250. if (classes_.Contains(name))
  251. return classes_[name];
  252. return 0;
  253. }
  254. void JSBModule::RegisterClass(String name)
  255. {
  256. String nativeName = name;
  257. if (classnames_.Contains(name))
  258. {
  259. if (classRenames_.Contains(name))
  260. {
  261. name = classRenames_[name];
  262. }
  263. if (JSBPackage::GetClassAllPackages(nativeName))
  264. {
  265. ErrorExit(ToString("Class collision: %s", name.CString()));
  266. }
  267. JSBClass* cls = new JSBClass(context_, this, name, nativeName);
  268. classes_[nativeName] = cls;
  269. package_->RegisterClass(cls);
  270. }
  271. }
  272. void JSBModule::RegisterEnum(JSBEnum* jenum)
  273. {
  274. if (JSBPackage::GetClassAllPackages(jenum->GetName()))
  275. {
  276. ErrorExit(ToString("Enum collision: %s", jenum->GetName().CString()));
  277. }
  278. enums_[jenum->GetName()] = jenum;
  279. }
  280. JSBEnum* JSBModule::GetEnum(const String& name)
  281. {
  282. if (enums_.Contains(name))
  283. {
  284. return enums_[name];
  285. }
  286. return 0;
  287. }
  288. bool JSBModule::ContainsConstant(const String& constantName)
  289. {
  290. return constants_.Contains(constantName);
  291. }
  292. void JSBModule::RegisterConstant(const String& constantName, const String& value, unsigned type, bool isUnsigned)
  293. {
  294. // MAX_CASCADE_SPLITS is defined differently for desktop/mobile
  295. if (constantName == "MAX_CASCADE_SPLITS" && JSBPackage::ContainsConstantAllPackages(constantName))
  296. {
  297. return;
  298. }
  299. if (JSBPackage::ContainsConstantAllPackages(constantName))
  300. {
  301. ErrorExit(ToString("Constant collision: %s", constantName.CString()));
  302. }
  303. Constant c;
  304. c.type = new JSBPrimitiveType(type, isUnsigned);
  305. c.value = value;
  306. constants_[constantName] = c;
  307. }
  308. bool JSBModule::Load(const String& jsonFilename)
  309. {
  310. ATOMIC_LOGINFOF("Loading Module: %s", jsonFilename.CString());
  311. SharedPtr<File> jsonFile(new File(context_, jsonFilename));
  312. if (!jsonFile->IsOpen())
  313. {
  314. ATOMIC_LOGERRORF("Unable to open module json: %s", jsonFilename.CString());
  315. return false;
  316. }
  317. moduleJSON_ = new JSONFile(context_);
  318. if (!moduleJSON_->BeginLoad(*jsonFile))
  319. {
  320. ATOMIC_LOGERRORF("Unable to parse module json: %s", jsonFilename.CString());
  321. return false;
  322. }
  323. JSONValue root = moduleJSON_->GetRoot();
  324. name_ = root.Get("name").GetString();
  325. JSONValue requires = root.Get("requires");
  326. if (requires.IsArray())
  327. {
  328. for (unsigned j = 0; j < requires.GetArray().Size(); j++)
  329. {
  330. requirements_.Push(requires[j].GetString());
  331. }
  332. }
  333. JSONArray classes = root.Get("classes").GetArray();
  334. for (unsigned i = 0; i < classes.Size(); i++)
  335. {
  336. classnames_.Push(classes[i].GetString());
  337. }
  338. JSONValue classes_rename = root.Get("classes_rename");
  339. if (classes_rename.IsObject())
  340. {
  341. Vector<String> childNames = classes_rename.GetObject().Keys();
  342. for (unsigned j = 0; j < childNames.Size(); j++)
  343. {
  344. String classname = childNames.At(j);
  345. String crename = classes_rename.Get(classname).GetString();
  346. classRenames_[classname] = crename;
  347. }
  348. }
  349. JSONValue includes = root.Get("includes");
  350. if (includes.IsArray())
  351. {
  352. for (unsigned j = 0; j < includes.GetArray().Size(); j++)
  353. {
  354. includes_.Push(includes.GetArray()[j].GetString());
  355. }
  356. }
  357. JSONValue jsmodulepreamble = root.Get("jsmodulepreamble");
  358. if (jsmodulepreamble.IsArray())
  359. {
  360. for (unsigned j = 0; j < jsmodulepreamble.GetArray().Size(); j++)
  361. {
  362. jsmodulePreamble_.Push(jsmodulepreamble.GetArray()[j].GetString());
  363. }
  364. }
  365. JSONValue sources = root.Get("sources");
  366. for (unsigned i = 0; i < sources.GetArray().Size(); i++)
  367. {
  368. sourceDirs_.Push(sources.GetArray()[i].GetString());
  369. }
  370. if (name_ == "Graphics")
  371. {
  372. #ifdef _MSC_VER
  373. if (jsbind->GetPlatform() == "ANDROID" || jsbind->GetPlatform() == "WEB")
  374. {
  375. sourceDirs_.Push("Source/Atomic/Graphics/OpenGL");
  376. }
  377. else
  378. {
  379. #ifdef ATOMIC_D3D11
  380. sourceDirs_.Push("Source/Atomic/Graphics/Direct3D11");
  381. #else
  382. sourceDirs_.Push("Source/Atomic/Graphics/Direct3D9");
  383. #endif
  384. }
  385. #else
  386. sourceDirs_.Push("Source/Atomic/Graphics/OpenGL");
  387. #endif
  388. }
  389. ScanHeaders();
  390. return true;
  391. }
  392. }