variant_utility.cpp 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. /*************************************************************************/
  2. /* variant_utility.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "variant.h"
  31. #include "core/core_string_names.h"
  32. #include "core/io/marshalls.h"
  33. #include "core/object/ref_counted.h"
  34. #include "core/os/os.h"
  35. #include "core/templates/oa_hash_map.h"
  36. #include "core/templates/rid.h"
  37. #include "core/templates/rid_owner.h"
  38. #include "core/variant/binder_common.h"
  39. #include "core/variant/variant_parser.h"
  40. struct VariantUtilityFunctions {
  41. // Math
  42. static inline double sin(double arg) {
  43. return Math::sin(arg);
  44. }
  45. static inline double cos(double arg) {
  46. return Math::cos(arg);
  47. }
  48. static inline double tan(double arg) {
  49. return Math::tan(arg);
  50. }
  51. static inline double sinh(double arg) {
  52. return Math::sinh(arg);
  53. }
  54. static inline double cosh(double arg) {
  55. return Math::cosh(arg);
  56. }
  57. static inline double tanh(double arg) {
  58. return Math::tanh(arg);
  59. }
  60. static inline double asin(double arg) {
  61. return Math::asin(arg);
  62. }
  63. static inline double acos(double arg) {
  64. return Math::acos(arg);
  65. }
  66. static inline double atan(double arg) {
  67. return Math::atan(arg);
  68. }
  69. static inline double atan2(double y, double x) {
  70. return Math::atan2(y, x);
  71. }
  72. static inline double sqrt(double x) {
  73. return Math::sqrt(x);
  74. }
  75. static inline double fmod(double b, double r) {
  76. return Math::fmod(b, r);
  77. }
  78. static inline double fposmod(double b, double r) {
  79. return Math::fposmod(b, r);
  80. }
  81. static inline int64_t posmod(int64_t b, int64_t r) {
  82. return Math::posmod(b, r);
  83. }
  84. static inline double floor(double x) {
  85. return Math::floor(x);
  86. }
  87. static inline double ceil(double x) {
  88. return Math::ceil(x);
  89. }
  90. static inline double round(double x) {
  91. return Math::round(x);
  92. }
  93. static inline Variant abs(const Variant &x, Callable::CallError &r_error) {
  94. r_error.error = Callable::CallError::CALL_OK;
  95. switch (x.get_type()) {
  96. case Variant::INT: {
  97. return ABS(VariantInternalAccessor<int64_t>::get(&x));
  98. } break;
  99. case Variant::FLOAT: {
  100. return Math::absd(VariantInternalAccessor<double>::get(&x));
  101. } break;
  102. case Variant::VECTOR2: {
  103. return VariantInternalAccessor<Vector2>::get(&x).abs();
  104. } break;
  105. case Variant::VECTOR2I: {
  106. return VariantInternalAccessor<Vector2i>::get(&x).abs();
  107. } break;
  108. case Variant::VECTOR3: {
  109. return VariantInternalAccessor<Vector3>::get(&x).abs();
  110. } break;
  111. case Variant::VECTOR3I: {
  112. return VariantInternalAccessor<Vector3i>::get(&x).abs();
  113. } break;
  114. default: {
  115. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  116. return Variant();
  117. }
  118. }
  119. }
  120. static inline double absf(double x) {
  121. return Math::absd(x);
  122. }
  123. static inline int64_t absi(int64_t x) {
  124. return ABS(x);
  125. }
  126. static inline Variant sign(const Variant &x, Callable::CallError &r_error) {
  127. r_error.error = Callable::CallError::CALL_OK;
  128. switch (x.get_type()) {
  129. case Variant::INT: {
  130. return SGN(VariantInternalAccessor<int64_t>::get(&x));
  131. } break;
  132. case Variant::FLOAT: {
  133. return SGN(VariantInternalAccessor<double>::get(&x));
  134. } break;
  135. case Variant::VECTOR2: {
  136. return VariantInternalAccessor<Vector2>::get(&x).sign();
  137. } break;
  138. case Variant::VECTOR2I: {
  139. return VariantInternalAccessor<Vector2i>::get(&x).sign();
  140. } break;
  141. case Variant::VECTOR3: {
  142. return VariantInternalAccessor<Vector3>::get(&x).sign();
  143. } break;
  144. case Variant::VECTOR3I: {
  145. return VariantInternalAccessor<Vector3i>::get(&x).sign();
  146. } break;
  147. default: {
  148. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  149. return Variant();
  150. }
  151. }
  152. }
  153. static inline double signf(double x) {
  154. return SGN(x);
  155. }
  156. static inline int64_t signi(int64_t x) {
  157. return SGN(x);
  158. }
  159. static inline double pow(double x, double y) {
  160. return Math::pow(x, y);
  161. }
  162. static inline double log(double x) {
  163. return Math::log(x);
  164. }
  165. static inline double exp(double x) {
  166. return Math::exp(x);
  167. }
  168. static inline bool is_nan(double x) {
  169. return Math::is_nan(x);
  170. }
  171. static inline bool is_inf(double x) {
  172. return Math::is_inf(x);
  173. }
  174. static inline bool is_equal_approx(double x, double y) {
  175. return Math::is_equal_approx(x, y);
  176. }
  177. static inline bool is_zero_approx(double x) {
  178. return Math::is_zero_approx(x);
  179. }
  180. static inline double ease(float x, float curve) {
  181. return Math::ease(x, curve);
  182. }
  183. static inline int step_decimals(float step) {
  184. return Math::step_decimals(step);
  185. }
  186. static inline int range_step_decimals(float step) {
  187. return Math::range_step_decimals(step);
  188. }
  189. static inline double snapped(double value, double step) {
  190. return Math::snapped(value, step);
  191. }
  192. static inline double lerp(double from, double to, double weight) {
  193. return Math::lerp(from, to, weight);
  194. }
  195. static inline double lerp_angle(double from, double to, double weight) {
  196. return Math::lerp_angle(from, to, weight);
  197. }
  198. static inline double inverse_lerp(double from, double to, double weight) {
  199. return Math::inverse_lerp(from, to, weight);
  200. }
  201. static inline double range_lerp(double value, double istart, double istop, double ostart, double ostop) {
  202. return Math::range_lerp(value, istart, istop, ostart, ostop);
  203. }
  204. static inline double smoothstep(double from, double to, double val) {
  205. return Math::smoothstep(from, to, val);
  206. }
  207. static inline double move_toward(double from, double to, double delta) {
  208. return Math::move_toward(from, to, delta);
  209. }
  210. static inline double deg2rad(double angle_deg) {
  211. return Math::deg2rad(angle_deg);
  212. }
  213. static inline double rad2deg(double angle_rad) {
  214. return Math::rad2deg(angle_rad);
  215. }
  216. static inline double linear2db(double linear) {
  217. return Math::linear2db(linear);
  218. }
  219. static inline double db2linear(double db) {
  220. return Math::db2linear(db);
  221. }
  222. static inline int64_t wrapi(int64_t value, int64_t min, int64_t max) {
  223. return Math::wrapi(value, min, max);
  224. }
  225. static inline double wrapf(double value, double min, double max) {
  226. return Math::wrapf(value, min, max);
  227. }
  228. static inline Variant max(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  229. if (p_argcount < 2) {
  230. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  231. r_error.expected = 2;
  232. return Variant();
  233. }
  234. Variant base = *p_args[0];
  235. Variant ret;
  236. for (int i = 1; i < p_argcount; i++) {
  237. bool valid;
  238. Variant::evaluate(Variant::OP_LESS, base, *p_args[i], ret, valid);
  239. if (!valid) {
  240. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  241. r_error.expected = base.get_type();
  242. r_error.argument = i;
  243. return Variant();
  244. }
  245. if (ret.booleanize()) {
  246. base = *p_args[i];
  247. }
  248. }
  249. r_error.error = Callable::CallError::CALL_OK;
  250. return base;
  251. }
  252. static inline double maxf(double x, double y) {
  253. return MAX(x, y);
  254. }
  255. static inline int64_t maxi(int64_t x, int64_t y) {
  256. return MAX(x, y);
  257. }
  258. static inline Variant min(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  259. if (p_argcount < 2) {
  260. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  261. r_error.expected = 2;
  262. return Variant();
  263. }
  264. Variant base = *p_args[0];
  265. Variant ret;
  266. for (int i = 1; i < p_argcount; i++) {
  267. bool valid;
  268. Variant::evaluate(Variant::OP_GREATER, base, *p_args[i], ret, valid);
  269. if (!valid) {
  270. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  271. r_error.expected = base.get_type();
  272. r_error.argument = i;
  273. return Variant();
  274. }
  275. if (ret.booleanize()) {
  276. base = *p_args[i];
  277. }
  278. }
  279. r_error.error = Callable::CallError::CALL_OK;
  280. return base;
  281. }
  282. static inline double minf(double x, double y) {
  283. return MIN(x, y);
  284. }
  285. static inline int64_t mini(int64_t x, int64_t y) {
  286. return MIN(x, y);
  287. }
  288. static inline Variant clamp(const Variant &x, const Variant &min, const Variant &max, Callable::CallError &r_error) {
  289. Variant value = x;
  290. Variant ret;
  291. bool valid;
  292. Variant::evaluate(Variant::OP_LESS, value, min, ret, valid);
  293. if (!valid) {
  294. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  295. r_error.expected = value.get_type();
  296. r_error.argument = 1;
  297. return Variant();
  298. }
  299. if (ret.booleanize()) {
  300. value = min;
  301. }
  302. Variant::evaluate(Variant::OP_GREATER, value, max, ret, valid);
  303. if (!valid) {
  304. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  305. r_error.expected = value.get_type();
  306. r_error.argument = 2;
  307. return Variant();
  308. }
  309. if (ret.booleanize()) {
  310. value = max;
  311. }
  312. r_error.error = Callable::CallError::CALL_OK;
  313. return value;
  314. }
  315. static inline double clampf(double x, double min, double max) {
  316. return CLAMP(x, min, max);
  317. }
  318. static inline int64_t clampi(int64_t x, int64_t min, int64_t max) {
  319. return CLAMP(x, min, max);
  320. }
  321. static inline int64_t nearest_po2(int64_t x) {
  322. return nearest_power_of_2_templated(uint64_t(x));
  323. }
  324. // Random
  325. static inline void randomize() {
  326. Math::randomize();
  327. }
  328. static inline int64_t randi() {
  329. return Math::rand();
  330. }
  331. static inline double randf() {
  332. return Math::randf();
  333. }
  334. static inline int64_t randi_range(int64_t from, int64_t to) {
  335. return Math::random((int32_t)from, (int32_t)to);
  336. }
  337. static inline double randf_range(double from, double to) {
  338. return Math::random(from, to);
  339. }
  340. static inline void seed(int64_t s) {
  341. return Math::seed(s);
  342. }
  343. static inline PackedInt64Array rand_from_seed(int64_t seed) {
  344. uint64_t s = seed;
  345. PackedInt64Array arr;
  346. arr.resize(2);
  347. arr.write[0] = Math::rand_from_seed(&s);
  348. arr.write[1] = s;
  349. return arr;
  350. }
  351. // Utility
  352. static inline Variant weakref(const Variant &obj, Callable::CallError &r_error) {
  353. if (obj.get_type() == Variant::OBJECT) {
  354. r_error.error = Callable::CallError::CALL_OK;
  355. if (obj.is_ref()) {
  356. Ref<WeakRef> wref = memnew(WeakRef);
  357. REF r = obj;
  358. if (r.is_valid()) {
  359. wref->set_ref(r);
  360. }
  361. return wref;
  362. } else {
  363. Ref<WeakRef> wref = memnew(WeakRef);
  364. Object *o = obj.get_validated_object();
  365. if (o) {
  366. wref->set_obj(o);
  367. }
  368. return wref;
  369. }
  370. } else if (obj.get_type() == Variant::NIL) {
  371. r_error.error = Callable::CallError::CALL_OK;
  372. Ref<WeakRef> wref = memnew(WeakRef);
  373. return wref;
  374. } else {
  375. r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
  376. r_error.argument = 0;
  377. r_error.expected = Variant::OBJECT;
  378. return Variant();
  379. }
  380. }
  381. static inline int64_t _typeof(const Variant &obj) {
  382. return obj.get_type();
  383. }
  384. static inline String str(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  385. if (p_arg_count < 1) {
  386. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  387. r_error.argument = 1;
  388. return String();
  389. }
  390. String str;
  391. for (int i = 0; i < p_arg_count; i++) {
  392. String os = p_args[i]->operator String();
  393. if (i == 0) {
  394. str = os;
  395. } else {
  396. str += os;
  397. }
  398. }
  399. r_error.error = Callable::CallError::CALL_OK;
  400. return str;
  401. }
  402. static inline String error_string(Error error) {
  403. if (error < 0 || error >= ERR_MAX) {
  404. return String("(invalid error code)");
  405. }
  406. return String(error_names[error]);
  407. }
  408. static inline void print(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  409. String str;
  410. for (int i = 0; i < p_arg_count; i++) {
  411. String os = p_args[i]->operator String();
  412. if (i == 0) {
  413. str = os;
  414. } else {
  415. str += os;
  416. }
  417. }
  418. print_line(str);
  419. r_error.error = Callable::CallError::CALL_OK;
  420. }
  421. static inline void print_verbose(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  422. if (OS::get_singleton()->is_stdout_verbose()) {
  423. String str;
  424. for (int i = 0; i < p_arg_count; i++) {
  425. String os = p_args[i]->operator String();
  426. if (i == 0) {
  427. str = os;
  428. } else {
  429. str += os;
  430. }
  431. }
  432. // No need to use `print_verbose()` as this call already only happens
  433. // when verbose mode is enabled. This avoids performing string argument concatenation
  434. // when not needed.
  435. print_line(str);
  436. }
  437. r_error.error = Callable::CallError::CALL_OK;
  438. }
  439. static inline void printerr(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  440. String str;
  441. for (int i = 0; i < p_arg_count; i++) {
  442. String os = p_args[i]->operator String();
  443. if (i == 0) {
  444. str = os;
  445. } else {
  446. str += os;
  447. }
  448. }
  449. print_error(str);
  450. r_error.error = Callable::CallError::CALL_OK;
  451. }
  452. static inline void printt(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  453. String str;
  454. for (int i = 0; i < p_arg_count; i++) {
  455. if (i) {
  456. str += "\t";
  457. }
  458. str += p_args[i]->operator String();
  459. }
  460. print_line(str);
  461. r_error.error = Callable::CallError::CALL_OK;
  462. }
  463. static inline void prints(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  464. String str;
  465. for (int i = 0; i < p_arg_count; i++) {
  466. if (i) {
  467. str += " ";
  468. }
  469. str += p_args[i]->operator String();
  470. }
  471. print_line(str);
  472. r_error.error = Callable::CallError::CALL_OK;
  473. }
  474. static inline void printraw(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  475. String str;
  476. for (int i = 0; i < p_arg_count; i++) {
  477. String os = p_args[i]->operator String();
  478. if (i == 0) {
  479. str = os;
  480. } else {
  481. str += os;
  482. }
  483. }
  484. OS::get_singleton()->print("%s", str.utf8().get_data());
  485. r_error.error = Callable::CallError::CALL_OK;
  486. }
  487. static inline void push_error(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  488. if (p_arg_count < 1) {
  489. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  490. r_error.argument = 1;
  491. }
  492. String str;
  493. for (int i = 0; i < p_arg_count; i++) {
  494. String os = p_args[i]->operator String();
  495. if (i == 0) {
  496. str = os;
  497. } else {
  498. str += os;
  499. }
  500. }
  501. ERR_PRINT(str);
  502. r_error.error = Callable::CallError::CALL_OK;
  503. }
  504. static inline void push_warning(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
  505. if (p_arg_count < 1) {
  506. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  507. r_error.argument = 1;
  508. }
  509. String str;
  510. for (int i = 0; i < p_arg_count; i++) {
  511. String os = p_args[i]->operator String();
  512. if (i == 0) {
  513. str = os;
  514. } else {
  515. str += os;
  516. }
  517. }
  518. WARN_PRINT(str);
  519. r_error.error = Callable::CallError::CALL_OK;
  520. }
  521. static inline String var2str(const Variant &p_var) {
  522. String vars;
  523. VariantWriter::write_to_string(p_var, vars);
  524. return vars;
  525. }
  526. static inline Variant str2var(const String &p_var) {
  527. VariantParser::StreamString ss;
  528. ss.s = p_var;
  529. String errs;
  530. int line;
  531. Variant ret;
  532. (void)VariantParser::parse(&ss, ret, errs, line);
  533. return ret;
  534. }
  535. static inline PackedByteArray var2bytes(const Variant &p_var) {
  536. int len;
  537. Error err = encode_variant(p_var, nullptr, len, false);
  538. if (err != OK) {
  539. return PackedByteArray();
  540. }
  541. PackedByteArray barr;
  542. barr.resize(len);
  543. {
  544. uint8_t *w = barr.ptrw();
  545. err = encode_variant(p_var, w, len, false);
  546. if (err != OK) {
  547. return PackedByteArray();
  548. }
  549. }
  550. return barr;
  551. }
  552. static inline PackedByteArray var2bytes_with_objects(const Variant &p_var) {
  553. int len;
  554. Error err = encode_variant(p_var, nullptr, len, true);
  555. if (err != OK) {
  556. return PackedByteArray();
  557. }
  558. PackedByteArray barr;
  559. barr.resize(len);
  560. {
  561. uint8_t *w = barr.ptrw();
  562. err = encode_variant(p_var, w, len, true);
  563. if (err != OK) {
  564. return PackedByteArray();
  565. }
  566. }
  567. return barr;
  568. }
  569. static inline Variant bytes2var(const PackedByteArray &p_arr) {
  570. Variant ret;
  571. {
  572. const uint8_t *r = p_arr.ptr();
  573. Error err = decode_variant(ret, r, p_arr.size(), nullptr, false);
  574. if (err != OK) {
  575. return Variant();
  576. }
  577. }
  578. return ret;
  579. }
  580. static inline Variant bytes2var_with_objects(const PackedByteArray &p_arr) {
  581. Variant ret;
  582. {
  583. const uint8_t *r = p_arr.ptr();
  584. Error err = decode_variant(ret, r, p_arr.size(), nullptr, true);
  585. if (err != OK) {
  586. return Variant();
  587. }
  588. }
  589. return ret;
  590. }
  591. static inline int64_t hash(const Variant &p_arr) {
  592. return p_arr.hash();
  593. }
  594. static inline Object *instance_from_id(int64_t p_id) {
  595. ObjectID id = ObjectID((uint64_t)p_id);
  596. Object *ret = ObjectDB::get_instance(id);
  597. return ret;
  598. }
  599. static inline bool is_instance_id_valid(int64_t p_id) {
  600. return ObjectDB::get_instance(ObjectID((uint64_t)p_id)) != nullptr;
  601. }
  602. static inline bool is_instance_valid(const Variant &p_instance) {
  603. if (p_instance.get_type() != Variant::OBJECT) {
  604. return false;
  605. }
  606. return p_instance.get_validated_object() != nullptr;
  607. }
  608. static inline uint64_t rid_allocate_id() {
  609. return RID_AllocBase::_gen_id();
  610. }
  611. static inline RID rid_from_int64(uint64_t p_base) {
  612. return RID::from_uint64(p_base);
  613. }
  614. };
  615. #ifdef DEBUG_METHODS_ENABLED
  616. #define VCALLR *ret = p_func(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...)
  617. #define VCALL p_func(VariantCasterAndValidate<P>::cast(p_args, Is, r_error)...)
  618. #else
  619. #define VCALLR *ret = p_func(VariantCaster<P>::cast(*p_args[Is])...)
  620. #define VCALL p_func(VariantCaster<P>::cast(*p_args[Is])...)
  621. #endif
  622. template <class R, class... P, size_t... Is>
  623. static _FORCE_INLINE_ void call_helperpr(R (*p_func)(P...), Variant *ret, const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) {
  624. r_error.error = Callable::CallError::CALL_OK;
  625. VCALLR;
  626. (void)p_args; // avoid gcc warning
  627. (void)r_error;
  628. }
  629. template <class R, class... P, size_t... Is>
  630. static _FORCE_INLINE_ void validated_call_helperpr(R (*p_func)(P...), Variant *ret, const Variant **p_args, IndexSequence<Is...>) {
  631. *ret = p_func(VariantCaster<P>::cast(*p_args[Is])...);
  632. (void)p_args;
  633. }
  634. template <class R, class... P, size_t... Is>
  635. static _FORCE_INLINE_ void ptr_call_helperpr(R (*p_func)(P...), void *ret, const void **p_args, IndexSequence<Is...>) {
  636. PtrToArg<R>::encode(p_func(PtrToArg<P>::convert(p_args[Is])...), ret);
  637. (void)p_args;
  638. }
  639. template <class R, class... P>
  640. static _FORCE_INLINE_ void call_helperr(R (*p_func)(P...), Variant *ret, const Variant **p_args, Callable::CallError &r_error) {
  641. call_helperpr(p_func, ret, p_args, r_error, BuildIndexSequence<sizeof...(P)>{});
  642. }
  643. template <class R, class... P>
  644. static _FORCE_INLINE_ void validated_call_helperr(R (*p_func)(P...), Variant *ret, const Variant **p_args) {
  645. validated_call_helperpr(p_func, ret, p_args, BuildIndexSequence<sizeof...(P)>{});
  646. }
  647. template <class R, class... P>
  648. static _FORCE_INLINE_ void ptr_call_helperr(R (*p_func)(P...), void *ret, const void **p_args) {
  649. ptr_call_helperpr(p_func, ret, p_args, BuildIndexSequence<sizeof...(P)>{});
  650. }
  651. template <class R, class... P>
  652. static _FORCE_INLINE_ int get_arg_count_helperr(R (*p_func)(P...)) {
  653. return sizeof...(P);
  654. }
  655. template <class R, class... P>
  656. static _FORCE_INLINE_ Variant::Type get_arg_type_helperr(R (*p_func)(P...), int p_arg) {
  657. return call_get_argument_type<P...>(p_arg);
  658. }
  659. template <class R, class... P>
  660. static _FORCE_INLINE_ Variant::Type get_ret_type_helperr(R (*p_func)(P...)) {
  661. return GetTypeInfo<R>::VARIANT_TYPE;
  662. }
  663. // WITHOUT RET
  664. template <class... P, size_t... Is>
  665. static _FORCE_INLINE_ void call_helperp(void (*p_func)(P...), const Variant **p_args, Callable::CallError &r_error, IndexSequence<Is...>) {
  666. r_error.error = Callable::CallError::CALL_OK;
  667. VCALL;
  668. (void)p_args;
  669. (void)r_error;
  670. }
  671. template <class... P, size_t... Is>
  672. static _FORCE_INLINE_ void validated_call_helperp(void (*p_func)(P...), const Variant **p_args, IndexSequence<Is...>) {
  673. p_func(VariantCaster<P>::cast(*p_args[Is])...);
  674. (void)p_args;
  675. }
  676. template <class... P, size_t... Is>
  677. static _FORCE_INLINE_ void ptr_call_helperp(void (*p_func)(P...), const void **p_args, IndexSequence<Is...>) {
  678. p_func(PtrToArg<P>::convert(p_args[Is])...);
  679. (void)p_args;
  680. }
  681. template <class... P>
  682. static _FORCE_INLINE_ void call_helper(void (*p_func)(P...), const Variant **p_args, Callable::CallError &r_error) {
  683. call_helperp(p_func, p_args, r_error, BuildIndexSequence<sizeof...(P)>{});
  684. }
  685. template <class... P>
  686. static _FORCE_INLINE_ void validated_call_helper(void (*p_func)(P...), const Variant **p_args) {
  687. validated_call_helperp(p_func, p_args, BuildIndexSequence<sizeof...(P)>{});
  688. }
  689. template <class... P>
  690. static _FORCE_INLINE_ void ptr_call_helper(void (*p_func)(P...), const void **p_args) {
  691. ptr_call_helperp(p_func, p_args, BuildIndexSequence<sizeof...(P)>{});
  692. }
  693. template <class... P>
  694. static _FORCE_INLINE_ int get_arg_count_helper(void (*p_func)(P...)) {
  695. return sizeof...(P);
  696. }
  697. template <class... P>
  698. static _FORCE_INLINE_ Variant::Type get_arg_type_helper(void (*p_func)(P...), int p_arg) {
  699. return call_get_argument_type<P...>(p_arg);
  700. }
  701. template <class... P>
  702. static _FORCE_INLINE_ Variant::Type get_ret_type_helper(void (*p_func)(P...)) {
  703. return Variant::NIL;
  704. }
  705. #define FUNCBINDR(m_func, m_args, m_category) \
  706. class Func_##m_func { \
  707. public: \
  708. static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
  709. call_helperr(VariantUtilityFunctions::m_func, r_ret, p_args, r_error); \
  710. } \
  711. static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
  712. validated_call_helperr(VariantUtilityFunctions::m_func, r_ret, p_args); \
  713. } \
  714. static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
  715. ptr_call_helperr(VariantUtilityFunctions::m_func, ret, p_args); \
  716. } \
  717. static int get_argument_count() { \
  718. return get_arg_count_helperr(VariantUtilityFunctions::m_func); \
  719. } \
  720. static Variant::Type get_argument_type(int p_arg) { \
  721. return get_arg_type_helperr(VariantUtilityFunctions::m_func, p_arg); \
  722. } \
  723. static Variant::Type get_return_type() { \
  724. return get_ret_type_helperr(VariantUtilityFunctions::m_func); \
  725. } \
  726. static bool has_return_type() { \
  727. return true; \
  728. } \
  729. static bool is_vararg() { return false; } \
  730. static Variant::UtilityFunctionType get_type() { return m_category; } \
  731. }; \
  732. register_utility_function<Func_##m_func>(#m_func, m_args)
  733. #define FUNCBINDVR(m_func, m_args, m_category) \
  734. class Func_##m_func { \
  735. public: \
  736. static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
  737. r_error.error = Callable::CallError::CALL_OK; \
  738. *r_ret = VariantUtilityFunctions::m_func(*p_args[0], r_error); \
  739. } \
  740. static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
  741. Callable::CallError ce; \
  742. *r_ret = VariantUtilityFunctions::m_func(*p_args[0], ce); \
  743. } \
  744. static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
  745. Callable::CallError ce; \
  746. PtrToArg<Variant>::encode(VariantUtilityFunctions::m_func(PtrToArg<Variant>::convert(p_args[0]), ce), ret); \
  747. } \
  748. static int get_argument_count() { \
  749. return 1; \
  750. } \
  751. static Variant::Type get_argument_type(int p_arg) { \
  752. return Variant::NIL; \
  753. } \
  754. static Variant::Type get_return_type() { \
  755. return Variant::NIL; \
  756. } \
  757. static bool has_return_type() { \
  758. return true; \
  759. } \
  760. static bool is_vararg() { return false; } \
  761. static Variant::UtilityFunctionType get_type() { return m_category; } \
  762. }; \
  763. register_utility_function<Func_##m_func>(#m_func, m_args)
  764. #define FUNCBINDVR3(m_func, m_args, m_category) \
  765. class Func_##m_func { \
  766. public: \
  767. static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
  768. r_error.error = Callable::CallError::CALL_OK; \
  769. *r_ret = VariantUtilityFunctions::m_func(*p_args[0], *p_args[1], *p_args[2], r_error); \
  770. } \
  771. static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
  772. Callable::CallError ce; \
  773. *r_ret = VariantUtilityFunctions::m_func(*p_args[0], *p_args[1], *p_args[2], ce); \
  774. } \
  775. static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
  776. Callable::CallError ce; \
  777. Variant r; \
  778. r = VariantUtilityFunctions::m_func(PtrToArg<Variant>::convert(p_args[0]), PtrToArg<Variant>::convert(p_args[1]), PtrToArg<Variant>::convert(p_args[2]), ce); \
  779. PtrToArg<Variant>::encode(r, ret); \
  780. } \
  781. static int get_argument_count() { \
  782. return 3; \
  783. } \
  784. static Variant::Type get_argument_type(int p_arg) { \
  785. return Variant::NIL; \
  786. } \
  787. static Variant::Type get_return_type() { \
  788. return Variant::NIL; \
  789. } \
  790. static bool has_return_type() { \
  791. return true; \
  792. } \
  793. static bool is_vararg() { return false; } \
  794. static Variant::UtilityFunctionType get_type() { return m_category; } \
  795. }; \
  796. register_utility_function<Func_##m_func>(#m_func, m_args)
  797. #define FUNCBINDVARARG(m_func, m_args, m_category) \
  798. class Func_##m_func { \
  799. public: \
  800. static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
  801. r_error.error = Callable::CallError::CALL_OK; \
  802. *r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, r_error); \
  803. } \
  804. static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
  805. Callable::CallError c; \
  806. *r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, c); \
  807. } \
  808. static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
  809. Vector<Variant> args; \
  810. for (int i = 0; i < p_argcount; i++) { \
  811. args.push_back(PtrToArg<Variant>::convert(p_args[i])); \
  812. } \
  813. Vector<const Variant *> argsp; \
  814. for (int i = 0; i < p_argcount; i++) { \
  815. argsp.push_back(&args[i]); \
  816. } \
  817. Variant r; \
  818. validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \
  819. PtrToArg<Variant>::encode(r, ret); \
  820. } \
  821. static int get_argument_count() { \
  822. return 2; \
  823. } \
  824. static Variant::Type get_argument_type(int p_arg) { \
  825. return Variant::NIL; \
  826. } \
  827. static Variant::Type get_return_type() { \
  828. return Variant::NIL; \
  829. } \
  830. static bool has_return_type() { \
  831. return true; \
  832. } \
  833. static bool is_vararg() { \
  834. return true; \
  835. } \
  836. static Variant::UtilityFunctionType get_type() { \
  837. return m_category; \
  838. } \
  839. }; \
  840. register_utility_function<Func_##m_func>(#m_func, m_args)
  841. #define FUNCBINDVARARGS(m_func, m_args, m_category) \
  842. class Func_##m_func { \
  843. public: \
  844. static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
  845. r_error.error = Callable::CallError::CALL_OK; \
  846. *r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, r_error); \
  847. } \
  848. static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
  849. Callable::CallError c; \
  850. *r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, c); \
  851. } \
  852. static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
  853. Vector<Variant> args; \
  854. for (int i = 0; i < p_argcount; i++) { \
  855. args.push_back(PtrToArg<Variant>::convert(p_args[i])); \
  856. } \
  857. Vector<const Variant *> argsp; \
  858. for (int i = 0; i < p_argcount; i++) { \
  859. argsp.push_back(&args[i]); \
  860. } \
  861. Variant r; \
  862. validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \
  863. PtrToArg<String>::encode(r.operator String(), ret); \
  864. } \
  865. static int get_argument_count() { \
  866. return 1; \
  867. } \
  868. static Variant::Type get_argument_type(int p_arg) { \
  869. return Variant::NIL; \
  870. } \
  871. static Variant::Type get_return_type() { \
  872. return Variant::STRING; \
  873. } \
  874. static bool has_return_type() { \
  875. return true; \
  876. } \
  877. static bool is_vararg() { \
  878. return true; \
  879. } \
  880. static Variant::UtilityFunctionType get_type() { \
  881. return m_category; \
  882. } \
  883. }; \
  884. register_utility_function<Func_##m_func>(#m_func, m_args)
  885. #define FUNCBINDVARARGV(m_func, m_args, m_category) \
  886. class Func_##m_func { \
  887. public: \
  888. static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
  889. r_error.error = Callable::CallError::CALL_OK; \
  890. VariantUtilityFunctions::m_func(p_args, p_argcount, r_error); \
  891. } \
  892. static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
  893. Callable::CallError c; \
  894. VariantUtilityFunctions::m_func(p_args, p_argcount, c); \
  895. } \
  896. static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
  897. Vector<Variant> args; \
  898. for (int i = 0; i < p_argcount; i++) { \
  899. args.push_back(PtrToArg<Variant>::convert(p_args[i])); \
  900. } \
  901. Vector<const Variant *> argsp; \
  902. for (int i = 0; i < p_argcount; i++) { \
  903. argsp.push_back(&args[i]); \
  904. } \
  905. Variant r; \
  906. validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \
  907. } \
  908. static int get_argument_count() { \
  909. return 1; \
  910. } \
  911. static Variant::Type get_argument_type(int p_arg) { \
  912. return Variant::NIL; \
  913. } \
  914. static Variant::Type get_return_type() { \
  915. return Variant::NIL; \
  916. } \
  917. static bool has_return_type() { \
  918. return false; \
  919. } \
  920. static bool is_vararg() { \
  921. return true; \
  922. } \
  923. static Variant::UtilityFunctionType get_type() { \
  924. return m_category; \
  925. } \
  926. }; \
  927. register_utility_function<Func_##m_func>(#m_func, m_args)
  928. #define FUNCBIND(m_func, m_args, m_category) \
  929. class Func_##m_func { \
  930. public: \
  931. static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \
  932. call_helper(VariantUtilityFunctions::m_func, p_args, r_error); \
  933. } \
  934. static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \
  935. validated_call_helper(VariantUtilityFunctions::m_func, p_args); \
  936. } \
  937. static void ptrcall(void *ret, const void **p_args, int p_argcount) { \
  938. ptr_call_helper(VariantUtilityFunctions::m_func, p_args); \
  939. } \
  940. static int get_argument_count() { \
  941. return get_arg_count_helper(VariantUtilityFunctions::m_func); \
  942. } \
  943. static Variant::Type get_argument_type(int p_arg) { \
  944. return get_arg_type_helper(VariantUtilityFunctions::m_func, p_arg); \
  945. } \
  946. static Variant::Type get_return_type() { \
  947. return get_ret_type_helper(VariantUtilityFunctions::m_func); \
  948. } \
  949. static bool has_return_type() { \
  950. return false; \
  951. } \
  952. static bool is_vararg() { return false; } \
  953. static Variant::UtilityFunctionType get_type() { return m_category; } \
  954. }; \
  955. register_utility_function<Func_##m_func>(#m_func, m_args)
  956. struct VariantUtilityFunctionInfo {
  957. void (*call_utility)(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
  958. Variant::ValidatedUtilityFunction validated_call_utility;
  959. Variant::PTRUtilityFunction ptr_call_utility;
  960. Vector<String> argnames;
  961. bool is_vararg;
  962. bool returns_value;
  963. int argcount;
  964. Variant::Type (*get_arg_type)(int);
  965. Variant::Type return_type;
  966. Variant::UtilityFunctionType type;
  967. };
  968. static OAHashMap<StringName, VariantUtilityFunctionInfo> utility_function_table;
  969. static List<StringName> utility_function_name_table;
  970. template <class T>
  971. static void register_utility_function(const String &p_name, const Vector<String> &argnames) {
  972. String name = p_name;
  973. if (name.begins_with("_")) {
  974. name = name.substr(1, name.length() - 1);
  975. }
  976. StringName sname = name;
  977. ERR_FAIL_COND(utility_function_table.has(sname));
  978. VariantUtilityFunctionInfo bfi;
  979. bfi.call_utility = T::call;
  980. bfi.validated_call_utility = T::validated_call;
  981. bfi.ptr_call_utility = T::ptrcall;
  982. bfi.is_vararg = T::is_vararg();
  983. bfi.argnames = argnames;
  984. bfi.argcount = T::get_argument_count();
  985. if (!bfi.is_vararg) {
  986. ERR_FAIL_COND_MSG(argnames.size() != bfi.argcount, "wrong number of arguments binding utility function: " + name);
  987. }
  988. bfi.get_arg_type = T::get_argument_type;
  989. bfi.return_type = T::get_return_type();
  990. bfi.type = T::get_type();
  991. bfi.returns_value = T::has_return_type();
  992. utility_function_table.insert(sname, bfi);
  993. utility_function_name_table.push_back(sname);
  994. }
  995. void Variant::_register_variant_utility_functions() {
  996. // Math
  997. FUNCBINDR(sin, sarray("angle_rad"), Variant::UTILITY_FUNC_TYPE_MATH);
  998. FUNCBINDR(cos, sarray("angle_rad"), Variant::UTILITY_FUNC_TYPE_MATH);
  999. FUNCBINDR(tan, sarray("angle_rad"), Variant::UTILITY_FUNC_TYPE_MATH);
  1000. FUNCBINDR(sinh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1001. FUNCBINDR(cosh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1002. FUNCBINDR(tanh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1003. FUNCBINDR(asin, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1004. FUNCBINDR(acos, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1005. FUNCBINDR(atan, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1006. FUNCBINDR(atan2, sarray("y", "x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1007. FUNCBINDR(sqrt, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1008. FUNCBINDR(fmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH);
  1009. FUNCBINDR(fposmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH);
  1010. FUNCBINDR(posmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH);
  1011. FUNCBINDR(floor, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1012. FUNCBINDR(ceil, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1013. FUNCBINDR(round, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1014. FUNCBINDVR(abs, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1015. FUNCBINDR(absf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1016. FUNCBINDR(absi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1017. FUNCBINDVR(sign, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1018. FUNCBINDR(signf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1019. FUNCBINDR(signi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1020. FUNCBINDR(pow, sarray("base", "exp"), Variant::UTILITY_FUNC_TYPE_MATH);
  1021. FUNCBINDR(log, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1022. FUNCBINDR(exp, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1023. FUNCBINDR(is_nan, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1024. FUNCBINDR(is_inf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1025. FUNCBINDR(is_equal_approx, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);
  1026. FUNCBINDR(is_zero_approx, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1027. FUNCBINDR(ease, sarray("x", "curve"), Variant::UTILITY_FUNC_TYPE_MATH);
  1028. FUNCBINDR(step_decimals, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1029. FUNCBINDR(range_step_decimals, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1030. FUNCBINDR(snapped, sarray("x", "step"), Variant::UTILITY_FUNC_TYPE_MATH);
  1031. FUNCBINDR(lerp, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
  1032. FUNCBINDR(lerp_angle, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
  1033. FUNCBINDR(inverse_lerp, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
  1034. FUNCBINDR(range_lerp, sarray("value", "istart", "istop", "ostart", "ostop"), Variant::UTILITY_FUNC_TYPE_MATH);
  1035. FUNCBINDR(smoothstep, sarray("from", "to", "x"), Variant::UTILITY_FUNC_TYPE_MATH);
  1036. FUNCBINDR(move_toward, sarray("from", "to", "delta"), Variant::UTILITY_FUNC_TYPE_MATH);
  1037. FUNCBINDR(deg2rad, sarray("deg"), Variant::UTILITY_FUNC_TYPE_MATH);
  1038. FUNCBINDR(rad2deg, sarray("rad"), Variant::UTILITY_FUNC_TYPE_MATH);
  1039. FUNCBINDR(linear2db, sarray("lin"), Variant::UTILITY_FUNC_TYPE_MATH);
  1040. FUNCBINDR(db2linear, sarray("db"), Variant::UTILITY_FUNC_TYPE_MATH);
  1041. FUNCBINDR(wrapi, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
  1042. FUNCBINDR(wrapf, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
  1043. FUNCBINDVARARG(max, sarray(), Variant::UTILITY_FUNC_TYPE_MATH);
  1044. FUNCBINDR(maxi, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);
  1045. FUNCBINDR(maxf, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);
  1046. FUNCBINDVARARG(min, sarray(), Variant::UTILITY_FUNC_TYPE_MATH);
  1047. FUNCBINDR(mini, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);
  1048. FUNCBINDR(minf, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH);
  1049. FUNCBINDVR3(clamp, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
  1050. FUNCBINDR(clampi, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
  1051. FUNCBINDR(clampf, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH);
  1052. FUNCBINDR(nearest_po2, sarray("value"), Variant::UTILITY_FUNC_TYPE_MATH);
  1053. // Random
  1054. FUNCBIND(randomize, sarray(), Variant::UTILITY_FUNC_TYPE_RANDOM);
  1055. FUNCBINDR(randi, sarray(), Variant::UTILITY_FUNC_TYPE_RANDOM);
  1056. FUNCBINDR(randf, sarray(), Variant::UTILITY_FUNC_TYPE_RANDOM);
  1057. FUNCBINDR(randi_range, sarray("from", "to"), Variant::UTILITY_FUNC_TYPE_RANDOM);
  1058. FUNCBINDR(randf_range, sarray("from", "to"), Variant::UTILITY_FUNC_TYPE_RANDOM);
  1059. FUNCBIND(seed, sarray("base"), Variant::UTILITY_FUNC_TYPE_RANDOM);
  1060. FUNCBINDR(rand_from_seed, sarray("seed"), Variant::UTILITY_FUNC_TYPE_RANDOM);
  1061. // Utility
  1062. FUNCBINDVR(weakref, sarray("obj"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1063. FUNCBINDR(_typeof, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1064. FUNCBINDVARARGS(str, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1065. FUNCBINDR(error_string, sarray("error"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1066. FUNCBINDVARARGV(print, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1067. FUNCBINDVARARGV(printerr, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1068. FUNCBINDVARARGV(printt, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1069. FUNCBINDVARARGV(prints, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1070. FUNCBINDVARARGV(printraw, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1071. FUNCBINDVARARGV(print_verbose, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1072. FUNCBINDVARARGV(push_error, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1073. FUNCBINDVARARGV(push_warning, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1074. FUNCBINDR(var2str, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1075. FUNCBINDR(str2var, sarray("string"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1076. FUNCBINDR(var2bytes, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1077. FUNCBINDR(bytes2var, sarray("bytes"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1078. FUNCBINDR(var2bytes_with_objects, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1079. FUNCBINDR(bytes2var_with_objects, sarray("bytes"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1080. FUNCBINDR(hash, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1081. FUNCBINDR(instance_from_id, sarray("instance_id"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1082. FUNCBINDR(is_instance_id_valid, sarray("id"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1083. FUNCBINDR(is_instance_valid, sarray("instance"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1084. FUNCBINDR(rid_allocate_id, Vector<String>(), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1085. FUNCBINDR(rid_from_int64, sarray("base"), Variant::UTILITY_FUNC_TYPE_GENERAL);
  1086. }
  1087. void Variant::_unregister_variant_utility_functions() {
  1088. utility_function_table.clear();
  1089. utility_function_name_table.clear();
  1090. }
  1091. void Variant::call_utility_function(const StringName &p_name, Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
  1092. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1093. if (!bfi) {
  1094. r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
  1095. r_error.argument = 0;
  1096. r_error.expected = 0;
  1097. return;
  1098. }
  1099. if (unlikely(!bfi->is_vararg && p_argcount < bfi->argcount)) {
  1100. r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
  1101. r_error.argument = 0;
  1102. r_error.expected = bfi->argcount;
  1103. return;
  1104. }
  1105. if (unlikely(!bfi->is_vararg && p_argcount > bfi->argcount)) {
  1106. r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
  1107. r_error.argument = 0;
  1108. r_error.expected = bfi->argcount;
  1109. return;
  1110. }
  1111. bfi->call_utility(r_ret, p_args, p_argcount, r_error);
  1112. }
  1113. bool Variant::has_utility_function(const StringName &p_name) {
  1114. return utility_function_table.has(p_name);
  1115. }
  1116. Variant::ValidatedUtilityFunction Variant::get_validated_utility_function(const StringName &p_name) {
  1117. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1118. if (!bfi) {
  1119. return nullptr;
  1120. }
  1121. return bfi->validated_call_utility;
  1122. }
  1123. Variant::PTRUtilityFunction Variant::get_ptr_utility_function(const StringName &p_name) {
  1124. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1125. if (!bfi) {
  1126. return nullptr;
  1127. }
  1128. return bfi->ptr_call_utility;
  1129. }
  1130. Variant::UtilityFunctionType Variant::get_utility_function_type(const StringName &p_name) {
  1131. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1132. if (!bfi) {
  1133. return Variant::UTILITY_FUNC_TYPE_MATH;
  1134. }
  1135. return bfi->type;
  1136. }
  1137. MethodInfo Variant::get_utility_function_info(const StringName &p_name) {
  1138. MethodInfo info;
  1139. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1140. if (bfi) {
  1141. info.name = p_name;
  1142. if (bfi->returns_value && bfi->return_type == Variant::NIL) {
  1143. info.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  1144. }
  1145. info.return_val.type = bfi->return_type;
  1146. if (bfi->is_vararg) {
  1147. info.flags |= METHOD_FLAG_VARARG;
  1148. }
  1149. for (int i = 0; i < bfi->argnames.size(); ++i) {
  1150. PropertyInfo arg;
  1151. arg.type = bfi->get_arg_type(i);
  1152. arg.name = bfi->argnames[i];
  1153. info.arguments.push_back(arg);
  1154. }
  1155. }
  1156. return info;
  1157. }
  1158. int Variant::get_utility_function_argument_count(const StringName &p_name) {
  1159. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1160. if (!bfi) {
  1161. return 0;
  1162. }
  1163. return bfi->argcount;
  1164. }
  1165. Variant::Type Variant::get_utility_function_argument_type(const StringName &p_name, int p_arg) {
  1166. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1167. if (!bfi) {
  1168. return Variant::NIL;
  1169. }
  1170. return bfi->get_arg_type(p_arg);
  1171. }
  1172. String Variant::get_utility_function_argument_name(const StringName &p_name, int p_arg) {
  1173. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1174. if (!bfi) {
  1175. return String();
  1176. }
  1177. ERR_FAIL_INDEX_V(p_arg, bfi->argnames.size(), String());
  1178. ERR_FAIL_COND_V(bfi->is_vararg, String());
  1179. return bfi->argnames[p_arg];
  1180. }
  1181. bool Variant::has_utility_function_return_value(const StringName &p_name) {
  1182. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1183. if (!bfi) {
  1184. return false;
  1185. }
  1186. return bfi->returns_value;
  1187. }
  1188. Variant::Type Variant::get_utility_function_return_type(const StringName &p_name) {
  1189. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1190. if (!bfi) {
  1191. return Variant::NIL;
  1192. }
  1193. return bfi->return_type;
  1194. }
  1195. bool Variant::is_utility_function_vararg(const StringName &p_name) {
  1196. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1197. if (!bfi) {
  1198. return false;
  1199. }
  1200. return bfi->is_vararg;
  1201. }
  1202. uint32_t Variant::get_utility_function_hash(const StringName &p_name) {
  1203. const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name);
  1204. ERR_FAIL_COND_V(!bfi, 0);
  1205. uint32_t hash = hash_djb2_one_32(bfi->is_vararg);
  1206. hash = hash_djb2_one_32(bfi->returns_value, hash);
  1207. if (bfi->returns_value) {
  1208. hash = hash_djb2_one_32(bfi->return_type, hash);
  1209. }
  1210. hash = hash_djb2_one_32(bfi->argcount, hash);
  1211. for (int i = 0; i < bfi->argcount; i++) {
  1212. hash = hash_djb2_one_32(bfi->get_arg_type(i), hash);
  1213. }
  1214. return hash;
  1215. }
  1216. void Variant::get_utility_function_list(List<StringName> *r_functions) {
  1217. for (const StringName &E : utility_function_name_table) {
  1218. r_functions->push_back(E);
  1219. }
  1220. }
  1221. int Variant::get_utility_function_count() {
  1222. return utility_function_name_table.size();
  1223. }