JSBFunction.cpp 4.5 KB

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