JSBFunction.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 (type_->asStringType() || type_->asStringHashType())
  48. {
  49. if (other->type_->asStringType() || other->type_->asStringHashType())
  50. return true;
  51. }
  52. if (isSharedPtr_ != other->isSharedPtr_)
  53. return false;
  54. if (isPointer_ != other->isPointer_)
  55. return false;
  56. if (isReference_ != other->isReference_)
  57. return false;
  58. if (isTemplate_ != other->isTemplate_)
  59. return false;
  60. if (isConst_ != other->isConst_)
  61. return false;
  62. if (name_ != other->name_)
  63. return false;
  64. if (!type_ && !other->type_)
  65. return true;
  66. if (!type_ || !other->type_)
  67. return false;
  68. return type_->Match(other->type_);
  69. }
  70. bool isSharedPtr_;
  71. bool isPointer_;
  72. bool isReference_;
  73. bool isTemplate_;
  74. bool isConst_;
  75. String name_;
  76. String initializer_;
  77. JSBType* type_;
  78. String ToString()
  79. {
  80. String tstring = type_->ToString();
  81. if (isPointer_)
  82. tstring += "*";
  83. if (isReference_)
  84. tstring += "&";
  85. if (name_.Length())
  86. {
  87. tstring += " " + name_;
  88. }
  89. return tstring;
  90. }
  91. String ToArgString(int index)
  92. {
  93. String tstring = type_->ToString();
  94. if (isPointer_)
  95. tstring += "*";
  96. if (isReference_ && tstring != "String")
  97. tstring += "&";
  98. if (name_.Length())
  99. {
  100. tstring.AppendWithFormat(" __arg%i", index);
  101. }
  102. return tstring;
  103. }
  104. };
  105. class JSBFunction : public JSBSymbol
  106. {
  107. friend class JSFunctionWriter;
  108. friend class CSFunctionWriter;
  109. public:
  110. JSBFunction(JSBClass* klass);
  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(BindingLanguage language = BINDINGLANGUAGE_ANY)
  121. {
  122. if (skip_ || language == BINDINGLANGUAGE_ANY)
  123. return skip_;
  124. return GetSkipLanguage(language);
  125. }
  126. unsigned GetID() const { return id_; }
  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. if (skip)
  148. {
  149. if (!skipLanguages_.Contains(language))
  150. skipLanguages_.Push(language);
  151. }
  152. else
  153. {
  154. if (skipLanguages_.Contains(language))
  155. skipLanguages_.Remove(language);
  156. }
  157. }
  158. /// Returns true is _skip is set or skip is set for specific binding language
  159. bool GetSkipLanguage(BindingLanguage language) const
  160. {
  161. if (skip_)
  162. return true;
  163. return skipLanguages_.Contains(language);
  164. }
  165. int FirstDefaultParameter()
  166. {
  167. for (unsigned i = 0; i < parameters_.Size(); i++)
  168. {
  169. if (parameters_[i]->initializer_.Length())
  170. return i;
  171. }
  172. return -1;
  173. }
  174. void AddParameter(JSBFunctionType* parm)
  175. {
  176. parameters_.Push(parm);
  177. }
  178. void Process();
  179. void WriteParameterMarshal(String& source);
  180. void WriteFunction(String& source);
  181. void WriteConstructor(String& source);
  182. void Write(String& source);
  183. void Dump()
  184. {
  185. String sig;
  186. if (!returnType_)
  187. sig += "void ";
  188. else
  189. sig += returnType_->ToString() + " ";
  190. sig += name_;
  191. sig += "(";
  192. for (unsigned i = 0; i < parameters_.Size(); i++)
  193. {
  194. sig += parameters_.At(i)->ToString();
  195. if (i != parameters_.Size() - 1)
  196. sig += ", ";
  197. }
  198. sig += ");";
  199. ATOMIC_LOGINFOF(" %s", sig.CString());
  200. }
  201. private:
  202. unsigned id_;
  203. static unsigned idCounter_;
  204. SharedPtr<JSBClass> class_;
  205. String name_;
  206. String propertyName_;
  207. JSBFunctionType* returnType_;
  208. Vector<JSBFunctionType*> parameters_;
  209. String docString_;
  210. bool isConstructor_;
  211. bool isDestructor_;
  212. bool isGetter_;
  213. bool isSetter_;
  214. bool isOverload_;
  215. bool isVirtual_;
  216. bool isStatic_;
  217. bool skip_;
  218. PODVector<BindingLanguage> skipLanguages_;
  219. };
  220. }