JSBClass.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Core/ProcessUtils.h>
  23. #include <Atomic/IO/Log.h>
  24. #include <Atomic/IO/File.h>
  25. #include <Atomic/Resource/JSONFile.h>
  26. #include "JSBType.h"
  27. #include "JSBModule.h"
  28. #include "JSBClass.h"
  29. #include "JSBFunction.h"
  30. namespace ToolCore
  31. {
  32. JSBFunctionSignature::JSBFunctionSignature(const String &name, const Vector<String>& sig) : parsed_(false)
  33. {
  34. name_ = name;
  35. sig_ = sig;
  36. }
  37. void JSBFunctionSignature::Parse()
  38. {
  39. if (parsed_)
  40. return;
  41. parsed_ = true;
  42. for (unsigned i = 0; i < sig_.Size(); i++)
  43. {
  44. JSBType* type = JSBType::Parse(sig_.At(i));
  45. if (!type)
  46. {
  47. ErrorExit(ToString("Unable to parse override type: %s", sig_.At(i).CString()));
  48. }
  49. types_.Push(type);
  50. }
  51. }
  52. bool JSBFunctionSignature::Match(JSBFunction* function)
  53. {
  54. if (name_ != function->GetName())
  55. return false;
  56. Vector<JSBFunctionType*>& parameters = function->GetParameters();
  57. if (types_.Size() != parameters.Size())
  58. return false;
  59. for (unsigned x = 0; x < parameters.Size(); x++)
  60. {
  61. JSBType* ot = types_[x];
  62. JSBType* pt = parameters[x]->type_;
  63. // should add an == operator
  64. if ((ot->asPrimitiveType() == NULL) != (pt->asPrimitiveType() == NULL) ||
  65. (ot->asClassType() == NULL) != (pt->asClassType() == NULL) )
  66. {
  67. return false;
  68. }
  69. if (ot->asPrimitiveType())
  70. {
  71. JSBPrimitiveType* pot = ot->asPrimitiveType();
  72. JSBPrimitiveType* ppt = pt->asPrimitiveType();
  73. if (pot->kind_ != ppt->kind_)
  74. {
  75. return false;
  76. }
  77. }
  78. if (ot->asClassType())
  79. {
  80. JSBClassType* cot = ot->asClassType();
  81. JSBClassType* cpt = pt->asClassType();
  82. if (cot->class_ != cpt->class_)
  83. {
  84. return false;
  85. }
  86. }
  87. if (ot->asStringType())
  88. {
  89. if (!pt->asStringType())
  90. {
  91. return false;
  92. }
  93. }
  94. if (ot->asStringHashType())
  95. {
  96. if (!pt->asStringHashType())
  97. {
  98. return false;
  99. }
  100. }
  101. }
  102. return true;
  103. }
  104. JSBClass::JSBClass(Context* context, JSBModule *module, const String& name, const String& nativeName) : Object(context),
  105. module_(module), name_(name), nativeName_(nativeName),
  106. isAbstract_(false), isObject_(false), isGeneric_(false),
  107. numberArrayElements_(0), arrayElementType_("float"),
  108. hasProperties_(false)
  109. {
  110. if (nativeName == "Object")
  111. SetObject(true);
  112. // detect array type, TODO: this could go in module json
  113. if (name_ == "Color")
  114. numberArrayElements_ = 4;
  115. else if (name_ == "Vector2")
  116. numberArrayElements_ = 2;
  117. else if (name_ == "Vector3")
  118. numberArrayElements_ = 3;
  119. else if (name_ == "Vector4")
  120. numberArrayElements_ = 4;
  121. else if (name_ == "Quaternion")
  122. numberArrayElements_ = 4;
  123. else if (name_ == "BoundingBox")
  124. numberArrayElements_ = 6;
  125. else if (name_ == "Rect")
  126. numberArrayElements_ = 4;
  127. else if (name_ == "IntRect")
  128. {
  129. numberArrayElements_ = 4;
  130. arrayElementType_ = "int";
  131. }
  132. else if (name_ == "IntVector2")
  133. {
  134. numberArrayElements_ = 2;
  135. arrayElementType_ = "int";
  136. }
  137. }
  138. JSBClass::~JSBClass()
  139. {
  140. }
  141. void JSBClass::AddFunction(JSBFunction* function)
  142. {
  143. functions_.Push(function);
  144. }
  145. JSBClass* JSBClass::GetBaseClass()
  146. {
  147. if (!baseClasses_.Size())
  148. return 0;
  149. return baseClasses_[0];
  150. }
  151. JSBFunction* JSBClass::GetConstructor()
  152. {
  153. {
  154. for (unsigned i = 0; i < functions_.Size(); i++)
  155. if (functions_[i]->IsConstructor() && !functions_[i]->Skip())
  156. return functions_[i];
  157. }
  158. return NULL;
  159. }
  160. void JSBClass::SetSkipFunction(const String& name, bool skip)
  161. {
  162. for (unsigned i = 0; i < functions_.Size(); i++)
  163. {
  164. if (functions_[i]->IsConstructor())
  165. continue;
  166. if (functions_[i]->GetName() == name)
  167. functions_[i]->SetSkip(skip);
  168. }
  169. }
  170. bool JSBClass::MatchProperty(JSBProperty* property, bool includeBases)
  171. {
  172. HashMap<String, JSBProperty*>::Iterator itr;
  173. for (itr = properties_.Begin(); itr != properties_.End(); itr++)
  174. {
  175. if (itr->first_ != property->name_)
  176. continue;
  177. return true;
  178. }
  179. if (!includeBases || !baseClasses_.Size())
  180. return 0;
  181. return baseClasses_[0]->MatchProperty(property, true);
  182. }
  183. JSBFunction* JSBClass::MatchFunction(JSBFunction *function, bool includeBases)
  184. {
  185. for (unsigned i = 0; i < functions_.Size(); i++)
  186. {
  187. JSBFunction* func = functions_[i];
  188. if (func->Match(function))
  189. return func;
  190. }
  191. if (!includeBases || !baseClasses_.Size())
  192. return 0;
  193. return baseClasses_[0]->MatchFunction(function, true);
  194. }
  195. void JSBClass::SetBaseClass(JSBClass *baseClass)
  196. {
  197. // we can't hook up chain here, as base class may not have been visited yet
  198. assert(!baseClasses_.Size());
  199. baseClasses_.Push(baseClass);
  200. }
  201. void JSBClass::RecursiveAddBaseClass(PODVector<JSBClass*>& baseClasses)
  202. {
  203. for (unsigned i = 0; i < baseClasses.Size(); i++)
  204. {
  205. JSBClass* base = baseClasses.At(i);
  206. if (base->IsObject())
  207. SetObject(true);
  208. if (!baseClasses_.Contains(base))
  209. baseClasses_.Push(base);
  210. RecursiveAddBaseClass(base->baseClasses_);
  211. }
  212. }
  213. void JSBClass::Preprocess()
  214. {
  215. RecursiveAddBaseClass(baseClasses_);
  216. }
  217. void JSBClass::Process()
  218. {
  219. for (unsigned j = 0; j < overrides_.Size(); j++)
  220. {
  221. overrides_.At(j)->Parse();
  222. }
  223. for (unsigned j = 0; j < excludes_.Size(); j++)
  224. {
  225. excludes_.At(j)->Parse();
  226. }
  227. // detect skips
  228. for (unsigned j = 0; j < functions_.Size(); j++)
  229. {
  230. for (unsigned k = 0; k < excludes_.Size(); k++)
  231. {
  232. if (excludes_[k]->Match(functions_[j]))
  233. {
  234. functions_[j]->SetSkip(true);
  235. break;
  236. }
  237. }
  238. }
  239. // detect overridden functions, only works for in class atm (not baseclasses)
  240. for (unsigned j = 0; j < functions_.Size(); j++)
  241. {
  242. JSBFunction* function = functions_[j];
  243. if (IsNumberArray())
  244. {
  245. function->SetSkip(true);
  246. continue;
  247. }
  248. // skip function if only one parameter of type Context, if not Constuctor
  249. if (!function->IsConstructor())
  250. {
  251. Vector<JSBFunctionType*>& parameters = function->GetParameters();
  252. if (parameters.Size() == 1 && parameters.At(0)->type_->asClassType())
  253. {
  254. if (parameters.At(0)->type_->asClassType()->class_->GetName() == "Context")
  255. {
  256. function->SetSkip(true);
  257. continue;
  258. }
  259. }
  260. }
  261. if (function->IsOverload())
  262. continue;
  263. for (unsigned k = 0; k < functions_.Size(); k++)
  264. {
  265. if (j == k)
  266. continue;
  267. JSBFunction* function2 = functions_[k];
  268. if (function->GetName() == function2->GetName())
  269. {
  270. function->SetOverload();
  271. function2->SetOverload();
  272. // initially set all overridden functions to skip
  273. function->SetSkip(true);
  274. function2->SetSkip(true);
  275. break;
  276. }
  277. }
  278. }
  279. // mark overrides
  280. for (unsigned j = 0; j < functions_.Size(); j++)
  281. {
  282. JSBFunction* function = functions_[j];
  283. if (function->IsOverload())
  284. {
  285. for (unsigned k = 0; k < overrides_.Size(); k++)
  286. {
  287. JSBFunctionSignature* override = overrides_[k];
  288. if (!override->Match(function))
  289. continue;
  290. function->SetSkip(false);
  291. break;
  292. }
  293. }
  294. }
  295. }
  296. void JSBClass::PostProcess()
  297. {
  298. for (unsigned j = 0; j < functions_.Size(); j++)
  299. {
  300. JSBFunction* function = functions_[j];
  301. function->Process();
  302. }
  303. }
  304. void JSBClass::AddPropertyFunction(JSBFunction* function)
  305. {
  306. hasProperties_ = true;
  307. if (!function->GetPropertyName().Length())
  308. {
  309. ErrorExit("Function has no property name");
  310. }
  311. if (!function->IsGetter() && !function->IsSetter())
  312. {
  313. ErrorExit("Function is not a getter or setter");
  314. }
  315. JSBProperty* prop = NULL;
  316. if (properties_.Contains(function->GetPropertyName()))
  317. {
  318. prop = properties_[function->GetPropertyName()];
  319. }
  320. if (!prop)
  321. {
  322. prop = new JSBProperty();
  323. prop->name_ = function->GetPropertyName();
  324. properties_[function->GetPropertyName()] = prop;
  325. }
  326. if (prop->getter_ && function->IsGetter())
  327. {
  328. ErrorExit("getter already registered");
  329. }
  330. if (prop->setter_ && function->IsSetter())
  331. {
  332. ErrorExit("setter already registered");
  333. }
  334. if (function->IsSetter())
  335. prop->setter_ = function;
  336. else
  337. prop->getter_ = function;
  338. }
  339. void JSBClass::Dump()
  340. {
  341. if (name_ != nativeName_)
  342. {
  343. ATOMIC_LOGINFOF("Class: %s (%s)", name_.CString(), nativeName_.CString());
  344. }
  345. else
  346. {
  347. ATOMIC_LOGINFOF("Class: %s", name_.CString());
  348. }
  349. ATOMIC_LOGINFOF(" IsObject: %s", IsObject() ? "true" : "false");
  350. if (baseClasses_.Size())
  351. {
  352. ATOMIC_LOGINFOF(" Bases:");
  353. for (unsigned i = 0; i < baseClasses_.Size(); i++)
  354. {
  355. ATOMIC_LOGINFOF(" %s", baseClasses_.At(i)->GetName().CString());
  356. }
  357. }
  358. if (functions_.Size())
  359. {
  360. for (unsigned i = 0; i < functions_.Size(); i++)
  361. {
  362. functions_.At(i)->Dump();
  363. }
  364. }
  365. }
  366. }