JSBFunction.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/IO/Log.h>
  9. #include "JSBClass.h"
  10. #include "JSBType.h"
  11. #include "JSBSymbol.h"
  12. using namespace Atomic;
  13. namespace ToolCore
  14. {
  15. class JSBFunctionType
  16. {
  17. public:
  18. JSBFunctionType(JSBType* type) : type_(type)
  19. {
  20. isSharedPtr_ = false;
  21. isPointer_ = false;
  22. isReference_ = false;
  23. isTemplate_ = false;
  24. isConst_ = false;
  25. }
  26. // returns true if the types match
  27. bool Match(const JSBFunctionType* other)
  28. {
  29. if (!other)
  30. return false;
  31. if (isSharedPtr_ != other->isSharedPtr_)
  32. return false;
  33. if (isPointer_ != other->isPointer_)
  34. return false;
  35. if (isReference_ != other->isReference_)
  36. return false;
  37. if (isTemplate_ != other->isTemplate_)
  38. return false;
  39. if (isConst_ != other->isConst_)
  40. return false;
  41. if (name_ != other->name_)
  42. return false;
  43. if (!type_ && !other->type_)
  44. return true;
  45. if (!type_ || !other->type_)
  46. return false;
  47. return type_->Match(other->type_);
  48. }
  49. bool isSharedPtr_;
  50. bool isPointer_;
  51. bool isReference_;
  52. bool isTemplate_;
  53. bool isConst_;
  54. String name_;
  55. String initializer_;
  56. JSBType* type_;
  57. String ToString()
  58. {
  59. String tstring = type_->ToString();
  60. if (isPointer_)
  61. tstring += "*";
  62. if (isReference_)
  63. tstring += "&";
  64. if (name_.Length())
  65. {
  66. tstring += " " + name_;
  67. }
  68. return tstring;
  69. }
  70. String ToArgString(int index)
  71. {
  72. String tstring = type_->ToString();
  73. if (isPointer_)
  74. tstring += "*";
  75. if (isReference_ && tstring != "String")
  76. tstring += "&";
  77. if (name_.Length())
  78. {
  79. tstring.AppendWithFormat(" __arg%i", index);
  80. }
  81. return tstring;
  82. }
  83. };
  84. class JSBFunction : public JSBSymbol
  85. {
  86. friend class JSFunctionWriter;
  87. friend class CSFunctionWriter;
  88. public:
  89. JSBFunction(JSBClass* klass) : class_(klass), returnType_(0),
  90. isConstructor_(false), isDestructor_(false),
  91. isGetter_(false), isSetter_(false),
  92. isOverload_(false), skip_(false), isVirtual_(false)
  93. {
  94. }
  95. const String& GetName() { return name_; }
  96. bool Match(JSBFunction* func);
  97. bool IsConstructor() { return isConstructor_; }
  98. bool IsDestructor() { return isDestructor_; }
  99. bool IsSetter() { return isSetter_; }
  100. bool IsGetter() { return isGetter_; }
  101. bool IsOverload() { return isOverload_; }
  102. bool IsVirtual() { return isVirtual_; }
  103. bool Skip() { return skip_; }
  104. JSBClass* GetClass() { return class_; }
  105. const String& GetPropertyName() { return propertyName_; }
  106. JSBFunctionType* GetReturnType() { return returnType_; }
  107. /// Get class return type or null
  108. JSBClass* GetReturnClass();
  109. Vector<JSBFunctionType*>& GetParameters() { return parameters_; }
  110. const String& GetDocString() { return docString_; }
  111. void SetName(const String& name) { name_ = name; }
  112. void SetConstructor(bool value = true) { isConstructor_ = value; }
  113. void SetDestructor(bool value = true) { isDestructor_ = value; }
  114. void SetSetter(bool value = true) { isSetter_ = value; }
  115. void SetGetter(bool value = true) { isGetter_ = value; }
  116. void SetOverload(bool value = true) { isOverload_ = value; }
  117. void SetVirtual(bool value = true) { isVirtual_ = value; }
  118. void SetSkip(bool value) { skip_ = value; }
  119. void SetReturnType(JSBFunctionType* retType) { returnType_ = retType; }
  120. void SetDocString(const String& docString) { docString_ = docString; }
  121. int FirstDefaultParameter()
  122. {
  123. for (unsigned i = 0; i < parameters_.Size(); i++)
  124. {
  125. if (parameters_[i]->initializer_.Length())
  126. return i;
  127. }
  128. return -1;
  129. }
  130. void AddParameter(JSBFunctionType* parm)
  131. {
  132. parameters_.Push(parm);
  133. }
  134. void Process();
  135. void WriteParameterMarshal(String& source);
  136. void WriteFunction(String& source);
  137. void WriteConstructor(String& source);
  138. void Write(String& source);
  139. void Dump()
  140. {
  141. String sig;
  142. if (!returnType_)
  143. sig += "void ";
  144. else
  145. sig += returnType_->ToString() + " ";
  146. sig += name_;
  147. sig += "(";
  148. for (unsigned i = 0; i < parameters_.Size(); i++)
  149. {
  150. sig += parameters_.At(i)->ToString();
  151. if (i != parameters_.Size() - 1)
  152. sig += ", ";
  153. }
  154. sig += ");";
  155. LOGINFOF(" %s", sig.CString());
  156. }
  157. private:
  158. SharedPtr<JSBClass> class_;
  159. String name_;
  160. String propertyName_;
  161. JSBFunctionType* returnType_;
  162. Vector<JSBFunctionType*> parameters_;
  163. String docString_;
  164. bool isConstructor_;
  165. bool isDestructor_;
  166. bool isGetter_;
  167. bool isSetter_;
  168. bool isOverload_;
  169. bool isVirtual_;
  170. bool skip_;
  171. };
  172. }