JSBFunction.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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),
  93. isVirtual_(false), isStatic_(false)
  94. {
  95. }
  96. const String& GetName() { return name_; }
  97. bool Match(JSBFunction* func);
  98. bool IsConstructor() { return isConstructor_; }
  99. bool IsDestructor() { return isDestructor_; }
  100. bool IsSetter() { return isSetter_; }
  101. bool IsGetter() { return isGetter_; }
  102. bool IsOverload() { return isOverload_; }
  103. bool IsVirtual() { return isVirtual_; }
  104. bool IsStatic() { return isStatic_; }
  105. bool Skip() { return skip_; }
  106. JSBClass* GetClass() { return class_; }
  107. const String& GetPropertyName() { return propertyName_; }
  108. JSBFunctionType* GetReturnType() { return returnType_; }
  109. /// Get class return type or null
  110. JSBClass* GetReturnClass();
  111. Vector<JSBFunctionType*>& GetParameters() { return parameters_; }
  112. const String& GetDocString() { return docString_; }
  113. void SetName(const String& name) { name_ = name; }
  114. void SetConstructor(bool value = true) { isConstructor_ = value; }
  115. void SetDestructor(bool value = true) { isDestructor_ = value; }
  116. void SetSetter(bool value = true) { isSetter_ = value; }
  117. void SetGetter(bool value = true) { isGetter_ = value; }
  118. void SetOverload(bool value = true) { isOverload_ = value; }
  119. void SetVirtual(bool value = true) { isVirtual_ = value; }
  120. void SetStatic(bool value = true) { isStatic_ = value; }
  121. void SetSkip(bool value) { skip_ = value; }
  122. void SetReturnType(JSBFunctionType* retType) { returnType_ = retType; }
  123. void SetDocString(const String& docString) { docString_ = docString; }
  124. int FirstDefaultParameter()
  125. {
  126. for (unsigned i = 0; i < parameters_.Size(); i++)
  127. {
  128. if (parameters_[i]->initializer_.Length())
  129. return i;
  130. }
  131. return -1;
  132. }
  133. void AddParameter(JSBFunctionType* parm)
  134. {
  135. parameters_.Push(parm);
  136. }
  137. void Process();
  138. void WriteParameterMarshal(String& source);
  139. void WriteFunction(String& source);
  140. void WriteConstructor(String& source);
  141. void Write(String& source);
  142. void Dump()
  143. {
  144. String sig;
  145. if (!returnType_)
  146. sig += "void ";
  147. else
  148. sig += returnType_->ToString() + " ";
  149. sig += name_;
  150. sig += "(";
  151. for (unsigned i = 0; i < parameters_.Size(); i++)
  152. {
  153. sig += parameters_.At(i)->ToString();
  154. if (i != parameters_.Size() - 1)
  155. sig += ", ";
  156. }
  157. sig += ");";
  158. LOGINFOF(" %s", sig.CString());
  159. }
  160. private:
  161. SharedPtr<JSBClass> class_;
  162. String name_;
  163. String propertyName_;
  164. JSBFunctionType* returnType_;
  165. Vector<JSBFunctionType*> parameters_;
  166. String docString_;
  167. bool isConstructor_;
  168. bool isDestructor_;
  169. bool isGetter_;
  170. bool isSetter_;
  171. bool isOverload_;
  172. bool isVirtual_;
  173. bool isStatic_;
  174. bool skip_;
  175. };
  176. }