JSBClass.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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, bool interface);
  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() const { return isGeneric_; }
  104. bool IsAbstract() const { return isAbstract_; }
  105. bool IsInterface() const { return isInterface_; }
  106. /// Note that if we at some point want to generate bindings for JSBClass
  107. /// this override will need to be addressed, as we'll need to know that JSBClass is
  108. /// itself an object
  109. bool IsObject() { return isObject_; }
  110. bool HasProperties() { return hasProperties_; }
  111. void GetPropertyNames(Vector<String>& names) { names = properties_.Keys(); }
  112. JSBProperty* GetProperty(const String& name)
  113. {
  114. if (!properties_.Contains(name))
  115. return 0;
  116. return properties_[name];
  117. }
  118. JSBFunction* MatchFunction(JSBFunction* function, bool includeBases = false);
  119. bool MatchProperty(JSBProperty* property, bool includeBases = false);
  120. JSBHeader* GetHeader() { return header_; }
  121. JSBModule* GetModule() { return module_; }
  122. JSBPackage* GetPackage() { return module_->GetPackage(); }
  123. const String& GetDocString() const { return docString_; }
  124. bool IsNumberArray() { return numberArrayElements_ != 0; }
  125. int GetNumberArrayElements() { return numberArrayElements_;}
  126. const String& GetArrayElementType() const { return arrayElementType_; }
  127. JSBFunction* GetConstructor(BindingLanguage bindingLanguage = BINDINGLANGUAGE_ANY );
  128. const PODVector<JSBClass*>& GetInterfaces() const { return interfaces_; }
  129. void SetAbstract(bool value = true) { isAbstract_ = value; }
  130. void SetObject(bool value = true) { isObject_ = value; }
  131. void SetGeneric(bool value = true) { isGeneric_ = value; }
  132. void SetHeader(JSBHeader* header) { header_ = header; }
  133. void SetDocString(const String& value) { docString_ = value; }
  134. void SetBaseClass(JSBClass* baseClass);
  135. void SetSkipFunction(const String& name, bool skip = true);
  136. void AddFunction(JSBFunction* function);
  137. void AddFunctionOverride(JSBFunctionSignature* override) { overrides_.Push(override); }
  138. void AddFunctionExclude(JSBFunctionSignature* exclude) { excludes_.Push(exclude); }
  139. void AddPropertyFunction(JSBFunction* function);
  140. void AddInterface(JSBClass* klass) { interfaces_.Push(klass); }
  141. void AddTypeScriptDecl(const String& decl) { typeScriptDecls_.Push(decl); }
  142. unsigned GetNumTypeScriptDecl() { return typeScriptDecls_.Size(); }
  143. const String& GetTypeScriptDecl(unsigned idx) { return typeScriptDecls_[idx]; }
  144. void AddHaxeDecl(const String& decl) { haxeDecls_.Push(decl); }
  145. unsigned GetNumHaxeDecl() { return haxeDecls_.Size(); }
  146. const String& GetHaxeDecl(unsigned idx) { return haxeDecls_[idx]; }
  147. /// CSharp bindings can add NET interfaces to native classes, for example IDisposable, IEquatable, etc
  148. void AddCSharpInterface(const String& interface) { csharpInterfacesDecls_.Push(interface); }
  149. /// Gets the C# interfaces implemented by this class
  150. const StringVector& GetCSharpInterfaces() const { return csharpInterfacesDecls_; }
  151. void Preprocess();
  152. void Process();
  153. void PostProcess();
  154. void Dump();
  155. private:
  156. void MergeInterface(JSBClass* interface);
  157. void RecursiveAddBaseClass(PODVector<JSBClass *> &baseClasses);
  158. String name_;
  159. String nativeName_;
  160. SharedPtr<JSBHeader> header_;
  161. SharedPtr<JSBModule> module_;
  162. PODVector<JSBFunction*> functions_;
  163. PODVector<JSBClass*> baseClasses_;
  164. PODVector<JSBClass*> interfaces_;
  165. PODVector<JSBFunctionSignature*> overrides_;
  166. PODVector<JSBFunctionSignature*> excludes_;
  167. Vector<String> typeScriptDecls_;
  168. Vector<String> haxeDecls_;
  169. Vector<String> csharpInterfacesDecls_;
  170. bool isAbstract_;
  171. bool isObject_;
  172. bool isGeneric_;
  173. bool isInterface_;
  174. String docString_;
  175. // Vector3, Color, etc are marshalled via arrays
  176. int numberArrayElements_;
  177. String arrayElementType_;
  178. bool hasProperties_;
  179. HashMap<String, JSBProperty*> properties_;
  180. };
  181. }