JSBClass.cpp 11 KB

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