JSBClass.h 5.1 KB

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