JSBModule.cpp 8.0 KB

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