JSBClass.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #include <Atomic/Core/ProcessUtils.h>
  2. #include <Atomic/IO/Log.h>
  3. #include <Atomic/IO/File.h>
  4. #include <Atomic/Resource/JSONFile.h>
  5. #include "JSBModule.h"
  6. #include "JSBClass.h"
  7. #include "JSBFunction.h"
  8. namespace ToolCore
  9. {
  10. JSBFunctionSignature::JSBFunctionSignature(const String &name, const Vector<String>& sig) : parsed_(false)
  11. {
  12. name_ = name;
  13. sig_ = sig;
  14. }
  15. void JSBFunctionSignature::Parse()
  16. {
  17. if (parsed_)
  18. return;
  19. parsed_ = true;
  20. for (unsigned i = 0; i < sig_.Size(); i++)
  21. {
  22. JSBType* type = JSBType::Parse(sig_.At(i));
  23. if (!type)
  24. {
  25. ErrorExit(ToString("Unable to parse override type: %s", sig_.At(i).CString()));
  26. }
  27. types_.Push(type);
  28. }
  29. }
  30. bool JSBFunctionSignature::Match(JSBFunction* function)
  31. {
  32. if (name_ != function->GetName())
  33. return false;
  34. Vector<JSBFunctionType*>& parameters = function->GetParameters();
  35. if (types_.Size() != parameters.Size())
  36. return false;
  37. for (unsigned x = 0; x < parameters.Size(); x++)
  38. {
  39. JSBType* ot = types_[x];
  40. JSBType* pt = parameters[x]->type_;
  41. // should add an == operator
  42. if ((ot->asPrimitiveType() == NULL) != (pt->asPrimitiveType() == NULL) ||
  43. (ot->asClassType() == NULL) != (pt->asClassType() == NULL) )
  44. {
  45. return false;
  46. }
  47. if (ot->asPrimitiveType())
  48. {
  49. JSBPrimitiveType* pot = ot->asPrimitiveType();
  50. JSBPrimitiveType* ppt = pt->asPrimitiveType();
  51. if (pot->kind_ != ppt->kind_)
  52. {
  53. return false;
  54. }
  55. }
  56. if (ot->asClassType())
  57. {
  58. JSBClassType* cot = ot->asClassType();
  59. JSBClassType* cpt = pt->asClassType();
  60. if (cot->class_ != cpt->class_)
  61. {
  62. return false;
  63. }
  64. }
  65. if (ot->asStringType())
  66. {
  67. if (!pt->asStringType())
  68. {
  69. return false;
  70. }
  71. }
  72. if (ot->asStringHashType())
  73. {
  74. if (!pt->asStringHashType())
  75. {
  76. return false;
  77. }
  78. }
  79. }
  80. return true;
  81. }
  82. JSBClass::JSBClass(Context* context, JSBModule *module, const String& name, const String& nativeName) : Object(context),
  83. module_(module), name_(name), nativeName_(nativeName),
  84. isAbstract_(false), isObject_(false),
  85. numberArrayElements_(0), arrayElementType_("float"),
  86. hasProperties_(false)
  87. {
  88. if (nativeName == "Object")
  89. SetObject(true);
  90. // detect array type, TODO: this could go in module json
  91. if (name_ == "Color")
  92. numberArrayElements_ = 4;
  93. else if (name_ == "Vector2")
  94. numberArrayElements_ = 2;
  95. else if (name_ == "Vector3")
  96. numberArrayElements_ = 3;
  97. else if (name_ == "Vector4")
  98. numberArrayElements_ = 4;
  99. else if (name_ == "Quaternion")
  100. numberArrayElements_ = 4;
  101. else if (name_ == "BoundingBox")
  102. numberArrayElements_ = 6;
  103. else if (name_ == "Rect")
  104. numberArrayElements_ = 4;
  105. else if (name_ == "IntRect")
  106. {
  107. numberArrayElements_ = 4;
  108. arrayElementType_ = "int";
  109. }
  110. else if (name_ == "IntVector2")
  111. {
  112. numberArrayElements_ = 2;
  113. arrayElementType_ = "int";
  114. }
  115. }
  116. JSBClass::~JSBClass()
  117. {
  118. }
  119. void JSBClass::AddFunction(JSBFunction* function)
  120. {
  121. functions_.Push(function);
  122. }
  123. JSBClass* JSBClass::GetBaseClass()
  124. {
  125. if (!baseClasses_.Size())
  126. return 0;
  127. return baseClasses_[0];
  128. }
  129. JSBFunction* JSBClass::GetConstructor()
  130. {
  131. {
  132. for (unsigned i = 0; i < functions_.Size(); i++)
  133. if (functions_[i]->IsConstructor() && !functions_[i]->Skip())
  134. return functions_[i];
  135. }
  136. return NULL;
  137. }
  138. void JSBClass::SetSkipFunction(const String& name, bool skip)
  139. {
  140. for (unsigned i = 0; i < functions_.Size(); i++)
  141. {
  142. if (functions_[i]->IsConstructor())
  143. continue;
  144. if (functions_[i]->GetName() == name)
  145. functions_[i]->SetSkip(skip);
  146. }
  147. }
  148. void JSBClass::SetBaseClass(JSBClass *baseClass)
  149. {
  150. // we can't hook up chain here, as base class may not have been visited yet
  151. assert(!baseClasses_.Size());
  152. baseClasses_.Push(baseClass);
  153. }
  154. void JSBClass::RecursiveAddBaseClass(PODVector<JSBClass*>& baseClasses)
  155. {
  156. for (unsigned i = 0; i < baseClasses.Size(); i++)
  157. {
  158. JSBClass* base = baseClasses.At(i);
  159. if (base->IsObject())
  160. SetObject(true);
  161. if (!baseClasses_.Contains(base))
  162. baseClasses_.Push(base);
  163. RecursiveAddBaseClass(base->baseClasses_);
  164. }
  165. }
  166. void JSBClass::Preprocess()
  167. {
  168. RecursiveAddBaseClass(baseClasses_);
  169. }
  170. void JSBClass::Process()
  171. {
  172. for (unsigned j = 0; j < overrides_.Size(); j++)
  173. {
  174. overrides_.At(j)->Parse();
  175. }
  176. for (unsigned j = 0; j < excludes_.Size(); j++)
  177. {
  178. excludes_.At(j)->Parse();
  179. }
  180. // detect skips
  181. for (unsigned j = 0; j < functions_.Size(); j++)
  182. {
  183. for (unsigned k = 0; k < excludes_.Size(); k++)
  184. {
  185. if (excludes_[k]->Match(functions_[j]))
  186. {
  187. functions_[j]->SetSkip(true);
  188. break;
  189. }
  190. }
  191. }
  192. // detect overridden functions, only works for in class atm (not baseclasses)
  193. for (unsigned j = 0; j < functions_.Size(); j++)
  194. {
  195. JSBFunction* function = functions_[j];
  196. if (IsNumberArray())
  197. {
  198. function->SetSkip(true);
  199. continue;
  200. }
  201. // skip function if only one parameter of type Context, if not Constuctor
  202. if (!function->IsConstructor())
  203. {
  204. Vector<JSBFunctionType*>& parameters = function->GetParameters();
  205. if (parameters.Size() == 1 && parameters.At(0)->type_->asClassType())
  206. {
  207. if (parameters.At(0)->type_->asClassType()->class_->GetName() == "Context")
  208. {
  209. function->SetSkip(true);
  210. continue;
  211. }
  212. }
  213. }
  214. if (function->IsOverride())
  215. continue;
  216. for (unsigned k = 0; k < functions_.Size(); k++)
  217. {
  218. if (j == k)
  219. continue;
  220. JSBFunction* function2 = functions_[k];
  221. if (function->GetName() == function2->GetName())
  222. {
  223. function->SetOverride();
  224. function2->SetOverride();
  225. // initially set all overridden functions to skip
  226. function->SetSkip(true);
  227. function2->SetSkip(true);
  228. break;
  229. }
  230. }
  231. }
  232. // mark overrides
  233. for (unsigned j = 0; j < functions_.Size(); j++)
  234. {
  235. JSBFunction* function = functions_[j];
  236. if (function->IsOverride())
  237. {
  238. for (unsigned k = 0; k < overrides_.Size(); k++)
  239. {
  240. JSBFunctionSignature* override = overrides_[k];
  241. if (!override->Match(function))
  242. continue;
  243. function->SetSkip(false);
  244. break;
  245. }
  246. }
  247. }
  248. }
  249. void JSBClass::PostProcess()
  250. {
  251. for (unsigned j = 0; j < functions_.Size(); j++)
  252. {
  253. JSBFunction* function = functions_[j];
  254. function->Process();
  255. }
  256. }
  257. void JSBClass::AddPropertyFunction(JSBFunction* function)
  258. {
  259. hasProperties_ = true;
  260. if (!function->GetPropertyName().Length())
  261. {
  262. ErrorExit("Function has no property name");
  263. }
  264. if (!function->IsGetter() && !function->IsSetter())
  265. {
  266. ErrorExit("Function is not a getter or setter");
  267. }
  268. JSBProperty* prop = NULL;
  269. if (properties_.Contains(function->GetPropertyName()))
  270. {
  271. prop = properties_[function->GetPropertyName()];
  272. }
  273. if (!prop)
  274. {
  275. prop = new JSBProperty();
  276. prop->name_ = function->GetPropertyName();
  277. properties_[function->GetPropertyName()] = prop;
  278. }
  279. if (prop->getter_ && function->IsGetter())
  280. {
  281. ErrorExit("getter already registered");
  282. }
  283. if (prop->setter_ && function->IsSetter())
  284. {
  285. ErrorExit("setter already registered");
  286. }
  287. if (function->IsSetter())
  288. prop->setter_ = function;
  289. else
  290. prop->getter_ = function;
  291. }
  292. void JSBClass::Dump()
  293. {
  294. if (name_ != nativeName_)
  295. {
  296. LOGINFOF("Class: %s (%s)", name_.CString(), nativeName_.CString());
  297. }
  298. else
  299. {
  300. LOGINFOF("Class: %s", name_.CString());
  301. }
  302. LOGINFOF(" IsObject: %s", IsObject() ? "true" : "false");
  303. if (baseClasses_.Size())
  304. {
  305. LOGINFOF(" Bases:");
  306. for (unsigned i = 0; i < baseClasses_.Size(); i++)
  307. {
  308. LOGINFOF(" %s", baseClasses_.At(i)->GetName().CString());
  309. }
  310. }
  311. if (functions_.Size())
  312. {
  313. for (unsigned i = 0; i < functions_.Size(); i++)
  314. {
  315. functions_.At(i)->Dump();
  316. }
  317. }
  318. }
  319. }