JSBFunction.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. if (!parameters_.Size() && returnType_)
  48. {
  49. if (name_.Length() > 3 && name_.StartsWith("Get") && isupper(name_[3]))
  50. {
  51. String pname = name_.Substring(3);
  52. class_->SetSkipFunction(pname);
  53. isGetter_ = true;
  54. propertyName_ = pname;
  55. }
  56. }
  57. }
  58. if (!isSetter_)
  59. {
  60. if (parameters_.Size() == 1 && !returnType_)
  61. {
  62. if (name_.Length() > 3 && name_.StartsWith("Set") && isupper(name_[3]))
  63. {
  64. String pname = name_.Substring(3);
  65. class_->SetSkipFunction(pname);
  66. isSetter_ = true;
  67. propertyName_ = pname;
  68. }
  69. }
  70. }
  71. if (isGetter_)
  72. class_->AddPropertyFunction(this);
  73. if (isSetter_)
  74. class_->AddPropertyFunction(this);
  75. }
  76. bool JSBFunction::Match(JSBFunction* func)
  77. {
  78. if (func->Skip() || func->GetName() != name_)
  79. return false;
  80. if (!returnType_)
  81. {
  82. if (func->GetReturnType())
  83. return false;
  84. }
  85. else
  86. {
  87. if (!returnType_->Match(func->GetReturnType()))
  88. return false;
  89. }
  90. const Vector<JSBFunctionType*>& fparams = func->GetParameters();
  91. if (parameters_.Size() != fparams.Size())
  92. return false;
  93. for ( unsigned j = 0; j < fparams.Size(); j++)
  94. {
  95. if (!parameters_[j]->Match(fparams[j]))
  96. return false;
  97. }
  98. return true;
  99. }
  100. JSBClass* JSBFunction::GetReturnClass()
  101. {
  102. if (!returnType_)
  103. return 0;
  104. if (!returnType_->type_->asClassType())
  105. return 0;
  106. return returnType_->type_->asClassType()->class_;
  107. }
  108. JSBFunction* JSBFunction::Clone(JSBClass* dstClass)
  109. {
  110. JSBFunction* dst = new JSBFunction(dstClass);
  111. dst->name_ = name_;
  112. dst->propertyName_ = propertyName_;;
  113. dst->returnType_ = returnType_;
  114. dst->parameters_ = parameters_;
  115. dst->docString_ = docString_;
  116. dst->isInterface_ = isInterface_;
  117. dst->isConstructor_ = isConstructor_;
  118. dst->isDestructor_ = isDestructor_;
  119. dst->isGetter_ = isGetter_;
  120. dst->isSetter_ = isSetter_;
  121. dst->isOverload_ = isOverload_;
  122. dst->isVirtual_ = isVirtual_;
  123. dst->isStatic_ = isStatic_;
  124. dst->skip_ = skip_;
  125. dst->skipLanguages_ = skipLanguages_;
  126. return dst;
  127. }
  128. }