JSBFunction.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #include "JSBFunction.h"
  23. namespace ToolCore
  24. {
  25. unsigned JSBFunction::idCounter_ = 1;
  26. JSBFunction::JSBFunction(JSBClass* klass) : class_(klass), returnType_(0),
  27. isInheritedInterface_(false),
  28. isConstructor_(false), isDestructor_(false),
  29. isGetter_(false), isSetter_(false),
  30. isOverload_(false), skip_(false),
  31. isVirtual_(false), isStatic_(false),
  32. hasMutatedReturn_(false)
  33. {
  34. id_ = idCounter_++;
  35. }
  36. void JSBFunction::Process()
  37. {
  38. if (skip_)
  39. {
  40. return;
  41. }
  42. // methods that return a VariantVector are mutated to take a ScriptVector argument
  43. if (!hasMutatedReturn_ && returnType_ && returnType_->type_->asVectorType())
  44. {
  45. JSBVectorType* vtype = returnType_->type_->asVectorType();
  46. if (vtype->isVariantVector_)
  47. {
  48. // mark up as mutated
  49. hasMutatedReturn_ = true;
  50. JSBFunctionType* ftype = new JSBFunctionType(returnType_->type_);
  51. ftype->name_ = "__retValue";
  52. parameters_.Push(ftype);
  53. returnType_ = 0;
  54. }
  55. }
  56. // only setup properties for methods which weren't skipped for JS, for example overloads
  57. if (GetSkipLanguage(BINDINGLANGUAGE_JAVASCRIPT))
  58. return;
  59. // if not already marked as a getter
  60. if (!isGetter_)
  61. {
  62. // Check for whether this is a property getter function
  63. // IsObject is special as we don't want a property to it
  64. if (!parameters_.Size() && returnType_ && name_ != "IsObject")
  65. {
  66. if ((name_.Length() > 3 && name_.StartsWith("Get") && isupper(name_[3])) ||
  67. (name_.Length() > 2 && name_.StartsWith("Is") && isupper(name_[2])) )
  68. {
  69. String pname = name_.Substring(name_.StartsWith("Get") ? 3 : 2);
  70. class_->SetSkipFunction(pname);
  71. isGetter_ = true;
  72. propertyName_ = pname;
  73. }
  74. }
  75. }
  76. if (!isSetter_)
  77. {
  78. if (parameters_.Size() == 1 && !returnType_)
  79. {
  80. if (name_.Length() > 3 && name_.StartsWith("Set") && isupper(name_[3]))
  81. {
  82. String pname = name_.Substring(3);
  83. class_->SetSkipFunction(pname);
  84. isSetter_ = true;
  85. propertyName_ = pname;
  86. }
  87. }
  88. }
  89. if (isGetter_)
  90. class_->AddPropertyFunction(this);
  91. if (isSetter_)
  92. class_->AddPropertyFunction(this);
  93. }
  94. bool JSBFunction::Match(JSBFunction* func)
  95. {
  96. if (func->Skip() || func->GetName() != name_)
  97. return false;
  98. if (!returnType_)
  99. {
  100. if (func->GetReturnType())
  101. return false;
  102. }
  103. else
  104. {
  105. if (!returnType_->Match(func->GetReturnType()))
  106. return false;
  107. }
  108. const Vector<JSBFunctionType*>& fparams = func->GetParameters();
  109. if (parameters_.Size() != fparams.Size())
  110. return false;
  111. for ( unsigned j = 0; j < fparams.Size(); j++)
  112. {
  113. if (!parameters_[j]->Match(fparams[j]))
  114. return false;
  115. }
  116. return true;
  117. }
  118. JSBClass* JSBFunction::GetReturnClass()
  119. {
  120. if (!returnType_)
  121. return 0;
  122. if (!returnType_->type_->asClassType())
  123. return 0;
  124. return returnType_->type_->asClassType()->class_;
  125. }
  126. JSBFunction* JSBFunction::Clone(JSBClass* dstClass)
  127. {
  128. JSBFunction* dst = new JSBFunction(dstClass);
  129. dst->name_ = name_;
  130. dst->propertyName_ = propertyName_;;
  131. dst->returnType_ = returnType_;
  132. dst->parameters_ = parameters_;
  133. dst->docString_ = docString_;
  134. dst->isInheritedInterface_ = isInheritedInterface_;
  135. dst->isConstructor_ = isConstructor_;
  136. dst->isDestructor_ = isDestructor_;
  137. dst->isGetter_ = isGetter_;
  138. dst->isSetter_ = isSetter_;
  139. dst->isOverload_ = isOverload_;
  140. dst->isVirtual_ = isVirtual_;
  141. dst->isStatic_ = isStatic_;
  142. dst->skip_ = skip_;
  143. dst->skipLanguages_ = skipLanguages_;
  144. return dst;
  145. }
  146. }