JSBClass.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <Atomic/Core/ProcessUtils.h>
  8. #include <Atomic/IO/Log.h>
  9. #include <Atomic/IO/File.h>
  10. #include <Atomic/Resource/JSONFile.h>
  11. #include "JSBModule.h"
  12. #include "JSBClass.h"
  13. #include "JSBFunction.h"
  14. namespace ToolCore
  15. {
  16. JSBFunctionSignature::JSBFunctionSignature(const String &name, const Vector<String>& sig) : parsed_(false)
  17. {
  18. name_ = name;
  19. sig_ = sig;
  20. }
  21. void JSBFunctionSignature::Parse()
  22. {
  23. if (parsed_)
  24. return;
  25. parsed_ = true;
  26. for (unsigned i = 0; i < sig_.Size(); i++)
  27. {
  28. JSBType* type = JSBType::Parse(sig_.At(i));
  29. if (!type)
  30. {
  31. ErrorExit(ToString("Unable to parse override type: %s", sig_.At(i).CString()));
  32. }
  33. types_.Push(type);
  34. }
  35. }
  36. bool JSBFunctionSignature::Match(JSBFunction* function)
  37. {
  38. if (name_ != function->GetName())
  39. return false;
  40. Vector<JSBFunctionType*>& parameters = function->GetParameters();
  41. if (types_.Size() != parameters.Size())
  42. return false;
  43. for (unsigned x = 0; x < parameters.Size(); x++)
  44. {
  45. JSBType* ot = types_[x];
  46. JSBType* pt = parameters[x]->type_;
  47. // should add an == operator
  48. if ((ot->asPrimitiveType() == NULL) != (pt->asPrimitiveType() == NULL) ||
  49. (ot->asClassType() == NULL) != (pt->asClassType() == NULL) )
  50. {
  51. return false;
  52. }
  53. if (ot->asPrimitiveType())
  54. {
  55. JSBPrimitiveType* pot = ot->asPrimitiveType();
  56. JSBPrimitiveType* ppt = pt->asPrimitiveType();
  57. if (pot->kind_ != ppt->kind_)
  58. {
  59. return false;
  60. }
  61. }
  62. if (ot->asClassType())
  63. {
  64. JSBClassType* cot = ot->asClassType();
  65. JSBClassType* cpt = pt->asClassType();
  66. if (cot->class_ != cpt->class_)
  67. {
  68. return false;
  69. }
  70. }
  71. if (ot->asStringType())
  72. {
  73. if (!pt->asStringType())
  74. {
  75. return false;
  76. }
  77. }
  78. if (ot->asStringHashType())
  79. {
  80. if (!pt->asStringHashType())
  81. {
  82. return false;
  83. }
  84. }
  85. }
  86. return true;
  87. }
  88. JSBClass::JSBClass(Context* context, JSBModule *module, const String& name, const String& nativeName) : Object(context),
  89. module_(module), name_(name), nativeName_(nativeName),
  90. isAbstract_(false), isObject_(false),
  91. numberArrayElements_(0), arrayElementType_("float"),
  92. hasProperties_(false)
  93. {
  94. if (nativeName == "Object")
  95. SetObject(true);
  96. // detect array type, TODO: this could go in module json
  97. if (name_ == "Color")
  98. numberArrayElements_ = 4;
  99. else if (name_ == "Vector2")
  100. numberArrayElements_ = 2;
  101. else if (name_ == "Vector3")
  102. numberArrayElements_ = 3;
  103. else if (name_ == "Vector4")
  104. numberArrayElements_ = 4;
  105. else if (name_ == "Quaternion")
  106. numberArrayElements_ = 4;
  107. else if (name_ == "BoundingBox")
  108. numberArrayElements_ = 6;
  109. else if (name_ == "Rect")
  110. numberArrayElements_ = 4;
  111. else if (name_ == "IntRect")
  112. {
  113. numberArrayElements_ = 4;
  114. arrayElementType_ = "int";
  115. }
  116. else if (name_ == "IntVector2")
  117. {
  118. numberArrayElements_ = 2;
  119. arrayElementType_ = "int";
  120. }
  121. }
  122. JSBClass::~JSBClass()
  123. {
  124. }
  125. void JSBClass::AddFunction(JSBFunction* function)
  126. {
  127. functions_.Push(function);
  128. }
  129. JSBClass* JSBClass::GetBaseClass()
  130. {
  131. if (!baseClasses_.Size())
  132. return 0;
  133. return baseClasses_[0];
  134. }
  135. JSBFunction* JSBClass::GetConstructor()
  136. {
  137. {
  138. for (unsigned i = 0; i < functions_.Size(); i++)
  139. if (functions_[i]->IsConstructor() && !functions_[i]->Skip())
  140. return functions_[i];
  141. }
  142. return NULL;
  143. }
  144. void JSBClass::SetSkipFunction(const String& name, bool skip)
  145. {
  146. for (unsigned i = 0; i < functions_.Size(); i++)
  147. {
  148. if (functions_[i]->IsConstructor())
  149. continue;
  150. if (functions_[i]->GetName() == name)
  151. functions_[i]->SetSkip(skip);
  152. }
  153. }
  154. void JSBClass::SetBaseClass(JSBClass *baseClass)
  155. {
  156. // we can't hook up chain here, as base class may not have been visited yet
  157. assert(!baseClasses_.Size());
  158. baseClasses_.Push(baseClass);
  159. }
  160. void JSBClass::RecursiveAddBaseClass(PODVector<JSBClass*>& baseClasses)
  161. {
  162. for (unsigned i = 0; i < baseClasses.Size(); i++)
  163. {
  164. JSBClass* base = baseClasses.At(i);
  165. if (base->IsObject())
  166. SetObject(true);
  167. if (!baseClasses_.Contains(base))
  168. baseClasses_.Push(base);
  169. RecursiveAddBaseClass(base->baseClasses_);
  170. }
  171. }
  172. void JSBClass::Preprocess()
  173. {
  174. RecursiveAddBaseClass(baseClasses_);
  175. }
  176. void JSBClass::Process()
  177. {
  178. for (unsigned j = 0; j < overrides_.Size(); j++)
  179. {
  180. overrides_.At(j)->Parse();
  181. }
  182. for (unsigned j = 0; j < excludes_.Size(); j++)
  183. {
  184. excludes_.At(j)->Parse();
  185. }
  186. // detect skips
  187. for (unsigned j = 0; j < functions_.Size(); j++)
  188. {
  189. for (unsigned k = 0; k < excludes_.Size(); k++)
  190. {
  191. if (excludes_[k]->Match(functions_[j]))
  192. {
  193. functions_[j]->SetSkip(true);
  194. break;
  195. }
  196. }
  197. }
  198. // detect overridden functions, only works for in class atm (not baseclasses)
  199. for (unsigned j = 0; j < functions_.Size(); j++)
  200. {
  201. JSBFunction* function = functions_[j];
  202. if (IsNumberArray())
  203. {
  204. function->SetSkip(true);
  205. continue;
  206. }
  207. // skip function if only one parameter of type Context, if not Constuctor
  208. if (!function->IsConstructor())
  209. {
  210. Vector<JSBFunctionType*>& parameters = function->GetParameters();
  211. if (parameters.Size() == 1 && parameters.At(0)->type_->asClassType())
  212. {
  213. if (parameters.At(0)->type_->asClassType()->class_->GetName() == "Context")
  214. {
  215. function->SetSkip(true);
  216. continue;
  217. }
  218. }
  219. }
  220. if (function->IsOverload())
  221. continue;
  222. for (unsigned k = 0; k < functions_.Size(); k++)
  223. {
  224. if (j == k)
  225. continue;
  226. JSBFunction* function2 = functions_[k];
  227. if (function->GetName() == function2->GetName())
  228. {
  229. function->SetOverload();
  230. function2->SetOverload();
  231. // initially set all overridden functions to skip
  232. function->SetSkip(true);
  233. function2->SetSkip(true);
  234. break;
  235. }
  236. }
  237. }
  238. // mark overrides
  239. for (unsigned j = 0; j < functions_.Size(); j++)
  240. {
  241. JSBFunction* function = functions_[j];
  242. if (function->IsOverload())
  243. {
  244. for (unsigned k = 0; k < overrides_.Size(); k++)
  245. {
  246. JSBFunctionSignature* override = overrides_[k];
  247. if (!override->Match(function))
  248. continue;
  249. function->SetSkip(false);
  250. break;
  251. }
  252. }
  253. }
  254. }
  255. void JSBClass::PostProcess()
  256. {
  257. for (unsigned j = 0; j < functions_.Size(); j++)
  258. {
  259. JSBFunction* function = functions_[j];
  260. function->Process();
  261. }
  262. }
  263. void JSBClass::AddPropertyFunction(JSBFunction* function)
  264. {
  265. hasProperties_ = true;
  266. if (!function->GetPropertyName().Length())
  267. {
  268. ErrorExit("Function has no property name");
  269. }
  270. if (!function->IsGetter() && !function->IsSetter())
  271. {
  272. ErrorExit("Function is not a getter or setter");
  273. }
  274. JSBProperty* prop = NULL;
  275. if (properties_.Contains(function->GetPropertyName()))
  276. {
  277. prop = properties_[function->GetPropertyName()];
  278. }
  279. if (!prop)
  280. {
  281. prop = new JSBProperty();
  282. prop->name_ = function->GetPropertyName();
  283. properties_[function->GetPropertyName()] = prop;
  284. }
  285. if (prop->getter_ && function->IsGetter())
  286. {
  287. ErrorExit("getter already registered");
  288. }
  289. if (prop->setter_ && function->IsSetter())
  290. {
  291. ErrorExit("setter already registered");
  292. }
  293. if (function->IsSetter())
  294. prop->setter_ = function;
  295. else
  296. prop->getter_ = function;
  297. }
  298. void JSBClass::Dump()
  299. {
  300. if (name_ != nativeName_)
  301. {
  302. LOGINFOF("Class: %s (%s)", name_.CString(), nativeName_.CString());
  303. }
  304. else
  305. {
  306. LOGINFOF("Class: %s", name_.CString());
  307. }
  308. LOGINFOF(" IsObject: %s", IsObject() ? "true" : "false");
  309. if (baseClasses_.Size())
  310. {
  311. LOGINFOF(" Bases:");
  312. for (unsigned i = 0; i < baseClasses_.Size(); i++)
  313. {
  314. LOGINFOF(" %s", baseClasses_.At(i)->GetName().CString());
  315. }
  316. }
  317. if (functions_.Size())
  318. {
  319. for (unsigned i = 0; i < functions_.Size(); i++)
  320. {
  321. functions_.At(i)->Dump();
  322. }
  323. }
  324. }
  325. }