From 635d33dc6cb2226135fadeca3a2082a3c24c3757 Mon Sep 17 00:00:00 2001 From: reduz Date: Wed, 11 Nov 2020 13:16:08 -0300 Subject: Refactor variant built-in methods yet again. * Using C-style function pointers now, InternalMethod is gone. * This ensures much better performance in typed code. * Renamed builtin_funcs to utility_funcs, to avoid naming confusion --- core/math/expression.cpp | 10 +- core/variant/binder_common.h | 33 + core/variant/variant.cpp | 7 +- core/variant/variant.h | 115 +- core/variant/variant_builtin_funcs.cpp | 1392 -------------------- core/variant/variant_call.cpp | 1155 ++++++++-------- core/variant/variant_utility.cpp | 1392 ++++++++++++++++++++ editor/doc_data.cpp | 20 +- modules/gdnative/gdnative/variant.cpp | 4 +- modules/gdscript/gdscript_function.cpp | 5 +- modules/visual_script/visual_script_expression.cpp | 2 +- modules/visual_script/visual_script_func_nodes.cpp | 34 +- 12 files changed, 2063 insertions(+), 2106 deletions(-) delete mode 100644 core/variant/variant_builtin_funcs.cpp create mode 100644 core/variant/variant_utility.cpp diff --git a/core/math/expression.cpp b/core/math/expression.cpp index 13c8904f7e..d1f15caa5e 100644 --- a/core/math/expression.cpp +++ b/core/math/expression.cpp @@ -410,7 +410,7 @@ Error Expression::_get_token(Token &r_token) { } else if (id == "self") { r_token.type = TK_SELF; } else { - if (Variant::has_builtin_func(id)) { + if (Variant::has_utility_function(id)) { r_token.type = TK_BUILTIN_FUNC; r_token.value = id; return OK; @@ -747,8 +747,8 @@ Expression::ENode *Expression::_parse_expression() { } } - if (!Variant::is_builtin_func_vararg(bifunc->func)) { - int expected_args = Variant::get_builtin_func_argument_count(bifunc->func); + if (!Variant::is_utility_function_vararg(bifunc->func)) { + int expected_args = Variant::get_utility_function_argument_count(bifunc->func); if (expected_args != bifunc->arguments.size()) { _set_error("Builtin func '" + String(bifunc->func) + "' expects " + itos(expected_args) + " arguments."); } @@ -1362,7 +1362,7 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: r_ret = Variant(); //may not return anything Callable::CallError ce; - Variant::call_builtin_func(bifunc->func, &r_ret, (const Variant **)argp.ptr(), argp.size(), ce); + Variant::call_utility_function(bifunc->func, &r_ret, (const Variant **)argp.ptr(), argp.size(), ce); if (ce.error != Callable::CallError::CALL_OK) { r_error_str = "Builtin Call Failed. " + Variant::get_call_error_text(bifunc->func, (const Variant **)argp.ptr(), argp.size(), ce); return true; @@ -1396,7 +1396,7 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: } Callable::CallError ce; - r_ret = base.call(call->method, (const Variant **)argp.ptr(), argp.size(), ce); + base.call(call->method, (const Variant **)argp.ptr(), argp.size(), r_ret, ce); if (ce.error != Callable::CallError::CALL_OK) { r_error_str = vformat(RTR("On call to '%s':"), String(call->method)); diff --git a/core/variant/binder_common.h b/core/variant/binder_common.h index e72f76c5e1..2e38ce5b06 100644 --- a/core/variant/binder_common.h +++ b/core/variant/binder_common.h @@ -650,6 +650,39 @@ void call_with_variant_args_retc_static_helper(T *p_instance, R (*p_method)(T *, (void)p_args; } +template +void call_with_variant_args_retc_static_helper_dv(T *p_instance, R (*p_method)(T *, P...), const Variant **p_args, int p_argcount, Variant &r_ret, const Vector &default_values, Callable::CallError &r_error) { +#ifdef DEBUG_ENABLED + if ((size_t)p_argcount > sizeof...(P)) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; + r_error.argument = sizeof...(P); + return; + } +#endif + + int32_t missing = (int32_t)sizeof...(P) - (int32_t)p_argcount; + + int32_t dvs = default_values.size(); +#ifdef DEBUG_ENABLED + if (missing > dvs) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = sizeof...(P); + return; + } +#endif + + const Variant *args[sizeof...(P) == 0 ? 1 : sizeof...(P)]; //avoid zero sized array + for (int32_t i = 0; i < (int32_t)sizeof...(P); i++) { + if (i < p_argcount) { + args[i] = p_args[i]; + } else { + args[i] = &default_values[i - p_argcount + (dvs - missing)]; + } + } + + call_with_variant_args_retc_static_helper(p_instance, p_method, args, r_ret, r_error, BuildIndexSequence{}); +} + #if defined(DEBUG_METHODS_ENABLED) && defined(__GNUC__) && !defined(__clang__) #pragma GCC diagnostic pop #endif diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp index 055f61cf92..741d05c139 100644 --- a/core/variant/variant.cpp +++ b/core/variant/variant.cpp @@ -3401,7 +3401,8 @@ Variant Variant::call(const StringName &p_method, VARIANT_ARG_DECLARE) { Callable::CallError error; - Variant ret = call(p_method, argptr, argc, error); + Variant ret; + call(p_method, argptr, argc, ret, error); switch (error.error) { case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: { @@ -3549,12 +3550,12 @@ void Variant::register_types() { _register_variant_methods(); _register_variant_setters_getters(); _register_variant_constructors(); - _register_variant_builtin_funcs(); + _register_variant_utility_functions(); } void Variant::unregister_types() { _unregister_variant_operators(); _unregister_variant_methods(); _unregister_variant_setters_getters(); _unregister_variant_constructors(); - _unregister_variant_builtin_funcs(); + _unregister_variant_utility_functions(); } diff --git a/core/variant/variant.h b/core/variant/variant.h index d3e2f84357..1a684eeea0 100644 --- a/core/variant/variant.h +++ b/core/variant/variant.h @@ -267,8 +267,8 @@ private: static void _unregister_variant_setters_getters(); static void _register_variant_constructors(); static void _unregister_variant_constructors(); - static void _register_variant_builtin_funcs(); - static void _unregister_variant_builtin_funcs(); + static void _register_variant_utility_functions(); + static void _unregister_variant_utility_functions(); public: _FORCE_INLINE_ Type get_type() const { @@ -482,54 +482,39 @@ public: static void blend(const Variant &a, const Variant &b, float c, Variant &r_dst); static void interpolate(const Variant &a, const Variant &b, float c, Variant &r_dst); - class InternalMethod { -#ifdef DEBUG_ENABLED - protected: - StringName method_name; - Variant::Type base_type; -#endif - public: - enum Flags { - FLAG_IS_CONST = 1, - FLAG_RETURNS_VARIANT = 2, - FLAG_NO_PTRCALL = 4, - FLAG_VARARGS = 8 - }; + /* Built-In Methods */ - virtual int get_argument_count() const = 0; - virtual Type get_argument_type(int p_arg) const = 0; - virtual Type get_return_type() const = 0; - virtual uint32_t get_flags() const = 0; + typedef void (*ValidatedBuiltInMethod)(Variant *base, const Variant **p_args, int p_argcount, Variant *r_ret); + typedef void (*PTRBuiltInMethod)(void *p_base, const void **p_args, void *r_ret, int p_argcount); -#ifdef DEBUG_ENABLED - virtual String get_argument_name(int p_arg) const = 0; - StringName get_name() const { - return method_name; - } - Variant::Type get_base_type() const { - return base_type; - } -#endif - virtual Vector get_default_arguments() const = 0; - virtual void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) = 0; - virtual void validated_call(Variant *base, const Variant **p_args, Variant *r_ret) = 0; -#ifdef PTRCALL_ENABLED - virtual void ptrcall(void *p_base, const void **p_args, void *r_ret) = 0; -#endif - virtual ~InternalMethod() {} - }; + static bool has_builtin_method(Variant::Type p_type, const StringName &p_method); + + static ValidatedBuiltInMethod get_validated_builtin_method(Variant::Type p_type, const StringName &p_method); + static PTRBuiltInMethod get_ptr_builtin_method(Variant::Type p_type, const StringName &p_method); - static InternalMethod *get_internal_method(Type p_type, const StringName &p_method_name); + static int get_builtin_method_argument_count(Variant::Type p_type, const StringName &p_method); + static Variant::Type get_builtin_method_argument_type(Variant::Type p_type, const StringName &p_method, int p_argument); + static String get_builtin_method_argument_name(Variant::Type p_type, const StringName &p_method, int p_argument); + static Vector get_builtin_method_default_arguments(Variant::Type p_type, const StringName &p_method); + static bool has_builtin_method_return_value(Variant::Type p_type, const StringName &p_method); + static Variant::Type get_builtin_method_return_type(Variant::Type p_type, const StringName &p_method); + static bool is_builtin_method_const(Variant::Type p_type, const StringName &p_method); + static bool is_builtin_method_vararg(Variant::Type p_type, const StringName &p_method); + static void get_builtin_method_list(Variant::Type p_type, List *p_list); - void call_ptr(const StringName &p_method, const Variant **p_args, int p_argcount, Variant *r_ret, Callable::CallError &r_error); - Variant call(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error); + void call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error); Variant call(const StringName &p_method, const Variant &p_arg1 = Variant(), const Variant &p_arg2 = Variant(), const Variant &p_arg3 = Variant(), const Variant &p_arg4 = Variant(), const Variant &p_arg5 = Variant()); static String get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce); static String get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce); static String get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce); - // constructor + //dynamic (includes Object) + void get_method_list(List *p_list) const; + bool has_method(const StringName &p_method) const; + + /* Constructors */ + typedef void (*ValidatedConstructor)(Variant &r_base, const Variant **p_args); typedef void (*PTRConstructor)(void *base, const void **p_args); @@ -543,13 +528,7 @@ public: static void get_constructor_list(Type p_type, List *r_list); //convenience - void get_method_list(List *p_list) const; - bool has_method(const StringName &p_method) const; - static Vector get_method_argument_types(Variant::Type p_type, const StringName &p_method); - static Vector get_method_default_arguments(Variant::Type p_type, const StringName &p_method); - static Variant::Type get_method_return_type(Variant::Type p_type, const StringName &p_method, bool *r_has_return = nullptr); - static Vector get_method_argument_names(Variant::Type p_type, const StringName &p_method); - static bool is_method_const(Variant::Type p_type, const StringName &p_method); + /* Properties */ void set_named(const StringName &p_member, const Variant &p_value, bool &r_valid); Variant get_named(const StringName &p_member, bool &r_valid) const; @@ -570,6 +549,8 @@ public: static PTRSetter get_member_ptr_setter(Variant::Type p_type, const StringName &p_member); static PTRGetter get_member_ptr_getter(Variant::Type p_type, const StringName &p_member); + /* Indexing */ + static bool has_indexing(Variant::Type p_type); static Variant::Type get_indexed_element_type(Variant::Type p_type); @@ -590,6 +571,8 @@ public: uint64_t get_indexed_size() const; + /* Keying */ + static bool is_keyed(Variant::Type p_type); typedef void (*ValidatedKeyedSetter)(Variant *base, const Variant *key, const Variant *value, bool &valid); @@ -612,6 +595,8 @@ public: Variant get_keyed(const Variant &p_key, bool &r_valid) const; bool has_key(const Variant &p_key, bool &r_valid) const; + /* Generic */ + void set(const Variant &p_index, const Variant &p_value, bool *r_valid = nullptr); Variant get(const Variant &p_index, bool *r_valid = nullptr) const; bool in(const Variant &p_index, bool *r_valid = nullptr) const; @@ -622,31 +607,31 @@ public: void get_property_list(List *p_list) const; - static void call_builtin_func(const StringName &p_name, Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error); - static bool has_builtin_func(const StringName &p_name); + static void call_utility_function(const StringName &p_name, Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error); + static bool has_utility_function(const StringName &p_name); - typedef void (*BuiltinFunctionValidatedCall)(Variant *r_ret, const Variant **p_args, int p_argcount); - typedef void (*BuiltinFunctionPTRCall)(void *r_ret, const void **p_args, int p_argcount); + typedef void (*ValidatedUtilityFunction)(Variant *r_ret, const Variant **p_args, int p_argcount); + typedef void (*PTRUtilityFunction)(void *r_ret, const void **p_args, int p_argcount); - static BuiltinFunctionValidatedCall get_builtin_validated_caller(const StringName &p_name); - static BuiltinFunctionPTRCall get_builtin_ptr_caller(const StringName &p_name); + static ValidatedUtilityFunction get_validated_utility_function(const StringName &p_name); + static PTRUtilityFunction get_ptr_utility_function(const StringName &p_name); - enum BuiltInFunctionType { - BUILTIN_FUNC_TYPE_MATH, - BUILTIN_FUNC_TYPE_RANDOM, - BUILTIN_FUNC_TYPE_UTILITY, + enum UtilityFunctionType { + UTILITY_FUNC_TYPE_MATH, + UTILITY_FUNC_TYPE_RANDOM, + UTILITY_FUNC_TYPE_GENERAL, }; - static BuiltInFunctionType get_builtin_func_type(const StringName &p_name); + static UtilityFunctionType get_utility_function_type(const StringName &p_name); - static int get_builtin_func_argument_count(const StringName &p_name); - static Variant::Type get_builtin_func_argument_type(const StringName &p_name, int p_arg); - static String get_builtin_func_argument_name(const StringName &p_name, int p_arg); - static bool has_builtin_func_return_value(const StringName &p_name); - static Variant::Type get_builtin_func_return_type(const StringName &p_name); - static bool is_builtin_func_vararg(const StringName &p_name); + static int get_utility_function_argument_count(const StringName &p_name); + static Variant::Type get_utility_function_argument_type(const StringName &p_name, int p_arg); + static String get_utility_function_argument_name(const StringName &p_name, int p_arg); + static bool has_utility_function_return_value(const StringName &p_name); + static Variant::Type get_utility_function_return_type(const StringName &p_name); + static bool is_utility_function_vararg(const StringName &p_name); - static void get_builtin_function_list(List *r_functions); + static void get_utility_function_list(List *r_functions); //argsVariant call() diff --git a/core/variant/variant_builtin_funcs.cpp b/core/variant/variant_builtin_funcs.cpp deleted file mode 100644 index e89b83e6dd..0000000000 --- a/core/variant/variant_builtin_funcs.cpp +++ /dev/null @@ -1,1392 +0,0 @@ -/*************************************************************************/ -/* variant_builtin_funcs.cpp */ -/*************************************************************************/ -/* This file is part of: */ -/* GODOT ENGINE */ -/* https://godotengine.org */ -/*************************************************************************/ -/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ -/* */ -/* Permission is hereby granted, free of charge, to any person obtaining */ -/* a copy of this software and associated documentation files (the */ -/* "Software"), to deal in the Software without restriction, including */ -/* without limitation the rights to use, copy, modify, merge, publish, */ -/* distribute, sublicense, and/or sell copies of the Software, and to */ -/* permit persons to whom the Software is furnished to do so, subject to */ -/* the following conditions: */ -/* */ -/* The above copyright notice and this permission notice shall be */ -/* included in all copies or substantial portions of the Software. */ -/* */ -/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ -/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ -/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ -/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ -/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ -/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ -/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -/*************************************************************************/ - -#include "variant.h" - -#include "core/core_string_names.h" -#include "core/io/marshalls.h" -#include "core/object/reference.h" -#include "core/os/os.h" -#include "core/templates/oa_hash_map.h" -#include "core/variant/binder_common.h" -#include "core/variant/variant_parser.h" - -struct VariantBuiltinFunctions { - // Math - static inline double sin(double arg) { - return Math::sin(arg); - } - static inline double cos(double arg) { - return Math::cos(arg); - } - static inline double tan(double arg) { - return Math::tan(arg); - } - - static inline double sinh(double arg) { - return Math::sinh(arg); - } - static inline double cosh(double arg) { - return Math::cosh(arg); - } - static inline double tanh(double arg) { - return Math::tanh(arg); - } - - static inline double asin(double arg) { - return Math::asin(arg); - } - static inline double acos(double arg) { - return Math::acos(arg); - } - static inline double atan(double arg) { - return Math::atan(arg); - } - - static inline double atan2(double y, double x) { - return Math::atan2(y, x); - } - - static inline double sqrt(double x) { - return Math::sqrt(x); - } - - static inline double fmod(double b, double r) { - return Math::fmod(b, r); - } - - static inline double fposmod(double b, double r) { - return Math::fposmod(b, r); - } - - static inline double floor(double x) { - return Math::floor(x); - } - - static inline double ceil(double x) { - return Math::ceil(x); - } - - static inline double round(double x) { - return Math::round(x); - } - - static inline Variant abs(const Variant &x, Callable::CallError &r_error) { - r_error.error = Callable::CallError::CALL_OK; - switch (x.get_type()) { - case Variant::INT: { - return ABS(VariantInternalAccessor::get(&x)); - } break; - case Variant::FLOAT: { - return Math::absd(VariantInternalAccessor::get(&x)); - } break; - case Variant::VECTOR2: { - return VariantInternalAccessor::get(&x).abs(); - } break; - case Variant::VECTOR2I: { - return VariantInternalAccessor::get(&x).abs(); - } break; - case Variant::VECTOR3: { - return VariantInternalAccessor::get(&x).abs(); - } break; - case Variant::VECTOR3I: { - return VariantInternalAccessor::get(&x).abs(); - } break; - default: { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; - return Variant(); - } - } - } - - static inline double absf(double x) { - return Math::absd(x); - } - - static inline int64_t absi(int64_t x) { - return ABS(x); - } - - static inline Variant sign(const Variant &x, Callable::CallError &r_error) { - r_error.error = Callable::CallError::CALL_OK; - switch (x.get_type()) { - case Variant::INT: { - return SGN(VariantInternalAccessor::get(&x)); - } break; - case Variant::FLOAT: { - return SGN(VariantInternalAccessor::get(&x)); - } break; - case Variant::VECTOR2: { - return VariantInternalAccessor::get(&x).sign(); - } break; - case Variant::VECTOR2I: { - return VariantInternalAccessor::get(&x).sign(); - } break; - case Variant::VECTOR3: { - return VariantInternalAccessor::get(&x).sign(); - } break; - case Variant::VECTOR3I: { - return VariantInternalAccessor::get(&x).sign(); - } break; - default: { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; - return Variant(); - } - } - } - - static inline double signf(double x) { - return SGN(x); - } - - static inline int64_t signi(int64_t x) { - return SGN(x); - } - - static inline double pow(double x, double y) { - return Math::pow(x, y); - } - static inline double log(double x) { - return Math::log(x); - } - - static inline double exp(double x) { - return Math::exp(x); - } - - static inline double is_nan(double x) { - return Math::is_nan(x); - } - - static inline double is_inf(double x) { - return Math::is_inf(x); - } - - static inline double is_equal_approx(double x, double y) { - return Math::is_equal_approx(x, y); - } - - static inline double is_zero_approx(double x) { - return Math::is_zero_approx(x); - } - - static inline double ease(float x, float c) { - return Math::ease(x, c); - } - - static inline int step_decimals(float step) { - return Math::step_decimals(step); - } - - static inline int range_step_decimals(float step) { - return Math::range_step_decimals(step); - } - - static inline double stepify(double value, double step) { - return Math::stepify(value, step); - } - - static inline double lerp(double from, double to, double weight) { - return Math::lerp(from, to, weight); - } - - static inline double lerp_angle(double from, double to, double weight) { - return Math::lerp_angle(from, to, weight); - } - - static inline double inverse_lerp(double from, double to, double weight) { - return Math::inverse_lerp(from, to, weight); - } - - static inline double range_lerp(double value, double istart, double istop, double ostart, double ostop) { - return Math::range_lerp(value, istart, istop, ostart, ostop); - } - - static inline double smoothstep(double from, double to, double val) { - return Math::smoothstep(from, to, val); - } - - static inline double move_toward(double from, double to, double delta) { - return Math::move_toward(from, to, delta); - } - - static inline double dectime(double value, double amount, double step) { - return Math::dectime(value, amount, step); - } - - static inline double deg2rad(double angle_deg) { - return Math::deg2rad(angle_deg); - } - - static inline double rad2deg(double angle_rad) { - return Math::rad2deg(angle_rad); - } - - static inline double linear2db(double linear) { - return Math::linear2db(linear); - } - - static inline double db2linear(double db) { - return Math::db2linear(db); - } - - static inline Vector2 polar2cartesian(double r, double th) { - return Vector2(r * Math::cos(th), r * Math::sin(th)); - } - - static inline Vector2 cartesian2polar(double x, double y) { - return Vector2(Math::sqrt(x * x + y * y), Math::atan2(y, x)); - } - - static inline int64_t wrapi(int64_t value, int64_t min, int64_t max) { - return Math::wrapi(value, min, max); - } - static inline double wrapf(double value, double min, double max) { - return Math::wrapf(value, min, max); - } - - static inline Variant max(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { - if (p_argcount < 2) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.expected = 2; - return Variant(); - } - Variant base = *p_args[0]; - Variant ret; - for (int i = 1; i < p_argcount; i++) { - bool valid; - Variant::evaluate(Variant::OP_GREATER, base, *p_args[i], ret, valid); - if (!valid) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.expected = base.get_type(); - r_error.argument = i; - return Variant(); - } - if (ret.booleanize()) { - base = *p_args[i]; - } - } - r_error.error = Callable::CallError::CALL_OK; - return base; - } - - static inline double maxf(double x, double y) { - return MAX(x, y); - } - - static inline int64_t maxi(int64_t x, int64_t y) { - return MAX(x, y); - } - - static inline Variant min(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { - if (p_argcount < 2) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.expected = 2; - return Variant(); - } - Variant base = *p_args[0]; - Variant ret; - for (int i = 1; i < p_argcount; i++) { - bool valid; - Variant::evaluate(Variant::OP_LESS, base, *p_args[i], ret, valid); - if (!valid) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.expected = base.get_type(); - r_error.argument = i; - return Variant(); - } - if (ret.booleanize()) { - base = *p_args[i]; - } - } - r_error.error = Callable::CallError::CALL_OK; - return base; - } - - static inline double minf(double x, double y) { - return MIN(x, y); - } - - static inline int64_t mini(int64_t x, int64_t y) { - return MIN(x, y); - } - - static inline Variant clamp(const Variant &x, const Variant &min, const Variant &max, Callable::CallError &r_error) { - Variant value = x; - - Variant ret; - - bool valid; - Variant::evaluate(Variant::OP_LESS, value, min, ret, valid); - if (!valid) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.expected = value.get_type(); - r_error.argument = 1; - return Variant(); - } - if (ret.booleanize()) { - value = min; - } - Variant::evaluate(Variant::OP_GREATER, value, max, ret, valid); - if (!valid) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.expected = value.get_type(); - r_error.argument = 2; - return Variant(); - } - if (ret.booleanize()) { - value = max; - } - - r_error.error = Callable::CallError::CALL_OK; - - return value; - } - - static inline double clampf(double x, double min, double max) { - return CLAMP(x, min, max); - } - - static inline int64_t clampi(int64_t x, int64_t min, int64_t max) { - return CLAMP(x, min, max); - } - - static inline int64_t nearest_po2(int64_t x) { - return nearest_power_of_2_templated(uint64_t(x)); - } - - // Random - - static inline void randomize() { - Math::randomize(); - } - - static inline int64_t randi() { - return Math::rand(); - } - - static inline double randf() { - return Math::randf(); - } - - static inline int64_t randi_range(int64_t from, int64_t to) { - return Math::random((int32_t)from, (int32_t)to); - } - - static inline double randf_range(double from, double to) { - return Math::random(from, to); - } - - static inline void seed(int64_t s) { - return Math::seed(s); - } - - static inline PackedInt64Array rand_from_seed(int64_t seed) { - uint64_t s = seed; - PackedInt64Array arr; - arr.resize(2); - arr.write[0] = Math::rand_from_seed(&s); - arr.write[1] = s; - return arr; - } - - // Utility - - static inline Variant weakref(const Variant &obj, Callable::CallError &r_error) { - if (obj.get_type() == Variant::OBJECT) { - r_error.error = Callable::CallError::CALL_OK; - if (obj.is_ref()) { - Ref wref = memnew(WeakRef); - REF r = obj; - if (r.is_valid()) { - wref->set_ref(r); - } - return wref; - } else { - Ref wref = memnew(WeakRef); - Object *o = obj.get_validated_object(); - if (o) { - wref->set_obj(o); - } - return wref; - } - } else if (obj.get_type() == Variant::NIL) { - r_error.error = Callable::CallError::CALL_OK; - Ref wref = memnew(WeakRef); - return wref; - } else { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; - r_error.argument = 0; - r_error.expected = Variant::OBJECT; - return Variant(); - } - } - - static inline int64_t _typeof(const Variant &obj) { - return obj.get_type(); - } - - static inline String str(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { - if (p_arg_count < 1) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 1; - return String(); - } - String str; - for (int i = 0; i < p_arg_count; i++) { - String os = p_args[i]->operator String(); - - if (i == 0) { - str = os; - } else { - str += os; - } - } - - r_error.error = Callable::CallError::CALL_OK; - - return str; - } - - static inline void print(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { - if (p_arg_count < 1) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 1; - } - String str; - for (int i = 0; i < p_arg_count; i++) { - String os = p_args[i]->operator String(); - - if (i == 0) { - str = os; - } else { - str += os; - } - } - - print_line(str); - r_error.error = Callable::CallError::CALL_OK; - } - - static inline void printerr(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { - if (p_arg_count < 1) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 1; - } - String str; - for (int i = 0; i < p_arg_count; i++) { - String os = p_args[i]->operator String(); - - if (i == 0) { - str = os; - } else { - str += os; - } - } - - print_error(str); - r_error.error = Callable::CallError::CALL_OK; - } - - static inline void printt(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { - if (p_arg_count < 1) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 1; - } - String str; - for (int i = 0; i < p_arg_count; i++) { - if (i) { - str += "\t"; - } - str += p_args[i]->operator String(); - } - - print_error(str); - r_error.error = Callable::CallError::CALL_OK; - } - - static inline void prints(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { - if (p_arg_count < 1) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 1; - } - String str; - for (int i = 0; i < p_arg_count; i++) { - if (i) { - str += " "; - } - str += p_args[i]->operator String(); - } - - print_error(str); - r_error.error = Callable::CallError::CALL_OK; - } - - static inline void printraw(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { - if (p_arg_count < 1) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 1; - } - String str; - for (int i = 0; i < p_arg_count; i++) { - String os = p_args[i]->operator String(); - - if (i == 0) { - str = os; - } else { - str += os; - } - } - - OS::get_singleton()->print("%s", str.utf8().get_data()); - r_error.error = Callable::CallError::CALL_OK; - } - - static inline void push_error(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { - if (p_arg_count < 1) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 1; - } - String str; - for (int i = 0; i < p_arg_count; i++) { - String os = p_args[i]->operator String(); - - if (i == 0) { - str = os; - } else { - str += os; - } - } - - ERR_PRINT(str); - r_error.error = Callable::CallError::CALL_OK; - } - - static inline void push_warning(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { - if (p_arg_count < 1) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 1; - } - String str; - for (int i = 0; i < p_arg_count; i++) { - String os = p_args[i]->operator String(); - - if (i == 0) { - str = os; - } else { - str += os; - } - } - - WARN_PRINT(str); - r_error.error = Callable::CallError::CALL_OK; - } - - static inline String var2str(const Variant &p_var) { - String vars; - VariantWriter::write_to_string(p_var, vars); - return vars; - } - - static inline Variant str2var(const String &p_var) { - VariantParser::StreamString ss; - ss.s = p_var; - - String errs; - int line; - Variant ret; - (void)VariantParser::parse(&ss, ret, errs, line); - - return ret; - } - - static inline PackedByteArray var2bytes(const Variant &p_var) { - int len; - Error err = encode_variant(p_var, nullptr, len, false); - if (err != OK) { - return PackedByteArray(); - } - - PackedByteArray barr; - barr.resize(len); - { - uint8_t *w = barr.ptrw(); - err = encode_variant(p_var, w, len, false); - if (err != OK) { - return PackedByteArray(); - } - } - - return barr; - } - - static inline PackedByteArray var2bytes_with_objects(const Variant &p_var) { - int len; - Error err = encode_variant(p_var, nullptr, len, true); - if (err != OK) { - return PackedByteArray(); - } - - PackedByteArray barr; - barr.resize(len); - { - uint8_t *w = barr.ptrw(); - err = encode_variant(p_var, w, len, true); - if (err != OK) { - return PackedByteArray(); - } - } - - return barr; - } - - static inline Variant bytes2var(const PackedByteArray &p_arr) { - Variant ret; - { - const uint8_t *r = p_arr.ptr(); - Error err = decode_variant(ret, r, p_arr.size(), nullptr, false); - if (err != OK) { - return Variant(); - } - } - return ret; - } - - static inline Variant bytes2var_with_objects(const PackedByteArray &p_arr) { - Variant ret; - { - const uint8_t *r = p_arr.ptr(); - Error err = decode_variant(ret, r, p_arr.size(), nullptr, true); - if (err != OK) { - return Variant(); - } - } - return ret; - } - - static inline int64_t hash(const Variant &p_arr) { - return p_arr.hash(); - } - - static inline Variant instance_from_id(int64_t p_id) { - ObjectID id = ObjectID((uint64_t)p_id); - Variant ret = ObjectDB::get_instance(id); - return ret; - } - - static inline bool is_instance_id_valid(int64_t p_id) { - return ObjectDB::get_instance(ObjectID((uint64_t)p_id)) != nullptr; - } - - static inline bool is_instance_valid(const Variant &p_instance) { - if (p_instance.get_type() != Variant::OBJECT) { - return false; - } - return p_instance.get_validated_object() != nullptr; - } -}; - -#ifdef DEBUG_METHODS_ENABLED -#define VCALLR *ret = p_func(VariantCasterAndValidate

::cast(p_args, Is, r_error)...) -#define VCALL p_func(VariantCasterAndValidate

::cast(p_args, Is, r_error)...) -#else -#define VCALLR *ret = p_func(VariantCaster

::cast(*p_args[Is])...) -#define VCALL p_func(VariantCaster

::cast(*p_args[Is])...) -#endif - -template -static _FORCE_INLINE_ void call_helperpr(R (*p_func)(P...), Variant *ret, const Variant **p_args, Callable::CallError &r_error, IndexSequence) { - r_error.error = Callable::CallError::CALL_OK; - VCALLR; - (void)p_args; - (void)r_error; -} - -template -static _FORCE_INLINE_ void validated_call_helperpr(R (*p_func)(P...), Variant *ret, const Variant **p_args, IndexSequence) { - *ret = p_func(VariantCaster

