JSBClass.h 5.9 KB

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