JSBFunction.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #pragma once
  5. #include <Atomic/IO/Log.h>
  6. #include "JSBClass.h"
  7. #include "JSBType.h"
  8. #include "JSBSymbol.h"
  9. using namespace Atomic;
  10. namespace ToolCore
  11. {
  12. class JSBFunctionType
  13. {
  14. public:
  15. JSBFunctionType(JSBType* type) : type_(type)
  16. {
  17. isSharedPtr_ = false;
  18. isPointer_ = false;
  19. isReference_ = false;
  20. isTemplate_ = false;
  21. isConst_ = false;
  22. }
  23. bool isSharedPtr_;
  24. bool isPointer_;
  25. bool isReference_;
  26. bool isTemplate_;
  27. bool isConst_;
  28. String name_;
  29. String initializer_;
  30. JSBType* type_;
  31. String ToString()
  32. {
  33. String tstring = type_->ToString();
  34. if (isPointer_)
  35. tstring += "*";
  36. if (isReference_)
  37. tstring += "&";
  38. if (name_.Length())
  39. {
  40. tstring += " " + name_;
  41. }
  42. return tstring;
  43. }
  44. String ToArgString(int index)
  45. {
  46. String tstring = type_->ToString();
  47. if (isPointer_)
  48. tstring += "*";
  49. if (isReference_ && tstring != "String")
  50. tstring += "&";
  51. if (name_.Length())
  52. {
  53. tstring.AppendWithFormat(" __arg%i", index);
  54. }
  55. return tstring;
  56. }
  57. };
  58. class JSBFunction : public JSBSymbol
  59. {
  60. friend class JSBFunctionWriter;
  61. public:
  62. JSBFunction(JSBClass* klass) : class_(klass), returnType_(0),
  63. isConstructor_(false), isDestructor_(false),
  64. isGetter_(false), isSetter_(false),
  65. isOverload_(false), skip_(false), isVirtual_(false)
  66. {
  67. }
  68. const String& GetName() { return name_; }
  69. bool IsConstructor() { return isConstructor_; }
  70. bool IsDestructor() { return isDestructor_; }
  71. bool IsSetter() { return isSetter_; }
  72. bool IsGetter() { return isGetter_; }
  73. bool IsOverload() { return isOverload_; }
  74. bool IsVirtual() { return isVirtual_; }
  75. bool Skip() { return skip_; }
  76. JSBClass* GetClass() { return class_; }
  77. const String& GetPropertyName() { return propertyName_; }
  78. JSBFunctionType* GetReturnType() { return returnType_; }
  79. Vector<JSBFunctionType*>& GetParameters() { return parameters_; }
  80. const String& GetDocString() { return docString_; }
  81. void SetName(const String& name) { name_ = name; }
  82. void SetConstructor(bool value = true) { isConstructor_ = value; }
  83. void SetDestructor(bool value = true) { isDestructor_ = value; }
  84. void SetSetter(bool value = true) { isSetter_ = value; }
  85. void SetGetter(bool value = true) { isGetter_ = value; }
  86. void SetOverload(bool value = true) { isOverload_ = value; }
  87. void SetVirtual(bool value = true) { isVirtual_ = value; }
  88. void SetSkip(bool value) { skip_ = value; }
  89. void SetReturnType(JSBFunctionType* retType) { returnType_ = retType; }
  90. void SetDocString(const String& docString) { docString_ = docString; }
  91. int FirstDefaultParameter()
  92. {
  93. for (unsigned i = 0; i < parameters_.Size(); i++)
  94. {
  95. if (parameters_[i]->initializer_.Length())
  96. return i;
  97. }
  98. return -1;
  99. }
  100. void AddParameter(JSBFunctionType* parm)
  101. {
  102. parameters_.Push(parm);
  103. }
  104. void Process();
  105. void WriteParameterMarshal(String& source);
  106. void WriteFunction(String& source);
  107. void WriteConstructor(String& source);
  108. void Write(String& source);
  109. void Dump()
  110. {
  111. String sig;
  112. if (!returnType_)
  113. sig += "void ";
  114. else
  115. sig += returnType_->ToString() + " ";
  116. sig += name_;
  117. sig += "(";
  118. for (unsigned i = 0; i < parameters_.Size(); i++)
  119. {
  120. sig += parameters_.At(i)->ToString();
  121. if (i != parameters_.Size() - 1)
  122. sig += ", ";
  123. }
  124. sig += ");";
  125. LOGINFOF(" %s", sig.CString());
  126. }
  127. private:
  128. SharedPtr<JSBClass> class_;
  129. String name_;
  130. String propertyName_;
  131. JSBFunctionType* returnType_;
  132. Vector<JSBFunctionType*> parameters_;
  133. String docString_;
  134. bool isConstructor_;
  135. bool isDestructor_;
  136. bool isGetter_;
  137. bool isSetter_;
  138. bool isOverload_;
  139. bool isVirtual_;
  140. bool skip_;
  141. };
  142. }