JSBFunction.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. bool isSharedPtr_;
  27. bool isPointer_;
  28. bool isReference_;
  29. bool isTemplate_;
  30. bool isConst_;
  31. String name_;
  32. String initializer_;
  33. JSBType* type_;
  34. String ToString()
  35. {
  36. String tstring = type_->ToString();
  37. if (isPointer_)
  38. tstring += "*";
  39. if (isReference_)
  40. tstring += "&";
  41. if (name_.Length())
  42. {
  43. tstring += " " + name_;
  44. }
  45. return tstring;
  46. }
  47. String ToArgString(int index)
  48. {
  49. String tstring = type_->ToString();
  50. if (isPointer_)
  51. tstring += "*";
  52. if (isReference_ && tstring != "String")
  53. tstring += "&";
  54. if (name_.Length())
  55. {
  56. tstring.AppendWithFormat(" __arg%i", index);
  57. }
  58. return tstring;
  59. }
  60. };
  61. class JSBFunction : public JSBSymbol
  62. {
  63. friend class JSBFunctionWriter;
  64. public:
  65. JSBFunction(JSBClass* klass) : class_(klass), returnType_(0),
  66. isConstructor_(false), isDestructor_(false),
  67. isGetter_(false), isSetter_(false),
  68. isOverload_(false), skip_(false), isVirtual_(false)
  69. {
  70. }
  71. const String& GetName() { return name_; }
  72. bool IsConstructor() { return isConstructor_; }
  73. bool IsDestructor() { return isDestructor_; }
  74. bool IsSetter() { return isSetter_; }
  75. bool IsGetter() { return isGetter_; }
  76. bool IsOverload() { return isOverload_; }
  77. bool IsVirtual() { return isVirtual_; }
  78. bool Skip() { return skip_; }
  79. JSBClass* GetClass() { return class_; }
  80. const String& GetPropertyName() { return propertyName_; }
  81. JSBFunctionType* GetReturnType() { return returnType_; }
  82. Vector<JSBFunctionType*>& GetParameters() { return parameters_; }
  83. const String& GetDocString() { return docString_; }
  84. void SetName(const String& name) { name_ = name; }
  85. void SetConstructor(bool value = true) { isConstructor_ = value; }
  86. void SetDestructor(bool value = true) { isDestructor_ = value; }
  87. void SetSetter(bool value = true) { isSetter_ = value; }
  88. void SetGetter(bool value = true) { isGetter_ = value; }
  89. void SetOverload(bool value = true) { isOverload_ = value; }
  90. void SetVirtual(bool value = true) { isVirtual_ = value; }
  91. void SetSkip(bool value) { skip_ = value; }
  92. void SetReturnType(JSBFunctionType* retType) { returnType_ = retType; }
  93. void SetDocString(const String& docString) { docString_ = docString; }
  94. int FirstDefaultParameter()
  95. {
  96. for (unsigned i = 0; i < parameters_.Size(); i++)
  97. {
  98. if (parameters_[i]->initializer_.Length())
  99. return i;
  100. }
  101. return -1;
  102. }
  103. void AddParameter(JSBFunctionType* parm)
  104. {
  105. parameters_.Push(parm);
  106. }
  107. void Process();
  108. void WriteParameterMarshal(String& source);
  109. void WriteFunction(String& source);
  110. void WriteConstructor(String& source);
  111. void Write(String& source);
  112. void Dump()
  113. {
  114. String sig;
  115. if (!returnType_)
  116. sig += "void ";
  117. else
  118. sig += returnType_->ToString() + " ";
  119. sig += name_;
  120. sig += "(";
  121. for (unsigned i = 0; i < parameters_.Size(); i++)
  122. {
  123. sig += parameters_.At(i)->ToString();
  124. if (i != parameters_.Size() - 1)
  125. sig += ", ";
  126. }
  127. sig += ");";
  128. LOGINFOF(" %s", sig.CString());
  129. }
  130. private:
  131. SharedPtr<JSBClass> class_;
  132. String name_;
  133. String propertyName_;
  134. JSBFunctionType* returnType_;
  135. Vector<JSBFunctionType*> parameters_;
  136. String docString_;
  137. bool isConstructor_;
  138. bool isDestructor_;
  139. bool isGetter_;
  140. bool isSetter_;
  141. bool isOverload_;
  142. bool isVirtual_;
  143. bool skip_;
  144. };
  145. }