JSBFunction.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. isConstructor_(false), isDestructor_(false),
  28. isGetter_(false), isSetter_(false),
  29. isOverload_(false), skip_(false),
  30. isVirtual_(false), isStatic_(false)
  31. {
  32. id_ = idCounter_++;
  33. }
  34. void JSBFunction::Process()
  35. {
  36. if (skip_)
  37. {
  38. return;
  39. }
  40. // only setup properties for methods which weren't skipped for JS, for example overloads
  41. if (GetSkipLanguage(BINDINGLANGUAGE_JAVASCRIPT))
  42. return;
  43. // if not already marked as a getter
  44. if (!isGetter_)
  45. {
  46. if (!parameters_.Size() && returnType_)
  47. {
  48. if (name_.Length() > 3 && name_.StartsWith("Get") && isupper(name_[3]))
  49. {
  50. String pname = name_.Substring(3);
  51. class_->SetSkipFunction(pname);
  52. isGetter_ = true;
  53. propertyName_ = pname;
  54. }
  55. }
  56. }
  57. if (!isSetter_)
  58. {
  59. if (parameters_.Size() == 1 && !returnType_)
  60. {
  61. if (name_.Length() > 3 && name_.StartsWith("Set") && isupper(name_[3]))
  62. {
  63. String pname = name_.Substring(3);
  64. class_->SetSkipFunction(pname);
  65. isSetter_ = true;
  66. propertyName_ = pname;
  67. }
  68. }
  69. }
  70. if (isGetter_)
  71. class_->AddPropertyFunction(this);
  72. if (isSetter_)
  73. class_->AddPropertyFunction(this);
  74. }
  75. bool JSBFunction::Match(JSBFunction* func)
  76. {
  77. if (func->Skip() || func->GetName() != name_)
  78. return false;
  79. if (!returnType_)
  80. {
  81. if (func->GetReturnType())
  82. return false;
  83. }
  84. else
  85. {
  86. if (!returnType_->Match(func->GetReturnType()))
  87. return false;
  88. }
  89. Vector<JSBFunctionType*>& fparams = func->GetParameters();
  90. if (parameters_.Size() != fparams.Size())
  91. return false;
  92. for ( unsigned j = 0; j < fparams.Size(); j++)
  93. {
  94. if (!parameters_[j]->Match(fparams[j]))
  95. return false;
  96. }
  97. return true;
  98. }
  99. JSBClass* JSBFunction::GetReturnClass()
  100. {
  101. if (!returnType_)
  102. return 0;
  103. if (!returnType_->type_->asClassType())
  104. return 0;
  105. return returnType_->type_->asClassType()->class_;
  106. }
  107. }