JSBFunction.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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() const { return name_; }
  112. bool Match(JSBFunction* func);
  113. // clones function and adds to specified class
  114. JSBFunction* Clone(JSBClass* dstClass);
  115. bool IsConstructor() const { return isConstructor_; }
  116. bool IsDestructor() const { return isDestructor_; }
  117. bool IsSetter() const { return isSetter_; }
  118. bool IsGetter() const { return isGetter_; }
  119. bool IsOverload() { return isOverload_; }
  120. bool IsVirtual() const { return isVirtual_; }
  121. bool IsStatic() const { return isStatic_; }
  122. // get whether this method is inherited from a parent interface
  123. bool IsInheritedInterface() const { return isInheritedInterface_; }
  124. // true if return value has been mutated to be passed in at end of parameters (optimization)
  125. bool HasMutatedReturn() const { return hasMutatedReturn_; }
  126. bool Skip(BindingLanguage language = BINDINGLANGUAGE_ANY) const
  127. {
  128. if (skip_ || language == BINDINGLANGUAGE_ANY)
  129. return skip_;
  130. return GetSkipLanguage(language);
  131. }
  132. unsigned GetID() const { return id_; }
  133. JSBClass* GetClass() const { return class_; }
  134. const String& GetPropertyName() { return propertyName_; }
  135. JSBFunctionType* GetReturnType() const { return returnType_; }
  136. /// Get class return type or null
  137. JSBClass* GetReturnClass();
  138. const Vector<JSBFunctionType*>& GetParameters() const { return parameters_; }
  139. const String& GetDocString() const { return docString_; }
  140. void SetName(const String& name) { name_ = name; }
  141. void SetClass(JSBClass* klass) { class_ = klass; }
  142. void SetConstructor(bool value = true) { isConstructor_ = value; }
  143. void SetDestructor(bool value = true) { isDestructor_ = value; }
  144. void SetSetter(bool value = true) { isSetter_ = value; }
  145. void SetGetter(bool value = true) { isGetter_ = value; }
  146. void SetOverload(bool value = true) { isOverload_ = value; }
  147. void SetVirtual(bool value = true) { isVirtual_ = value; }
  148. void SetStatic(bool value = true) { isStatic_ = value; }
  149. void SetSkip(bool value) { skip_ = value; }
  150. void SetReturnType(JSBFunctionType* retType) { returnType_ = retType; }
  151. void SetInheritedInterface(bool interface) { isInheritedInterface_ = interface; }
  152. void SetDocString(const String& docString) { docString_ = docString; }
  153. void SetSkipLanguage(BindingLanguage language, bool skip = true)
  154. {
  155. if (skip)
  156. {
  157. if (!skipLanguages_.Contains(language))
  158. skipLanguages_.Push(language);
  159. }
  160. else
  161. {
  162. if (skipLanguages_.Contains(language))
  163. skipLanguages_.Remove(language);
  164. }
  165. }
  166. /// Returns true is _skip is set or skip is set for specific binding language
  167. bool GetSkipLanguage(BindingLanguage language) const
  168. {
  169. if (skip_)
  170. return true;
  171. return skipLanguages_.Contains(language);
  172. }
  173. int FirstDefaultParameter()
  174. {
  175. for (unsigned i = 0; i < parameters_.Size(); i++)
  176. {
  177. if (parameters_[i]->initializer_.Length())
  178. return i;
  179. }
  180. return -1;
  181. }
  182. void AddParameter(JSBFunctionType* parm)
  183. {
  184. parameters_.Push(parm);
  185. }
  186. void Process();
  187. void WriteParameterMarshal(String& source);
  188. void WriteFunction(String& source);
  189. void WriteConstructor(String& source);
  190. void Write(String& source);
  191. void Dump()
  192. {
  193. String sig = ToString("Function ID: %u - ", id_);
  194. if (!returnType_)
  195. sig += "void ";
  196. else
  197. sig += returnType_->ToString() + " ";
  198. sig += name_;
  199. sig += "(";
  200. for (unsigned i = 0; i < parameters_.Size(); i++)
  201. {
  202. sig += parameters_.At(i)->ToString();
  203. if (i != parameters_.Size() - 1)
  204. sig += ", ";
  205. }
  206. sig += ");";
  207. String skipText;
  208. if (GetSkipLanguage(BINDINGLANGUAGE_CSHARP))
  209. skipText += "C#";
  210. if (GetSkipLanguage(BINDINGLANGUAGE_JAVASCRIPT))
  211. skipText += " JavaScript";
  212. if (skipText.Length())
  213. {
  214. sig += ToString(" (Skipped: %s)", skipText.CString());
  215. }
  216. ATOMIC_LOGINFOF(" %s", sig.CString());
  217. }
  218. private:
  219. unsigned id_;
  220. static unsigned idCounter_;
  221. SharedPtr<JSBClass> class_;
  222. String name_;
  223. String propertyName_;
  224. JSBFunctionType* returnType_;
  225. Vector<JSBFunctionType*> parameters_;
  226. String docString_;
  227. bool isInheritedInterface_;
  228. bool isConstructor_;
  229. bool isDestructor_;
  230. bool isGetter_;
  231. bool isSetter_;
  232. bool isOverload_;
  233. bool isVirtual_;
  234. bool isStatic_;
  235. bool hasMutatedReturn_;
  236. bool skip_;
  237. PODVector<BindingLanguage> skipLanguages_;
  238. };
  239. }