JSBFunction.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "JSBindTypes.h"
  25. #include "JSBClass.h"
  26. #include "JSBType.h"
  27. #include "JSBSymbol.h"
  28. using namespace Atomic;
  29. namespace ToolCore
  30. {
  31. class JSBFunctionType
  32. {
  33. public:
  34. JSBFunctionType(JSBType* type) : type_(type)
  35. {
  36. isSharedPtr_ = false;
  37. isPointer_ = false;
  38. isReference_ = false;
  39. isTemplate_ = false;
  40. isConst_ = false;
  41. }
  42. // returns true if the types match
  43. bool Match(const JSBFunctionType* other)
  44. {
  45. if (!other)
  46. return false;
  47. if (isSharedPtr_ != other->isSharedPtr_)
  48. return false;
  49. if (isPointer_ != other->isPointer_)
  50. return false;
  51. if (isReference_ != other->isReference_)
  52. return false;
  53. if (isTemplate_ != other->isTemplate_)
  54. return false;
  55. if (isConst_ != other->isConst_)
  56. return false;
  57. if (name_ != other->name_)
  58. return false;
  59. if (!type_ && !other->type_)
  60. return true;
  61. if (!type_ || !other->type_)
  62. return false;
  63. return type_->Match(other->type_);
  64. }
  65. bool isSharedPtr_;
  66. bool isPointer_;
  67. bool isReference_;
  68. bool isTemplate_;
  69. bool isConst_;
  70. String name_;
  71. String initializer_;
  72. JSBType* type_;
  73. String ToString()
  74. {
  75. String tstring = type_->ToString();
  76. if (isPointer_)
  77. tstring += "*";
  78. if (isReference_)
  79. tstring += "&";
  80. if (name_.Length())
  81. {
  82. tstring += " " + name_;
  83. }
  84. return tstring;
  85. }
  86. String ToArgString(int index)
  87. {
  88. String tstring = type_->ToString();
  89. if (isPointer_)
  90. tstring += "*";
  91. if (isReference_ && tstring != "String")
  92. tstring += "&";
  93. if (name_.Length())
  94. {
  95. tstring.AppendWithFormat(" __arg%i", index);
  96. }
  97. return tstring;
  98. }
  99. };
  100. class JSBFunction : public JSBSymbol
  101. {
  102. friend class JSFunctionWriter;
  103. friend class CSFunctionWriter;
  104. public:
  105. JSBFunction(JSBClass* klass) : class_(klass), returnType_(0),
  106. isConstructor_(false), isDestructor_(false),
  107. isGetter_(false), isSetter_(false),
  108. isOverload_(false), skip_(false),
  109. isVirtual_(false), isStatic_(false)
  110. {
  111. }
  112. const String& GetName() { return name_; }
  113. bool Match(JSBFunction* func);
  114. bool IsConstructor() { return isConstructor_; }
  115. bool IsDestructor() { return isDestructor_; }
  116. bool IsSetter() { return isSetter_; }
  117. bool IsGetter() { return isGetter_; }
  118. bool IsOverload() { return isOverload_; }
  119. bool IsVirtual() { return isVirtual_; }
  120. bool IsStatic() { return isStatic_; }
  121. bool Skip(BindingLanguage language = BINDINGLANGUAGE_ANY)
  122. {
  123. if (skip_ || language == BINDINGLANGUAGE_ANY)
  124. return skip_;
  125. return GetSkipLanguage(language);
  126. }
  127. JSBClass* GetClass() { return class_; }
  128. const String& GetPropertyName() { return propertyName_; }
  129. JSBFunctionType* GetReturnType() { return returnType_; }
  130. /// Get class return type or null
  131. JSBClass* GetReturnClass();
  132. Vector<JSBFunctionType*>& GetParameters() { return parameters_; }
  133. const String& GetDocString() { return docString_; }
  134. void SetName(const String& name) { name_ = name; }
  135. void SetConstructor(bool value = true) { isConstructor_ = value; }
  136. void SetDestructor(bool value = true) { isDestructor_ = value; }
  137. void SetSetter(bool value = true) { isSetter_ = value; }
  138. void SetGetter(bool value = true) { isGetter_ = value; }
  139. void SetOverload(bool value = true) { isOverload_ = value; }
  140. void SetVirtual(bool value = true) { isVirtual_ = value; }
  141. void SetStatic(bool value = true) { isStatic_ = value; }
  142. void SetSkip(bool value) { skip_ = value; }
  143. void SetReturnType(JSBFunctionType* retType) { returnType_ = retType; }
  144. void SetDocString(const String& docString) { docString_ = docString; }
  145. void SetSkipLanguage(BindingLanguage language, bool skip = true)
  146. {
  147. bindSkip_[language] = skip;
  148. }
  149. /// Returns true is _skip is set or skip is set for specific binding language
  150. bool GetSkipLanguage(BindingLanguage language) const
  151. {
  152. if (skip_)
  153. return true;
  154. if (bindSkip_.Contains(language))
  155. return bindSkip_[language];
  156. return false;
  157. }
  158. int FirstDefaultParameter()
  159. {
  160. for (unsigned i = 0; i < parameters_.Size(); i++)
  161. {
  162. if (parameters_[i]->initializer_.Length())
  163. return i;
  164. }
  165. return -1;
  166. }
  167. void AddParameter(JSBFunctionType* parm)
  168. {
  169. parameters_.Push(parm);
  170. }
  171. void Process();
  172. void WriteParameterMarshal(String& source);
  173. void WriteFunction(String& source);
  174. void WriteConstructor(String& source);
  175. void Write(String& source);
  176. void Dump()
  177. {
  178. String sig;
  179. if (!returnType_)
  180. sig += "void ";
  181. else
  182. sig += returnType_->ToString() + " ";
  183. sig += name_;
  184. sig += "(";
  185. for (unsigned i = 0; i < parameters_.Size(); i++)
  186. {
  187. sig += parameters_.At(i)->ToString();
  188. if (i != parameters_.Size() - 1)
  189. sig += ", ";
  190. }
  191. sig += ");";
  192. ATOMIC_LOGINFOF(" %s", sig.CString());
  193. }
  194. private:
  195. SharedPtr<JSBClass> class_;
  196. String name_;
  197. String propertyName_;
  198. JSBFunctionType* returnType_;
  199. Vector<JSBFunctionType*> parameters_;
  200. String docString_;
  201. bool isConstructor_;
  202. bool isDestructor_;
  203. bool isGetter_;
  204. bool isSetter_;
  205. bool isOverload_;
  206. bool isVirtual_;
  207. bool isStatic_;
  208. bool skip_;
  209. HashMap<unsigned, bool> bindSkip_;
  210. };
  211. }