::cast(*p_args[Is])...); - (void)p_args; -} - -template -static _FORCE_INLINE_ void ptr_call_helperpr(R (*p_func)(P...), void *ret, const void **p_args, IndexSequence) { - PtrToArg::encode(p_func(PtrToArg

::convert(p_args[Is])...), ret); - (void)p_args; -} - -template -static _FORCE_INLINE_ void call_helperr(R (*p_func)(P...), Variant *ret, const Variant **p_args, Callable::CallError &r_error) { - call_helperpr(p_func, ret, p_args, r_error, BuildIndexSequence{}); -} - -template -static _FORCE_INLINE_ void validated_call_helperr(R (*p_func)(P...), Variant *ret, const Variant **p_args) { - validated_call_helperpr(p_func, ret, p_args, BuildIndexSequence{}); -} - -template -static _FORCE_INLINE_ void ptr_call_helperr(R (*p_func)(P...), void *ret, const void **p_args) { - ptr_call_helperpr(p_func, ret, p_args, BuildIndexSequence{}); -} - -template -static _FORCE_INLINE_ int get_arg_count_helperr(R (*p_func)(P...)) { - return sizeof...(P); -} - -template -static _FORCE_INLINE_ Variant::Type get_arg_type_helperr(R (*p_func)(P...), int p_arg) { - return call_get_argument_type(p_arg); -} - -template -static _FORCE_INLINE_ Variant::Type get_ret_type_helperr(R (*p_func)(P...)) { - return GetTypeInfo::VARIANT_TYPE; -} - -// WITHOUT RET - -template -static _FORCE_INLINE_ void call_helperp(void (*p_func)(P...), const Variant **p_args, Callable::CallError &r_error, IndexSequence) { - r_error.error = Callable::CallError::CALL_OK; - VCALL; - (void)p_args; - (void)r_error; -} - -template -static _FORCE_INLINE_ void validated_call_helperp(void (*p_func)(P...), const Variant **p_args, IndexSequence) { - p_func(VariantCaster

