JSBFunction.h 8.0 KB

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