JSBClass.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #pragma once
  23. #include <Atomic/Core/Object.h>
  24. #include "JSBindTypes.h"
  25. #include "JSBHeader.h"
  26. #include "JSBModule.h"
  27. using namespace Atomic;
  28. namespace ToolCore
  29. {
  30. class JSBPackage;
  31. class JSBFunction;
  32. class JSBFunctionType;
  33. class JSBType;
  34. // chosen function overrides
  35. class JSBFunctionSignature
  36. {
  37. public:
  38. JSBFunctionSignature(const String& name, const Vector<String>& sig);
  39. String name_;
  40. Vector<String> sig_;
  41. Vector<JSBType*> types_;
  42. bool Match(JSBFunction* function);
  43. void Parse() ;
  44. bool parsed_;
  45. };
  46. class JSBProperty
  47. {
  48. public:
  49. String name_;
  50. JSBFunction* getter_;
  51. JSBFunction* setter_;
  52. JSBProperty() : getter_(0), setter_(0)
  53. {
  54. }
  55. // returns proper case for property name
  56. // based on whether the Getter/Setter is all caps
  57. // GetMyValue -> myValue;
  58. // GetGUID -> guid
  59. // URLEnabled -> urlEnabled
  60. String GetCasePropertyName()
  61. {
  62. if (!name_.Length())
  63. return name_;
  64. bool allUpper = true;
  65. // TODO: https://github.com/AtomicGameEngine/AtomicGameEngine/issues/587
  66. if (name_ == "URLEnabled")
  67. {
  68. return "urlEnabled";
  69. }
  70. for (unsigned k = 0; k < name_.Length(); k++)
  71. {
  72. if (!isupper(name_[k]))
  73. {
  74. allUpper = false;
  75. break;
  76. }
  77. }
  78. if (allUpper)
  79. {
  80. return name_.ToLower();
  81. }
  82. String name = name_;
  83. name[0] = tolower(name[0]);
  84. return name;
  85. }
  86. };
  87. class JSBClass : public Object
  88. {
  89. friend class JSClassWriter;
  90. friend class CSClassWriter;
  91. ATOMIC_OBJECT(JSBClass, Object)
  92. public:
  93. JSBClass(Context* context, JSBModule* module, const String& name, const String& nativeName);
  94. virtual ~JSBClass();
  95. const String& GetName() const { return name_; }
  96. const String& GetNativeName() { return nativeName_; }
  97. JSBClass* GetBaseClass();
  98. PODVector<JSBClass*>& GetBaseClasses() {return baseClasses_; }
  99. PODVector<JSBFunction*>& GetFunctions() { return functions_; }
  100. // Get all functions, including those in base classes
  101. void GetAllFunctions(PODVector<JSBFunction*>& functions);
  102. bool IsGeneric() { return isGeneric_; }
  103. bool IsAbstract() { return isAbstract_; }
  104. /// Note that if we at some point want to generate bindings for JSBClass
  105. /// this override will need to be addressed, as we'll need to know that JSBClass is
  106. /// itself an object
  107. bool IsObject() { return isObject_; }
  108. bool HasProperties() { return hasProperties_; }
  109. void GetPropertyNames(Vector<String>& names) { names = properties_.Keys(); }
  110. JSBProperty* GetProperty(const String& name)
  111. {
  112. if (!properties_.Contains(name))
  113. return 0;
  114. return properties_[name];
  115. }
  116. JSBFunction* MatchFunction(JSBFunction* function, bool includeBases = false);
  117. bool MatchProperty(JSBProperty* property, bool includeBases = false);
  118. JSBHeader* GetHeader() { return header_; }
  119. JSBModule* GetModule() { return module_; }
  120. JSBPackage* GetPackage() { return module_->GetPackage(); }
  121. bool IsNumberArray() { return numberArrayElements_ != 0; }
  122. int GetNumberArrayElements() { return numberArrayElements_;}
  123. const String& GetArrayElementType() const { return arrayElementType_; }
  124. JSBFunction* GetConstructor(BindingLanguage bindingLanguage = BINDINGLANGUAGE_ANY );
  125. void SetAbstract(bool value = true) { isAbstract_ = value; }
  126. void SetObject(bool value = true) { isObject_ = value; }
  127. void SetGeneric(bool value = true) { isGeneric_ = value; }
  128. void SetHeader(JSBHeader* header) { header_ = header; }
  129. void SetBaseClass(JSBClass* baseClass);
  130. void SetSkipFunction(const String& name, bool skip = true);
  131. void AddFunction(JSBFunction* function);
  132. void AddFunctionOverride(JSBFunctionSignature* override) { overrides_.Push(override); }
  133. void AddFunctionExclude(JSBFunctionSignature* exclude) { excludes_.Push(exclude); }
  134. void AddPropertyFunction(JSBFunction* function);
  135. void AddTypeScriptDecl(const String& decl) { typeScriptDecls_.Push(decl); }
  136. unsigned GetNumTypeScriptDecl() { return typeScriptDecls_.Size(); }
  137. const String& GetTypeScriptDecl(unsigned idx) { return typeScriptDecls_[idx]; }
  138. void AddHaxeDecl(const String& decl) { haxeDecls_.Push(decl); }
  139. unsigned GetNumHaxeDecl() { return haxeDecls_.Size(); }
  140. const String& GetHaxeDecl(unsigned idx) { return haxeDecls_[idx]; }
  141. void Preprocess();
  142. void Process();
  143. void PostProcess();
  144. void Dump();
  145. private:
  146. void RecursiveAddBaseClass(PODVector<JSBClass *> &baseClasses);
  147. String name_;
  148. String nativeName_;
  149. SharedPtr<JSBHeader> header_;
  150. SharedPtr<JSBModule> module_;
  151. PODVector<JSBFunction*> functions_;
  152. PODVector<JSBClass*> baseClasses_;
  153. PODVector<JSBFunctionSignature*> overrides_;
  154. PODVector<JSBFunctionSignature*> excludes_;
  155. Vector<String> typeScriptDecls_;
  156. Vector<String> haxeDecls_;
  157. bool isAbstract_;
  158. bool isObject_;
  159. bool isGeneric_;
  160. // Vector3, Color, etc are marshalled via arrays
  161. int numberArrayElements_;
  162. String arrayElementType_;
  163. bool hasProperties_;
  164. HashMap<String, JSBProperty*> properties_;
  165. };
  166. }