JSBClass.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #pragma once
  2. #include <Atomic/IO/Log.h>
  3. #include <Atomic/Container/Str.h>
  4. #include <Atomic/Container/Vector.h>
  5. using namespace Atomic;
  6. class JSBModule;
  7. class JSBFunction;
  8. class JSBHeader;
  9. class JSBType;
  10. // chosen function overrides
  11. class JSBFunctionOverride
  12. {
  13. public:
  14. JSBFunctionOverride(const String& name, const Vector<String>& sig);
  15. String name_;
  16. Vector<String> sig_;
  17. Vector<JSBType*> types_;
  18. void Parse() ;
  19. bool parsed_;
  20. };
  21. class JSBEnum
  22. {
  23. public:
  24. JSBHeader* header_;
  25. String name_;
  26. Vector<String> values_;
  27. };
  28. class JSBProperty
  29. {
  30. public:
  31. String name_;
  32. JSBFunction* getter_;
  33. JSBFunction* setter_;
  34. JSBProperty() : getter_(0), setter_(0)
  35. {
  36. }
  37. };
  38. class JSBClass
  39. {
  40. // class name, possibly renamed for bindings
  41. String name_;
  42. // original classname
  43. String className_;
  44. bool isObject_;
  45. bool isAbstract_;
  46. bool hasProperties_;
  47. HashMap<String, JSBProperty*> properties_;
  48. // Vector3, Color, etc can be marshalled via arrays
  49. int numberArrayElements_;
  50. String arrayElementType_;
  51. JSBHeader* header_;
  52. JSBModule* module_;
  53. Vector<JSBClass*> baseClasses_;
  54. static Vector<JSBClass*> allClasses_;
  55. Vector<JSBFunction*> functions_;
  56. Vector<JSBFunctionOverride*> overrides_;
  57. static void RecursiveAddBaseClass(JSBClass* klass, Vector<JSBClass*>& baseClasses)
  58. {
  59. for (unsigned i = 0; i < baseClasses.Size(); i++)
  60. {
  61. JSBClass* base = baseClasses.At(i);
  62. if (!klass->baseClasses_.Contains(base))
  63. klass->baseClasses_.Push(base);
  64. RecursiveAddBaseClass(klass, base->baseClasses_);
  65. }
  66. }
  67. static void WriteProtoTypeRecursive(String& source, JSBClass* klass, Vector<JSBClass *> &written);
  68. public:
  69. JSBClass(const String& name, const String& className = String::EMPTY) :
  70. isObject_(false), isAbstract_(false) , hasProperties_(false), numberArrayElements_(0), arrayElementType_("float"),
  71. header_(0), module_(0)
  72. {
  73. name_ = name;
  74. if (className.Length())
  75. className_ = className;
  76. else
  77. className_ = name_;
  78. if (name_ == "Color")
  79. numberArrayElements_ = 4;
  80. else if (name_ == "Vector2")
  81. numberArrayElements_ = 2;
  82. else if (name_ == "Vector3")
  83. numberArrayElements_ = 3;
  84. else if (name_ == "Vector4")
  85. numberArrayElements_ = 4;
  86. else if (name_ == "Quaternion")
  87. numberArrayElements_ = 4;
  88. else if (name_ == "BoundingBox")
  89. numberArrayElements_ = 6;
  90. else if (name_ == "Rect")
  91. numberArrayElements_ = 4;
  92. else if (name_ == "IntRect")
  93. {
  94. numberArrayElements_ = 4;
  95. arrayElementType_ = "int";
  96. }
  97. allClasses_.Push(this);
  98. }
  99. JSBClass* GetBaseClass() { return baseClasses_.Size() ? baseClasses_[0] : NULL; }
  100. void AddBaseClass(JSBClass* base)
  101. {
  102. baseClasses_.Push(base);
  103. }
  104. void SetSkipFunction(const String& name, bool skip = true);
  105. JSBFunction* GetFunction(const String& name);
  106. void GetPropertyNames(Vector<String>& names) { names = properties_.Keys(); }
  107. JSBProperty* GetProperty(const String& name)
  108. {
  109. if (!properties_.Contains(name))
  110. return 0;
  111. return properties_[name];
  112. }
  113. void AddFunction(JSBFunction* function);
  114. void AddFunctionOverride(JSBFunctionOverride* functionOverride);
  115. void SetModule(JSBModule* module) {module_ = module;}
  116. JSBModule* GetModule() {return module_;}
  117. void SetHeader(JSBHeader* header) {header_ = header;}
  118. JSBHeader* GetHeader() {return header_;}
  119. void Dump();
  120. static void Preprocess();
  121. static void Process();
  122. void AddPropertyFunction(JSBFunction* function);
  123. unsigned GetFunctionCount() {return functions_.Size(); }
  124. JSBFunction* GetFunction(unsigned idx) {return functions_.At(idx); }
  125. // get constructor if defined and not skipped
  126. JSBFunction* GetConstructor();
  127. static void DumpAllClasses()
  128. {
  129. for (unsigned i = 0; i < allClasses_.Size(); i++)
  130. {
  131. allClasses_[i]->Dump();
  132. }
  133. }
  134. bool isAbstract() { return isAbstract_;}
  135. void setAbstract(bool value) { isAbstract_ = value;}
  136. bool isObject() { return isObject_;}
  137. void setObject(bool value) { isObject_ = value;}
  138. bool hasProperties() { return hasProperties_; }
  139. bool isNumberArray() { return numberArrayElements_ != 0; }
  140. int GetNumberArrayElements() { return numberArrayElements_;}
  141. const String& GetArrayElementType() const { return arrayElementType_; }
  142. const String& GetName() { return name_; }
  143. const String& GetClassName() { return className_; }
  144. void Write(String& source);
  145. void WriteFunctions(String& source);
  146. void WriteDefinition(String& source);
  147. void WriteForwardDeclarations(String& source);
  148. static void WriteProtoTypeSetup(String& source);
  149. };