JSBFunction.h 4.1 KB

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