::cast(*p_args[Is])...); - (void)p_args; -} - -template -static _FORCE_INLINE_ void ptr_call_helperp(void (*p_func)(P...), const void **p_args, IndexSequence) { - p_func(PtrToArg

::convert(p_args[Is])...); - (void)p_args; -} - -template -static _FORCE_INLINE_ void call_helper(void (*p_func)(P...), const Variant **p_args, Callable::CallError &r_error) { - call_helperp(p_func, p_args, r_error, BuildIndexSequence{}); -} - -template -static _FORCE_INLINE_ void validated_call_helper(void (*p_func)(P...), const Variant **p_args) { - validated_call_helperp(p_func, p_args, BuildIndexSequence{}); -} - -template -static _FORCE_INLINE_ void ptr_call_helper(void (*p_func)(P...), const void **p_args) { - ptr_call_helperp(p_func, p_args, BuildIndexSequence{}); -} - -template -static _FORCE_INLINE_ int get_arg_count_helper(void (*p_func)(P...)) { - return sizeof...(P); -} - -template -static _FORCE_INLINE_ Variant::Type get_arg_type_helper(void (*p_func)(P...), int p_arg) { - return call_get_argument_type(p_arg); -} - -template -static _FORCE_INLINE_ Variant::Type get_ret_type_helper(void (*p_func)(P...)) { - return Variant::NIL; -} - -#define FUNCBINDR(m_func, m_args, m_category) \ - class Func_##m_func { \ - public: \ - static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ - call_helperr(VariantBuiltinFunctions::m_func, r_ret, p_args, r_error); \ - } \ - \ - static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ - validated_call_helperr(VariantBuiltinFunctions::m_func, r_ret, p_args); \ - } \ - static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ - ptr_call_helperr(VariantBuiltinFunctions::m_func, ret, p_args); \ - } \ - \ - static int get_argument_count() { \ - return get_arg_count_helperr(VariantBuiltinFunctions::m_func); \ - } \ - \ - static Variant::Type get_argument_type(int p_arg) { \ - return get_arg_type_helperr(VariantBuiltinFunctions::m_func, p_arg); \ - } \ - \ - static Variant::Type get_return_type() { \ - return get_ret_type_helperr(VariantBuiltinFunctions::m_func); \ - } \ - static bool has_return_type() { \ - return true; \ - } \ - static bool is_vararg() { return false; } \ - static Variant::BuiltInFunctionType get_type() { return m_category; } \ - }; \ - register_builtin_function(#m_func, m_args) - -#define FUNCBINDVR(m_func, m_args, m_category) \ - class Func_##m_func { \ - public: \ - static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ - r_error.error = Callable::CallError::CALL_OK; \ - *r_ret = VariantBuiltinFunctions::m_func(*p_args[0], r_error); \ - } \ - \ - static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ - Callable::CallError ce; \ - *r_ret = VariantBuiltinFunctions::m_func(*p_args[0], ce); \ - } \ - static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ - Callable::CallError ce; \ - PtrToArg::encode(VariantBuiltinFunctions::m_func(PtrToArg::convert(p_args[0]), ce), ret); \ - } \ - \ - static int get_argument_count() { \ - return 1; \ - } \ - \ - static Variant::Type get_argument_type(int p_arg) { \ - return Variant::NIL; \ - } \ - \ - static Variant::Type get_return_type() { \ - return Variant::NIL; \ - } \ - static bool has_return_type() { \ - return true; \ - } \ - static bool is_vararg() { return false; } \ - static Variant::BuiltInFunctionType get_type() { return m_category; } \ - }; \ - register_builtin_function(#m_func, m_args) - -#define FUNCBINDVR3(m_func, m_args, m_category) \ - class Func_##m_func { \ - public: \ - static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ - r_error.error = Callable::CallError::CALL_OK; \ - *r_ret = VariantBuiltinFunctions::m_func(*p_args[0], *p_args[1], *p_args[2], r_error); \ - } \ - \ - static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ - Callable::CallError ce; \ - *r_ret = VariantBuiltinFunctions::m_func(*p_args[0], *p_args[1], *p_args[2], ce); \ - } \ - static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ - Callable::CallError ce; \ - Variant r; \ - r = VariantBuiltinFunctions::m_func(PtrToArg::convert(p_args[0]), PtrToArg::convert(p_args[1]), PtrToArg::convert(p_args[2]), ce); \ - PtrToArg::encode(r, ret); \ - } \ - \ - static int get_argument_count() { \ - return 3; \ - } \ - \ - static Variant::Type get_argument_type(int p_arg) { \ - return Variant::NIL; \ - } \ - \ - static Variant::Type get_return_type() { \ - return Variant::NIL; \ - } \ - static bool has_return_type() { \ - return true; \ - } \ - static bool is_vararg() { return false; } \ - static Variant::BuiltInFunctionType get_type() { return m_category; } \ - }; \ - register_builtin_function(#m_func, m_args) - -#define FUNCBINDVARARG(m_func, m_args, m_category) \ - class Func_##m_func { \ - public: \ - static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ - r_error.error = Callable::CallError::CALL_OK; \ - *r_ret = VariantBuiltinFunctions::m_func(p_args, p_argcount, r_error); \ - } \ - \ - static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ - Callable::CallError c; \ - *r_ret = VariantBuiltinFunctions::m_func(p_args, p_argcount, c); \ - } \ - static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ - Vector args; \ - for (int i = 0; i < p_argcount; i++) { \ - args.push_back(PtrToArg::convert(p_args[i])); \ - } \ - Vector argsp; \ - for (int i = 0; i < p_argcount; i++) { \ - argsp.push_back(&args[i]); \ - } \ - Variant r; \ - validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \ - PtrToArg::encode(r, ret); \ - } \ - \ - static int get_argument_count() { \ - return 2; \ - } \ - \ - static Variant::Type get_argument_type(int p_arg) { \ - return Variant::NIL; \ - } \ - \ - static Variant::Type get_return_type() { \ - return Variant::NIL; \ - } \ - static bool has_return_type() { \ - return true; \ - } \ - static bool is_vararg() { \ - return true; \ - } \ - static Variant::BuiltInFunctionType get_type() { \ - return m_category; \ - } \ - }; \ - register_builtin_function(#m_func, m_args) - -#define FUNCBINDVARARGS(m_func, m_args, m_category) \ - class Func_##m_func { \ - public: \ - static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ - r_error.error = Callable::CallError::CALL_OK; \ - *r_ret = VariantBuiltinFunctions::m_func(p_args, p_argcount, r_error); \ - } \ - \ - static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ - Callable::CallError c; \ - *r_ret = VariantBuiltinFunctions::m_func(p_args, p_argcount, c); \ - } \ - static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ - Vector args; \ - for (int i = 0; i < p_argcount; i++) { \ - args.push_back(PtrToArg::convert(p_args[i])); \ - } \ - Vector argsp; \ - for (int i = 0; i < p_argcount; i++) { \ - argsp.push_back(&args[i]); \ - } \ - Variant r; \ - validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \ - PtrToArg::encode(r.operator String(), ret); \ - } \ - \ - static int get_argument_count() { \ - return 1; \ - } \ - \ - static Variant::Type get_argument_type(int p_arg) { \ - return Variant::NIL; \ - } \ - \ - static Variant::Type get_return_type() { \ - return Variant::STRING; \ - } \ - static bool has_return_type() { \ - return true; \ - } \ - static bool is_vararg() { \ - return true; \ - } \ - static Variant::BuiltInFunctionType get_type() { \ - return m_category; \ - } \ - }; \ - register_builtin_function(#m_func, m_args) - -#define FUNCBINDVARARGV(m_func, m_args, m_category) \ - class Func_##m_func { \ - public: \ - static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ - r_error.error = Callable::CallError::CALL_OK; \ - VariantBuiltinFunctions::m_func(p_args, p_argcount, r_error); \ - } \ - \ - static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ - Callable::CallError c; \ - VariantBuiltinFunctions::m_func(p_args, p_argcount, c); \ - } \ - static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ - Vector args; \ - for (int i = 0; i < p_argcount; i++) { \ - args.push_back(PtrToArg::convert(p_args[i])); \ - } \ - Vector argsp; \ - for (int i = 0; i < p_argcount; i++) { \ - argsp.push_back(&args[i]); \ - } \ - Variant r; \ - validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \ - } \ - \ - static int get_argument_count() { \ - return 1; \ - } \ - \ - static Variant::Type get_argument_type(int p_arg) { \ - return Variant::NIL; \ - } \ - \ - static Variant::Type get_return_type() { \ - return Variant::NIL; \ - } \ - static bool has_return_type() { \ - return false; \ - } \ - static bool is_vararg() { \ - return true; \ - } \ - static Variant::BuiltInFunctionType get_type() { \ - return m_category; \ - } \ - }; \ - register_builtin_function(#m_func, m_args) - -#define FUNCBIND(m_func, m_args, m_category) \ - class Func_##m_func { \ - public: \ - static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ - call_helper(VariantBuiltinFunctions::m_func, p_args, r_error); \ - } \ - \ - static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ - validated_call_helper(VariantBuiltinFunctions::m_func, p_args); \ - } \ - static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ - ptr_call_helper(VariantBuiltinFunctions::m_func, p_args); \ - } \ - \ - static int get_argument_count() { \ - return get_arg_count_helper(VariantBuiltinFunctions::m_func); \ - } \ - \ - static Variant::Type get_argument_type(int p_arg) { \ - return get_arg_type_helper(VariantBuiltinFunctions::m_func, p_arg); \ - } \ - \ - static Variant::Type get_return_type() { \ - return get_ret_type_helper(VariantBuiltinFunctions::m_func); \ - } \ - static bool has_return_type() { \ - return false; \ - } \ - static bool is_vararg() { return false; } \ - static Variant::BuiltInFunctionType get_type() { return m_category; } \ - }; \ - register_builtin_function(#m_func, m_args) - -struct VariantBuiltinFunctionInfo { - void (*call_builtin)(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error); - Variant::BuiltinFunctionValidatedCall validated_call_builtin; - Variant::BuiltinFunctionPTRCall ptr_call_builtin; - Vector argnames; - bool is_vararg; - bool returns_value; - int argcount; - Variant::Type (*get_arg_type)(int); - Variant::Type return_type; - Variant::BuiltInFunctionType type; -}; - -static OAHashMap builtin_function_table; -static List builtin_function_name_table; - -template -static void register_builtin_function(const String &p_name, const Vector &argnames) { - String name = p_name; - if (name.begins_with("_")) { - name = name.substr(1, name.length() - 1); - } - StringName sname = name; - ERR_FAIL_COND(builtin_function_table.has(sname)); - - VariantBuiltinFunctionInfo bfi; - bfi.call_builtin = T::call; - bfi.validated_call_builtin = T::validated_call; - bfi.ptr_call_builtin = T::ptrcall; - bfi.is_vararg = T::is_vararg(); - bfi.argnames = argnames; - bfi.argcount = T::get_argument_count(); - if (!bfi.is_vararg) { - ERR_FAIL_COND_MSG(argnames.size() != bfi.argcount, "wrong number of arguments binding builtin function: " + name); - } - bfi.get_arg_type = T::get_argument_type; - bfi.return_type = T::get_return_type(); - bfi.type = T::get_type(); - bfi.returns_value = T::has_return_type(); - - builtin_function_table.insert(sname, bfi); - builtin_function_name_table.push_back(sname); -} - -void Variant::_register_variant_builtin_funcs() { - // Math - - FUNCBINDR(sin, sarray("angle_rad"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(cos, sarray("angle_rad"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(tan, sarray("angle_rad"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(sinh, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(cosh, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(tanh, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(asin, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(acos, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(atan, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(atan2, sarray("y", "x"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(sqrt, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(fmod, sarray("x", "y"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(fposmod, sarray("x", "y"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(floor, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(ceil, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(round, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDVR(abs, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(absf, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(absi, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDVR(sign, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(signf, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(signi, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(pow, sarray("x", "y"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(log, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(exp, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(is_nan, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(is_inf, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(is_equal_approx, sarray("a", "b"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(is_zero_approx, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(ease, sarray("x", "c"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(step_decimals, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(range_step_decimals, sarray("x"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(stepify, sarray("x", "y"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(lerp, sarray("from", "to", "c"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(lerp_angle, sarray("from", "to", "c"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(inverse_lerp, sarray("from", "to", "c"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(range_lerp, sarray("value", "istart", "istop", "ostart", "ostop"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(smoothstep, sarray("from", "to", "c"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(move_toward, sarray("from", "to", "delta"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(dectime, sarray("value", "amount", "step"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(deg2rad, sarray("deg"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(rad2deg, sarray("rad"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(linear2db, sarray("lin"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(db2linear, sarray("db"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(polar2cartesian, sarray("r", "th"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(cartesian2polar, sarray("x", "y"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(wrapi, sarray("value", "min", "max"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(wrapf, sarray("value", "min", "max"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDVARARG(max, sarray(), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(maxi, sarray("a", "b"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(maxf, sarray("a", "b"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDVARARG(min, sarray(), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(mini, sarray("a", "b"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(minf, sarray("a", "b"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDVR3(clamp, sarray("value", "min", "max"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(clampi, sarray("value", "min", "max"), Variant::BUILTIN_FUNC_TYPE_MATH); - FUNCBINDR(clampf, sarray("value", "min", "max"), Variant::BUILTIN_FUNC_TYPE_MATH); - - FUNCBINDR(nearest_po2, sarray("value"), Variant::BUILTIN_FUNC_TYPE_MATH); - - //Random - - FUNCBIND(randomize, sarray(), Variant::BUILTIN_FUNC_TYPE_RANDOM); - FUNCBINDR(randi, sarray(), Variant::BUILTIN_FUNC_TYPE_RANDOM); - FUNCBINDR(randf, sarray(), Variant::BUILTIN_FUNC_TYPE_RANDOM); - FUNCBINDR(randi_range, sarray("from", "to"), Variant::BUILTIN_FUNC_TYPE_RANDOM); - FUNCBINDR(randf_range, sarray("from", "to"), Variant::BUILTIN_FUNC_TYPE_RANDOM); - FUNCBIND(seed, sarray("base"), Variant::BUILTIN_FUNC_TYPE_RANDOM); - FUNCBINDR(rand_from_seed, sarray("seed"), Variant::BUILTIN_FUNC_TYPE_RANDOM); - - // Utility - FUNCBINDVR(weakref, sarray("from"), Variant::BUILTIN_FUNC_TYPE_UTILITY); - FUNCBINDR(_typeof, sarray("variable"), Variant::BUILTIN_FUNC_TYPE_UTILITY); - FUNCBINDVARARGS(str, sarray(), Variant::BUILTIN_FUNC_TYPE_UTILITY); - FUNCBINDVARARGV(print, sarray(), Variant::BUILTIN_FUNC_TYPE_UTILITY); - FUNCBINDVARARGV(printerr, sarray(), Variant::BUILTIN_FUNC_TYPE_UTILITY); - FUNCBINDVARARGV(printt, sarray(), Variant::BUILTIN_FUNC_TYPE_UTILITY); - FUNCBINDVARARGV(prints, sarray(), Variant::BUILTIN_FUNC_TYPE_UTILITY); - FUNCBINDVARARGV(printraw, sarray(), Variant::BUILTIN_FUNC_TYPE_UTILITY); - FUNCBINDVARARGV(push_error, sarray(), Variant::BUILTIN_FUNC_TYPE_UTILITY); - FUNCBINDVARARGV(push_warning, sarray(), Variant::BUILTIN_FUNC_TYPE_UTILITY); - - FUNCBINDR(var2str, sarray("variable"), Variant::BUILTIN_FUNC_TYPE_UTILITY); - FUNCBINDR(str2var, sarray("string"), Variant::BUILTIN_FUNC_TYPE_UTILITY); - - FUNCBINDR(var2bytes, sarray("variable"), Variant::BUILTIN_FUNC_TYPE_UTILITY); - FUNCBINDR(bytes2var, sarray("bytes"), Variant::BUILTIN_FUNC_TYPE_UTILITY); - - FUNCBINDR(var2bytes_with_objects, sarray("variable"), Variant::BUILTIN_FUNC_TYPE_UTILITY); - FUNCBINDR(bytes2var_with_objects, sarray("bytes"), Variant::BUILTIN_FUNC_TYPE_UTILITY); - - FUNCBINDR(hash, sarray("variable"), Variant::BUILTIN_FUNC_TYPE_UTILITY); - - FUNCBINDR(instance_from_id, sarray("id"), Variant::BUILTIN_FUNC_TYPE_UTILITY); - FUNCBINDR(is_instance_id_valid, sarray("id"), Variant::BUILTIN_FUNC_TYPE_UTILITY); - FUNCBINDR(is_instance_valid, sarray("instance"), Variant::BUILTIN_FUNC_TYPE_UTILITY); -} -void Variant::_unregister_variant_builtin_funcs() { - builtin_function_table.clear(); - builtin_function_name_table.clear(); -} - -void Variant::call_builtin_func(const StringName &p_name, Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { - const VariantBuiltinFunctionInfo *bfi = builtin_function_table.lookup_ptr(p_name); - if (!bfi) { - r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; - r_error.argument = 0; - r_error.expected = 0; - return; - } - - if (unlikely(!bfi->is_vararg && p_argcount < bfi->argcount)) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = 0; - r_error.expected = bfi->argcount; - return; - } - - if (unlikely(!bfi->is_vararg && p_argcount > bfi->argcount)) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; - r_error.argument = 0; - r_error.expected = bfi->argcount; - return; - } - - bfi->call_builtin(r_ret, p_args, p_argcount, r_error); -} - -bool Variant::has_builtin_func(const StringName &p_name) { - return builtin_function_table.has(p_name); -} - -Variant::BuiltinFunctionValidatedCall Variant::get_builtin_validated_caller(const StringName &p_name) { - const VariantBuiltinFunctionInfo *bfi = builtin_function_table.lookup_ptr(p_name); - if (!bfi) { - return nullptr; - } - - return bfi->validated_call_builtin; -} -Variant::BuiltinFunctionPTRCall Variant::get_builtin_ptr_caller(const StringName &p_name) { - const VariantBuiltinFunctionInfo *bfi = builtin_function_table.lookup_ptr(p_name); - if (!bfi) { - return nullptr; - } - - return bfi->ptr_call_builtin; -} - -Variant::BuiltInFunctionType Variant::get_builtin_func_type(const StringName &p_name) { - const VariantBuiltinFunctionInfo *bfi = builtin_function_table.lookup_ptr(p_name); - if (!bfi) { - return Variant::BUILTIN_FUNC_TYPE_MATH; - } - - return bfi->type; -} - -int Variant::get_builtin_func_argument_count(const StringName &p_name) { - const VariantBuiltinFunctionInfo *bfi = builtin_function_table.lookup_ptr(p_name); - if (!bfi) { - return 0; - } - - return bfi->argcount; -} -Variant::Type Variant::get_builtin_func_argument_type(const StringName &p_name, int p_arg) { - const VariantBuiltinFunctionInfo *bfi = builtin_function_table.lookup_ptr(p_name); - if (!bfi) { - return Variant::NIL; - } - - return bfi->get_arg_type(p_arg); -} -String Variant::get_builtin_func_argument_name(const StringName &p_name, int p_arg) { - const VariantBuiltinFunctionInfo *bfi = builtin_function_table.lookup_ptr(p_name); - if (!bfi) { - return String(); - } - ERR_FAIL_COND_V(bfi->is_vararg, String()); - ERR_FAIL_INDEX_V(p_arg, bfi->argnames.size(), String()); - return bfi->argnames[p_arg]; -} -bool Variant::has_builtin_func_return_value(const StringName &p_name) { - const VariantBuiltinFunctionInfo *bfi = builtin_function_table.lookup_ptr(p_name); - if (!bfi) { - return false; - } - return bfi->returns_value; -} -Variant::Type Variant::get_builtin_func_return_type(const StringName &p_name) { - const VariantBuiltinFunctionInfo *bfi = builtin_function_table.lookup_ptr(p_name); - if (!bfi) { - return Variant::NIL; - } - - return bfi->return_type; -} -bool Variant::is_builtin_func_vararg(const StringName &p_name) { - const VariantBuiltinFunctionInfo *bfi = builtin_function_table.lookup_ptr(p_name); - if (!bfi) { - return false; - } - - return bfi->is_vararg; -} - -void Variant::get_builtin_function_list(List *r_functions) { - for (List::Element *E = builtin_function_name_table.front(); E; E = E->next()) { - r_functions->push_back(E->get()); - } -} diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 39290f2b04..4cb8457ccd 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -36,6 +36,7 @@ #include "core/io/compression.h" #include "core/object/class_db.h" #include "core/os/os.h" +#include "core/templates/local_vector.h" #include "core/templates/oa_hash_map.h" typedef void (*VariantFunc)(Variant &r_ret, Variant &p_self, const Variant **p_args); @@ -62,499 +63,330 @@ struct TypeAdjust { } }; -struct _VariantCall { - template - class InternalMethod : public Variant::InternalMethod { - public: - void (T::*method)(P...); - Vector default_values; -#ifdef DEBUG_ENABLED - Vector argument_names; -#endif - - virtual int get_argument_count() const { - return sizeof...(P); - } - virtual Variant::Type get_argument_type(int p_arg) const { - return call_get_argument_type(p_arg); - } -#ifdef DEBUG_ENABLED - virtual String get_argument_name(int p_arg) const { - ERR_FAIL_INDEX_V(p_arg, argument_names.size(), String()); - return argument_names[p_arg]; - } -#endif - virtual Vector get_default_arguments() const { - return default_values; - } - - virtual Variant::Type get_return_type() const { - return Variant::NIL; - } - virtual uint32_t get_flags() const { - return 0; - } - - virtual void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) { - call_with_variant_args_dv(VariantGetInternalPtr::get_ptr(base), method, p_args, p_argcount, r_error, default_values); - } - - virtual void validated_call(Variant *base, const Variant **p_args, Variant *r_ret) { - call_with_validated_variant_args(base, method, p_args); - } - -#ifdef PTRCALL_ENABLED - virtual void ptrcall(void *p_base, const void **p_args, void *r_ret) { - call_with_ptr_args(reinterpret_cast(p_base), method, p_args); - } -#endif - InternalMethod(void (T::*p_method)(P...), const Vector &p_default_args -#ifdef DEBUG_ENABLED - , - const Vector &p_arg_names, const StringName &p_method_name, Variant::Type p_base_type -#endif - ) { - method = p_method; - default_values = p_default_args; -#ifdef DEBUG_ENABLED - argument_names = p_arg_names; - method_name = p_method_name; - base_type = p_base_type; -#endif - } - }; - - template - class InternalMethodR : public Variant::InternalMethod { - public: - R(T::*method) - (P...); - Vector default_values; -#ifdef DEBUG_ENABLED - Vector argument_names; -#endif - - virtual int get_argument_count() const { - return sizeof...(P); - } - virtual Variant::Type get_argument_type(int p_arg) const { - return call_get_argument_type(p_arg); - return Variant::NIL; - } -#ifdef DEBUG_ENABLED - virtual String get_argument_name(int p_arg) const { - ERR_FAIL_INDEX_V(p_arg, argument_names.size(), String()); - return argument_names[p_arg]; - } -#endif - virtual Vector get_default_arguments() const { - return default_values; - } +template +static _FORCE_INLINE_ void vc_method_call(R (T::*method)(P...), Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector &p_defvals, Callable::CallError &r_error) { + call_with_variant_args_ret_dv(VariantGetInternalPtr::get_ptr(base), method, p_args, p_argcount, r_ret, r_error, p_defvals); +} - virtual Variant::Type get_return_type() const { - return GetTypeInfo::VARIANT_TYPE; - } - virtual uint32_t get_flags() const { - uint32_t f = 0; - if (get_return_type() == Variant::NIL) { - f |= FLAG_RETURNS_VARIANT; - } - return f; - } +template +static _FORCE_INLINE_ void vc_method_call(R (T::*method)(P...) const, Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector &p_defvals, Callable::CallError &r_error) { + call_with_variant_args_retc_dv(VariantGetInternalPtr::get_ptr(base), method, p_args, p_argcount, r_ret, r_error, p_defvals); +} - virtual void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) { - call_with_variant_args_ret_dv(VariantGetInternalPtr::get_ptr(base), method, p_args, p_argcount, r_ret, r_error, default_values); - } +template +static _FORCE_INLINE_ void vc_method_call(void (T::*method)(P...), Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector &p_defvals, Callable::CallError &r_error) { + call_with_variant_args_dv(VariantGetInternalPtr::get_ptr(base), method, p_args, p_argcount, r_error, p_defvals); +} - virtual void validated_call(Variant *base, const Variant **p_args, Variant *r_ret) { - TypeAdjust::adjust(r_ret); - call_with_validated_variant_args_ret(base, method, p_args, r_ret); - } -#ifdef PTRCALL_ENABLED - virtual void ptrcall(void *p_base, const void **p_args, void *r_ret) { - call_with_ptr_args_ret(reinterpret_cast(p_base), method, p_args, r_ret); - } -#endif - InternalMethodR(R (T::*p_method)(P...), const Vector &p_default_args -#ifdef DEBUG_ENABLED - , - const Vector &p_arg_names, const StringName &p_method_name, Variant::Type p_base_type -#endif - ) { - method = p_method; - default_values = p_default_args; -#ifdef DEBUG_ENABLED - argument_names = p_arg_names; - method_name = p_method_name; - base_type = p_base_type; -#endif - } - }; +template +static _FORCE_INLINE_ void vc_method_call(void (T::*method)(P...) const, Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector &p_defvals, Callable::CallError &r_error) { + call_with_variant_argsc_dv(VariantGetInternalPtr::get_ptr(base), method, p_args, p_argcount, r_error, p_defvals); +} - template - class InternalMethodRC : public Variant::InternalMethod { - public: - R(T::*method) - (P...) const; - Vector default_values; -#ifdef DEBUG_ENABLED - Vector argument_names; -#endif +template +static _FORCE_INLINE_ void vc_validated_call(R (T::*method)(P...), Variant *base, const Variant **p_args, Variant *r_ret) { + call_with_validated_variant_args_ret(base, method, p_args, r_ret); +} - virtual int get_argument_count() const { - return sizeof...(P); - } - virtual Variant::Type get_argument_type(int p_arg) const { - return call_get_argument_type(p_arg); - } -#ifdef DEBUG_ENABLED - virtual String get_argument_name(int p_arg) const { - ERR_FAIL_INDEX_V(p_arg, argument_names.size(), String()); - return argument_names[p_arg]; - } -#endif - virtual Vector get_default_arguments() const { - return default_values; - } +template +static _FORCE_INLINE_ void vc_validated_call(R (T::*method)(P...) const, Variant *base, const Variant **p_args, Variant *r_ret) { + call_with_validated_variant_args_retc(base, method, p_args, r_ret); +} +template +static _FORCE_INLINE_ void vc_validated_call(void (T::*method)(P...), Variant *base, const Variant **p_args, Variant *r_ret) { + call_with_validated_variant_args(base, method, p_args); +} - virtual Variant::Type get_return_type() const { - return GetTypeInfo::VARIANT_TYPE; - } - virtual uint32_t get_flags() const { - uint32_t f = FLAG_IS_CONST; - if (get_return_type() == Variant::NIL) { - f |= FLAG_RETURNS_VARIANT; - } - return f; - } +template +static _FORCE_INLINE_ void vc_validated_call(void (T::*method)(P...) const, Variant *base, const Variant **p_args, Variant *r_ret) { + call_with_validated_variant_argsc(base, method, p_args); +} - virtual void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) { - call_with_variant_args_retc_dv(VariantGetInternalPtr::get_ptr(base), method, p_args, p_argcount, r_ret, r_error, default_values); - } +template +static _FORCE_INLINE_ void vc_ptrcall(R (T::*method)(P...), void *p_base, const void **p_args, void *r_ret) { + call_with_ptr_args_ret(reinterpret_cast(p_base), method, p_args, r_ret); +} - virtual void validated_call(Variant *base, const Variant **p_args, Variant *r_ret) { - TypeAdjust::adjust(r_ret); - call_with_validated_variant_args_retc(base, method, p_args, r_ret); - } -#ifdef PTRCALL_ENABLED - virtual void ptrcall(void *p_base, const void **p_args, void *r_ret) { - call_with_ptr_args_retc(reinterpret_cast(p_base), method, p_args, r_ret); - } -#endif - InternalMethodRC(R (T::*p_method)(P...) const, const Vector &p_default_args -#ifdef DEBUG_ENABLED - , - const Vector &p_arg_names, const StringName &p_method_name, Variant::Type p_base_type -#endif - ) { - method = p_method; - default_values = p_default_args; -#ifdef DEBUG_ENABLED - argument_names = p_arg_names; - method_name = p_method_name; - base_type = p_base_type; -#endif - } - }; +template +static _FORCE_INLINE_ void vc_ptrcall(R (T::*method)(P...) const, void *p_base, const void **p_args, void *r_ret) { + call_with_ptr_args_retc(reinterpret_cast(p_base), method, p_args, r_ret); +} - template - class InternalMethodRS : public Variant::InternalMethod { - public: - R(*method) - (T *, P...); - Vector default_values; -#ifdef DEBUG_ENABLED - Vector argument_names; -#endif +template +static _FORCE_INLINE_ void vc_ptrcall(void (T::*method)(P...), void *p_base, const void **p_args, void *r_ret) { + call_with_ptr_args(reinterpret_cast(p_base), method, p_args); +} - virtual int get_argument_count() const { - return sizeof...(P); - } - virtual Variant::Type get_argument_type(int p_arg) const { - return call_get_argument_type(p_arg); - } -#ifdef DEBUG_ENABLED - virtual String get_argument_name(int p_arg) const { - ERR_FAIL_INDEX_V(p_arg, argument_names.size(), String()); - return argument_names[p_arg]; - } -#endif - virtual Vector get_default_arguments() const { - return default_values; - } +template +static _FORCE_INLINE_ void vc_ptrcall(void (T::*method)(P...) const, void *p_base, const void **p_args, void *r_ret) { + call_with_ptr_argsc(reinterpret_cast(p_base), method, p_args); +} - virtual Variant::Type get_return_type() const { - return GetTypeInfo::VARIANT_TYPE; - } - virtual uint32_t get_flags() const { - uint32_t f = 0; - if (get_return_type() == Variant::NIL) { - f |= FLAG_RETURNS_VARIANT; - } - return f; - } +template +static _FORCE_INLINE_ int vc_get_argument_count(R (T::*method)(P...)) { + return sizeof...(P); +} +template +static _FORCE_INLINE_ int vc_get_argument_count(R (T::*method)(P...) const) { + return sizeof...(P); +} - virtual void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) { - const Variant **args = p_args; -#ifdef DEBUG_ENABLED - if ((size_t)p_argcount > sizeof...(P)) { - r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; - r_error.argument = sizeof...(P); - return; - } -#endif - if ((size_t)p_argcount < sizeof...(P)) { - size_t missing = sizeof...(P) - (size_t)p_argcount; - if (missing <= (size_t)default_values.size()) { - args = (const Variant **)alloca(sizeof...(P) * sizeof(const Variant *)); - // GCC fails to see that `sizeof...(P)` cannot be 0 here given the previous - // conditions, so it raises a warning on the potential use of `i < 0` as the - // execution condition. -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wtype-limits" -#endif - for (size_t i = 0; i < sizeof...(P); i++) { - if (i < (size_t)p_argcount) { - args[i] = p_args[i]; - } else { - args[i] = &default_values[i - p_argcount + (default_values.size() - missing)]; - } - } -#if defined(__GNUC__) && !defined(__clang__) -#pragma GCC diagnostic pop -#endif - } else { -#ifdef DEBUG_ENABLED - r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; - r_error.argument = sizeof...(P); -#endif - return; - } - } - call_with_variant_args_retc_static_helper(VariantGetInternalPtr::get_ptr(base), method, args, r_ret, r_error, BuildIndexSequence{}); - } +template +static _FORCE_INLINE_ int vc_get_argument_count(void (T::*method)(P...)) { + return sizeof...(P); +} - virtual void validated_call(Variant *base, const Variant **p_args, Variant *r_ret) { - TypeAdjust::adjust(r_ret); - call_with_validated_variant_args_static_retc(base, method, p_args, r_ret); - } -#ifdef PTRCALL_ENABLED - virtual void ptrcall(void *p_base, const void **p_args, void *r_ret) { - call_with_ptr_args_static_retc(reinterpret_cast(p_base), method, p_args, r_ret); - } -#endif - InternalMethodRS(R (*p_method)(T *, P...), const Vector &p_default_args -#ifdef DEBUG_ENABLED - , - const Vector &p_arg_names, const StringName &p_method_name, Variant::Type p_base_type -#endif - ) { - method = p_method; - default_values = p_default_args; -#ifdef DEBUG_ENABLED - argument_names = p_arg_names; - method_name = p_method_name; - base_type = p_base_type; -#endif - } - }; +template +static _FORCE_INLINE_ int vc_get_argument_count(void (T::*method)(P...) const) { + return sizeof...(P); +} - class InternalMethodVC : public Variant::InternalMethod { - public: - typedef void (*MethodVC)(Variant *, const Variant **, int, Variant &r_ret, Callable::CallError &); - MethodVC methodvc = nullptr; - uint32_t base_flags = 0; - Vector argument_names; - Vector argument_types; - Variant::Type return_type = Variant::NIL; - - virtual int get_argument_count() const { - return argument_names.size(); - } - virtual Variant::Type get_argument_type(int p_arg) const { - ERR_FAIL_INDEX_V(p_arg, argument_types.size(), Variant::NIL); - return argument_types[p_arg]; - } -#ifdef DEBUG_ENABLED - virtual String get_argument_name(int p_arg) const { - ERR_FAIL_INDEX_V(p_arg, argument_names.size(), String()); - return argument_names[p_arg]; - } -#endif - virtual Vector get_default_arguments() const { - return Vector(); - } +template +static _FORCE_INLINE_ int vc_get_argument_count(R (*method)(T *, P...)) { + return sizeof...(P); +} - virtual Variant::Type get_return_type() const { - return return_type; - } - virtual uint32_t get_flags() const { - return base_flags | FLAG_NO_PTRCALL; - } +template +static _FORCE_INLINE_ Variant::Type vc_get_argument_type(R (T::*method)(P...), int p_arg) { + return call_get_argument_type(p_arg); +} +template +static _FORCE_INLINE_ Variant::Type vc_get_argument_type(R (T::*method)(P...) const, int p_arg) { + return call_get_argument_type(p_arg); +} - virtual void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) { - methodvc(base, p_args, p_argcount, r_ret, r_error); - } +template +static _FORCE_INLINE_ Variant::Type vc_get_argument_type(void (T::*method)(P...), int p_arg) { + return call_get_argument_type(p_arg); +} - virtual void validated_call(Variant *base, const Variant **p_args, Variant *r_ret) { - ERR_FAIL_MSG("No support for validated call"); - } -#ifdef PTRCALL_ENABLED - virtual void ptrcall(void *p_base, const void **p_args, void *r_ret) { - ERR_FAIL_MSG("No support for ptrcall call"); - } -#endif - InternalMethodVC(MethodVC p_method, uint32_t p_flags, const Vector &p_argument_types, const Variant::Type &p_return_type -#ifdef DEBUG_ENABLED - , - const Vector &p_arg_names, const StringName &p_method_name, Variant::Type p_base_type -#endif - ) { - methodvc = p_method; - argument_types = p_argument_types; - return_type = p_return_type; - base_flags = p_flags; -#ifdef DEBUG_ENABLED - argument_names = p_arg_names; - method_name = p_method_name; - base_type = p_base_type; -#endif - } - }; +template +static _FORCE_INLINE_ Variant::Type vc_get_argument_type(void (T::*method)(P...) const, int p_arg) { + return call_get_argument_type(p_arg); +} - typedef OAHashMap MethodMap; - static MethodMap *type_internal_methods; - static List *type_internal_method_names; +template +static _FORCE_INLINE_ Variant::Type vc_get_argument_type(R (*method)(T *, P...), int p_arg) { + return call_get_argument_type(p_arg); +} - template - static void _bind_method(const StringName &p_name, void (T::*p_method)(P...), const Vector &p_default_args = Vector() -#ifdef DEBUG_ENABLED - , - const Vector &p_argument_names = Vector() -#endif - ) { +template +static _FORCE_INLINE_ Variant::Type vc_get_return_type(R (T::*method)(P...)) { + return GetTypeInfo::VARIANT_TYPE; +} -#ifdef DEBUG_ENABLED - ERR_FAIL_COND_MSG(p_argument_names.size() != sizeof...(P), "Wrong argument name count supplied for method: " + Variant::get_type_name(GetTypeInfo::VARIANT_TYPE) + "::" + String(p_name)); - ERR_FAIL_COND(type_internal_methods[GetTypeInfo::VARIANT_TYPE].has(p_name)); -#endif -#ifdef DEBUG_ENABLED - Variant::InternalMethod *m = memnew((InternalMethod)(p_method, p_default_args, p_argument_names, p_name, GetTypeInfo::VARIANT_TYPE)); -#else - Variant::InternalMethod *m = memnew((InternalMethod)(p_method, p_default_args)); -#endif +template +static _FORCE_INLINE_ Variant::Type vc_get_return_type(R (T::*method)(P...) const) { + return GetTypeInfo::VARIANT_TYPE; +} - type_internal_methods[GetTypeInfo::VARIANT_TYPE].insert(p_name, m); - type_internal_method_names[GetTypeInfo::VARIANT_TYPE].push_back(p_name); - } +template +static _FORCE_INLINE_ Variant::Type vc_get_return_type(void (T::*method)(P...)) { + return Variant::NIL; +} - template - static void _bind_method(const StringName &p_name, R (T::*p_method)(P...) const, const Vector &p_default_args = Vector() -#ifdef DEBUG_ENABLED - , - const Vector &p_argument_names = Vector() -#endif - ) { -#ifdef DEBUG_ENABLED - ERR_FAIL_COND_MSG(p_argument_names.size() != sizeof...(P), "Wrong argument name count supplied for method: " + Variant::get_type_name(GetTypeInfo::VARIANT_TYPE) + "::" + String(p_name)); - ERR_FAIL_COND_MSG(type_internal_methods[GetTypeInfo::VARIANT_TYPE].has(p_name), " Method already registered: " + Variant::get_type_name(GetTypeInfo::VARIANT_TYPE) + "::" + String(p_name)); +template +static _FORCE_INLINE_ Variant::Type vc_get_return_type(void (T::*method)(P...) const) { + return Variant::NIL; +} -#endif -#ifdef DEBUG_ENABLED - Variant::InternalMethod *m = memnew((InternalMethodRC)(p_method, p_default_args, p_argument_names, p_name, GetTypeInfo::VARIANT_TYPE)); -#else - Variant::InternalMethod *m = memnew((InternalMethodRC)(p_method, p_default_args)); -#endif +template +static _FORCE_INLINE_ Variant::Type vc_get_return_type(R (*method)(P...)) { + return GetTypeInfo::VARIANT_TYPE; +} - type_internal_methods[GetTypeInfo::VARIANT_TYPE].insert(p_name, m); - type_internal_method_names[GetTypeInfo::VARIANT_TYPE].push_back(p_name); - } +template +static _FORCE_INLINE_ bool vc_has_return_type(R (T::*method)(P...)) { + return true; +} +template +static _FORCE_INLINE_ bool vc_has_return_type(R (T::*method)(P...) const) { + return true; +} - template - static void _bind_method(const StringName &p_name, R (T::*p_method)(P...), const Vector &p_default_args = Vector() -#ifdef DEBUG_ENABLED - , - const Vector &p_argument_names = Vector() -#endif - ) { -#ifdef DEBUG_ENABLED - ERR_FAIL_COND_MSG(p_argument_names.size() != sizeof...(P), "Wrong argument name count supplied for method: " + Variant::get_type_name(GetTypeInfo::VARIANT_TYPE) + "::" + String(p_name)); - ERR_FAIL_COND_MSG(type_internal_methods[GetTypeInfo::VARIANT_TYPE].has(p_name), " Method already registered: " + Variant::get_type_name(GetTypeInfo::VARIANT_TYPE) + "::" + String(p_name)); -#endif +template +static _FORCE_INLINE_ bool vc_has_return_type(void (T::*method)(P...)) { + return false; +} -#ifdef DEBUG_ENABLED - Variant::InternalMethod *m = memnew((InternalMethodR)(p_method, p_default_args, p_argument_names, p_name, GetTypeInfo::VARIANT_TYPE)); -#else - Variant::InternalMethod *m = memnew((InternalMethodR)(p_method, p_default_args)); -#endif - type_internal_methods[GetTypeInfo::VARIANT_TYPE].insert(p_name, m); - type_internal_method_names[GetTypeInfo::VARIANT_TYPE].push_back(p_name); - } +template +static _FORCE_INLINE_ bool vc_has_return_type(void (T::*method)(P...) const) { + return false; +} -#ifdef DEBUG_ENABLED -#define bind_method(m_type, m_method, m_arg_names, m_default_args) _VariantCall::_bind_method(#m_method, &m_type ::m_method, m_default_args, m_arg_names) -#else -#define bind_method(m_type, m_method, m_arg_names, m_default_args) _VariantCall::_bind_method(#m_method, &m_type ::m_method, m_default_args) -#endif +template +static _FORCE_INLINE_ bool vc_is_const(R (T::*method)(P...)) { + return false; +} +template +static _FORCE_INLINE_ bool vc_is_const(R (T::*method)(P...) const) { + return true; +} -#ifdef DEBUG_ENABLED -#define bind_methodv(m_name, m_method, m_arg_names, m_default_args) _VariantCall::_bind_method(#m_name, m_method, m_default_args, m_arg_names) -#else -#define bind_methodv(m_name, m_method, m_arg_names, m_default_args) _VariantCall::_bind_method(#m_name, m_method, m_default_args) -#endif +template +static _FORCE_INLINE_ bool vc_is_const(void (T::*method)(P...)) { + return false; +} - template - static void _bind_function(const StringName &p_name, R (*p_method)(T *, P...), const Vector &p_default_args = Vector() -#ifdef DEBUG_ENABLED - , - const Vector &p_argument_names = Vector() -#endif - ) { -#ifdef DEBUG_ENABLED - ERR_FAIL_COND_MSG(p_argument_names.size() != sizeof...(P), "Wrong argument name count supplied for method: " + Variant::get_type_name(GetTypeInfo::VARIANT_TYPE) + "::" + String(p_name)); - ERR_FAIL_COND_MSG(type_internal_methods[GetTypeInfo::VARIANT_TYPE].has(p_name), " Method already registered: " + Variant::get_type_name(GetTypeInfo::VARIANT_TYPE) + "::" + String(p_name)); -#endif +template +static _FORCE_INLINE_ bool vc_is_const(void (T::*method)(P...) const) { + return true; +} -#ifdef DEBUG_ENABLED - Variant::InternalMethod *m = memnew((InternalMethodRS)(p_method, p_default_args, p_argument_names, p_name, GetTypeInfo::VARIANT_TYPE)); -#else - Variant::InternalMethod *m = memnew((InternalMethodRS)(p_method, p_default_args)); -#endif +template +static _FORCE_INLINE_ Variant::Type vc_get_base_type(R (T::*method)(P...)) { + return GetTypeInfo::VARIANT_TYPE; +} +template +static _FORCE_INLINE_ Variant::Type vc_get_base_type(R (T::*method)(P...) const) { + return GetTypeInfo::VARIANT_TYPE; +} - type_internal_methods[GetTypeInfo::VARIANT_TYPE].insert(p_name, m); - type_internal_method_names[GetTypeInfo::VARIANT_TYPE].push_back(p_name); - } +template +static _FORCE_INLINE_ Variant::Type vc_get_base_type(void (T::*method)(P...)) { + return GetTypeInfo::VARIANT_TYPE; +} -#ifdef DEBUG_ENABLED -#define bind_function(m_name, m_method, m_arg_names, m_default_args) _VariantCall::_bind_function(m_name, m_method, m_default_args, m_arg_names) -#else -#define bind_function(m_name, m_method, m_arg_names, m_default_args) _VariantCall::_bind_function(m_name, m_method, m_default_args) -#endif +template +static _FORCE_INLINE_ Variant::Type vc_get_base_type(void (T::*method)(P...) const) { + return GetTypeInfo::VARIANT_TYPE; +} - static void _bind_custom(Variant::Type p_type, const StringName &p_name, InternalMethodVC::MethodVC p_method, uint32_t p_flags, const Vector &p_argument_types, const Variant::Type &p_return_type -#ifdef DEBUG_ENABLED - , - const Vector &p_argument_names = Vector() -#endif - ) { +#define METHOD_CLASS(m_class, m_method_name, m_method_ptr) \ + struct Method_##m_class##_##m_method_name { \ + static void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector &p_defvals, Callable::CallError &r_error) { \ + vc_method_call(m_method_ptr, base, p_args, p_argcount, r_ret, p_defvals, r_error); \ + } \ + static void validated_call(Variant *base, const Variant **p_args, int p_argcount, Variant *r_ret) { \ + TypeAdjust::adjust(r_ret); \ + vc_validated_call(m_method_ptr, base, p_args, r_ret); \ + } \ + static void ptrcall(void *p_base, const void **p_args, void *r_ret, int p_argcount) { \ + vc_ptrcall(m_method_ptr, p_base, p_args, r_ret); \ + } \ + static int get_argument_count() { \ + return vc_get_argument_count(m_method_ptr); \ + } \ + static Variant::Type get_argument_type(int p_arg) { \ + return vc_get_argument_type(m_method_ptr, p_arg); \ + } \ + static Variant::Type get_return_type() { \ + return vc_get_return_type(m_method_ptr); \ + } \ + static bool has_return_type() { \ + return vc_has_return_type(m_method_ptr); \ + } \ + static bool is_const() { \ + return vc_is_const(m_method_ptr); \ + } \ + static bool is_vararg() { \ + return false; \ + } \ + static Variant::Type get_base_type() { \ + return vc_get_base_type(m_method_ptr); \ + } \ + static StringName get_name() { \ + return #m_method_name; \ + } \ + }; -#ifdef DEBUG_ENABLED - Variant::InternalMethod *m = memnew(InternalMethodVC(p_method, p_flags, p_argument_types, p_return_type, p_argument_names, p_name, p_type)); -#else - Variant::InternalMethod *m = memnew(InternalMethodVC(p_method, p_flags, p_argument_types, p_return_type)); -#endif +template +static _FORCE_INLINE_ void vc_ptrcall(R (*method)(T *, P...), void *p_base, const void **p_args, void *r_ret) { + call_with_ptr_args_static_retc(reinterpret_cast(p_base), method, p_args, r_ret); +} - type_internal_methods[p_type].insert(p_name, m); - type_internal_method_names[p_type].push_back(p_name); - } +#define FUNCTION_CLASS(m_class, m_method_name, m_method_ptr) \ + struct Method_##m_class##_##m_method_name { \ + static void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector &p_defvals, Callable::CallError &r_error) { \ + call_with_variant_args_retc_static_helper_dv(VariantGetInternalPtr::get_ptr(base), m_method_ptr, p_args, p_argcount, r_ret, p_defvals, r_error); \ + } \ + static void validated_call(Variant *base, const Variant **p_args, int p_argcount, Variant *r_ret) { \ + TypeAdjust::adjust(r_ret); \ + call_with_validated_variant_args_static_retc(base, m_method_ptr, p_args, r_ret); \ + } \ + static void ptrcall(void *p_base, const void **p_args, void *r_ret, int p_argcount) { \ + vc_ptrcall(m_method_ptr, p_base, p_args, r_ret); \ + } \ + static int get_argument_count() { \ + return vc_get_argument_count(m_method_ptr); \ + } \ + static Variant::Type get_argument_type(int p_arg) { \ + return vc_get_argument_type(m_method_ptr, p_arg); \ + } \ + static Variant::Type get_return_type() { \ + return vc_get_return_type(m_method_ptr); \ + } \ + static bool has_return_type() { \ + return true; \ + } \ + static bool is_const() { \ + return true; \ + } \ + static bool is_vararg() { \ + return false; \ + } \ + static Variant::Type get_base_type() { \ + return GetTypeInfo::VARIANT_TYPE; \ + } \ + static StringName get_name() { \ + return #m_method_name; \ + } \ + }; -#ifdef DEBUG_ENABLED -#define bind_custom(m_type, m_name, m_method, m_flags, m_arg_types, m_ret_type, m_arg_names) _VariantCall::_bind_custom(m_type, m_name, m_method, m_flags, m_arg_types, m_ret_type, m_arg_names) -#else -#define bind_custom(m_type, m_name, m_method, m_flags, m_arg_types, m_ret_type, m_arg_names) _VariantCall::_bind_custom(m_type, m_name, m_method, m_flags, m_arg_types, m_ret_type) -#endif +#define VARARG_CLASS(m_class, m_method_name, m_method_ptr, m_has_return, m_return_type) \ + struct Method_##m_class##_##m_method_name { \ + static void call(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector &p_defvals, Callable::CallError &r_error) { \ + m_method_ptr(base, p_args, p_argcount, r_ret, r_error); \ + } \ + static void validated_call(Variant *base, const Variant **p_args, int p_argcount, Variant *r_ret) { \ + Callable::CallError ce; \ + m_method_ptr(base, p_args, p_argcount, *r_ret, ce); \ + } \ + static void ptrcall(void *p_base, const void **p_args, void *r_ret, int p_argcount) { \ + LocalVector vars; \ + vars.resize(p_argcount); \ + LocalVector vars_ptrs; \ + vars_ptrs.resize(p_argcount); \ + for (int i = 0; i < p_argcount; i++) { \ + vars[i] = PtrToArg::convert(p_args[i]); \ + vars_ptrs[i] = &vars[i]; \ + } \ + Variant base = PtrToArg::convert(p_base); \ + Variant ret; \ + Callable::CallError ce; \ + m_method_ptr(&base, (const Variant **)&vars_ptrs[0], p_argcount, ret, ce); \ + if (m_has_return) { \ + m_return_type r = ret; \ + PtrToArg::encode(ret, r_ret); \ + } \ + } \ + static int get_argument_count() { \ + return 0; \ + } \ + static Variant::Type get_argument_type(int p_arg) { \ + return Variant::NIL; \ + } \ + static Variant::Type get_return_type() { \ + return GetTypeInfo::VARIANT_TYPE; \ + } \ + static bool has_return_type() { \ + return m_has_return; \ + } \ + static bool is_const() { \ + return true; \ + } \ + static bool is_vararg() { \ + return true; \ + } \ + static Variant::Type get_base_type() { \ + return GetTypeInfo::VARIANT_TYPE; \ + } \ + static StringName get_name() { \ + return #m_method_name; \ + } \ + }; +struct _VariantCall { static String func_PackedByteArray_get_string_from_ascii(PackedByteArray *p_instance) { String s; if (p_instance->size() > 0) { @@ -703,18 +535,61 @@ struct _VariantCall { }; _VariantCall::ConstantData *_VariantCall::constant_data = nullptr; -_VariantCall::MethodMap *_VariantCall::type_internal_methods = nullptr; -List *_VariantCall::type_internal_method_names = nullptr; -Variant Variant::call(const StringName &p_method, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { - Variant ret; - call_ptr(p_method, p_args, p_argcount, &ret, r_error); - return ret; -} +struct VariantBuiltInMethodInfo { + void (*call)(Variant *base, const Variant **p_args, int p_argcount, Variant &r_ret, const Vector &p_defvals, Callable::CallError &r_error); + Variant::ValidatedBuiltInMethod validated_call; + Variant::PTRBuiltInMethod ptrcall; -void Variant::call_ptr(const StringName &p_method, const Variant **p_args, int p_argcount, Variant *r_ret, Callable::CallError &r_error) { - Variant ret; + Vector default_arguments; + Vector argument_names; + bool is_const; + bool has_return_type; + bool is_vararg; + Variant::Type return_type; + int argument_count; + Variant::Type (*get_argument_type)(int p_arg); +}; + +typedef OAHashMap BuiltinMethodMap; +static BuiltinMethodMap *builtin_method_info; +static List *builtin_method_names; + +template +static void register_builtin_method(const Vector &p_argnames, const Vector &p_def_args) { + StringName name = T::get_name(); + + ERR_FAIL_COND(builtin_method_info[T::get_base_type()].has(name)); + + VariantBuiltInMethodInfo imi; + + imi.call = T::call; + imi.validated_call = T::validated_call; + if (T::is_vararg()) { + imi.ptrcall = nullptr; + } else { + imi.ptrcall = T::ptrcall; + } + + imi.default_arguments = p_def_args; + imi.argument_names = p_argnames; + + imi.is_const = T::is_const(); + imi.is_vararg = T::is_vararg(); + imi.has_return_type = T::has_return_type(); + imi.return_type = T::get_return_type(); + imi.argument_count = T::get_argument_count(); + imi.get_argument_type = T::get_argument_type; +#ifdef DEBUG_METHODS_ENABLED + ERR_FAIL_COND(!imi.is_vararg && imi.argument_count != imi.argument_names.size()); +#endif + + builtin_method_info[T::get_base_type()].insert(name, imi); + builtin_method_names[T::get_base_type()].push_back(name); +} + +void Variant::call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error) { if (type == Variant::OBJECT) { //call object Object *obj = _get_obj().obj; @@ -729,31 +604,24 @@ void Variant::call_ptr(const StringName &p_method, const Variant **p_args, int p } #endif - ret = _get_obj().obj->call(p_method, p_args, p_argcount, r_error); + r_ret = _get_obj().obj->call(p_method, p_args, p_argcount, r_error); //else if (type==Variant::METHOD) { } else { r_error.error = Callable::CallError::CALL_OK; - Variant::InternalMethod **m = _VariantCall::type_internal_methods[type].lookup_ptr(p_method); + const VariantBuiltInMethodInfo *imf = builtin_method_info[type].lookup_ptr(p_method); - if (m) { - (*m)->call((Variant *)this, p_args, p_argcount, ret, r_error); - } else { - //ok fail because not found + if (!imf) { r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; return; } - } - if (r_error.error == Callable::CallError::CALL_OK && r_ret) { - *r_ret = ret; + imf->call(this, p_args, p_argcount, r_ret, imf->default_arguments, r_error); } } -#define VCALL(m_type, m_method) _VariantCall::_call_##m_type##_##m_method - bool Variant::has_method(const StringName &p_method) const { if (type == OBJECT) { Object *obj = get_validated_object(); @@ -764,97 +632,143 @@ bool Variant::has_method(const StringName &p_method) const { return obj->has_method(p_method); } - return _VariantCall::type_internal_methods[type].has(p_method); + return builtin_method_info[type].has(p_method); } -Vector Variant::get_method_argument_types(Variant::Type p_type, const StringName &p_method) { - Vector types; +bool Variant::has_builtin_method(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, false); + return builtin_method_info[p_type].has(p_method); +} - Variant::InternalMethod **m = _VariantCall::type_internal_methods[p_type].lookup_ptr(p_method); - if (*m) { - types.resize((*m)->get_argument_count()); - for (int i = 0; i < (*m)->get_argument_count(); i++) { - types.write[i] = (*m)->get_argument_type(i); - } - } +Variant::ValidatedBuiltInMethod Variant::get_validated_builtin_method(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, nullptr); + return method->validated_call; +} - return types; +Variant::PTRBuiltInMethod Variant::get_ptr_builtin_method(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, nullptr); + return method->ptrcall; } -bool Variant::is_method_const(Variant::Type p_type, const StringName &p_method) { - Variant::InternalMethod **m = _VariantCall::type_internal_methods[p_type].lookup_ptr(p_method); - if (*m) { - return (*m)->get_flags() & Variant::InternalMethod::FLAG_IS_CONST; - } - return false; +int Variant::get_builtin_method_argument_count(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, 0); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, 0); + return method->argument_count; } -Vector Variant::get_method_argument_names(Variant::Type p_type, const StringName &p_method) { - Vector argnames; +Variant::Type Variant::get_builtin_method_argument_type(Variant::Type p_type, const StringName &p_method, int p_argument) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, Variant::NIL); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, Variant::NIL); + ERR_FAIL_INDEX_V(p_argument, method->argument_count, Variant::NIL); + return method->get_argument_type(p_argument); +} -#ifdef DEBUG_ENABLED - Variant::InternalMethod **m = _VariantCall::type_internal_methods[p_type].lookup_ptr(p_method); - if (*m) { - argnames.resize((*m)->get_argument_count()); - for (int i = 0; i < (*m)->get_argument_count(); i++) { - argnames.write[i] = (*m)->get_argument_name(i); - } - } +String Variant::get_builtin_method_argument_name(Variant::Type p_type, const StringName &p_method, int p_argument) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, String()); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, String()); +#ifdef DEBUG_METHODS_ENABLED + ERR_FAIL_INDEX_V(p_argument, method->argument_count, String()); + return method->argument_names[p_argument]; +#else + return "arg" + itos(p_argument + 1); #endif - return argnames; } -Variant::Type Variant::get_method_return_type(Variant::Type p_type, const StringName &p_method, bool *r_has_return) { - Variant::Type rt = Variant::NIL; - Variant::InternalMethod **m = _VariantCall::type_internal_methods[p_type].lookup_ptr(p_method); - if (*m) { - rt = (*m)->get_return_type(); - if (r_has_return) { - *r_has_return = ((*m)->get_flags() & Variant::InternalMethod::FLAG_RETURNS_VARIANT) || rt != Variant::NIL; - } - } - return rt; +Vector Variant::get_builtin_method_default_arguments(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, Vector()); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, Vector()); + return method->default_arguments; } -Vector Variant::get_method_default_arguments(Variant::Type p_type, const StringName &p_method) { - Variant::InternalMethod **m = _VariantCall::type_internal_methods[p_type].lookup_ptr(p_method); - if (*m) { - return (*m)->get_default_arguments(); +bool Variant::has_builtin_method_return_value(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, false); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, false); + return method->has_return_type; +} + +void Variant::get_builtin_method_list(Variant::Type p_type, List *p_list) { + ERR_FAIL_INDEX(p_type, Variant::VARIANT_MAX); + for (List::Element *E = builtin_method_names[p_type].front(); E; E = E->next()) { + p_list->push_back(E->get()); } - return Vector(); +} + +Variant::Type Variant::get_builtin_method_return_type(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, Variant::NIL); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, Variant::NIL); + return method->return_type; +} + +bool Variant::is_builtin_method_const(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, false); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, false); + return method->is_const; +} + +bool Variant::is_builtin_method_vararg(Variant::Type p_type, const StringName &p_method) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, false); + const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); + ERR_FAIL_COND_V(!method, false); + return method->is_vararg; } void Variant::get_method_list(List *p_list) const { - for (List::Element *E = _VariantCall::type_internal_method_names[type].front(); E; E = E->next()) { - Variant::InternalMethod **m = _VariantCall::type_internal_methods[type].lookup_ptr(E->get()); - ERR_CONTINUE(!*m); - - MethodInfo mi; - mi.name = E->get(); - mi.return_val.type = (*m)->get_return_type(); - if ((*m)->get_flags() & Variant::InternalMethod::FLAG_RETURNS_VARIANT) { - mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT; - } - if ((*m)->get_flags() & Variant::InternalMethod::FLAG_IS_CONST) { - mi.flags |= METHOD_FLAG_CONST; - } - if ((*m)->get_flags() & Variant::InternalMethod::FLAG_VARARGS) { - mi.flags |= METHOD_FLAG_VARARG; + if (type == OBJECT) { + Object *obj = get_validated_object(); + if (obj) { + obj->get_method_list(p_list); } + } else { + for (List::Element *E = builtin_method_names[type].front(); E; E = E->next()) { + const VariantBuiltInMethodInfo *method = builtin_method_info[type].lookup_ptr(E->get()); + ERR_CONTINUE(!method); + + MethodInfo mi; + mi.name = E->get(); + + //return type + if (method->has_return_type) { + mi.return_val.type = method->return_type; + if (mi.return_val.type == Variant::NIL) { + mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT; + } + } - for (int i = 0; i < (*m)->get_argument_count(); i++) { - PropertyInfo arg; -#ifdef DEBUG_ENABLED - arg.name = (*m)->get_argument_name(i); + if (method->is_const) { + mi.flags |= METHOD_FLAG_CONST; + } + if (method->is_vararg) { + mi.flags |= METHOD_FLAG_VARARG; + } + + for (int i = 0; i < method->argument_count; i++) { + PropertyInfo pi; +#ifdef DEBUG_METHODS_ENABLED + pi.name = method->argument_names[i]; #else - arg.name = "arg" + itos(i + 1); + pi.name = "arg" + itos(i + 1); #endif - arg.type = (*m)->get_argument_type(i); - mi.arguments.push_back(arg); - } + pi.type = method->get_argument_type(i); + if (pi.type == Variant::NIL) { + pi.usage |= PROPERTY_USAGE_NIL_IS_VARIANT; + } + mi.arguments.push_back(pi); + } - mi.default_arguments = (*m)->get_default_arguments(); - p_list->push_back(mi); + mi.default_arguments = method->default_arguments; + p_list->push_back(mi); + } } } @@ -915,20 +829,44 @@ Variant Variant::get_constant_value(Variant::Type p_type, const StringName &p_va return E->get(); } -Variant::InternalMethod *Variant::get_internal_method(Type p_type, const StringName &p_method_name) { - ERR_FAIL_INDEX_V(p_type, VARIANT_MAX, nullptr); +#ifdef DEBUG_METHODS_ENABLED +#define bind_method(m_type, m_method, m_arg_names, m_default_args) \ + METHOD_CLASS(m_type, m_method, &m_type::m_method); \ + register_builtin_method(m_arg_names, m_default_args); +#else +#define bind_method(m_type, m_method, m_arg_names, m_default_args) \ + METHOD_CLASS(m_type, m_method, &m_type ::m_method); \ + register_builtin_method(sarray(), m_default_args); +#endif - Variant::InternalMethod **m = _VariantCall::type_internal_methods[p_type].lookup_ptr(p_method_name); - if (*m) { - return *m; - } - return nullptr; -} +#ifdef DEBUG_METHODS_ENABLED +#define bind_methodv(m_type, m_name, m_method, m_arg_names, m_default_args) \ + METHOD_CLASS(m_type, m_name, m_method); \ + register_builtin_method(m_arg_names, m_default_args); +#else +#define bind_methodv(m_type, m_name, m_method, m_arg_names, m_default_args) \ + METHOD_CLASS(m_type, m_name, m_method); \ + register_builtin_method(sarray(), m_default_args); +#endif -void Variant::_register_variant_methods() { - _VariantCall::type_internal_methods = memnew_arr(_VariantCall::MethodMap, Variant::VARIANT_MAX); - _VariantCall::type_internal_method_names = memnew_arr(List, Variant::VARIANT_MAX); +#ifdef DEBUG_METHODS_ENABLED +#define bind_function(m_type, m_name, m_method, m_arg_names, m_default_args) \ + FUNCTION_CLASS(m_type, m_name, m_method); \ + register_builtin_method(m_arg_names, m_default_args); +#else +#define bind_function(m_type, m_name, m_method, m_arg_names, m_default_args) \ + FUNCTION_CLASS(m_type, m_name, m_method); \ + register_builtin_method(sarray(), m_default_args); +#endif + +#define bind_custom(m_type, m_name, m_method, m_has_return, m_ret_type) \ + VARARG_CLASS(m_type, m_name, m_method, m_has_return, m_ret_type) \ + register_builtin_method(sarray(), Vector()); + +static void _register_variant_builtin_methods() { _VariantCall::constant_data = memnew_arr(_VariantCall::ConstantData, Variant::VARIANT_MAX); + builtin_method_info = memnew_arr(BuiltinMethodMap, Variant::VARIANT_MAX); + builtin_method_names = memnew_arr(List, Variant::VARIANT_MAX); /* String */ @@ -937,7 +875,7 @@ void Variant::_register_variant_methods() { bind_method(String, naturalnocasecmp_to, sarray("to"), varray()); bind_method(String, length, sarray(), varray()); bind_method(String, substr, sarray("from", "len"), varray(-1)); - bind_methodv(find, static_cast(&String::find), sarray("what", "from"), varray(0)); + bind_methodv(String, find, static_cast(&String::find), sarray("what", "from"), varray(0)); bind_method(String, count, sarray("what", "from", "to"), varray(0, 0)); bind_method(String, countn, sarray("what", "from", "to"), varray(0, 0)); bind_method(String, findn, sarray("what", "from"), varray(0)); @@ -945,7 +883,7 @@ void Variant::_register_variant_methods() { bind_method(String, rfindn, sarray("what", "from"), varray(-1)); bind_method(String, match, sarray("expr"), varray()); bind_method(String, matchn, sarray("expr"), varray()); - bind_methodv(begins_with, static_cast(&String::begins_with), sarray("text"), varray()); + bind_methodv(String, begins_with, static_cast(&String::begins_with), sarray("text"), varray()); bind_method(String, ends_with, sarray("text"), varray()); bind_method(String, is_subsequence_of, sarray("text"), varray()); bind_method(String, is_subsequence_ofi, sarray("text"), varray()); @@ -953,7 +891,7 @@ void Variant::_register_variant_methods() { bind_method(String, similarity, sarray("text"), varray()); bind_method(String, format, sarray("values", "placeholder"), varray("{_}")); - bind_methodv(replace, static_cast(&String::replace), sarray("what", "forwhat"), varray()); + bind_methodv(String, replace, static_cast(&String::replace), sarray("what", "forwhat"), varray()); bind_method(String, replacen, sarray("what", "forwhat"), varray()); bind_method(String, repeat, sarray("count"), varray()); bind_method(String, insert, sarray("position", "what"), varray()); @@ -1084,7 +1022,7 @@ void Variant::_register_variant_methods() { bind_method(Rect2, merge, sarray("b"), varray()); bind_method(Rect2, expand, sarray("to"), varray()); bind_method(Rect2, grow, sarray("by"), varray()); - bind_methodv(grow_margin, &Rect2::grow_margin_bind, sarray("margin", "by"), varray()); + bind_methodv(Rect2, grow_margin, &Rect2::grow_margin_bind, sarray("margin", "by"), varray()); bind_method(Rect2, grow_individual, sarray("left", "top", "right", "bottom"), varray()); bind_method(Rect2, abs, sarray(), varray()); @@ -1099,7 +1037,7 @@ void Variant::_register_variant_methods() { bind_method(Rect2i, merge, sarray("b"), varray()); bind_method(Rect2i, expand, sarray("to"), varray()); bind_method(Rect2i, grow, sarray("by"), varray()); - bind_methodv(grow_margin, &Rect2i::grow_margin_bind, sarray("margin", "by"), varray()); + bind_methodv(Rect2i, grow_margin, &Rect2i::grow_margin_bind, sarray("margin", "by"), varray()); bind_method(Rect2i, grow_individual, sarray("left", "top", "right", "bottom"), varray()); bind_method(Rect2i, abs, sarray(), varray()); @@ -1155,9 +1093,9 @@ void Variant::_register_variant_methods() { bind_method(Plane, distance_to, sarray("point"), varray()); bind_method(Plane, has_point, sarray("point", "epsilon"), varray(CMP_EPSILON)); bind_method(Plane, project, sarray("point"), varray()); - bind_methodv(intersect_3, &Plane::intersect_3_bind, sarray("b", "c"), varray()); - bind_methodv(intersects_ray, &Plane::intersects_ray_bind, sarray("from", "dir"), varray()); - bind_methodv(intersects_segment, &Plane::intersects_segment_bind, sarray("from", "to"), varray()); + bind_methodv(Plane, intersect_3, &Plane::intersect_3_bind, sarray("b", "c"), varray()); + bind_methodv(Plane, intersects_ray, &Plane::intersects_ray_bind, sarray("from", "dir"), varray()); + bind_methodv(Plane, intersects_segment, &Plane::intersects_segment_bind, sarray("from", "to"), varray()); /* Quat */ @@ -1199,7 +1137,7 @@ void Variant::_register_variant_methods() { /* RID */ - bind_method(::RID, get_id, sarray(), varray()); + bind_method(RID, get_id, sarray(), varray()); /* NodePath */ @@ -1223,9 +1161,9 @@ void Variant::_register_variant_methods() { bind_method(Callable, hash, sarray(), varray()); bind_method(Callable, unbind, sarray("argcount"), varray()); - bind_custom(Variant::CALLABLE, "call", _VariantCall::func_Callable_call, Variant::InternalMethod::FLAG_VARARGS | Variant::InternalMethod::FLAG_RETURNS_VARIANT, Vector(), Variant::NIL, sarray()); - bind_custom(Variant::CALLABLE, "call_deferred", _VariantCall::func_Callable_call_deferred, Variant::InternalMethod::FLAG_VARARGS, Vector(), Variant::NIL, sarray()); - bind_custom(Variant::CALLABLE, "bind", _VariantCall::func_Callable_bind, Variant::InternalMethod::FLAG_VARARGS, Vector(), Variant::CALLABLE, sarray()); + bind_custom(Callable, call, _VariantCall::func_Callable_call, true, Variant); + bind_custom(Callable, call_deferred, _VariantCall::func_Callable_call_deferred, false, Variant); + bind_custom(Callable, bind, _VariantCall::func_Callable_bind, true, Callable); /* Signal */ @@ -1239,7 +1177,7 @@ void Variant::_register_variant_methods() { bind_method(Signal, is_connected, sarray("callable"), varray()); bind_method(Signal, get_connections, sarray(), varray()); - bind_custom(Variant::SIGNAL, "emit", _VariantCall::func_Signal_emit, Variant::InternalMethod::FLAG_VARARGS, Vector(), Variant::NIL, sarray()); + bind_custom(Signal, emit, _VariantCall::func_Signal_emit, false, Variant); /* Transform2D */ @@ -1263,7 +1201,7 @@ void Variant::_register_variant_methods() { bind_method(Basis, transposed, sarray(), varray()); bind_method(Basis, orthonormalized, sarray(), varray()); bind_method(Basis, determinant, sarray(), varray()); - bind_methodv(rotated, static_cast(&Basis::rotated), sarray("axis", "phi"), varray()); + bind_methodv(Basis, rotated, static_cast(&Basis::rotated), sarray("axis", "phi"), varray()); bind_method(Basis, scaled, sarray("scale"), varray()); bind_method(Basis, get_scale, sarray(), varray()); bind_method(Basis, get_euler, sarray(), varray()); @@ -1277,29 +1215,29 @@ void Variant::_register_variant_methods() { /* AABB */ - bind_method(::AABB, abs, sarray(), varray()); - bind_method(::AABB, get_area, sarray(), varray()); - bind_method(::AABB, has_no_area, sarray(), varray()); - bind_method(::AABB, has_no_surface, sarray(), varray()); - bind_method(::AABB, has_point, sarray("point"), varray()); - bind_method(::AABB, is_equal_approx, sarray("aabb"), varray()); - bind_method(::AABB, intersects, sarray("with"), varray()); - bind_method(::AABB, encloses, sarray("with"), varray()); - bind_method(::AABB, intersects_plane, sarray("plane"), varray()); - bind_method(::AABB, intersection, sarray("with"), varray()); - bind_method(::AABB, merge, sarray("with"), varray()); - bind_method(::AABB, expand, sarray("to_point"), varray()); - bind_method(::AABB, grow, sarray("by"), varray()); - bind_method(::AABB, get_support, sarray("dir"), varray()); - bind_method(::AABB, get_longest_axis, sarray(), varray()); - bind_method(::AABB, get_longest_axis_index, sarray(), varray()); - bind_method(::AABB, get_longest_axis_size, sarray(), varray()); - bind_method(::AABB, get_shortest_axis, sarray(), varray()); - bind_method(::AABB, get_shortest_axis_index, sarray(), varray()); - bind_method(::AABB, get_shortest_axis_size, sarray(), varray()); - bind_method(::AABB, get_endpoint, sarray("idx"), varray()); - bind_methodv(intersects_segment, &AABB::intersects_segment_bind, sarray("from", "to"), varray()); - bind_methodv(intersects_ray, &AABB::intersects_ray_bind, sarray("from", "dir"), varray()); + bind_method(AABB, abs, sarray(), varray()); + bind_method(AABB, get_area, sarray(), varray()); + bind_method(AABB, has_no_area, sarray(), varray()); + bind_method(AABB, has_no_surface, sarray(), varray()); + bind_method(AABB, has_point, sarray("point"), varray()); + bind_method(AABB, is_equal_approx, sarray("aabb"), varray()); + bind_method(AABB, intersects, sarray("with"), varray()); + bind_method(AABB, encloses, sarray("with"), varray()); + bind_method(AABB, intersects_plane, sarray("plane"), varray()); + bind_method(AABB, intersection, sarray("with"), varray()); + bind_method(AABB, merge, sarray("with"), varray()); + bind_method(AABB, expand, sarray("to_point"), varray()); + bind_method(AABB, grow, sarray("by"), varray()); + bind_method(AABB, get_support, sarray("dir"), varray()); + bind_method(AABB, get_longest_axis, sarray(), varray()); + bind_method(AABB, get_longest_axis_index, sarray(), varray()); + bind_method(AABB, get_longest_axis_size, sarray(), varray()); + bind_method(AABB, get_shortest_axis, sarray(), varray()); + bind_method(AABB, get_shortest_axis_index, sarray(), varray()); + bind_method(AABB, get_shortest_axis_size, sarray(), varray()); + bind_method(AABB, get_endpoint, sarray("idx"), varray()); + bind_methodv(AABB, intersects_segment, &AABB::intersects_segment_bind, sarray("from", "to"), varray()); + bind_methodv(AABB, intersects_ray, &AABB::intersects_ray_bind, sarray("from", "dir"), varray()); /* Transform */ @@ -1376,14 +1314,14 @@ void Variant::_register_variant_methods() { bind_method(PackedByteArray, subarray, sarray("from", "to"), varray()); bind_method(PackedByteArray, sort, sarray(), varray()); - bind_function("get_string_from_ascii", _VariantCall::func_PackedByteArray_get_string_from_ascii, sarray(), varray()); - bind_function("get_string_from_utf8", _VariantCall::func_PackedByteArray_get_string_from_utf8, sarray(), varray()); - bind_function("get_string_from_utf16", _VariantCall::func_PackedByteArray_get_string_from_utf16, sarray(), varray()); - bind_function("get_string_from_utf32", _VariantCall::func_PackedByteArray_get_string_from_utf32, sarray(), varray()); - bind_function("hex_encode", _VariantCall::func_PackedByteArray_hex_encode, sarray(), varray()); - bind_function("compress", _VariantCall::func_PackedByteArray_compress, sarray("compression_mode"), varray(0)); - bind_function("decompress", _VariantCall::func_PackedByteArray_decompress, sarray("buffer_size", "compression_mode"), varray(0)); - bind_function("decompress_dynamic", _VariantCall::func_PackedByteArray_decompress_dynamic, sarray("max_output_size", "compression_mode"), varray(0)); + bind_function(PackedByteArray, get_string_from_ascii, _VariantCall::func_PackedByteArray_get_string_from_ascii, sarray(), varray()); + bind_function(PackedByteArray, get_string_from_utf8, _VariantCall::func_PackedByteArray_get_string_from_utf8, sarray(), varray()); + bind_function(PackedByteArray, get_string_from_utf16, _VariantCall::func_PackedByteArray_get_string_from_utf16, sarray(), varray()); + bind_function(PackedByteArray, get_string_from_utf32, _VariantCall::func_PackedByteArray_get_string_from_utf32, sarray(), varray()); + bind_function(PackedByteArray, hex_encode, _VariantCall::func_PackedByteArray_hex_encode, sarray(), varray()); + bind_function(PackedByteArray, compress, _VariantCall::func_PackedByteArray_compress, sarray("compression_mode"), varray(0)); + bind_function(PackedByteArray, decompress, _VariantCall::func_PackedByteArray_decompress, sarray("buffer_size", "compression_mode"), varray(0)); + bind_function(PackedByteArray, decompress_dynamic, _VariantCall::func_PackedByteArray_decompress_dynamic, sarray("max_output_size", "compression_mode"), varray(0)); /* Int32 Array */ @@ -1605,18 +1543,13 @@ void Variant::_register_variant_methods() { _VariantCall::add_variant_constant(Variant::QUAT, "IDENTITY", Quat(0, 0, 0, 1)); } +void Variant::_register_variant_methods() { + _register_variant_builtin_methods(); //needs to be out due to namespace +} + void Variant::_unregister_variant_methods() { //clear methods - for (int i = 0; i < Variant::VARIANT_MAX; i++) { - for (List::Element *E = _VariantCall::type_internal_method_names[i].front(); E; E = E->next()) { - Variant::InternalMethod **m = _VariantCall::type_internal_methods[i].lookup_ptr(E->get()); - if (*m) { - memdelete(*m); - } - } - } - - memdelete_arr(_VariantCall::type_internal_methods); - memdelete_arr(_VariantCall::type_internal_method_names); + memdelete_arr(builtin_method_names); + memdelete_arr(builtin_method_info); memdelete_arr(_VariantCall::constant_data); } diff --git a/core/variant/variant_utility.cpp b/core/variant/variant_utility.cpp new file mode 100644 index 0000000000..91a1b0262c --- /dev/null +++ b/core/variant/variant_utility.cpp @@ -0,0 +1,1392 @@ +/*************************************************************************/ +/* variant_utility.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "variant.h" + +#include "core/core_string_names.h" +#include "core/io/marshalls.h" +#include "core/object/reference.h" +#include "core/os/os.h" +#include "core/templates/oa_hash_map.h" +#include "core/variant/binder_common.h" +#include "core/variant/variant_parser.h" + +struct VariantUtilityFunctions { + // Math + static inline double sin(double arg) { + return Math::sin(arg); + } + static inline double cos(double arg) { + return Math::cos(arg); + } + static inline double tan(double arg) { + return Math::tan(arg); + } + + static inline double sinh(double arg) { + return Math::sinh(arg); + } + static inline double cosh(double arg) { + return Math::cosh(arg); + } + static inline double tanh(double arg) { + return Math::tanh(arg); + } + + static inline double asin(double arg) { + return Math::asin(arg); + } + static inline double acos(double arg) { + return Math::acos(arg); + } + static inline double atan(double arg) { + return Math::atan(arg); + } + + static inline double atan2(double y, double x) { + return Math::atan2(y, x); + } + + static inline double sqrt(double x) { + return Math::sqrt(x); + } + + static inline double fmod(double b, double r) { + return Math::fmod(b, r); + } + + static inline double fposmod(double b, double r) { + return Math::fposmod(b, r); + } + + static inline double floor(double x) { + return Math::floor(x); + } + + static inline double ceil(double x) { + return Math::ceil(x); + } + + static inline double round(double x) { + return Math::round(x); + } + + static inline Variant abs(const Variant &x, Callable::CallError &r_error) { + r_error.error = Callable::CallError::CALL_OK; + switch (x.get_type()) { + case Variant::INT: { + return ABS(VariantInternalAccessor::get(&x)); + } break; + case Variant::FLOAT: { + return Math::absd(VariantInternalAccessor::get(&x)); + } break; + case Variant::VECTOR2: { + return VariantInternalAccessor::get(&x).abs(); + } break; + case Variant::VECTOR2I: { + return VariantInternalAccessor::get(&x).abs(); + } break; + case Variant::VECTOR3: { + return VariantInternalAccessor::get(&x).abs(); + } break; + case Variant::VECTOR3I: { + return VariantInternalAccessor::get(&x).abs(); + } break; + default: { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; + return Variant(); + } + } + } + + static inline double absf(double x) { + return Math::absd(x); + } + + static inline int64_t absi(int64_t x) { + return ABS(x); + } + + static inline Variant sign(const Variant &x, Callable::CallError &r_error) { + r_error.error = Callable::CallError::CALL_OK; + switch (x.get_type()) { + case Variant::INT: { + return SGN(VariantInternalAccessor::get(&x)); + } break; + case Variant::FLOAT: { + return SGN(VariantInternalAccessor::get(&x)); + } break; + case Variant::VECTOR2: { + return VariantInternalAccessor::get(&x).sign(); + } break; + case Variant::VECTOR2I: { + return VariantInternalAccessor::get(&x).sign(); + } break; + case Variant::VECTOR3: { + return VariantInternalAccessor::get(&x).sign(); + } break; + case Variant::VECTOR3I: { + return VariantInternalAccessor::get(&x).sign(); + } break; + default: { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; + return Variant(); + } + } + } + + static inline double signf(double x) { + return SGN(x); + } + + static inline int64_t signi(int64_t x) { + return SGN(x); + } + + static inline double pow(double x, double y) { + return Math::pow(x, y); + } + static inline double log(double x) { + return Math::log(x); + } + + static inline double exp(double x) { + return Math::exp(x); + } + + static inline double is_nan(double x) { + return Math::is_nan(x); + } + + static inline double is_inf(double x) { + return Math::is_inf(x); + } + + static inline double is_equal_approx(double x, double y) { + return Math::is_equal_approx(x, y); + } + + static inline double is_zero_approx(double x) { + return Math::is_zero_approx(x); + } + + static inline double ease(float x, float c) { + return Math::ease(x, c); + } + + static inline int step_decimals(float step) { + return Math::step_decimals(step); + } + + static inline int range_step_decimals(float step) { + return Math::range_step_decimals(step); + } + + static inline double stepify(double value, double step) { + return Math::stepify(value, step); + } + + static inline double lerp(double from, double to, double weight) { + return Math::lerp(from, to, weight); + } + + static inline double lerp_angle(double from, double to, double weight) { + return Math::lerp_angle(from, to, weight); + } + + static inline double inverse_lerp(double from, double to, double weight) { + return Math::inverse_lerp(from, to, weight); + } + + static inline double range_lerp(double value, double istart, double istop, double ostart, double ostop) { + return Math::range_lerp(value, istart, istop, ostart, ostop); + } + + static inline double smoothstep(double from, double to, double val) { + return Math::smoothstep(from, to, val); + } + + static inline double move_toward(double from, double to, double delta) { + return Math::move_toward(from, to, delta); + } + + static inline double dectime(double value, double amount, double step) { + return Math::dectime(value, amount, step); + } + + static inline double deg2rad(double angle_deg) { + return Math::deg2rad(angle_deg); + } + + static inline double rad2deg(double angle_rad) { + return Math::rad2deg(angle_rad); + } + + static inline double linear2db(double linear) { + return Math::linear2db(linear); + } + + static inline double db2linear(double db) { + return Math::db2linear(db); + } + + static inline Vector2 polar2cartesian(double r, double th) { + return Vector2(r * Math::cos(th), r * Math::sin(th)); + } + + static inline Vector2 cartesian2polar(double x, double y) { + return Vector2(Math::sqrt(x * x + y * y), Math::atan2(y, x)); + } + + static inline int64_t wrapi(int64_t value, int64_t min, int64_t max) { + return Math::wrapi(value, min, max); + } + static inline double wrapf(double value, double min, double max) { + return Math::wrapf(value, min, max); + } + + static inline Variant max(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { + if (p_argcount < 2) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.expected = 2; + return Variant(); + } + Variant base = *p_args[0]; + Variant ret; + for (int i = 1; i < p_argcount; i++) { + bool valid; + Variant::evaluate(Variant::OP_GREATER, base, *p_args[i], ret, valid); + if (!valid) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.expected = base.get_type(); + r_error.argument = i; + return Variant(); + } + if (ret.booleanize()) { + base = *p_args[i]; + } + } + r_error.error = Callable::CallError::CALL_OK; + return base; + } + + static inline double maxf(double x, double y) { + return MAX(x, y); + } + + static inline int64_t maxi(int64_t x, int64_t y) { + return MAX(x, y); + } + + static inline Variant min(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { + if (p_argcount < 2) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.expected = 2; + return Variant(); + } + Variant base = *p_args[0]; + Variant ret; + for (int i = 1; i < p_argcount; i++) { + bool valid; + Variant::evaluate(Variant::OP_LESS, base, *p_args[i], ret, valid); + if (!valid) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.expected = base.get_type(); + r_error.argument = i; + return Variant(); + } + if (ret.booleanize()) { + base = *p_args[i]; + } + } + r_error.error = Callable::CallError::CALL_OK; + return base; + } + + static inline double minf(double x, double y) { + return MIN(x, y); + } + + static inline int64_t mini(int64_t x, int64_t y) { + return MIN(x, y); + } + + static inline Variant clamp(const Variant &x, const Variant &min, const Variant &max, Callable::CallError &r_error) { + Variant value = x; + + Variant ret; + + bool valid; + Variant::evaluate(Variant::OP_LESS, value, min, ret, valid); + if (!valid) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.expected = value.get_type(); + r_error.argument = 1; + return Variant(); + } + if (ret.booleanize()) { + value = min; + } + Variant::evaluate(Variant::OP_GREATER, value, max, ret, valid); + if (!valid) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.expected = value.get_type(); + r_error.argument = 2; + return Variant(); + } + if (ret.booleanize()) { + value = max; + } + + r_error.error = Callable::CallError::CALL_OK; + + return value; + } + + static inline double clampf(double x, double min, double max) { + return CLAMP(x, min, max); + } + + static inline int64_t clampi(int64_t x, int64_t min, int64_t max) { + return CLAMP(x, min, max); + } + + static inline int64_t nearest_po2(int64_t x) { + return nearest_power_of_2_templated(uint64_t(x)); + } + + // Random + + static inline void randomize() { + Math::randomize(); + } + + static inline int64_t randi() { + return Math::rand(); + } + + static inline double randf() { + return Math::randf(); + } + + static inline int64_t randi_range(int64_t from, int64_t to) { + return Math::random((int32_t)from, (int32_t)to); + } + + static inline double randf_range(double from, double to) { + return Math::random(from, to); + } + + static inline void seed(int64_t s) { + return Math::seed(s); + } + + static inline PackedInt64Array rand_from_seed(int64_t seed) { + uint64_t s = seed; + PackedInt64Array arr; + arr.resize(2); + arr.write[0] = Math::rand_from_seed(&s); + arr.write[1] = s; + return arr; + } + + // Utility + + static inline Variant weakref(const Variant &obj, Callable::CallError &r_error) { + if (obj.get_type() == Variant::OBJECT) { + r_error.error = Callable::CallError::CALL_OK; + if (obj.is_ref()) { + Ref wref = memnew(WeakRef); + REF r = obj; + if (r.is_valid()) { + wref->set_ref(r); + } + return wref; + } else { + Ref wref = memnew(WeakRef); + Object *o = obj.get_validated_object(); + if (o) { + wref->set_obj(o); + } + return wref; + } + } else if (obj.get_type() == Variant::NIL) { + r_error.error = Callable::CallError::CALL_OK; + Ref wref = memnew(WeakRef); + return wref; + } else { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; + r_error.argument = 0; + r_error.expected = Variant::OBJECT; + return Variant(); + } + } + + static inline int64_t _typeof(const Variant &obj) { + return obj.get_type(); + } + + static inline String str(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (p_arg_count < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 1; + return String(); + } + String str; + for (int i = 0; i < p_arg_count; i++) { + String os = p_args[i]->operator String(); + + if (i == 0) { + str = os; + } else { + str += os; + } + } + + r_error.error = Callable::CallError::CALL_OK; + + return str; + } + + static inline void print(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (p_arg_count < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 1; + } + String str; + for (int i = 0; i < p_arg_count; i++) { + String os = p_args[i]->operator String(); + + if (i == 0) { + str = os; + } else { + str += os; + } + } + + print_line(str); + r_error.error = Callable::CallError::CALL_OK; + } + + static inline void printerr(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (p_arg_count < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 1; + } + String str; + for (int i = 0; i < p_arg_count; i++) { + String os = p_args[i]->operator String(); + + if (i == 0) { + str = os; + } else { + str += os; + } + } + + print_error(str); + r_error.error = Callable::CallError::CALL_OK; + } + + static inline void printt(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (p_arg_count < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 1; + } + String str; + for (int i = 0; i < p_arg_count; i++) { + if (i) { + str += "\t"; + } + str += p_args[i]->operator String(); + } + + print_error(str); + r_error.error = Callable::CallError::CALL_OK; + } + + static inline void prints(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (p_arg_count < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 1; + } + String str; + for (int i = 0; i < p_arg_count; i++) { + if (i) { + str += " "; + } + str += p_args[i]->operator String(); + } + + print_error(str); + r_error.error = Callable::CallError::CALL_OK; + } + + static inline void printraw(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (p_arg_count < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 1; + } + String str; + for (int i = 0; i < p_arg_count; i++) { + String os = p_args[i]->operator String(); + + if (i == 0) { + str = os; + } else { + str += os; + } + } + + OS::get_singleton()->print("%s", str.utf8().get_data()); + r_error.error = Callable::CallError::CALL_OK; + } + + static inline void push_error(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (p_arg_count < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 1; + } + String str; + for (int i = 0; i < p_arg_count; i++) { + String os = p_args[i]->operator String(); + + if (i == 0) { + str = os; + } else { + str += os; + } + } + + ERR_PRINT(str); + r_error.error = Callable::CallError::CALL_OK; + } + + static inline void push_warning(const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { + if (p_arg_count < 1) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 1; + } + String str; + for (int i = 0; i < p_arg_count; i++) { + String os = p_args[i]->operator String(); + + if (i == 0) { + str = os; + } else { + str += os; + } + } + + WARN_PRINT(str); + r_error.error = Callable::CallError::CALL_OK; + } + + static inline String var2str(const Variant &p_var) { + String vars; + VariantWriter::write_to_string(p_var, vars); + return vars; + } + + static inline Variant str2var(const String &p_var) { + VariantParser::StreamString ss; + ss.s = p_var; + + String errs; + int line; + Variant ret; + (void)VariantParser::parse(&ss, ret, errs, line); + + return ret; + } + + static inline PackedByteArray var2bytes(const Variant &p_var) { + int len; + Error err = encode_variant(p_var, nullptr, len, false); + if (err != OK) { + return PackedByteArray(); + } + + PackedByteArray barr; + barr.resize(len); + { + uint8_t *w = barr.ptrw(); + err = encode_variant(p_var, w, len, false); + if (err != OK) { + return PackedByteArray(); + } + } + + return barr; + } + + static inline PackedByteArray var2bytes_with_objects(const Variant &p_var) { + int len; + Error err = encode_variant(p_var, nullptr, len, true); + if (err != OK) { + return PackedByteArray(); + } + + PackedByteArray barr; + barr.resize(len); + { + uint8_t *w = barr.ptrw(); + err = encode_variant(p_var, w, len, true); + if (err != OK) { + return PackedByteArray(); + } + } + + return barr; + } + + static inline Variant bytes2var(const PackedByteArray &p_arr) { + Variant ret; + { + const uint8_t *r = p_arr.ptr(); + Error err = decode_variant(ret, r, p_arr.size(), nullptr, false); + if (err != OK) { + return Variant(); + } + } + return ret; + } + + static inline Variant bytes2var_with_objects(const PackedByteArray &p_arr) { + Variant ret; + { + const uint8_t *r = p_arr.ptr(); + Error err = decode_variant(ret, r, p_arr.size(), nullptr, true); + if (err != OK) { + return Variant(); + } + } + return ret; + } + + static inline int64_t hash(const Variant &p_arr) { + return p_arr.hash(); + } + + static inline Variant instance_from_id(int64_t p_id) { + ObjectID id = ObjectID((uint64_t)p_id); + Variant ret = ObjectDB::get_instance(id); + return ret; + } + + static inline bool is_instance_id_valid(int64_t p_id) { + return ObjectDB::get_instance(ObjectID((uint64_t)p_id)) != nullptr; + } + + static inline bool is_instance_valid(const Variant &p_instance) { + if (p_instance.get_type() != Variant::OBJECT) { + return false; + } + return p_instance.get_validated_object() != nullptr; + } +}; + +#ifdef DEBUG_METHODS_ENABLED +#define VCALLR *ret = p_func(VariantCasterAndValidate

