JSBModule.cpp 9.3 KB

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