callable.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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()) {
  326. if (!script->get_global_name().is_empty()) {
  327. class_name += "(" + script->get_global_name() + ")";
  328. } else if (script->get_path().is_resource_file()) {
  329. class_name += "(" + script->get_path().get_file() + ")";
  330. }
  331. }
  332. return class_name + "::" + String(method);
  333. } else {
  334. return "null::" + String(method);
  335. }
  336. }
  337. }
  338. Callable Callable::create(const Variant &p_variant, const StringName &p_method) {
  339. ERR_FAIL_COND_V_MSG(p_method == StringName(), Callable(), "Method argument to Callable::create method must be a non-empty string.");
  340. switch (p_variant.get_type()) {
  341. case Variant::NIL:
  342. return Callable(ObjectID(), p_method);
  343. case Variant::OBJECT:
  344. return Callable(p_variant.operator ObjectID(), p_method);
  345. default:
  346. return Callable(memnew(VariantCallable(p_variant, p_method)));
  347. }
  348. }
  349. Callable::Callable(const Object *p_object, const StringName &p_method) {
  350. if (unlikely(p_method == StringName())) {
  351. object = 0;
  352. ERR_FAIL_MSG("Method argument to Callable constructor must be a non-empty string.");
  353. }
  354. if (unlikely(p_object == nullptr)) {
  355. object = 0;
  356. ERR_FAIL_MSG("Object argument to Callable constructor must be non-null.");
  357. }
  358. object = p_object->get_instance_id();
  359. method = p_method;
  360. }
  361. Callable::Callable(ObjectID p_object, const StringName &p_method) {
  362. if (unlikely(p_method == StringName())) {
  363. object = 0;
  364. ERR_FAIL_MSG("Method argument to Callable constructor must be a non-empty string.");
  365. }
  366. object = p_object;
  367. method = p_method;
  368. }
  369. Callable::Callable(CallableCustom *p_custom) {
  370. if (unlikely(p_custom->referenced)) {
  371. object = 0;
  372. ERR_FAIL_MSG("Callable custom is already referenced.");
  373. }
  374. p_custom->referenced = true;
  375. object = 0; //ensure object is all zero, since pointer may be 32 bits
  376. custom = p_custom;
  377. }
  378. Callable::Callable(const Callable &p_callable) {
  379. if (p_callable.is_custom()) {
  380. if (!p_callable.custom->ref_count.ref()) {
  381. object = 0;
  382. } else {
  383. object = 0;
  384. custom = p_callable.custom;
  385. }
  386. } else {
  387. method = p_callable.method;
  388. object = p_callable.object;
  389. }
  390. }
  391. Callable::~Callable() {
  392. if (is_custom()) {
  393. if (custom->ref_count.unref()) {
  394. memdelete(custom);
  395. custom = nullptr;
  396. }
  397. }
  398. }
  399. bool CallableCustom::is_valid() const {
  400. // Sensible default implementation so most custom callables don't need their own.
  401. return ObjectDB::get_instance(get_object());
  402. }
  403. StringName CallableCustom::get_method() const {
  404. ERR_FAIL_V_MSG(StringName(), vformat("Can't get method on CallableCustom \"%s\".", get_as_text()));
  405. }
  406. Error CallableCustom::rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const {
  407. r_call_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  408. r_call_error.argument = 0;
  409. r_call_error.expected = 0;
  410. return ERR_UNCONFIGURED;
  411. }
  412. const Callable *CallableCustom::get_base_comparator() const {
  413. return nullptr;
  414. }
  415. int CallableCustom::get_argument_count(bool &r_is_valid) const {
  416. r_is_valid = false;
  417. return 0;
  418. }
  419. int CallableCustom::get_bound_arguments_count() const {
  420. return 0;
  421. }
  422. void CallableCustom::get_bound_arguments(Vector<Variant> &r_arguments) const {
  423. r_arguments.clear();
  424. }
  425. int CallableCustom::get_unbound_arguments_count() const {
  426. return 0;
  427. }
  428. CallableCustom::CallableCustom() {
  429. ref_count.init();
  430. }
  431. //////////////////////////////////
  432. Object *Signal::get_object() const {
  433. return ObjectDB::get_instance(object);
  434. }
  435. ObjectID Signal::get_object_id() const {
  436. return object;
  437. }
  438. StringName Signal::get_name() const {
  439. return name;
  440. }
  441. bool Signal::operator==(const Signal &p_signal) const {
  442. return object == p_signal.object && name == p_signal.name;
  443. }
  444. bool Signal::operator!=(const Signal &p_signal) const {
  445. return object != p_signal.object || name != p_signal.name;
  446. }
  447. bool Signal::operator<(const Signal &p_signal) const {
  448. if (object == p_signal.object) {
  449. return name < p_signal.name;
  450. } else {
  451. return object < p_signal.object;
  452. }
  453. }
  454. Signal::operator String() const {
  455. Object *base = get_object();
  456. if (base) {
  457. String class_name = base->get_class();
  458. Ref<Script> script = base->get_script();
  459. if (script.is_valid() && script->get_path().is_resource_file()) {
  460. class_name += "(" + script->get_path().get_file() + ")";
  461. }
  462. return class_name + "::[signal]" + String(name);
  463. } else {
  464. return "null::[signal]" + String(name);
  465. }
  466. }
  467. Error Signal::emit(const Variant **p_arguments, int p_argcount) const {
  468. Object *obj = ObjectDB::get_instance(object);
  469. if (!obj) {
  470. return ERR_INVALID_DATA;
  471. }
  472. return obj->emit_signalp(name, p_arguments, p_argcount);
  473. }
  474. Error Signal::connect(const Callable &p_callable, uint32_t p_flags) {
  475. Object *obj = get_object();
  476. ERR_FAIL_NULL_V(obj, ERR_UNCONFIGURED);
  477. return obj->connect(name, p_callable, p_flags);
  478. }
  479. void Signal::disconnect(const Callable &p_callable) {
  480. Object *obj = get_object();
  481. ERR_FAIL_NULL(obj);
  482. obj->disconnect(name, p_callable);
  483. }
  484. bool Signal::is_connected(const Callable &p_callable) const {
  485. Object *obj = get_object();
  486. ERR_FAIL_NULL_V(obj, false);
  487. return obj->is_connected(name, p_callable);
  488. }
  489. bool Signal::has_connections() const {
  490. Object *obj = get_object();
  491. ERR_FAIL_NULL_V(obj, false);
  492. return obj->has_connections(name);
  493. }
  494. Array Signal::get_connections() const {
  495. Object *obj = get_object();
  496. if (!obj) {
  497. return Array();
  498. }
  499. List<Object::Connection> connections;
  500. obj->get_signal_connection_list(name, &connections);
  501. Array arr;
  502. for (const Object::Connection &E : connections) {
  503. arr.push_back(E);
  504. }
  505. return arr;
  506. }
  507. Signal::Signal(const Object *p_object, const StringName &p_name) {
  508. ERR_FAIL_NULL_MSG(p_object, "Object argument to Signal constructor must be non-null.");
  509. object = p_object->get_instance_id();
  510. name = p_name;
  511. }
  512. Signal::Signal(ObjectID p_object, const StringName &p_name) {
  513. object = p_object;
  514. name = p_name;
  515. }
  516. bool CallableComparator::operator()(const Variant &p_l, const Variant &p_r) const {
  517. const Variant *args[2] = { &p_l, &p_r };
  518. Callable::CallError err;
  519. Variant res;
  520. func.callp(args, 2, res, err);
  521. ERR_FAIL_COND_V_MSG(err.error != Callable::CallError::CALL_OK, false,
  522. "Error calling compare method: " + Variant::get_callable_error_text(func, args, 2, err));
  523. return res;
  524. }