JSBClass.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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/Math/Plane.h>
  23. #include <Atomic/Core/ProcessUtils.h>
  24. #include <Atomic/IO/Log.h>
  25. #include <Atomic/IO/File.h>
  26. #include <Atomic/Resource/JSONFile.h>
  27. #include "JSBType.h"
  28. #include "JSBModule.h"
  29. #include "JSBClass.h"
  30. #include "JSBFunction.h"
  31. namespace ToolCore
  32. {
  33. JSBFunctionSignature::JSBFunctionSignature(const String &name, const Vector<String>& sig) : parsed_(false)
  34. {
  35. name_ = name;
  36. sig_ = sig;
  37. }
  38. void JSBFunctionSignature::Parse()
  39. {
  40. if (parsed_)
  41. return;
  42. parsed_ = true;
  43. for (unsigned i = 0; i < sig_.Size(); i++)
  44. {
  45. JSBType* type = JSBType::Parse(sig_.At(i));
  46. if (!type)
  47. {
  48. ErrorExit(ToString("Unable to parse override type: %s", sig_.At(i).CString()));
  49. }
  50. types_.Push(type);
  51. }
  52. }
  53. bool JSBFunctionSignature::Match(JSBFunction* function)
  54. {
  55. if (name_ != function->GetName())
  56. return false;
  57. const Vector<JSBFunctionType*>& parameters = function->GetParameters();
  58. if (types_.Size() != parameters.Size())
  59. return false;
  60. for (unsigned x = 0; x < parameters.Size(); x++)
  61. {
  62. JSBType* ot = types_[x];
  63. JSBType* pt = parameters[x]->type_;
  64. // should add an == operator
  65. if ((ot->asPrimitiveType() == NULL) != (pt->asPrimitiveType() == NULL) ||
  66. (ot->asClassType() == NULL) != (pt->asClassType() == NULL) )
  67. {
  68. return false;
  69. }
  70. if (ot->asPrimitiveType())
  71. {
  72. JSBPrimitiveType* pot = ot->asPrimitiveType();
  73. JSBPrimitiveType* ppt = pt->asPrimitiveType();
  74. if (pot->kind_ != ppt->kind_)
  75. {
  76. return false;
  77. }
  78. }
  79. if (ot->asClassType())
  80. {
  81. JSBClassType* cot = ot->asClassType();
  82. JSBClassType* cpt = pt->asClassType();
  83. if (cot->class_ != cpt->class_)
  84. {
  85. return false;
  86. }
  87. }
  88. if (ot->asStringType())
  89. {
  90. if (!pt->asStringType())
  91. {
  92. return false;
  93. }
  94. }
  95. if (ot->asStringHashType())
  96. {
  97. if (!pt->asStringHashType())
  98. {
  99. return false;
  100. }
  101. }
  102. }
  103. return true;
  104. }
  105. JSBClass::JSBClass(Context* context, JSBModule *module, const String& name, const String& nativeName, bool interface) : Object(context),
  106. module_(module), name_(name), nativeName_(nativeName),
  107. isAbstract_(false), isObject_(false), isGeneric_(false), isInterface_(interface),
  108. numberArrayElements_(0), arrayElementType_("float"),
  109. hasProperties_(false)
  110. {
  111. if (nativeName == "Object")
  112. SetObject(true);
  113. // detect array type, TODO: this could go in module json
  114. if (name_ == "Color")
  115. numberArrayElements_ = 4;
  116. else if (name_ == "Vector2")
  117. numberArrayElements_ = 2;
  118. else if (name_ == "Vector3")
  119. numberArrayElements_ = 3;
  120. else if (name_ == "Vector4")
  121. numberArrayElements_ = 4;
  122. else if (name_ == "Quaternion")
  123. numberArrayElements_ = 4;
  124. else if (name_ == "BoundingBox")
  125. numberArrayElements_ = 6;
  126. else if (name_ == "Rect")
  127. numberArrayElements_ = 4;
  128. else if (name_ == "IntRect")
  129. {
  130. numberArrayElements_ = 4;
  131. arrayElementType_ = "int";
  132. }
  133. else if (name_ == "IntVector2")
  134. {
  135. numberArrayElements_ = 2;
  136. arrayElementType_ = "int";
  137. }
  138. else if (name_ == "Frustum")
  139. {
  140. // NUM_FRUSTUM_PLANES = 6;
  141. numberArrayElements_ = (int) ((sizeof(Plane) * 6) / sizeof(float));
  142. // NUM_FRUSTUM_VERTICES = 8;
  143. numberArrayElements_ += (int) ((sizeof(Vector3) * 8) / sizeof(float));
  144. }
  145. }
  146. JSBClass::~JSBClass()
  147. {
  148. }
  149. void JSBClass::AddFunction(JSBFunction* function)
  150. {
  151. functions_.Push(function);
  152. }
  153. JSBClass* JSBClass::GetBaseClass()
  154. {
  155. if (!baseClasses_.Size())
  156. return 0;
  157. return baseClasses_[0];
  158. }
  159. void JSBClass::GetAllFunctions(PODVector<JSBFunction*>& functions)
  160. {
  161. if (baseClasses_.Size())
  162. baseClasses_[0]->GetAllFunctions(functions);
  163. functions += functions_;
  164. }
  165. JSBFunction* JSBClass::GetConstructor(BindingLanguage bindingLanguage)
  166. {
  167. {
  168. for (unsigned i = 0; i < functions_.Size(); i++)
  169. if (functions_[i]->IsConstructor() && !functions_[i]->Skip(bindingLanguage))
  170. return functions_[i];
  171. }
  172. return NULL;
  173. }
  174. void JSBClass::SetSkipFunction(const String& name, bool skip)
  175. {
  176. for (unsigned i = 0; i < functions_.Size(); i++)
  177. {
  178. if (functions_[i]->IsConstructor())
  179. continue;
  180. if (functions_[i]->GetName() == name)
  181. functions_[i]->SetSkip(skip);
  182. }
  183. }
  184. bool JSBClass::MatchProperty(JSBProperty* property, bool includeBases)
  185. {
  186. HashMap<String, JSBProperty*>::Iterator itr;
  187. for (itr = properties_.Begin(); itr != properties_.End(); itr++)
  188. {
  189. if (itr->first_ != property->name_)
  190. continue;
  191. return true;
  192. }
  193. if (!includeBases || !baseClasses_.Size())
  194. return 0;
  195. return baseClasses_[0]->MatchProperty(property, true);
  196. }
  197. JSBFunction* JSBClass::MatchFunction(JSBFunction *function, bool includeBases)
  198. {
  199. for (unsigned i = 0; i < functions_.Size(); i++)
  200. {
  201. JSBFunction* func = functions_[i];
  202. if (func->Match(function))
  203. return func;
  204. }
  205. if (!includeBases || !baseClasses_.Size())
  206. return 0;
  207. return baseClasses_[0]->MatchFunction(function, true);
  208. }
  209. void JSBClass::SetBaseClass(JSBClass *baseClass)
  210. {
  211. // we can't hook up chain here, as base class may not have been visited yet
  212. assert(!baseClasses_.Size());
  213. baseClasses_.Push(baseClass);
  214. }
  215. void JSBClass::RecursiveAddBaseClass(PODVector<JSBClass*>& baseClasses)
  216. {
  217. for (unsigned i = 0; i < baseClasses.Size(); i++)
  218. {
  219. JSBClass* base = baseClasses.At(i);
  220. if (base->IsObject())
  221. SetObject(true);
  222. if (!baseClasses_.Contains(base))
  223. baseClasses_.Push(base);
  224. RecursiveAddBaseClass(base->baseClasses_);
  225. }
  226. }
  227. void JSBClass::MergeInterface(JSBClass* interface)
  228. {
  229. if (!interface->IsInterface())
  230. {
  231. ATOMIC_LOGERRORF("JSBClass::MergeInterface - attempting to merge non-interface class %s", interface->GetName().CString());
  232. return;
  233. }
  234. // We merge interfaces via cloning the method for those which aren't implemented in the class itself
  235. // 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)
  236. for (unsigned i = 0; i < interface->functions_.Size(); i++)
  237. {
  238. JSBFunction* src = interface->functions_[i];
  239. if (src->Skip() || src->IsConstructor() || src->IsDestructor())
  240. continue;
  241. if (!MatchFunction(src))
  242. {
  243. JSBFunction* function = src->Clone(this);
  244. function->SetInterface(true);
  245. AddFunction(function);
  246. }
  247. }
  248. }
  249. void JSBClass::Preprocess()
  250. {
  251. // merge interfaces
  252. for (unsigned i = 0; i < interfaces_.Size(); i++)
  253. {
  254. MergeInterface(interfaces_[i]);
  255. }
  256. RecursiveAddBaseClass(baseClasses_);
  257. }
  258. void JSBClass::Process()
  259. {
  260. for (unsigned j = 0; j < overrides_.Size(); j++)
  261. {
  262. overrides_.At(j)->Parse();
  263. }
  264. for (unsigned j = 0; j < excludes_.Size(); j++)
  265. {
  266. excludes_.At(j)->Parse();
  267. }
  268. // detect skips
  269. for (unsigned j = 0; j < functions_.Size(); j++)
  270. {
  271. for (unsigned k = 0; k < excludes_.Size(); k++)
  272. {
  273. JSBFunctionSignature* sig = excludes_[k];
  274. if (sig->Match(functions_[j]))
  275. {
  276. if (!sig->associatedBindings_.Size())
  277. {
  278. functions_[j]->SetSkip(true);
  279. }
  280. else
  281. {
  282. for (unsigned x = 0; x < sig->associatedBindings_.Size(); x++)
  283. {
  284. // This shouldn't happen
  285. if (sig->associatedBindings_[x] == BINDINGLANGUAGE_ANY)
  286. continue;
  287. functions_[j]->SetSkipLanguage(sig->associatedBindings_[x]);
  288. }
  289. }
  290. break;
  291. }
  292. }
  293. }
  294. // detect overridden functions, only works for in class atm (not baseclasses)
  295. for (unsigned j = 0; j < functions_.Size(); j++)
  296. {
  297. JSBFunction* function = functions_[j];
  298. if (IsNumberArray())
  299. {
  300. function->SetSkip(true);
  301. continue;
  302. }
  303. // skip function if only one parameter of type Context, if not Constuctor
  304. if (!function->IsConstructor())
  305. {
  306. const Vector<JSBFunctionType*>& parameters = function->GetParameters();
  307. if (parameters.Size() == 1 && parameters.At(0)->type_->asClassType())
  308. {
  309. if (parameters.At(0)->type_->asClassType()->class_->GetName() == "Context")
  310. {
  311. function->SetSkip(true);
  312. continue;
  313. }
  314. }
  315. }
  316. if (function->IsOverload())
  317. continue;
  318. for (unsigned k = 0; k < functions_.Size(); k++)
  319. {
  320. if (j == k)
  321. continue;
  322. JSBFunction* function2 = functions_[k];
  323. if (function->GetName() == function2->GetName())
  324. {
  325. function->SetOverload();
  326. function2->SetOverload();
  327. // initially set all overridden functions to skip (for JavaScript)
  328. function->SetSkipLanguage(BINDINGLANGUAGE_JAVASCRIPT);
  329. function2->SetSkipLanguage(BINDINGLANGUAGE_JAVASCRIPT);
  330. break;
  331. }
  332. }
  333. }
  334. // mark overrides
  335. for (unsigned j = 0; j < functions_.Size(); j++)
  336. {
  337. JSBFunction* function = functions_[j];
  338. if (function->IsOverload())
  339. {
  340. for (unsigned k = 0; k < overrides_.Size(); k++)
  341. {
  342. JSBFunctionSignature* override = overrides_[k];
  343. if (!override->Match(function))
  344. continue;
  345. function->SetSkipLanguage(BINDINGLANGUAGE_JAVASCRIPT, false);
  346. break;
  347. }
  348. }
  349. }
  350. }
  351. void JSBClass::PostProcess()
  352. {
  353. for (unsigned j = 0; j < functions_.Size(); j++)
  354. {
  355. JSBFunction* function = functions_[j];
  356. function->Process();
  357. }
  358. }
  359. void JSBClass::AddPropertyFunction(JSBFunction* function)
  360. {
  361. hasProperties_ = true;
  362. if (!function->GetPropertyName().Length())
  363. {
  364. ErrorExit("Function has no property name");
  365. }
  366. if (!function->IsGetter() && !function->IsSetter())
  367. {
  368. ErrorExit("Function is not a getter or setter");
  369. }
  370. JSBProperty* prop = NULL;
  371. if (properties_.Contains(function->GetPropertyName()))
  372. {
  373. prop = properties_[function->GetPropertyName()];
  374. }
  375. if (!prop)
  376. {
  377. prop = new JSBProperty();
  378. prop->name_ = function->GetPropertyName();
  379. properties_[function->GetPropertyName()] = prop;
  380. }
  381. if (prop->getter_ && function->IsGetter())
  382. {
  383. ErrorExit("getter already registered");
  384. }
  385. if (prop->setter_ && function->IsSetter())
  386. {
  387. ErrorExit("setter already registered");
  388. }
  389. if (function->IsSetter())
  390. prop->setter_ = function;
  391. else
  392. prop->getter_ = function;
  393. }
  394. void JSBClass::Dump()
  395. {
  396. if (name_ != nativeName_)
  397. {
  398. ATOMIC_LOGINFOF("Class: %s (%s)", name_.CString(), nativeName_.CString());
  399. }
  400. else
  401. {
  402. ATOMIC_LOGINFOF("Class: %s", name_.CString());
  403. }
  404. ATOMIC_LOGINFOF(" IsObject: %s", IsObject() ? "true" : "false");
  405. if (baseClasses_.Size())
  406. {
  407. ATOMIC_LOGINFOF(" Bases:");
  408. for (unsigned i = 0; i < baseClasses_.Size(); i++)
  409. {
  410. ATOMIC_LOGINFOF(" %s", baseClasses_.At(i)->GetName().CString());
  411. }
  412. }
  413. if (functions_.Size())
  414. {
  415. for (unsigned i = 0; i < functions_.Size(); i++)
  416. {
  417. functions_.At(i)->Dump();
  418. }
  419. }
  420. }
  421. }