JSBClass.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. const 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, bool interface) : Object(context),
  105. module_(module), name_(name), nativeName_(nativeName),
  106. isAbstract_(false), isObject_(false), isGeneric_(false), isInterface_(interface),
  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. void JSBClass::GetAllFunctions(PODVector<JSBFunction*>& functions)
  152. {
  153. if (baseClasses_.Size())
  154. baseClasses_[0]->GetAllFunctions(functions);
  155. functions += functions_;
  156. }
  157. JSBFunction* JSBClass::GetConstructor(BindingLanguage bindingLanguage)
  158. {
  159. {
  160. for (unsigned i = 0; i < functions_.Size(); i++)
  161. if (functions_[i]->IsConstructor() && !functions_[i]->Skip(bindingLanguage))
  162. return functions_[i];
  163. }
  164. return NULL;
  165. }
  166. void JSBClass::SetSkipFunction(const String& name, bool skip)
  167. {
  168. for (unsigned i = 0; i < functions_.Size(); i++)
  169. {
  170. if (functions_[i]->IsConstructor())
  171. continue;
  172. if (functions_[i]->GetName() == name)
  173. functions_[i]->SetSkip(skip);
  174. }
  175. }
  176. bool JSBClass::MatchProperty(JSBProperty* property, bool includeBases)
  177. {
  178. HashMap<String, JSBProperty*>::Iterator itr;
  179. for (itr = properties_.Begin(); itr != properties_.End(); itr++)
  180. {
  181. if (itr->first_ != property->name_)
  182. continue;
  183. return true;
  184. }
  185. if (!includeBases || !baseClasses_.Size())
  186. return 0;
  187. return baseClasses_[0]->MatchProperty(property, true);
  188. }
  189. JSBFunction* JSBClass::MatchFunction(JSBFunction *function, bool includeBases)
  190. {
  191. for (unsigned i = 0; i < functions_.Size(); i++)
  192. {
  193. JSBFunction* func = functions_[i];
  194. if (func->Match(function))
  195. return func;
  196. }
  197. if (!includeBases || !baseClasses_.Size())
  198. return 0;
  199. return baseClasses_[0]->MatchFunction(function, true);
  200. }
  201. void JSBClass::SetBaseClass(JSBClass *baseClass)
  202. {
  203. // we can't hook up chain here, as base class may not have been visited yet
  204. assert(!baseClasses_.Size());
  205. baseClasses_.Push(baseClass);
  206. }
  207. void JSBClass::RecursiveAddBaseClass(PODVector<JSBClass*>& baseClasses)
  208. {
  209. for (unsigned i = 0; i < baseClasses.Size(); i++)
  210. {
  211. JSBClass* base = baseClasses.At(i);
  212. if (base->IsObject())
  213. SetObject(true);
  214. if (!baseClasses_.Contains(base))
  215. baseClasses_.Push(base);
  216. RecursiveAddBaseClass(base->baseClasses_);
  217. }
  218. }
  219. void JSBClass::MergeInterface(JSBClass* interface)
  220. {
  221. if (!interface->IsInterface())
  222. {
  223. ATOMIC_LOGERRORF("JSBClass::MergeInterface - attempting to merge non-interface class %s", interface->GetName().CString());
  224. return;
  225. }
  226. // We merge interfaces via cloning the method for those which aren't implemented in the class itself
  227. // We don't use a common native wrapper, as we need to cast (sometimes using multiple inheritance, and this avoids an error prone dynamic cast)
  228. for (unsigned i = 0; i < interface->functions_.Size(); i++)
  229. {
  230. JSBFunction* src = interface->functions_[i];
  231. if (src->Skip() || src->IsConstructor() || src->IsDestructor())
  232. continue;
  233. if (!MatchFunction(src))
  234. {
  235. JSBFunction* function = src->Clone(this);
  236. function->SetInterface(true);
  237. AddFunction(function);
  238. }
  239. }
  240. }
  241. void JSBClass::Preprocess()
  242. {
  243. // merge interfaces
  244. for (unsigned i = 0; i < interfaces_.Size(); i++)
  245. {
  246. MergeInterface(interfaces_[i]);
  247. }
  248. RecursiveAddBaseClass(baseClasses_);
  249. }
  250. void JSBClass::Process()
  251. {
  252. for (unsigned j = 0; j < overrides_.Size(); j++)
  253. {
  254. overrides_.At(j)->Parse();
  255. }
  256. for (unsigned j = 0; j < excludes_.Size(); j++)
  257. {
  258. excludes_.At(j)->Parse();
  259. }
  260. // detect skips
  261. for (unsigned j = 0; j < functions_.Size(); j++)
  262. {
  263. for (unsigned k = 0; k < excludes_.Size(); k++)
  264. {
  265. JSBFunctionSignature* sig = excludes_[k];
  266. if (sig->Match(functions_[j]))
  267. {
  268. if (!sig->associatedBindings_.Size())
  269. {
  270. functions_[j]->SetSkip(true);
  271. }
  272. else
  273. {
  274. for (unsigned x = 0; x < sig->associatedBindings_.Size(); x++)
  275. {
  276. // This shouldn't happen
  277. if (sig->associatedBindings_[x] == BINDINGLANGUAGE_ANY)
  278. continue;
  279. functions_[j]->SetSkipLanguage(sig->associatedBindings_[x]);
  280. }
  281. }
  282. break;
  283. }
  284. }
  285. }
  286. // detect overridden functions, only works for in class atm (not baseclasses)
  287. for (unsigned j = 0; j < functions_.Size(); j++)
  288. {
  289. JSBFunction* function = functions_[j];
  290. if (IsNumberArray())
  291. {
  292. function->SetSkip(true);
  293. continue;
  294. }
  295. // skip function if only one parameter of type Context, if not Constuctor
  296. if (!function->IsConstructor())
  297. {
  298. const Vector<JSBFunctionType*>& parameters = function->GetParameters();
  299. if (parameters.Size() == 1 && parameters.At(0)->type_->asClassType())
  300. {
  301. if (parameters.At(0)->type_->asClassType()->class_->GetName() == "Context")
  302. {
  303. function->SetSkip(true);
  304. continue;
  305. }
  306. }
  307. }
  308. if (function->IsOverload())
  309. continue;
  310. for (unsigned k = 0; k < functions_.Size(); k++)
  311. {
  312. if (j == k)
  313. continue;
  314. JSBFunction* function2 = functions_[k];
  315. if (function->GetName() == function2->GetName())
  316. {
  317. function->SetOverload();
  318. function2->SetOverload();
  319. // initially set all overridden functions to skip (for JavaScript)
  320. function->SetSkipLanguage(BINDINGLANGUAGE_JAVASCRIPT);
  321. function2->SetSkipLanguage(BINDINGLANGUAGE_JAVASCRIPT);
  322. break;
  323. }
  324. }
  325. }
  326. // mark overrides
  327. for (unsigned j = 0; j < functions_.Size(); j++)
  328. {
  329. JSBFunction* function = functions_[j];
  330. if (function->IsOverload())
  331. {
  332. for (unsigned k = 0; k < overrides_.Size(); k++)
  333. {
  334. JSBFunctionSignature* override = overrides_[k];
  335. if (!override->Match(function))
  336. continue;
  337. function->SetSkipLanguage(BINDINGLANGUAGE_JAVASCRIPT, false);
  338. break;
  339. }
  340. }
  341. }
  342. }
  343. void JSBClass::PostProcess()
  344. {
  345. for (unsigned j = 0; j < functions_.Size(); j++)
  346. {
  347. JSBFunction* function = functions_[j];
  348. function->Process();
  349. }
  350. }
  351. void JSBClass::AddPropertyFunction(JSBFunction* function)
  352. {
  353. hasProperties_ = true;
  354. if (!function->GetPropertyName().Length())
  355. {
  356. ErrorExit("Function has no property name");
  357. }
  358. if (!function->IsGetter() && !function->IsSetter())
  359. {
  360. ErrorExit("Function is not a getter or setter");
  361. }
  362. JSBProperty* prop = NULL;
  363. if (properties_.Contains(function->GetPropertyName()))
  364. {
  365. prop = properties_[function->GetPropertyName()];
  366. }
  367. if (!prop)
  368. {
  369. prop = new JSBProperty();
  370. prop->name_ = function->GetPropertyName();
  371. properties_[function->GetPropertyName()] = prop;
  372. }
  373. if (prop->getter_ && function->IsGetter())
  374. {
  375. ErrorExit("getter already registered");
  376. }
  377. if (prop->setter_ && function->IsSetter())
  378. {
  379. ErrorExit("setter already registered");
  380. }
  381. if (function->IsSetter())
  382. prop->setter_ = function;
  383. else
  384. prop->getter_ = function;
  385. }
  386. void JSBClass::Dump()
  387. {
  388. if (name_ != nativeName_)
  389. {
  390. ATOMIC_LOGINFOF("Class: %s (%s)", name_.CString(), nativeName_.CString());
  391. }
  392. else
  393. {
  394. ATOMIC_LOGINFOF("Class: %s", name_.CString());
  395. }
  396. ATOMIC_LOGINFOF(" IsObject: %s", IsObject() ? "true" : "false");
  397. if (baseClasses_.Size())
  398. {
  399. ATOMIC_LOGINFOF(" Bases:");
  400. for (unsigned i = 0; i < baseClasses_.Size(); i++)
  401. {
  402. ATOMIC_LOGINFOF(" %s", baseClasses_.At(i)->GetName().CString());
  403. }
  404. }
  405. if (functions_.Size())
  406. {
  407. for (unsigned i = 0; i < functions_.Size(); i++)
  408. {
  409. functions_.At(i)->Dump();
  410. }
  411. }
  412. }
  413. }