JSBModule.cpp 7.9 KB

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