JSBModule.cpp 10 KB

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