JSBFunction.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. isOverride_(false), skip_(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 IsOverride() { return isOverride_; }
  74. bool Skip() { return skip_; }
  75. JSBClass* GetClass() { return class_; }
  76. const String& GetPropertyName() { return propertyName_; }
  77. JSBFunctionType* GetReturnType() { return returnType_; }
  78. Vector<JSBFunctionType*>& GetParameters() { return parameters_; }
  79. const String& GetDocString() { return docString_; }
  80. void SetName(const String& name) { name_ = name; }
  81. void SetConstructor(bool value = true) { isConstructor_ = value; }
  82. void SetDestructor(bool value = true) { isDestructor_ = value; }
  83. void SetSetter(bool value = true) { isSetter_ = value; }
  84. void SetGetter(bool value = true) { isGetter_ = value; }
  85. void SetOverride(bool value = true) { isOverride_ = value; }
  86. void SetSkip(bool value) { skip_ = value; }
  87. void SetReturnType(JSBFunctionType* retType) { returnType_ = retType; }
  88. void SetDocString(const String& docString) { docString_ = docString; }
  89. int FirstDefaultParameter()
  90. {
  91. for (unsigned i = 0; i < parameters_.Size(); i++)
  92. {
  93. if (parameters_[i]->initializer_.Length())
  94. return i;
  95. }
  96. return -1;
  97. }
  98. void AddParameter(JSBFunctionType* parm)
  99. {
  100. parameters_.Push(parm);
  101. }
  102. void Process();
  103. void WriteParameterMarshal(String& source);
  104. void WriteFunction(String& source);
  105. void WriteConstructor(String& source);
  106. void Write(String& source);
  107. void Dump()
  108. {
  109. String sig;
  110. if (!returnType_)
  111. sig += "void ";
  112. else
  113. sig += returnType_->ToString() + " ";
  114. sig += name_;
  115. sig += "(";
  116. for (unsigned i = 0; i < parameters_.Size(); i++)
  117. {
  118. sig += parameters_.At(i)->ToString();
  119. if (i != parameters_.Size() - 1)
  120. sig += ", ";
  121. }
  122. sig += ");";
  123. LOGINFOF(" %s", sig.CString());
  124. }
  125. private:
  126. SharedPtr<JSBClass> class_;
  127. String name_;
  128. String propertyName_;
  129. JSBFunctionType* returnType_;
  130. Vector<JSBFunctionType*> parameters_;
  131. String docString_;
  132. bool isConstructor_;
  133. bool isDestructor_;
  134. bool isGetter_;
  135. bool isSetter_;
  136. bool isOverride_;
  137. bool skip_;
  138. };
  139. }