::cast(p_args, Is, r_error)...) +#define VCALL p_func(VariantCasterAndValidate

::cast(p_args, Is, r_error)...) +#else +#define VCALLR *ret = p_func(VariantCaster

::cast(*p_args[Is])...) +#define VCALL p_func(VariantCaster

::cast(*p_args[Is])...) +#endif + +template +static _FORCE_INLINE_ void call_helperpr(R (*p_func)(P...), Variant *ret, const Variant **p_args, Callable::CallError &r_error, IndexSequence) { + r_error.error = Callable::CallError::CALL_OK; + VCALLR; + (void)p_args; // avoid gcc warning + (void)r_error; +} + +template +static _FORCE_INLINE_ void validated_call_helperpr(R (*p_func)(P...), Variant *ret, const Variant **p_args, IndexSequence) { + *ret = p_func(VariantCaster

::cast(*p_args[Is])...); + (void)p_args; +} + +template +static _FORCE_INLINE_ void ptr_call_helperpr(R (*p_func)(P...), void *ret, const void **p_args, IndexSequence) { + PtrToArg::encode(p_func(PtrToArg

::convert(p_args[Is])...), ret); + (void)p_args; +} + +template +static _FORCE_INLINE_ void call_helperr(R (*p_func)(P...), Variant *ret, const Variant **p_args, Callable::CallError &r_error) { + call_helperpr(p_func, ret, p_args, r_error, BuildIndexSequence{}); +} + +template +static _FORCE_INLINE_ void validated_call_helperr(R (*p_func)(P...), Variant *ret, const Variant **p_args) { + validated_call_helperpr(p_func, ret, p_args, BuildIndexSequence{}); +} + +template +static _FORCE_INLINE_ void ptr_call_helperr(R (*p_func)(P...), void *ret, const void **p_args) { + ptr_call_helperpr(p_func, ret, p_args, BuildIndexSequence{}); +} + +template +static _FORCE_INLINE_ int get_arg_count_helperr(R (*p_func)(P...)) { + return sizeof...(P); +} + +template +static _FORCE_INLINE_ Variant::Type get_arg_type_helperr(R (*p_func)(P...), int p_arg) { + return call_get_argument_type(p_arg); +} + +template +static _FORCE_INLINE_ Variant::Type get_ret_type_helperr(R (*p_func)(P...)) { + return GetTypeInfo::VARIANT_TYPE; +} + +// WITHOUT RET + +template +static _FORCE_INLINE_ void call_helperp(void (*p_func)(P...), const Variant **p_args, Callable::CallError &r_error, IndexSequence) { + r_error.error = Callable::CallError::CALL_OK; + VCALL; + (void)p_args; + (void)r_error; +} + +template +static _FORCE_INLINE_ void validated_call_helperp(void (*p_func)(P...), const Variant **p_args, IndexSequence) { + p_func(VariantCaster

