callable.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /**************************************************************************/
  2. /* callable.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "callable.h"
  31. #include "core/object/object.h"
  32. #include "core/object/ref_counted.h"
  33. #include "core/object/script_language.h"
  34. #include "core/variant/callable_bind.h"
  35. #include "core/variant/variant_callable.h"
  36. void Callable::call_deferredp(const Variant **p_arguments, int p_argcount) const {
  37. MessageQueue::get_singleton()->push_callablep(*this, p_arguments, p_argcount, true);
  38. }
  39. void Callable::callp(const Variant **p_arguments, int p_argcount, Variant &r_return_value, CallError &r_call_error) const {
  40. if (is_null()) {
  41. r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
  42. r_call_error.argument = 0;
  43. r_call_error.expected = 0;
  44. r_return_value = Variant();
  45. } else if (is_custom()) {
  46. if (!is_valid()) {
  47. r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
  48. r_call_error.argument = 0;
  49. r_call_error.expected = 0;
  50. r_return_value = Variant();
  51. return;
  52. }
  53. custom->call(p_arguments, p_argcount, r_return_value, r_call_error);
  54. } else {
  55. Object *obj = ObjectDB::get_instance(ObjectID(object));
  56. #ifdef DEBUG_ENABLED
  57. if (!obj) {
  58. r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
  59. r_call_error.argument = 0;
  60. r_call_error.expected = 0;
  61. r_return_value = Variant();
  62. return;
  63. }
  64. #endif
  65. r_return_value = obj->callp(method, p_arguments, p_argcount, r_call_error);
  66. }
  67. }
  68. Variant Callable::callv(const Array &p_arguments) const {
  69. int argcount = p_arguments.size();
  70. const Variant **argptrs = nullptr;
  71. if (argcount) {
  72. argptrs = (const Variant **)alloca(sizeof(Variant *) * argcount);
  73. for (int i = 0; i < argcount; i++) {
  74. argptrs[i] = &p_arguments[i];
  75. }
  76. }
  77. CallError ce;
  78. Variant ret;
  79. callp(argptrs, argcount, ret, ce);
  80. return ret;
  81. }
  82. Error Callable::rpcp(int p_id, const Variant **p_arguments, int p_argcount, CallError &r_call_error) const {
  83. if (is_null()) {
  84. r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
  85. r_call_error.argument = 0;
  86. r_call_error.expected = 0;
  87. return ERR_UNCONFIGURED;
  88. } else if (!is_custom()) {
  89. Object *obj = ObjectDB::get_instance(ObjectID(object));
  90. #ifdef DEBUG_ENABLED
  91. if (!obj || !obj->is_class("Node")) {
  92. r_call_error.error = CallError::CALL_ERROR_INSTANCE_IS_NULL;
  93. r_call_error.argument = 0;
  94. r_call_error.expected = 0;
  95. return ERR_UNCONFIGURED;
  96. }
  97. #endif
  98. int argcount = p_argcount + 2;
  99. const Variant **argptrs = (const Variant **)alloca(sizeof(Variant *) * argcount);
  100. const Variant args[2] = { p_id, method };
  101. argptrs[0] = &args[0];
  102. argptrs[1] = &args[1];
  103. for (int i = 0; i < p_argcount; ++i) {
  104. argptrs[i + 2] = p_arguments[i];
  105. }
  106. CallError tmp; // TODO: Check `tmp`?
  107. Error err = (Error)obj->callp(SNAME("rpc_id"), argptrs, argcount, tmp).operator int64_t();
  108. r_call_error.error = Callable::CallError::CALL_OK;
  109. return err;
  110. } else {
  111. return custom->rpc(p_id, p_arguments, p_argcount, r_call_error);
  112. }
  113. }
  114. Callable Callable::bindp(const Variant **p_arguments, int p_argcount) const {
  115. Vector<Variant> args;
  116. args.resize(p_argcount);
  117. for (int i = 0; i < p_argcount; i++) {
  118. args.write[i] = *p_arguments[i];
  119. }
  120. return Callable(memnew(CallableCustomBind(*this, args)));
  121. }
  122. Callable Callable::bindv(const Array &p_arguments) {
  123. if (p_arguments.is_empty()) {
  124. return *this; // No point in creating a new callable if nothing is bound.
  125. }
  126. Vector<Variant> args;
  127. args.resize(p_arguments.size());
  128. for (int i = 0; i < p_arguments.size(); i++) {
  129. args.write[i] = p_arguments[i];
  130. }
  131. return Callable(memnew(CallableCustomBind(*this, args)));
  132. }
  133. Callable Callable::unbind(int p_argcount) const {
  134. ERR_FAIL_COND_V_MSG(p_argcount <= 0, Callable(*this), "Amount of unbind() arguments must be 1 or greater.");
  135. return Callable(memnew(CallableCustomUnbind(*this, p_argcount)));
  136. }
  137. bool Callable::is_valid() const {
  138. if (is_custom()) {
  139. return get_custom()->is_valid();
  140. } else {
  141. return get_object() && get_object()->has_method(get_method());
  142. }
  143. }
  144. Object *Callable::get_object() const {
  145. if (is_null()) {
  146. return nullptr;
  147. } else if (is_custom()) {
  148. return ObjectDB::get_instance(custom->get_object());
  149. } else {
  150. return ObjectDB::get_instance(ObjectID(object));
  151. }
  152. }
  153. ObjectID Callable::get_object_id() const {
  154. if (is_null()) {
  155. return ObjectID();
  156. } else if (is_custom()) {
  157. return custom->get_object();
  158. } else {
  159. return ObjectID(object);
  160. }
  161. }
  162. StringName Callable::get_method() const {
  163. if (is_custom()) {
  164. return get_custom()->get_method();
  165. }
  166. return method;
  167. }
  168. int Callable::get_argument_count(bool *r_is_valid) const {
  169. if (is_custom()) {
  170. bool valid = false;
  171. return custom->get_argument_count(r_is_valid ? *r_is_valid : valid);
  172. } else if (!is_null()) {
  173. return get_object()->get_method_argument_count(method, r_is_valid);
  174. } else {
  175. if (r_is_valid) {
  176. *r_is_valid = false;
  177. }
  178. return 0;
  179. }
  180. }
  181. int Callable::get_bound_arguments_count() const {
  182. if (!is_null() && is_custom()) {
  183. return custom->get_bound_arguments_count();
  184. } else {
  185. return 0;
  186. }
  187. }
  188. void Callable::get_bound_arguments_ref(Vector<Variant> &r_arguments) const {
  189. if (!is_null() && is_custom()) {
  190. custom->get_bound_arguments(r_arguments);
  191. } else {
  192. r_arguments.clear();
  193. }
  194. }
  195. Array Callable::get_bound_arguments() const {
  196. Vector<Variant> arr;
  197. get_bound_arguments_ref(arr);
  198. Array ret;
  199. ret.resize(arr.size());
  200. for (int i = 0; i < arr.size(); i++) {
  201. ret[i] = arr[i];
  202. }
  203. return ret;
  204. }
  205. int Callable::get_unbound_arguments_count() const {
  206. if (!is_null() && is_custom()) {
  207. return custom->get_unbound_arguments_count();
  208. } else {
  209. return 0;
  210. }
  211. }
  212. CallableCustom *Callable::get_custom() const {
  213. ERR_FAIL_COND_V_MSG(!is_custom(), nullptr,
  214. vformat("Can't get custom on non-CallableCustom \"%s\".", operator String()));
  215. return custom;
  216. }
  217. const Callable *Callable::get_base_comparator() const {
  218. const Callable *comparator = nullptr;
  219. if (is_custom()) {
  220. comparator = custom->get_base_comparator();
  221. }
  222. if (comparator) {
  223. return comparator;
  224. } else {
  225. return this;
  226. }
  227. }
  228. uint32_t Callable::hash() const {
  229. if (is_custom()) {
  230. return custom->hash();
  231. } else {
  232. uint32_t hash = method.hash();
  233. hash = hash_murmur3_one_64(object, hash);
  234. return hash_fmix32(hash);
  235. }
  236. }
  237. bool Callable::operator==(const Callable &p_callable) const {
  238. bool custom_a = is_custom();
  239. bool custom_b = p_callable.is_custom();
  240. if (custom_a == custom_b) {
  241. if (custom_a) {
  242. if (custom == p_callable.custom) {
  243. return true; //same pointer, don't even compare
  244. }
  245. CallableCustom::CompareEqualFunc eq_a = custom->get_compare_equal_func();
  246. CallableCustom::CompareEqualFunc eq_b = p_callable.custom->get_compare_equal_func();
  247. if (eq_a == eq_b) {
  248. return eq_a(custom, p_callable.custom);
  249. } else {
  250. return false;
  251. }
  252. } else {
  253. return object == p_callable.object && method == p_callable.method;
  254. }
  255. } else {
  256. return false;
  257. }
  258. }
  259. bool Callable::operator!=(const Callable &p_callable) const {
  260. return !(*this == p_callable);
  261. }
  262. bool Callable::operator<(const Callable &p_callable) const {
  263. bool custom_a = is_custom();
  264. bool custom_b = p_callable.is_custom();
  265. if (custom_a == custom_b) {
  266. if (custom_a) {
  267. if (custom == p_callable.custom) {
  268. return false; //same pointer, don't even compare
  269. }
  270. CallableCustom::CompareLessFunc less_a = custom->get_compare_less_func();
  271. CallableCustom::CompareLessFunc less_b = p_callable.custom->get_compare_less_func();
  272. if (less_a == less_b) {
  273. return less_a(custom, p_callable.custom);
  274. } else {
  275. return less_a < less_b; //it's something..
  276. }
  277. } else {
  278. if (object == p_callable.object) {
  279. return method < p_callable.method;
  280. } else {
  281. return object < p_callable.object;
  282. }
  283. }
  284. } else {
  285. return int(custom_a ? 1 : 0) < int(custom_b ? 1 : 0);
  286. }
  287. }
  288. void Callable::operator=(const Callable &p_callable) {
  289. CallableCustom *cleanup_ref = nullptr;
  290. if (is_custom()) {
  291. if (p_callable.is_custom()) {
  292. if (custom == p_callable.custom) {
  293. return;
  294. }
  295. }
  296. cleanup_ref = custom;
  297. custom = nullptr;
  298. }
  299. if (p_callable.is_custom()) {
  300. method = StringName();
  301. object = 0;
  302. if (p_callable.custom->ref_count.ref()) {
  303. custom = p_callable.custom;
  304. }
  305. } else {
  306. method = p_callable.method;
  307. object = p_callable.object;
  308. }
  309. if (cleanup_ref != nullptr && cleanup_ref->ref_count.unref()) {
  310. memdelete(cleanup_ref);
  311. }
  312. cleanup_ref = nullptr;
  313. }
  314. Callable::operator String() const {
  315. if (is_custom()) {
  316. return custom->get_as_text();
  317. } else {
  318. if (is_null()) {
  319. return "null::null";
  320. }
  321. Object *base = get_object();
  322. if (base) {
  323. String class_name = base->get_class();
  324. Ref<Script> script = base->get_script();
  325. if (script.is_valid() && script->get_path().is_resource_file()) {
  326. class_name += "(" + script->get_path().get_file() + ")";
  327. }
  328. return class_name + "::" + String(method);
  329. } else {
  330. return "null::" + String(method);
  331. }
  332. }
  333. }
  334. Callable Callable::create(const Variant &p_variant, const StringName &p_method) {
  335. ERR_FAIL_COND_V_MSG(p_method == StringName(), Callable(), "Method argument to Callable::create method must be a non-empty string.");
  336. switch (p_variant.get_type()) {
  337. case Variant::NIL:
  338. return Callable(ObjectID(), p_method);
  339. case Variant::OBJECT:
  340. return Callable(p_variant.operator ObjectID(), p_method);
  341. default:
  342. return Callable(memnew(VariantCallable(p_variant, p_method)));
  343. }
  344. }
  345. Callable::Callable(const Object *p_object, const StringName &p_method) {
  346. if (unlikely(p_method == StringName())) {
  347. object = 0;
  348. ERR_FAIL_MSG("Method argument to Callable constructor must be a non-empty string.");
  349. }
  350. if (unlikely(p_object == nullptr)) {
  351. object = 0;
  352. ERR_FAIL_MSG("Object argument to Callable constructor must be non-null.");
  353. }
  354. object = p_object->get_instance_id();
  355. method = p_method;
  356. }
  357. Callable::Callable(ObjectID p_object, const StringName &p_method) {
  358. if (unlikely(p_method == StringName())) {
  359. object = 0;
  360. ERR_FAIL_MSG("Method argument to Callable constructor must be a non-empty string.");
  361. }
  362. object = p_object;
  363. method = p_method;
  364. }
  365. Callable::Callable(CallableCustom *p_custom) {
  366. if (unlikely(p_custom->referenced)) {
  367. object = 0;
  368. ERR_FAIL_MSG("Callable custom is already referenced.");
  369. }
  370. p_custom->referenced = true;
  371. object = 0; //ensure object is all zero, since pointer may be 32 bits
  372. custom = p_custom;
  373. }
  374. Callable::Callable(const Callable &p_callable) {
  375. if (p_callable.is_custom()) {
  376. if (!p_callable.custom->ref_count.ref()) {
  377. object = 0;
  378. } else {
  379. object = 0;
  380. custom = p_callable.custom;
  381. }
  382. } else {
  383. method = p_callable.method;
  384. object = p_callable.object;
  385. }
  386. }
  387. Callable::~Callable() {
  388. if (is_custom()) {
  389. if (custom->ref_count.unref()) {
  390. memdelete(custom);
  391. custom = nullptr;
  392. }
  393. }
  394. }
  395. bool CallableCustom::is_valid() const {
  396. // Sensible default implementation so most custom callables don't need their own.
  397. return ObjectDB::get_instance(get_object());
  398. }
  399. StringName CallableCustom::get_method() const {
  400. ERR_FAIL_V_MSG(StringName(), vformat("Can't get method on CallableCustom \"%s\".", get_as_text()));
  401. }
  402. Error CallableCustom::rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const {
  403. r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  404. r_call_error.argument = 0;
  405. r_call_error.expected = 0;
  406. return ERR_UNCONFIGURED;
  407. }
  408. const Callable *CallableCustom::get_base_comparator() const {
  409. return nullptr;
  410. }
  411. int CallableCustom::get_argument_count(bool &r_is_valid) const {
  412. r_is_valid = false;
  413. return 0;
  414. }
  415. int CallableCustom::get_bound_arguments_count() const {
  416. return 0;
  417. }
  418. void CallableCustom::get_bound_arguments(Vector<Variant> &r_arguments) const {
  419. r_arguments.clear();
  420. }
  421. int CallableCustom::get_unbound_arguments_count() const {
  422. return 0;
  423. }
  424. CallableCustom::CallableCustom() {
  425. ref_count.init();
  426. }
  427. //////////////////////////////////
  428. Object *Signal::get_object() const {
  429. return ObjectDB::get_instance(object);
  430. }
  431. ObjectID Signal::get_object_id() const {
  432. return object;
  433. }
  434. StringName Signal::get_name() const {
  435. return name;
  436. }
  437. bool Signal::operator==(const Signal &p_signal) const {
  438. return object == p_signal.object && name == p_signal.name;
  439. }
  440. bool Signal::operator!=(const Signal &p_signal) const {
  441. return object != p_signal.object || name != p_signal.name;
  442. }
  443. bool Signal::operator<(const Signal &p_signal) const {
  444. if (object == p_signal.object) {
  445. return name < p_signal.name;
  446. } else {
  447. return object < p_signal.object;
  448. }
  449. }
  450. Signal::operator String() const {
  451. Object *base = get_object();
  452. if (base) {
  453. String class_name = base->get_class();
  454. Ref<Script> script = base->get_script();
  455. if (script.is_valid() && script->get_path().is_resource_file()) {
  456. class_name += "(" + script->get_path().get_file() + ")";
  457. }
  458. return class_name + "::[signal]" + String(name);
  459. } else {
  460. return "null::[signal]" + String(name);
  461. }
  462. }
  463. Error Signal::emit(const Variant **p_arguments, int p_argcount) const {
  464. Object *obj = ObjectDB::get_instance(object);
  465. if (!obj) {
  466. return ERR_INVALID_DATA;
  467. }
  468. return obj->emit_signalp(name, p_arguments, p_argcount);
  469. }
  470. Error Signal::connect(const Callable &p_callable, uint32_t p_flags) {
  471. Object *obj = get_object();
  472. ERR_FAIL_NULL_V(obj, ERR_UNCONFIGURED);
  473. return obj->connect(name, p_callable, p_flags);
  474. }
  475. void Signal::disconnect(const Callable &p_callable) {
  476. Object *obj = get_object();
  477. ERR_FAIL_NULL(obj);
  478. obj->disconnect(name, p_callable);
  479. }
  480. bool Signal::is_connected(const Callable &p_callable) const {
  481. Object *obj = get_object();
  482. ERR_FAIL_NULL_V(obj, false);
  483. return obj->is_connected(name, p_callable);
  484. }
  485. bool Signal::has_connections() const {
  486. Object *obj = get_object();
  487. ERR_FAIL_NULL_V(obj, false);
  488. return obj->has_connections(name);
  489. }
  490. Array Signal::get_connections() const {
  491. Object *obj = get_object();
  492. if (!obj) {
  493. return Array();
  494. }
  495. List<Object::Connection> connections;
  496. obj->get_signal_connection_list(name, &connections);
  497. Array arr;
  498. for (const Object::Connection &E : connections) {
  499. arr.push_back(E);
  500. }
  501. return arr;
  502. }
  503. Signal::Signal(const Object *p_object, const StringName &p_name) {
  504. ERR_FAIL_NULL_MSG(p_object, "Object argument to Signal constructor must be non-null.");
  505. object = p_object->get_instance_id();
  506. name = p_name;
  507. }
  508. Signal::Signal(ObjectID p_object, const StringName &p_name) {
  509. object = p_object;
  510. name = p_name;
  511. }
  512. bool CallableComparator::operator()(const Variant &p_l, const Variant &p_r) const {
  513. const Variant *args[2] = { &p_l, &p_r };
  514. Callable::CallError err;
  515. Variant res;
  516. func.callp(args, 2, res, err);
  517. ERR_FAIL_COND_V_MSG(err.error != Callable::CallError::CALL_OK, false,
  518. "Error calling compare method: " + Variant::get_callable_error_text(func, args, 2, err));
  519. return res;
  520. }