JSBFunction.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include <Atomic/IO/Log.h>
  24. #include "JSBClass.h"
  25. #include "JSBType.h"
  26. #include "JSBSymbol.h"
  27. using namespace Atomic;
  28. namespace ToolCore
  29. {
  30. class JSBFunctionType
  31. {
  32. public:
  33. JSBFunctionType(JSBType* type) : type_(type)
  34. {
  35. isSharedPtr_ = false;
  36. isPointer_ = false;
  37. isReference_ = false;
  38. isTemplate_ = false;
  39. isConst_ = false;
  40. }
  41. // returns true if the types match
  42. bool Match(const JSBFunctionType* other)
  43. {
  44. if (!other)
  45. return false;
  46. if (isSharedPtr_ != other->isSharedPtr_)
  47. return false;
  48. if (isPointer_ != other->isPointer_)
  49. return false;
  50. if (isReference_ != other->isReference_)
  51. return false;
  52. if (isTemplate_ != other->isTemplate_)
  53. return false;
  54. if (isConst_ != other->isConst_)
  55. return false;
  56. if (name_ != other->name_)
  57. return false;
  58. if (!type_ && !other->type_)
  59. return true;
  60. if (!type_ || !other->type_)
  61. return false;
  62. return type_->Match(other->type_);
  63. }
  64. bool isSharedPtr_;
  65. bool isPointer_;
  66. bool isReference_;
  67. bool isTemplate_;
  68. bool isConst_;
  69. String name_;
  70. String initializer_;
  71. JSBType* type_;
  72. String ToString()
  73. {
  74. String tstring = type_->ToString();
  75. if (isPointer_)
  76. tstring += "*";
  77. if (isReference_)
  78. tstring += "&";
  79. if (name_.Length())
  80. {
  81. tstring += " " + name_;
  82. }
  83. return tstring;
  84. }
  85. String ToArgString(int index)
  86. {
  87. String tstring = type_->ToString();
  88. if (isPointer_)
  89. tstring += "*";
  90. if (isReference_ && tstring != "String")
  91. tstring += "&";
  92. if (name_.Length())
  93. {
  94. tstring.AppendWithFormat(" __arg%i", index);
  95. }
  96. return tstring;
  97. }
  98. };
  99. class JSBFunction : public JSBSymbol
  100. {
  101. friend class JSFunctionWriter;
  102. friend class CSFunctionWriter;
  103. public:
  104. JSBFunction(JSBClass* klass) : class_(klass), returnType_(0),
  105. isConstructor_(false), isDestructor_(false),
  106. isGetter_(false), isSetter_(false),
  107. isOverload_(false), skip_(false),
  108. isVirtual_(false), isStatic_(false)
  109. {
  110. }
  111. const String& GetName() { return name_; }
  112. bool Match(JSBFunction* func);
  113. bool IsConstructor() { return isConstructor_; }
  114. bool IsDestructor() { return isDestructor_; }
  115. bool IsSetter() { return isSetter_; }
  116. bool IsGetter() { return isGetter_; }
  117. bool IsOverload() { return isOverload_; }
  118. bool IsVirtual() { return isVirtual_; }
  119. bool IsStatic() { return isStatic_; }
  120. bool Skip() { return skip_; }
  121. JSBClass* GetClass() { return class_; }
  122. const String& GetPropertyName() { return propertyName_; }
  123. JSBFunctionType* GetReturnType() { return returnType_; }
  124. /// Get class return type or null
  125. JSBClass* GetReturnClass();
  126. Vector<JSBFunctionType*>& GetParameters() { return parameters_; }
  127. const String& GetDocString() { return docString_; }
  128. void SetName(const String& name) { name_ = name; }
  129. void SetConstructor(bool value = true) { isConstructor_ = value; }
  130. void SetDestructor(bool value = true) { isDestructor_ = value; }
  131. void SetSetter(bool value = true) { isSetter_ = value; }
  132. void SetGetter(bool value = true) { isGetter_ = value; }
  133. void SetOverload(bool value = true) { isOverload_ = value; }
  134. void SetVirtual(bool value = true) { isVirtual_ = value; }
  135. void SetStatic(bool value = true) { isStatic_ = value; }
  136. void SetSkip(bool value) { skip_ = value; }
  137. void SetReturnType(JSBFunctionType* retType) { returnType_ = retType; }
  138. void SetDocString(const String& docString) { docString_ = docString; }
  139. int FirstDefaultParameter()
  140. {
  141. for (unsigned i = 0; i < parameters_.Size(); i++)
  142. {
  143. if (parameters_[i]->initializer_.Length())
  144. return i;
  145. }
  146. return -1;
  147. }
  148. void AddParameter(JSBFunctionType* parm)
  149. {
  150. parameters_.Push(parm);
  151. }
  152. void Process();
  153. void WriteParameterMarshal(String& source);
  154. void WriteFunction(String& source);
  155. void WriteConstructor(String& source);
  156. void Write(String& source);
  157. void Dump()
  158. {
  159. String sig;
  160. if (!returnType_)
  161. sig += "void ";
  162. else
  163. sig += returnType_->ToString() + " ";
  164. sig += name_;
  165. sig += "(";
  166. for (unsigned i = 0; i < parameters_.Size(); i++)
  167. {
  168. sig += parameters_.At(i)->ToString();
  169. if (i != parameters_.Size() - 1)
  170. sig += ", ";
  171. }
  172. sig += ");";
  173. LOGINFOF(" %s", sig.CString());
  174. }
  175. private:
  176. SharedPtr<JSBClass> class_;
  177. String name_;
  178. String propertyName_;
  179. JSBFunctionType* returnType_;
  180. Vector<JSBFunctionType*> parameters_;
  181. String docString_;
  182. bool isConstructor_;
  183. bool isDestructor_;
  184. bool isGetter_;
  185. bool isSetter_;
  186. bool isOverload_;
  187. bool isVirtual_;
  188. bool isStatic_;
  189. bool skip_;
  190. };
  191. }