::cast(*p_args[Is])...); + (void)p_args; +} + +template +static _FORCE_INLINE_ void ptr_call_helperp(void (*p_func)(P...), const void **p_args, IndexSequence) { + p_func(PtrToArg

::convert(p_args[Is])...); + (void)p_args; +} + +template +static _FORCE_INLINE_ void call_helper(void (*p_func)(P...), const Variant **p_args, Callable::CallError &r_error) { + call_helperp(p_func, p_args, r_error, BuildIndexSequence{}); +} + +template +static _FORCE_INLINE_ void validated_call_helper(void (*p_func)(P...), const Variant **p_args) { + validated_call_helperp(p_func, p_args, BuildIndexSequence{}); +} + +template +static _FORCE_INLINE_ void ptr_call_helper(void (*p_func)(P...), const void **p_args) { + ptr_call_helperp(p_func, p_args, BuildIndexSequence{}); +} + +template +static _FORCE_INLINE_ int get_arg_count_helper(void (*p_func)(P...)) { + return sizeof...(P); +} + +template +static _FORCE_INLINE_ Variant::Type get_arg_type_helper(void (*p_func)(P...), int p_arg) { + return call_get_argument_type(p_arg); +} + +template +static _FORCE_INLINE_ Variant::Type get_ret_type_helper(void (*p_func)(P...)) { + return Variant::NIL; +} + +#define FUNCBINDR(m_func, m_args, m_category) \ + class Func_##m_func { \ + public: \ + static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ + call_helperr(VariantUtilityFunctions::m_func, r_ret, p_args, r_error); \ + } \ + \ + static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ + validated_call_helperr(VariantUtilityFunctions::m_func, r_ret, p_args); \ + } \ + static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ + ptr_call_helperr(VariantUtilityFunctions::m_func, ret, p_args); \ + } \ + \ + static int get_argument_count() { \ + return get_arg_count_helperr(VariantUtilityFunctions::m_func); \ + } \ + \ + static Variant::Type get_argument_type(int p_arg) { \ + return get_arg_type_helperr(VariantUtilityFunctions::m_func, p_arg); \ + } \ + \ + static Variant::Type get_return_type() { \ + return get_ret_type_helperr(VariantUtilityFunctions::m_func); \ + } \ + static bool has_return_type() { \ + return true; \ + } \ + static bool is_vararg() { return false; } \ + static Variant::UtilityFunctionType get_type() { return m_category; } \ + }; \ + register_utility_function(#m_func, m_args) + +#define FUNCBINDVR(m_func, m_args, m_category) \ + class Func_##m_func { \ + public: \ + static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ + r_error.error = Callable::CallError::CALL_OK; \ + *r_ret = VariantUtilityFunctions::m_func(*p_args[0], r_error); \ + } \ + \ + static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ + Callable::CallError ce; \ + *r_ret = VariantUtilityFunctions::m_func(*p_args[0], ce); \ + } \ + static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ + Callable::CallError ce; \ + PtrToArg::encode(VariantUtilityFunctions::m_func(PtrToArg::convert(p_args[0]), ce), ret); \ + } \ + \ + static int get_argument_count() { \ + return 1; \ + } \ + \ + static Variant::Type get_argument_type(int p_arg) { \ + return Variant::NIL; \ + } \ + \ + static Variant::Type get_return_type() { \ + return Variant::NIL; \ + } \ + static bool has_return_type() { \ + return true; \ + } \ + static bool is_vararg() { return false; } \ + static Variant::UtilityFunctionType get_type() { return m_category; } \ + }; \ + register_utility_function(#m_func, m_args) + +#define FUNCBINDVR3(m_func, m_args, m_category) \ + class Func_##m_func { \ + public: \ + static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ + r_error.error = Callable::CallError::CALL_OK; \ + *r_ret = VariantUtilityFunctions::m_func(*p_args[0], *p_args[1], *p_args[2], r_error); \ + } \ + \ + static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ + Callable::CallError ce; \ + *r_ret = VariantUtilityFunctions::m_func(*p_args[0], *p_args[1], *p_args[2], ce); \ + } \ + static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ + Callable::CallError ce; \ + Variant r; \ + r = VariantUtilityFunctions::m_func(PtrToArg::convert(p_args[0]), PtrToArg::convert(p_args[1]), PtrToArg::convert(p_args[2]), ce); \ + PtrToArg::encode(r, ret); \ + } \ + \ + static int get_argument_count() { \ + return 3; \ + } \ + \ + static Variant::Type get_argument_type(int p_arg) { \ + return Variant::NIL; \ + } \ + \ + static Variant::Type get_return_type() { \ + return Variant::NIL; \ + } \ + static bool has_return_type() { \ + return true; \ + } \ + static bool is_vararg() { return false; } \ + static Variant::UtilityFunctionType get_type() { return m_category; } \ + }; \ + register_utility_function(#m_func, m_args) + +#define FUNCBINDVARARG(m_func, m_args, m_category) \ + class Func_##m_func { \ + public: \ + static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ + r_error.error = Callable::CallError::CALL_OK; \ + *r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, r_error); \ + } \ + \ + static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ + Callable::CallError c; \ + *r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, c); \ + } \ + static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ + Vector args; \ + for (int i = 0; i < p_argcount; i++) { \ + args.push_back(PtrToArg::convert(p_args[i])); \ + } \ + Vector argsp; \ + for (int i = 0; i < p_argcount; i++) { \ + argsp.push_back(&args[i]); \ + } \ + Variant r; \ + validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \ + PtrToArg::encode(r, ret); \ + } \ + \ + static int get_argument_count() { \ + return 2; \ + } \ + \ + static Variant::Type get_argument_type(int p_arg) { \ + return Variant::NIL; \ + } \ + \ + static Variant::Type get_return_type() { \ + return Variant::NIL; \ + } \ + static bool has_return_type() { \ + return true; \ + } \ + static bool is_vararg() { \ + return true; \ + } \ + static Variant::UtilityFunctionType get_type() { \ + return m_category; \ + } \ + }; \ + register_utility_function(#m_func, m_args) + +#define FUNCBINDVARARGS(m_func, m_args, m_category) \ + class Func_##m_func { \ + public: \ + static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ + r_error.error = Callable::CallError::CALL_OK; \ + *r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, r_error); \ + } \ + \ + static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ + Callable::CallError c; \ + *r_ret = VariantUtilityFunctions::m_func(p_args, p_argcount, c); \ + } \ + static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ + Vector args; \ + for (int i = 0; i < p_argcount; i++) { \ + args.push_back(PtrToArg::convert(p_args[i])); \ + } \ + Vector argsp; \ + for (int i = 0; i < p_argcount; i++) { \ + argsp.push_back(&args[i]); \ + } \ + Variant r; \ + validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \ + PtrToArg::encode(r.operator String(), ret); \ + } \ + \ + static int get_argument_count() { \ + return 1; \ + } \ + \ + static Variant::Type get_argument_type(int p_arg) { \ + return Variant::NIL; \ + } \ + \ + static Variant::Type get_return_type() { \ + return Variant::STRING; \ + } \ + static bool has_return_type() { \ + return true; \ + } \ + static bool is_vararg() { \ + return true; \ + } \ + static Variant::UtilityFunctionType get_type() { \ + return m_category; \ + } \ + }; \ + register_utility_function(#m_func, m_args) + +#define FUNCBINDVARARGV(m_func, m_args, m_category) \ + class Func_##m_func { \ + public: \ + static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ + r_error.error = Callable::CallError::CALL_OK; \ + VariantUtilityFunctions::m_func(p_args, p_argcount, r_error); \ + } \ + \ + static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ + Callable::CallError c; \ + VariantUtilityFunctions::m_func(p_args, p_argcount, c); \ + } \ + static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ + Vector args; \ + for (int i = 0; i < p_argcount; i++) { \ + args.push_back(PtrToArg::convert(p_args[i])); \ + } \ + Vector argsp; \ + for (int i = 0; i < p_argcount; i++) { \ + argsp.push_back(&args[i]); \ + } \ + Variant r; \ + validated_call(&r, (const Variant **)argsp.ptr(), p_argcount); \ + } \ + \ + static int get_argument_count() { \ + return 1; \ + } \ + \ + static Variant::Type get_argument_type(int p_arg) { \ + return Variant::NIL; \ + } \ + \ + static Variant::Type get_return_type() { \ + return Variant::NIL; \ + } \ + static bool has_return_type() { \ + return false; \ + } \ + static bool is_vararg() { \ + return true; \ + } \ + static Variant::UtilityFunctionType get_type() { \ + return m_category; \ + } \ + }; \ + register_utility_function(#m_func, m_args) + +#define FUNCBIND(m_func, m_args, m_category) \ + class Func_##m_func { \ + public: \ + static void call(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { \ + call_helper(VariantUtilityFunctions::m_func, p_args, r_error); \ + } \ + \ + static void validated_call(Variant *r_ret, const Variant **p_args, int p_argcount) { \ + validated_call_helper(VariantUtilityFunctions::m_func, p_args); \ + } \ + static void ptrcall(void *ret, const void **p_args, int p_argcount) { \ + ptr_call_helper(VariantUtilityFunctions::m_func, p_args); \ + } \ + \ + static int get_argument_count() { \ + return get_arg_count_helper(VariantUtilityFunctions::m_func); \ + } \ + \ + static Variant::Type get_argument_type(int p_arg) { \ + return get_arg_type_helper(VariantUtilityFunctions::m_func, p_arg); \ + } \ + \ + static Variant::Type get_return_type() { \ + return get_ret_type_helper(VariantUtilityFunctions::m_func); \ + } \ + static bool has_return_type() { \ + return false; \ + } \ + static bool is_vararg() { return false; } \ + static Variant::UtilityFunctionType get_type() { return m_category; } \ + }; \ + register_utility_function(#m_func, m_args) + +struct VariantUtilityFunctionInfo { + void (*call_utility)(Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error); + Variant::ValidatedUtilityFunction validated_call_utility; + Variant::PTRUtilityFunction ptr_call_utility; + Vector argnames; + bool is_vararg; + bool returns_value; + int argcount; + Variant::Type (*get_arg_type)(int); + Variant::Type return_type; + Variant::UtilityFunctionType type; +}; + +static OAHashMap utility_function_table; +static List utility_function_name_table; + +template +static void register_utility_function(const String &p_name, const Vector &argnames) { + String name = p_name; + if (name.begins_with("_")) { + name = name.substr(1, name.length() - 1); + } + StringName sname = name; + ERR_FAIL_COND(utility_function_table.has(sname)); + + VariantUtilityFunctionInfo bfi; + bfi.call_utility = T::call; + bfi.validated_call_utility = T::validated_call; + bfi.ptr_call_utility = T::ptrcall; + bfi.is_vararg = T::is_vararg(); + bfi.argnames = argnames; + bfi.argcount = T::get_argument_count(); + if (!bfi.is_vararg) { + ERR_FAIL_COND_MSG(argnames.size() != bfi.argcount, "wrong number of arguments binding utility function: " + name); + } + bfi.get_arg_type = T::get_argument_type; + bfi.return_type = T::get_return_type(); + bfi.type = T::get_type(); + bfi.returns_value = T::has_return_type(); + + utility_function_table.insert(sname, bfi); + utility_function_name_table.push_back(sname); +} + +void Variant::_register_variant_utility_functions() { + // Math + + FUNCBINDR(sin, sarray("angle_rad"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(cos, sarray("angle_rad"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(tan, sarray("angle_rad"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(sinh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(cosh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(tanh, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(asin, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(acos, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(atan, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(atan2, sarray("y", "x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(sqrt, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(fmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(fposmod, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(floor, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(ceil, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(round, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDVR(abs, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(absf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(absi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDVR(sign, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(signf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(signi, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(pow, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(log, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(exp, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(is_nan, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(is_inf, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(is_equal_approx, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(is_zero_approx, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(ease, sarray("x", "c"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(step_decimals, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(range_step_decimals, sarray("x"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(stepify, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(lerp, sarray("from", "to", "c"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(lerp_angle, sarray("from", "to", "c"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(inverse_lerp, sarray("from", "to", "c"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(range_lerp, sarray("value", "istart", "istop", "ostart", "ostop"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(smoothstep, sarray("from", "to", "c"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(move_toward, sarray("from", "to", "delta"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(dectime, sarray("value", "amount", "step"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(deg2rad, sarray("deg"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(rad2deg, sarray("rad"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(linear2db, sarray("lin"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(db2linear, sarray("db"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(polar2cartesian, sarray("r", "th"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(cartesian2polar, sarray("x", "y"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(wrapi, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(wrapf, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDVARARG(max, sarray(), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(maxi, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(maxf, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDVARARG(min, sarray(), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(mini, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(minf, sarray("a", "b"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDVR3(clamp, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(clampi, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(clampf, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH); + + FUNCBINDR(nearest_po2, sarray("value"), Variant::UTILITY_FUNC_TYPE_MATH); + + //Random + + FUNCBIND(randomize, sarray(), Variant::UTILITY_FUNC_TYPE_RANDOM); + FUNCBINDR(randi, sarray(), Variant::UTILITY_FUNC_TYPE_RANDOM); + FUNCBINDR(randf, sarray(), Variant::UTILITY_FUNC_TYPE_RANDOM); + FUNCBINDR(randi_range, sarray("from", "to"), Variant::UTILITY_FUNC_TYPE_RANDOM); + FUNCBINDR(randf_range, sarray("from", "to"), Variant::UTILITY_FUNC_TYPE_RANDOM); + FUNCBIND(seed, sarray("base"), Variant::UTILITY_FUNC_TYPE_RANDOM); + FUNCBINDR(rand_from_seed, sarray("seed"), Variant::UTILITY_FUNC_TYPE_RANDOM); + + // Utility + FUNCBINDVR(weakref, sarray("from"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(_typeof, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGS(str, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGV(print, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGV(printerr, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGV(printt, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGV(prints, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGV(printraw, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGV(push_error, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDVARARGV(push_warning, sarray(), Variant::UTILITY_FUNC_TYPE_GENERAL); + + FUNCBINDR(var2str, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(str2var, sarray("string"), Variant::UTILITY_FUNC_TYPE_GENERAL); + + FUNCBINDR(var2bytes, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(bytes2var, sarray("bytes"), Variant::UTILITY_FUNC_TYPE_GENERAL); + + FUNCBINDR(var2bytes_with_objects, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(bytes2var_with_objects, sarray("bytes"), Variant::UTILITY_FUNC_TYPE_GENERAL); + + FUNCBINDR(hash, sarray("variable"), Variant::UTILITY_FUNC_TYPE_GENERAL); + + FUNCBINDR(instance_from_id, sarray("id"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(is_instance_id_valid, sarray("id"), Variant::UTILITY_FUNC_TYPE_GENERAL); + FUNCBINDR(is_instance_valid, sarray("instance"), Variant::UTILITY_FUNC_TYPE_GENERAL); +} +void Variant::_unregister_variant_utility_functions() { + utility_function_table.clear(); + utility_function_name_table.clear(); +} + +void Variant::call_utility_function(const StringName &p_name, Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; + r_error.argument = 0; + r_error.expected = 0; + return; + } + + if (unlikely(!bfi->is_vararg && p_argcount < bfi->argcount)) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; + r_error.argument = 0; + r_error.expected = bfi->argcount; + return; + } + + if (unlikely(!bfi->is_vararg && p_argcount > bfi->argcount)) { + r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; + r_error.argument = 0; + r_error.expected = bfi->argcount; + return; + } + + bfi->call_utility(r_ret, p_args, p_argcount, r_error); +} + +bool Variant::has_utility_function(const StringName &p_name) { + return utility_function_table.has(p_name); +} + +Variant::ValidatedUtilityFunction Variant::get_validated_utility_function(const StringName &p_name) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return nullptr; + } + + return bfi->validated_call_utility; +} +Variant::PTRUtilityFunction Variant::get_ptr_utility_function(const StringName &p_name) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return nullptr; + } + + return bfi->ptr_call_utility; +} + +Variant::UtilityFunctionType Variant::get_utility_function_type(const StringName &p_name) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return Variant::UTILITY_FUNC_TYPE_MATH; + } + + return bfi->type; +} + +int Variant::get_utility_function_argument_count(const StringName &p_name) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return 0; + } + + return bfi->argcount; +} +Variant::Type Variant::get_utility_function_argument_type(const StringName &p_name, int p_arg) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return Variant::NIL; + } + + return bfi->get_arg_type(p_arg); +} +String Variant::get_utility_function_argument_name(const StringName &p_name, int p_arg) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return String(); + } + ERR_FAIL_COND_V(bfi->is_vararg, String()); + ERR_FAIL_INDEX_V(p_arg, bfi->argnames.size(), String()); + return bfi->argnames[p_arg]; +} +bool Variant::has_utility_function_return_value(const StringName &p_name) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return false; + } + return bfi->returns_value; +} +Variant::Type Variant::get_utility_function_return_type(const StringName &p_name) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return Variant::NIL; + } + + return bfi->return_type; +} +bool Variant::is_utility_function_vararg(const StringName &p_name) { + const VariantUtilityFunctionInfo *bfi = utility_function_table.lookup_ptr(p_name); + if (!bfi) { + return false; + } + + return bfi->is_vararg; +} + +void Variant::get_utility_function_list(List *r_functions) { + for (List::Element *E = utility_function_name_table.front(); E; E = E->next()) { + r_functions->push_back(E->get()); + } +} diff --git a/editor/doc_data.cpp b/editor/doc_data.cpp index 78c601d4bc..8504d61d2f 100644 --- a/editor/doc_data.cpp +++ b/editor/doc_data.cpp @@ -737,16 +737,16 @@ void DocData::generate(bool p_basic_types) { c.properties.push_back(pd); } - List builtin_funcs; - Variant::get_builtin_function_list(&builtin_funcs); - builtin_funcs.sort_custom(); - for (List::Element *E = builtin_funcs.front(); E; E = E->next()) { + List utility_functions; + Variant::get_utility_function_list(&utility_functions); + utility_functions.sort_custom(); + for (List::Element *E = utility_functions.front(); E; E = E->next()) { MethodDoc md; md.name = E->get(); //return - if (Variant::has_builtin_func_return_value(E->get())) { + if (Variant::has_utility_function_return_value(E->get())) { PropertyInfo pi; - pi.type = Variant::get_builtin_func_return_type(E->get()); + pi.type = Variant::get_utility_function_return_type(E->get()); if (pi.type == Variant::NIL) { pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT; } @@ -755,13 +755,13 @@ void DocData::generate(bool p_basic_types) { md.return_type = ad.type; } - if (Variant::is_builtin_func_vararg(E->get())) { + if (Variant::is_utility_function_vararg(E->get())) { md.qualifiers = "vararg"; } else { - for (int i = 0; i < Variant::get_builtin_func_argument_count(E->get()); i++) { + for (int i = 0; i < Variant::get_utility_function_argument_count(E->get()); i++) { PropertyInfo pi; - pi.type = Variant::get_builtin_func_argument_type(E->get(), i); - pi.name = Variant::get_builtin_func_argument_name(E->get(), i); + pi.type = Variant::get_utility_function_argument_type(E->get(), i); + pi.name = Variant::get_utility_function_argument_name(E->get(), i); if (pi.type == Variant::NIL) { pi.usage = PROPERTY_USAGE_NIL_IS_VARIANT; } diff --git a/modules/gdnative/gdnative/variant.cpp b/modules/gdnative/gdnative/variant.cpp index 8e30eaae4d..417abeaad3 100644 --- a/modules/gdnative/gdnative/variant.cpp +++ b/modules/gdnative/gdnative/variant.cpp @@ -576,7 +576,9 @@ godot_variant GDAPI godot_variant_call(godot_variant *p_self, const godot_string godot_variant raw_dest; Variant *dest = (Variant *)&raw_dest; Callable::CallError error; - memnew_placement_custom(dest, Variant, Variant(self->call(*method, args, p_argcount, error))); + Variant ret; + self->call(*method, args, p_argcount, ret, error); + memnew_placement_custom(dest, Variant, Variant(ret)); if (r_error) { r_error->error = (godot_variant_call_error_error)error.error; r_error->argument = error.argument; diff --git a/modules/gdscript/gdscript_function.cpp b/modules/gdscript/gdscript_function.cpp index 59f3deb736..8372672cf7 100644 --- a/modules/gdscript/gdscript_function.cpp +++ b/modules/gdscript/gdscript_function.cpp @@ -1048,7 +1048,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a Callable::CallError err; if (call_ret) { GET_VARIANT_PTR(ret, argc); - base->call_ptr(*methodname, (const Variant **)argptrs, argc, ret, err); + base->call(*methodname, (const Variant **)argptrs, argc, *ret, err); #ifdef DEBUG_ENABLED if (!call_async && ret->get_type() == Variant::OBJECT) { // Check if getting a function state without await. @@ -1066,7 +1066,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a } #endif } else { - base->call_ptr(*methodname, (const Variant **)argptrs, argc, nullptr, err); + Variant ret; + base->call(*methodname, (const Variant **)argptrs, argc, ret, err); } #ifdef DEBUG_ENABLED if (GDScriptLanguage::get_singleton()->profiling) { diff --git a/modules/visual_script/visual_script_expression.cpp b/modules/visual_script/visual_script_expression.cpp index bb015b118d..10a18dfd5e 100644 --- a/modules/visual_script/visual_script_expression.cpp +++ b/modules/visual_script/visual_script_expression.cpp @@ -1463,7 +1463,7 @@ public: argp.write[i] = &arr[i]; } - r_ret = base.call(call->method, (const Variant **)argp.ptr(), argp.size(), ce); + base.call(call->method, (const Variant **)argp.ptr(), argp.size(), r_ret, ce); if (ce.error != Callable::CallError::CALL_OK) { r_error_str = "On call to '" + String(call->method) + "':"; diff --git a/modules/visual_script/visual_script_func_nodes.cpp b/modules/visual_script/visual_script_func_nodes.cpp index 3b46af3cbd..34a1cfc4fc 100644 --- a/modules/visual_script/visual_script_func_nodes.cpp +++ b/modules/visual_script/visual_script_func_nodes.cpp @@ -42,7 +42,7 @@ ////////////////////////////////////////// int VisualScriptFunctionCall::get_output_sequence_port_count() const { - if ((method_cache.flags & METHOD_FLAG_CONST && call_mode != CALL_MODE_INSTANCE) || (call_mode == CALL_MODE_BASIC_TYPE && Variant::is_method_const(basic_type, function))) { + if ((method_cache.flags & METHOD_FLAG_CONST && call_mode != CALL_MODE_INSTANCE) || (call_mode == CALL_MODE_BASIC_TYPE && Variant::is_builtin_method_const(basic_type, function))) { return 0; } else { return 1; @@ -50,7 +50,7 @@ int VisualScriptFunctionCall::get_output_sequence_port_count() const { } bool VisualScriptFunctionCall::has_input_sequence_port() const { - return !((method_cache.flags & METHOD_FLAG_CONST && call_mode != CALL_MODE_INSTANCE) || (call_mode == CALL_MODE_BASIC_TYPE && Variant::is_method_const(basic_type, function))); + return !((method_cache.flags & METHOD_FLAG_CONST && call_mode != CALL_MODE_INSTANCE) || (call_mode == CALL_MODE_BASIC_TYPE && Variant::is_builtin_method_const(basic_type, function))); } #ifdef TOOLS_ENABLED @@ -130,7 +130,11 @@ StringName VisualScriptFunctionCall::_get_base_type() const { int VisualScriptFunctionCall::get_input_value_port_count() const { if (call_mode == CALL_MODE_BASIC_TYPE) { - Vector types = Variant::get_method_argument_types(basic_type, function); + Vector types; + int argc = Variant::get_builtin_method_argument_count(basic_type, function); + for (int i = 0; i < argc; i++) { + types.push_back(Variant::get_builtin_method_argument_type(basic_type, function, i)); + } return types.size() + (rpc_call_mode >= RPC_RELIABLE_TO_ID ? 1 : 0) + 1; } else { @@ -147,8 +151,7 @@ int VisualScriptFunctionCall::get_input_value_port_count() const { int VisualScriptFunctionCall::get_output_value_port_count() const { if (call_mode == CALL_MODE_BASIC_TYPE) { - bool returns = false; - Variant::get_method_return_type(basic_type, function, &returns); + bool returns = Variant::has_builtin_method_return_value(basic_type, function); return returns ? 1 : 0; } else { @@ -195,10 +198,7 @@ PropertyInfo VisualScriptFunctionCall::get_input_value_port_info(int p_idx) cons #ifdef DEBUG_METHODS_ENABLED if (call_mode == CALL_MODE_BASIC_TYPE) { - Vector names = Variant::get_method_argument_names(basic_type, function); - Vector types = Variant::get_method_argument_types(basic_type, function); - return PropertyInfo(types[p_idx], names[p_idx]); - + return PropertyInfo(Variant::get_builtin_method_argument_type(basic_type, function, p_idx), Variant::get_builtin_method_argument_name(basic_type, function, p_idx)); } else { MethodBind *mb = ClassDB::get_method(_get_base_type(), function); if (mb) { @@ -220,7 +220,7 @@ PropertyInfo VisualScriptFunctionCall::get_output_value_port_info(int p_idx) con #ifdef DEBUG_METHODS_ENABLED if (call_mode == CALL_MODE_BASIC_TYPE) { - return PropertyInfo(Variant::get_method_return_type(basic_type, function), ""); + return PropertyInfo(Variant::get_builtin_method_return_type(basic_type, function), ""); } else { if (call_mode == CALL_MODE_INSTANCE) { if (p_idx == 0) { @@ -419,7 +419,7 @@ void VisualScriptFunctionCall::set_function(const StringName &p_type) { function = p_type; if (call_mode == CALL_MODE_BASIC_TYPE) { - use_default_args = Variant::get_method_default_arguments(basic_type, function).size(); + use_default_args = Variant::get_builtin_method_default_arguments(basic_type, function).size(); } else { //update all caches @@ -606,7 +606,7 @@ void VisualScriptFunctionCall::_validate_property(PropertyInfo &property) const int mc = 0; if (call_mode == CALL_MODE_BASIC_TYPE) { - mc = Variant::get_method_default_arguments(basic_type, function).size(); + mc = Variant::get_builtin_method_default_arguments(basic_type, function).size(); } else { MethodBind *mb = ClassDB::get_method(_get_base_type(), function); if (mb) { @@ -805,19 +805,21 @@ public: } else if (returns) { if (call_mode == VisualScriptFunctionCall::CALL_MODE_INSTANCE) { if (returns >= 2) { - *p_outputs[1] = v.call(function, p_inputs + 1, input_args, r_error); + v.call(function, p_inputs + 1, input_args, *p_outputs[1], r_error); } else if (returns == 1) { - v.call(function, p_inputs + 1, input_args, r_error); + Variant ret; + v.call(function, p_inputs + 1, input_args, ret, r_error); } else { r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; r_error_str = "Invalid returns count for call_mode == CALL_MODE_INSTANCE"; return 0; } } else { - *p_outputs[0] = v.call(function, p_inputs + 1, input_args, r_error); + v.call(function, p_inputs + 1, input_args, *p_outputs[0], r_error); } } else { - v.call(function, p_inputs + 1, input_args, r_error); + Variant ret; + v.call(function, p_inputs + 1, input_args, ret, r_error); } if (call_mode == VisualScriptFunctionCall::CALL_MODE_INSTANCE) { -- cgit v1.2.3