JSBClass.h 6.5 KB

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