JSBClass.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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::Preprocess()
  220. {
  221. RecursiveAddBaseClass(baseClasses_);
  222. }
  223. void JSBClass::Process()
  224. {
  225. for (unsigned j = 0; j < overrides_.Size(); j++)
  226. {
  227. overrides_.At(j)->Parse();
  228. }
  229. for (unsigned j = 0; j < excludes_.Size(); j++)
  230. {
  231. excludes_.At(j)->Parse();
  232. }
  233. // detect skips
  234. for (unsigned j = 0; j < functions_.Size(); j++)
  235. {
  236. for (unsigned k = 0; k < excludes_.Size(); k++)
  237. {
  238. JSBFunctionSignature* sig = excludes_[k];
  239. if (sig->Match(functions_[j]))
  240. {
  241. if (!sig->associatedBindings_.Size())
  242. {
  243. functions_[j]->SetSkip(true);
  244. }
  245. else
  246. {
  247. for (unsigned x = 0; x < sig->associatedBindings_.Size(); x++)
  248. {
  249. // This shouldn't happen
  250. if (sig->associatedBindings_[x] == BINDINGLANGUAGE_ANY)
  251. continue;
  252. functions_[j]->SetSkipLanguage(sig->associatedBindings_[x]);
  253. }
  254. }
  255. break;
  256. }
  257. }
  258. }
  259. // detect overridden functions, only works for in class atm (not baseclasses)
  260. for (unsigned j = 0; j < functions_.Size(); j++)
  261. {
  262. JSBFunction* function = functions_[j];
  263. if (IsNumberArray())
  264. {
  265. function->SetSkip(true);
  266. continue;
  267. }
  268. // skip function if only one parameter of type Context, if not Constuctor
  269. if (!function->IsConstructor())
  270. {
  271. const Vector<JSBFunctionType*>& parameters = function->GetParameters();
  272. if (parameters.Size() == 1 && parameters.At(0)->type_->asClassType())
  273. {
  274. if (parameters.At(0)->type_->asClassType()->class_->GetName() == "Context")
  275. {
  276. function->SetSkip(true);
  277. continue;
  278. }
  279. }
  280. }
  281. if (function->IsOverload())
  282. continue;
  283. for (unsigned k = 0; k < functions_.Size(); k++)
  284. {
  285. if (j == k)
  286. continue;
  287. JSBFunction* function2 = functions_[k];
  288. if (function->GetName() == function2->GetName())
  289. {
  290. function->SetOverload();
  291. function2->SetOverload();
  292. // initially set all overridden functions to skip (for JavaScript)
  293. function->SetSkipLanguage(BINDINGLANGUAGE_JAVASCRIPT);
  294. function2->SetSkipLanguage(BINDINGLANGUAGE_JAVASCRIPT);
  295. break;
  296. }
  297. }
  298. }
  299. // mark overrides
  300. for (unsigned j = 0; j < functions_.Size(); j++)
  301. {
  302. JSBFunction* function = functions_[j];
  303. if (function->IsOverload())
  304. {
  305. for (unsigned k = 0; k < overrides_.Size(); k++)
  306. {
  307. JSBFunctionSignature* override = overrides_[k];
  308. if (!override->Match(function))
  309. continue;
  310. function->SetSkipLanguage(BINDINGLANGUAGE_JAVASCRIPT, false);
  311. break;
  312. }
  313. }
  314. }
  315. }
  316. void JSBClass::PostProcess()
  317. {
  318. for (unsigned j = 0; j < functions_.Size(); j++)
  319. {
  320. JSBFunction* function = functions_[j];
  321. function->Process();
  322. }
  323. }
  324. void JSBClass::AddPropertyFunction(JSBFunction* function)
  325. {
  326. hasProperties_ = true;
  327. if (!function->GetPropertyName().Length())
  328. {
  329. ErrorExit("Function has no property name");
  330. }
  331. if (!function->IsGetter() && !function->IsSetter())
  332. {
  333. ErrorExit("Function is not a getter or setter");
  334. }
  335. JSBProperty* prop = NULL;
  336. if (properties_.Contains(function->GetPropertyName()))
  337. {
  338. prop = properties_[function->GetPropertyName()];
  339. }
  340. if (!prop)
  341. {
  342. prop = new JSBProperty();
  343. prop->name_ = function->GetPropertyName();
  344. properties_[function->GetPropertyName()] = prop;
  345. }
  346. if (prop->getter_ && function->IsGetter())
  347. {
  348. ErrorExit("getter already registered");
  349. }
  350. if (prop->setter_ && function->IsSetter())
  351. {
  352. ErrorExit("setter already registered");
  353. }
  354. if (function->IsSetter())
  355. prop->setter_ = function;
  356. else
  357. prop->getter_ = function;
  358. }
  359. void JSBClass::Dump()
  360. {
  361. if (name_ != nativeName_)
  362. {
  363. ATOMIC_LOGINFOF("Class: %s (%s)", name_.CString(), nativeName_.CString());
  364. }
  365. else
  366. {
  367. ATOMIC_LOGINFOF("Class: %s", name_.CString());
  368. }
  369. ATOMIC_LOGINFOF(" IsObject: %s", IsObject() ? "true" : "false");
  370. if (baseClasses_.Size())
  371. {
  372. ATOMIC_LOGINFOF(" Bases:");
  373. for (unsigned i = 0; i < baseClasses_.Size(); i++)
  374. {
  375. ATOMIC_LOGINFOF(" %s", baseClasses_.At(i)->GetName().CString());
  376. }
  377. }
  378. if (functions_.Size())
  379. {
  380. for (unsigned i = 0; i < functions_.Size(); i++)
  381. {
  382. functions_.At(i)->Dump();
  383. }
  384. }
  385. }
  386. }