GDScript: Rework GDScriptUtilityFunctions macros

This commit is contained in:
Danil Alexeev 2024-10-25 17:45:37 +03:00
parent 6732a0fd86
commit 4dc568856a
No known key found for this signature in database
GPG Key ID: 124453E157DA8DC7
4 changed files with 194 additions and 317 deletions

View File

@ -61,7 +61,7 @@
<method name="convert" deprecated="Use [method @GlobalScope.type_convert] instead."> <method name="convert" deprecated="Use [method @GlobalScope.type_convert] instead.">
<return type="Variant" /> <return type="Variant" />
<param index="0" name="what" type="Variant" /> <param index="0" name="what" type="Variant" />
<param index="1" name="type" type="int" /> <param index="1" name="type" type="int" enum="Variant.Type" />
<description> <description>
Converts [param what] to [param type] in the best way possible. The [param type] uses the [enum Variant.Type] values. Converts [param what] to [param type] in the best way possible. The [param type] uses the [enum Variant.Type] values.
[codeblock] [codeblock]

View File

@ -34,7 +34,6 @@
#include "core/io/resource_loader.h" #include "core/io/resource_loader.h"
#include "core/object/class_db.h" #include "core/object/class_db.h"
#include "core/object/method_bind.h"
#include "core/object/object.h" #include "core/object/object.h"
#include "core/templates/oa_hash_map.h" #include "core/templates/oa_hash_map.h"
#include "core/templates/vector.h" #include "core/templates/vector.h"
@ -42,101 +41,105 @@
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
#define VALIDATE_ARG_COUNT(m_count) \ #define DEBUG_VALIDATE_ARG_COUNT(m_min_count, m_max_count) \
if (p_arg_count < m_count) { \ if (unlikely(p_arg_count < m_min_count)) { \
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; \
r_error.expected = m_count; \
*r_ret = Variant(); \ *r_ret = Variant(); \
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; \
r_error.expected = m_min_count; \
return; \ return; \
} \ } \
if (p_arg_count > m_count) { \ if (unlikely(p_arg_count > m_max_count)) { \
*r_ret = Variant(); \
r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; \ r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; \
r_error.expected = m_count; \ r_error.expected = m_max_count; \
*r_ret = Variant(); \
return; \ return; \
} }
#define VALIDATE_ARG_INT(m_arg) \ #define DEBUG_VALIDATE_ARG_TYPE(m_arg, m_type) \
if (p_args[m_arg]->get_type() != Variant::INT) { \ if (unlikely(!Variant::can_convert_strict(p_args[m_arg]->get_type(), m_type))) { \
*r_ret = Variant(); \
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; \ r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; \
r_error.argument = m_arg; \ r_error.argument = m_arg; \
r_error.expected = Variant::INT; \ r_error.expected = m_type; \
*r_ret = Variant(); \
return; \ return; \
} }
#define VALIDATE_ARG_NUM(m_arg) \ #define DEBUG_VALIDATE_ARG_CUSTOM(m_arg, m_type, m_cond, m_msg) \
if (!p_args[m_arg]->is_num()) { \ if (unlikely(m_cond)) { \
*r_ret = m_msg; \
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; \ r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; \
r_error.argument = m_arg; \ r_error.argument = m_arg; \
r_error.expected = Variant::FLOAT; \ r_error.expected = m_type; \
*r_ret = Variant(); \
return; \ return; \
} }
#else // !DEBUG_ENABLED #else // !DEBUG_ENABLED
#define VALIDATE_ARG_COUNT(m_count) #define DEBUG_VALIDATE_ARG_COUNT(m_min_count, m_max_count)
#define VALIDATE_ARG_INT(m_arg) #define DEBUG_VALIDATE_ARG_TYPE(m_arg, m_type)
#define VALIDATE_ARG_NUM(m_arg) #define DEBUG_VALIDATE_ARG_CUSTOM(m_arg, m_type, m_cond, m_msg)
#endif // DEBUG_ENABLED #endif // DEBUG_ENABLED
#define VALIDATE_ARG_CUSTOM(m_arg, m_type, m_cond, m_msg) \
if (unlikely(m_cond)) { \
*r_ret = m_msg; \
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; \
r_error.argument = m_arg; \
r_error.expected = m_type; \
return; \
}
#define GDFUNC_FAIL_COND_MSG(m_cond, m_msg) \
if (unlikely(m_cond)) { \
*r_ret = m_msg; \
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD; \
return; \
}
struct GDScriptUtilityFunctionsDefinitions { struct GDScriptUtilityFunctionsDefinitions {
#ifndef DISABLE_DEPRECATED #ifndef DISABLE_DEPRECATED
static inline void convert(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { static inline void convert(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
VALIDATE_ARG_COUNT(2); DEBUG_VALIDATE_ARG_COUNT(2, 2);
VALIDATE_ARG_INT(1); DEBUG_VALIDATE_ARG_TYPE(1, Variant::INT);
int type = *p_args[1];
if (type < 0 || type >= Variant::VARIANT_MAX) { int type = *p_args[1];
*r_ret = RTR("Invalid type argument to convert(), use TYPE_* constants."); DEBUG_VALIDATE_ARG_CUSTOM(1, Variant::INT, type < 0 || type >= Variant::VARIANT_MAX,
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; RTR("Invalid type argument to convert(), use TYPE_* constants."));
r_error.argument = 0;
r_error.expected = Variant::INT;
return;
} else {
Variant::construct(Variant::Type(type), *r_ret, p_args, 1, r_error); Variant::construct(Variant::Type(type), *r_ret, p_args, 1, r_error);
if (r_error.error != Callable::CallError::CALL_OK) {
*r_ret = vformat(RTR(R"(Cannot convert "%s" to "%s".)"), Variant::get_type_name(p_args[0]->get_type()), Variant::get_type_name(Variant::Type(type)));
}
}
} }
#endif // DISABLE_DEPRECATED #endif // DISABLE_DEPRECATED
static inline void type_exists(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { static inline void type_exists(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
VALIDATE_ARG_COUNT(1); DEBUG_VALIDATE_ARG_COUNT(1, 1);
DEBUG_VALIDATE_ARG_TYPE(0, Variant::STRING_NAME);
*r_ret = ClassDB::class_exists(*p_args[0]); *r_ret = ClassDB::class_exists(*p_args[0]);
} }
static inline void _char(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { static inline void _char(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
VALIDATE_ARG_COUNT(1); DEBUG_VALIDATE_ARG_COUNT(1, 1);
VALIDATE_ARG_INT(0); DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT);
char32_t result[2] = { *p_args[0], 0 }; char32_t result[2] = { *p_args[0], 0 };
*r_ret = String(result); *r_ret = String(result);
} }
static inline void range(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { static inline void range(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
DEBUG_VALIDATE_ARG_COUNT(1, 3);
switch (p_arg_count) { switch (p_arg_count) {
case 0: {
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
r_error.expected = 1;
*r_ret = Variant();
} break;
case 1: { case 1: {
VALIDATE_ARG_NUM(0); DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT);
int count = *p_args[0]; int count = *p_args[0];
Array arr; Array arr;
if (count <= 0) { if (count <= 0) {
*r_ret = arr; *r_ret = arr;
return; return;
} }
Error err = arr.resize(count); Error err = arr.resize(count);
if (err != OK) { GDFUNC_FAIL_COND_MSG(err != OK, RTR("Cannot resize array."));
*r_ret = RTR("Cannot resize array.");
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
return;
}
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
arr[i] = i; arr[i] = i;
@ -145,8 +148,8 @@ struct GDScriptUtilityFunctionsDefinitions {
*r_ret = arr; *r_ret = arr;
} break; } break;
case 2: { case 2: {
VALIDATE_ARG_NUM(0); DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT);
VALIDATE_ARG_NUM(1); DEBUG_VALIDATE_ARG_TYPE(1, Variant::INT);
int from = *p_args[0]; int from = *p_args[0];
int to = *p_args[1]; int to = *p_args[1];
@ -156,30 +159,26 @@ struct GDScriptUtilityFunctionsDefinitions {
*r_ret = arr; *r_ret = arr;
return; return;
} }
Error err = arr.resize(to - from); Error err = arr.resize(to - from);
if (err != OK) { GDFUNC_FAIL_COND_MSG(err != OK, RTR("Cannot resize array."));
*r_ret = RTR("Cannot resize array.");
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
return;
}
for (int i = from; i < to; i++) { for (int i = from; i < to; i++) {
arr[i - from] = i; arr[i - from] = i;
} }
*r_ret = arr; *r_ret = arr;
} break; } break;
case 3: { case 3: {
VALIDATE_ARG_NUM(0); DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT);
VALIDATE_ARG_NUM(1); DEBUG_VALIDATE_ARG_TYPE(1, Variant::INT);
VALIDATE_ARG_NUM(2); DEBUG_VALIDATE_ARG_TYPE(2, Variant::INT);
int from = *p_args[0]; int from = *p_args[0];
int to = *p_args[1]; int to = *p_args[1];
int incr = *p_args[2]; int incr = *p_args[2];
if (incr == 0) {
*r_ret = RTR("Step argument is zero!"); VALIDATE_ARG_CUSTOM(2, Variant::INT, incr == 0, RTR("Step argument is zero!"));
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
return;
}
Array arr; Array arr;
if (from >= to && incr > 0) { if (from >= to && incr > 0) {
@ -200,12 +199,7 @@ struct GDScriptUtilityFunctionsDefinitions {
} }
Error err = arr.resize(count); Error err = arr.resize(count);
GDFUNC_FAIL_COND_MSG(err != OK, RTR("Cannot resize array."));
if (err != OK) {
*r_ret = RTR("Cannot resize array.");
r_error.error = Callable::CallError::CALL_ERROR_INVALID_METHOD;
return;
}
if (incr > 0) { if (incr > 0) {
int idx = 0; int idx = 0;
@ -221,59 +215,39 @@ struct GDScriptUtilityFunctionsDefinitions {
*r_ret = arr; *r_ret = arr;
} break; } break;
default: {
r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
r_error.expected = 3;
*r_ret = Variant();
} break;
} }
} }
static inline void load(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { static inline void load(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
VALIDATE_ARG_COUNT(1); DEBUG_VALIDATE_ARG_COUNT(1, 1);
if (!p_args[0]->is_string()) { DEBUG_VALIDATE_ARG_TYPE(0, Variant::STRING);
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::STRING;
*r_ret = Variant();
} else {
*r_ret = ResourceLoader::load(*p_args[0]); *r_ret = ResourceLoader::load(*p_args[0]);
} }
}
static inline void inst_to_dict(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { static inline void inst_to_dict(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
VALIDATE_ARG_COUNT(1); DEBUG_VALIDATE_ARG_COUNT(1, 1);
DEBUG_VALIDATE_ARG_TYPE(0, Variant::OBJECT);
if (p_args[0]->get_type() == Variant::NIL) { if (p_args[0]->get_type() == Variant::NIL) {
*r_ret = Variant(); *r_ret = Variant();
} else if (p_args[0]->get_type() != Variant::OBJECT) { return;
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; }
r_error.argument = 0;
r_error.expected = Variant::OBJECT;
*r_ret = Variant();
} else {
Object *obj = *p_args[0]; Object *obj = *p_args[0];
if (!obj) { if (!obj) {
*r_ret = Variant(); *r_ret = Variant();
} else if (!obj->get_script_instance() || obj->get_script_instance()->get_language() != GDScriptLanguage::get_singleton()) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::DICTIONARY;
*r_ret = RTR("Not a script with an instance");
return;
} else {
GDScriptInstance *ins = static_cast<GDScriptInstance *>(obj->get_script_instance());
Ref<GDScript> base = ins->get_script();
if (base.is_null()) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::DICTIONARY;
*r_ret = RTR("Not based on a script");
return; return;
} }
VALIDATE_ARG_CUSTOM(0, Variant::OBJECT,
!obj->get_script_instance() || obj->get_script_instance()->get_language() != GDScriptLanguage::get_singleton(),
RTR("Not a script with an instance."));
GDScriptInstance *inst = static_cast<GDScriptInstance *>(obj->get_script_instance());
Ref<GDScript> base = inst->get_script();
VALIDATE_ARG_CUSTOM(0, Variant::OBJECT, base.is_null(), RTR("Not based on a script."));
GDScript *p = base.ptr(); GDScript *p = base.ptr();
String path = p->get_script_path(); String path = p->get_script_path();
Vector<StringName> sname; Vector<StringName> sname;
@ -284,16 +258,7 @@ struct GDScriptUtilityFunctionsDefinitions {
} }
sname.reverse(); sname.reverse();
if (!path.is_resource_file()) { VALIDATE_ARG_CUSTOM(0, Variant::OBJECT, !path.is_resource_file(), RTR("Not based on a resource file."));
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::DICTIONARY;
*r_ret = Variant();
*r_ret = RTR("Not based on a resource file");
return;
}
NodePath cp(sname, Vector<StringName>(), false); NodePath cp(sname, Vector<StringName>(), false);
@ -303,56 +268,26 @@ struct GDScriptUtilityFunctionsDefinitions {
for (const KeyValue<StringName, GDScript::MemberInfo> &E : base->member_indices) { for (const KeyValue<StringName, GDScript::MemberInfo> &E : base->member_indices) {
if (!d.has(E.key)) { if (!d.has(E.key)) {
d[E.key] = ins->members[E.value.index]; d[E.key] = inst->members[E.value.index];
} }
} }
*r_ret = d; *r_ret = d;
} }
}
}
static inline void dict_to_inst(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { static inline void dict_to_inst(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
VALIDATE_ARG_COUNT(1); DEBUG_VALIDATE_ARG_COUNT(1, 1);
DEBUG_VALIDATE_ARG_TYPE(0, Variant::DICTIONARY);
if (p_args[0]->get_type() != Variant::DICTIONARY) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::DICTIONARY;
*r_ret = Variant();
return;
}
Dictionary d = *p_args[0]; Dictionary d = *p_args[0];
if (!d.has("@path")) { VALIDATE_ARG_CUSTOM(0, Variant::DICTIONARY, !d.has("@path"), RTR("Invalid instance dictionary format (missing @path)."));
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::OBJECT;
*r_ret = RTR("Invalid instance dictionary format (missing @path)");
return;
}
Ref<Script> scr = ResourceLoader::load(d["@path"]); Ref<Script> scr = ResourceLoader::load(d["@path"]);
if (!scr.is_valid()) { VALIDATE_ARG_CUSTOM(0, Variant::DICTIONARY, !scr.is_valid(), RTR("Invalid instance dictionary format (can't load script at @path)."));
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::OBJECT;
*r_ret = RTR("Invalid instance dictionary format (can't load script at @path)");
return;
}
Ref<GDScript> gdscr = scr; Ref<GDScript> gdscr = scr;
VALIDATE_ARG_CUSTOM(0, Variant::DICTIONARY, !gdscr.is_valid(), RTR("Invalid instance dictionary format (invalid script at @path)."));
if (!gdscr.is_valid()) {
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::OBJECT;
*r_ret = Variant();
*r_ret = RTR("Invalid instance dictionary format (invalid script at @path)");
return;
}
NodePath sub; NodePath sub;
if (d.has("@subpath")) { if (d.has("@subpath")) {
@ -361,54 +296,35 @@ struct GDScriptUtilityFunctionsDefinitions {
for (int i = 0; i < sub.get_name_count(); i++) { for (int i = 0; i < sub.get_name_count(); i++) {
gdscr = gdscr->subclasses[sub.get_name(i)]; gdscr = gdscr->subclasses[sub.get_name(i)];
if (!gdscr.is_valid()) { VALIDATE_ARG_CUSTOM(0, Variant::DICTIONARY, !gdscr.is_valid(), RTR("Invalid instance dictionary (invalid subclasses)."));
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::OBJECT;
*r_ret = Variant();
*r_ret = RTR("Invalid instance dictionary (invalid subclasses)");
return;
} }
}
*r_ret = gdscr->_new(nullptr, -1 /*skip initializer*/, r_error);
*r_ret = gdscr->_new(nullptr, -1 /* skip initializer */, r_error);
if (r_error.error != Callable::CallError::CALL_OK) { if (r_error.error != Callable::CallError::CALL_OK) {
*r_ret = RTR("Cannot instantiate GDScript class."); *r_ret = RTR("Cannot instantiate GDScript class.");
return; return;
} }
GDScriptInstance *ins = static_cast<GDScriptInstance *>(static_cast<Object *>(*r_ret)->get_script_instance()); GDScriptInstance *inst = static_cast<GDScriptInstance *>(static_cast<Object *>(*r_ret)->get_script_instance());
Ref<GDScript> gd_ref = ins->get_script(); Ref<GDScript> gd_ref = inst->get_script();
for (KeyValue<StringName, GDScript::MemberInfo> &E : gd_ref->member_indices) { for (KeyValue<StringName, GDScript::MemberInfo> &E : gd_ref->member_indices) {
if (d.has(E.key)) { if (d.has(E.key)) {
ins->members.write[E.value.index] = d[E.key]; inst->members.write[E.value.index] = d[E.key];
} }
} }
} }
static inline void Color8(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { static inline void Color8(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
if (p_arg_count < 3) { DEBUG_VALIDATE_ARG_COUNT(3, 4);
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT);
r_error.expected = 3; DEBUG_VALIDATE_ARG_TYPE(1, Variant::INT);
*r_ret = Variant(); DEBUG_VALIDATE_ARG_TYPE(2, Variant::INT);
return;
}
if (p_arg_count > 4) {
r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
r_error.expected = 4;
*r_ret = Variant();
return;
}
VALIDATE_ARG_INT(0);
VALIDATE_ARG_INT(1);
VALIDATE_ARG_INT(2);
Color color((int64_t)*p_args[0] / 255.0f, (int64_t)*p_args[1] / 255.0f, (int64_t)*p_args[2] / 255.0f); Color color((int64_t)*p_args[0] / 255.0f, (int64_t)*p_args[1] / 255.0f, (int64_t)*p_args[2] / 255.0f);
if (p_arg_count == 4) { if (p_arg_count == 4) {
VALIDATE_ARG_INT(3); DEBUG_VALIDATE_ARG_TYPE(3, Variant::INT);
color.a = (int64_t)*p_args[3] / 255.0f; color.a = (int64_t)*p_args[3] / 255.0f;
} }
@ -435,7 +351,8 @@ struct GDScriptUtilityFunctionsDefinitions {
} }
static inline void print_stack(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { static inline void print_stack(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
VALIDATE_ARG_COUNT(0); DEBUG_VALIDATE_ARG_COUNT(0, 0);
if (Thread::get_caller_id() != Thread::get_main_id()) { if (Thread::get_caller_id() != Thread::get_main_id()) {
print_line("Cannot retrieve debug info outside the main thread. Thread ID: " + itos(Thread::get_caller_id())); print_line("Cannot retrieve debug info outside the main thread. Thread ID: " + itos(Thread::get_caller_id()));
return; return;
@ -449,7 +366,8 @@ struct GDScriptUtilityFunctionsDefinitions {
} }
static inline void get_stack(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { static inline void get_stack(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
VALIDATE_ARG_COUNT(0); DEBUG_VALIDATE_ARG_COUNT(0, 0);
if (Thread::get_caller_id() != Thread::get_main_id()) { if (Thread::get_caller_id() != Thread::get_main_id()) {
*r_ret = TypedArray<Dictionary>(); *r_ret = TypedArray<Dictionary>();
return; return;
@ -468,7 +386,7 @@ struct GDScriptUtilityFunctionsDefinitions {
} }
static inline void len(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { static inline void len(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
VALIDATE_ARG_COUNT(1); DEBUG_VALIDATE_ARG_COUNT(1, 1);
switch (p_args[0]->get_type()) { switch (p_args[0]->get_type()) {
case Variant::STRING: case Variant::STRING:
case Variant::STRING_NAME: { case Variant::STRING_NAME: {
@ -524,56 +442,34 @@ struct GDScriptUtilityFunctionsDefinitions {
*r_ret = d.size(); *r_ret = d.size();
} break; } break;
default: { default: {
*r_ret = vformat(RTR("Value of type '%s' can't provide a length."), Variant::get_type_name(p_args[0]->get_type()));
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0; r_error.argument = 0;
r_error.expected = Variant::NIL; r_error.expected = Variant::NIL;
*r_ret = vformat(RTR("Value of type '%s' can't provide a length."), Variant::get_type_name(p_args[0]->get_type())); } break;
}
} }
} }
static inline void is_instance_of(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) { static inline void is_instance_of(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
VALIDATE_ARG_COUNT(2); DEBUG_VALIDATE_ARG_COUNT(2, 2);
if (p_args[1]->get_type() == Variant::INT) { if (p_args[1]->get_type() == Variant::INT) {
int builtin_type = *p_args[1]; int builtin_type = *p_args[1];
if (builtin_type < 0 || builtin_type >= Variant::VARIANT_MAX) { DEBUG_VALIDATE_ARG_CUSTOM(1, Variant::NIL, builtin_type < 0 || builtin_type >= Variant::VARIANT_MAX,
*r_ret = RTR("Invalid type argument for is_instance_of(), use TYPE_* constants for built-in types."); RTR("Invalid type argument for is_instance_of(), use TYPE_* constants for built-in types."));
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 1;
r_error.expected = Variant::NIL;
return;
}
*r_ret = p_args[0]->get_type() == builtin_type; *r_ret = p_args[0]->get_type() == builtin_type;
return; return;
} }
bool was_type_freed = false; bool was_type_freed = false;
Object *type_object = p_args[1]->get_validated_object_with_check(was_type_freed); Object *type_object = p_args[1]->get_validated_object_with_check(was_type_freed);
if (was_type_freed) { VALIDATE_ARG_CUSTOM(1, Variant::NIL, was_type_freed, RTR("Type argument is a previously freed instance."));
*r_ret = RTR("Type argument is a previously freed instance."); VALIDATE_ARG_CUSTOM(1, Variant::NIL, !type_object,
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT; RTR("Invalid type argument for is_instance_of(), should be a TYPE_* constant, a class or a script."));
r_error.argument = 1;
r_error.expected = Variant::NIL;
return;
}
if (!type_object) {
*r_ret = RTR("Invalid type argument for is_instance_of(), should be a TYPE_* constant, a class or a script.");
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 1;
r_error.expected = Variant::NIL;
return;
}
bool was_value_freed = false; bool was_value_freed = false;
Object *value_object = p_args[0]->get_validated_object_with_check(was_value_freed); Object *value_object = p_args[0]->get_validated_object_with_check(was_value_freed);
if (was_value_freed) { VALIDATE_ARG_CUSTOM(0, Variant::NIL, was_value_freed, RTR("Value argument is a previously freed instance."));
*r_ret = RTR("Value argument is a previously freed instance.");
r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
r_error.argument = 0;
r_error.expected = Variant::NIL;
return;
}
if (!value_object) { if (!value_object) {
*r_ret = false; *r_ret = false;
return; return;
@ -618,113 +514,77 @@ struct GDScriptUtilityFunctionInfo {
static OAHashMap<StringName, GDScriptUtilityFunctionInfo> utility_function_table; static OAHashMap<StringName, GDScriptUtilityFunctionInfo> utility_function_table;
static List<StringName> utility_function_name_table; static List<StringName> utility_function_name_table;
static void _register_function(const String &p_name, const MethodInfo &p_method_info, GDScriptUtilityFunctions::FunctionPtr p_function, bool p_is_const) { static void _register_function(const StringName &p_name, const MethodInfo &p_method_info, GDScriptUtilityFunctions::FunctionPtr p_function, bool p_is_const) {
StringName sname(p_name); ERR_FAIL_COND(utility_function_table.has(p_name));
ERR_FAIL_COND(utility_function_table.has(sname));
GDScriptUtilityFunctionInfo function; GDScriptUtilityFunctionInfo function;
function.function = p_function; function.function = p_function;
function.info = p_method_info; function.info = p_method_info;
function.is_constant = p_is_const; function.is_constant = p_is_const;
utility_function_table.insert(sname, function); utility_function_table.insert(p_name, function);
utility_function_name_table.push_back(sname); utility_function_name_table.push_back(p_name);
} }
#define REGISTER_FUNC(m_func, m_is_const, m_return_type, ...) \ #define REGISTER_FUNC(m_func, m_is_const, m_return, m_args, m_is_vararg, m_default_args) \
{ \ { \
String name(#m_func); \ String name(#m_func); \
if (name.begins_with("_")) { \ if (name.begins_with("_")) { \
name = name.substr(1, name.length() - 1); \ name = name.substr(1); \
} \ } \
MethodInfo info = MethodInfo(name, __VA_ARGS__); \ MethodInfo info = m_args; \
info.return_val.type = m_return_type; \ info.name = name; \
_register_function(name, info, GDScriptUtilityFunctionsDefinitions::m_func, m_is_const); \ info.return_val = m_return; \
} info.default_arguments = m_default_args; \
if (m_is_vararg) { \
#define REGISTER_FUNC_NO_ARGS(m_func, m_is_const, m_return_type) \
{ \
String name(#m_func); \
if (name.begins_with("_")) { \
name = name.substr(1, name.length() - 1); \
} \
MethodInfo info = MethodInfo(name); \
info.return_val.type = m_return_type; \
_register_function(name, info, GDScriptUtilityFunctionsDefinitions::m_func, m_is_const); \
}
#define REGISTER_VARARG_FUNC(m_func, m_is_const, m_return_type) \
{ \
String name(#m_func); \
if (name.begins_with("_")) { \
name = name.substr(1, name.length() - 1); \
} \
MethodInfo info = MethodInfo(name); \
info.return_val.type = m_return_type; \
info.flags |= METHOD_FLAG_VARARG; \ info.flags |= METHOD_FLAG_VARARG; \
} \
_register_function(name, info, GDScriptUtilityFunctionsDefinitions::m_func, m_is_const); \ _register_function(name, info, GDScriptUtilityFunctionsDefinitions::m_func, m_is_const); \
} }
#define REGISTER_VARIANT_FUNC(m_func, m_is_const, ...) \ #define RET(m_type) \
{ \ PropertyInfo(Variant::m_type, "")
String name(#m_func); \
if (name.begins_with("_")) { \
name = name.substr(1, name.length() - 1); \
} \
MethodInfo info = MethodInfo(name, __VA_ARGS__); \
info.return_val.type = Variant::NIL; \
info.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT; \
_register_function(name, info, GDScriptUtilityFunctionsDefinitions::m_func, m_is_const); \
}
#define REGISTER_CLASS_FUNC(m_func, m_is_const, m_return_type, ...) \ #define RETVAR \
{ \ PropertyInfo(Variant::NIL, "", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)
String name(#m_func); \
if (name.begins_with("_")) { \
name = name.substr(1, name.length() - 1); \
} \
MethodInfo info = MethodInfo(name, __VA_ARGS__); \
info.return_val.type = Variant::OBJECT; \
info.return_val.hint = PROPERTY_HINT_RESOURCE_TYPE; \
info.return_val.class_name = m_return_type; \
_register_function(name, info, GDScriptUtilityFunctionsDefinitions::m_func, m_is_const); \
}
#define REGISTER_FUNC_DEF(m_func, m_is_const, m_default, m_return_type, ...) \ #define RETCLS(m_class) \
{ \ PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, m_class)
String name(#m_func); \
if (name.begins_with("_")) { \ #define NOARGS \
name = name.substr(1, name.length() - 1); \ MethodInfo()
} \
MethodInfo info = MethodInfo(name, __VA_ARGS__); \ #define ARGS(...) \
info.return_val.type = m_return_type; \ MethodInfo("", __VA_ARGS__)
info.default_arguments.push_back(m_default); \
_register_function(name, info, GDScriptUtilityFunctionsDefinitions::m_func, m_is_const); \
}
#define ARG(m_name, m_type) \ #define ARG(m_name, m_type) \
PropertyInfo(m_type, m_name) PropertyInfo(Variant::m_type, m_name)
#define VARARG(m_name) \ #define ARGVAR(m_name) \
PropertyInfo(Variant::NIL, m_name, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_NIL_IS_VARIANT) PropertyInfo(Variant::NIL, m_name, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NIL_IS_VARIANT)
#define ARGTYPE(m_name) \
PropertyInfo(Variant::INT, m_name, PROPERTY_HINT_NONE, "", PROPERTY_USAGE_CLASS_IS_ENUM, "Variant.Type")
void GDScriptUtilityFunctions::register_functions() { void GDScriptUtilityFunctions::register_functions() {
/* clang-format off */
#ifndef DISABLE_DEPRECATED #ifndef DISABLE_DEPRECATED
REGISTER_VARIANT_FUNC(convert, true, VARARG("what"), ARG("type", Variant::INT)); REGISTER_FUNC( convert, true, RETVAR, ARGS( ARGVAR("what"), ARGTYPE("type") ), false, varray( ));
#endif // DISABLE_DEPRECATED #endif // DISABLE_DEPRECATED
REGISTER_FUNC(type_exists, true, Variant::BOOL, ARG("type", Variant::STRING_NAME)); REGISTER_FUNC( type_exists, true, RET(BOOL), ARGS( ARG("type", STRING_NAME) ), false, varray( ));
REGISTER_FUNC(_char, true, Variant::STRING, ARG("char", Variant::INT)); REGISTER_FUNC( _char, true, RET(STRING), ARGS( ARG("char", INT) ), false, varray( ));
REGISTER_VARARG_FUNC(range, false, Variant::ARRAY); REGISTER_FUNC( range, false, RET(ARRAY), NOARGS, true, varray( ));
REGISTER_CLASS_FUNC(load, false, "Resource", ARG("path", Variant::STRING)); REGISTER_FUNC( load, false, RETCLS("Resource"), ARGS( ARG("path", STRING) ), false, varray( ));
REGISTER_FUNC(inst_to_dict, false, Variant::DICTIONARY, ARG("instance", Variant::OBJECT)); REGISTER_FUNC( inst_to_dict, false, RET(DICTIONARY), ARGS( ARG("instance", OBJECT) ), false, varray( ));
REGISTER_FUNC(dict_to_inst, false, Variant::OBJECT, ARG("dictionary", Variant::DICTIONARY)); REGISTER_FUNC( dict_to_inst, false, RET(OBJECT), ARGS( ARG("dictionary", DICTIONARY) ), false, varray( ));
REGISTER_FUNC_DEF(Color8, true, 255, Variant::COLOR, ARG("r8", Variant::INT), ARG("g8", Variant::INT), ARG("b8", Variant::INT), ARG("a8", Variant::INT)); REGISTER_FUNC( Color8, true, RET(COLOR), ARGS( ARG("r8", INT), ARG("g8", INT),
REGISTER_VARARG_FUNC(print_debug, false, Variant::NIL); ARG("b8", INT), ARG("a8", INT) ), false, varray( 255 ));
REGISTER_FUNC_NO_ARGS(print_stack, false, Variant::NIL); REGISTER_FUNC( print_debug, false, RET(NIL), NOARGS, true, varray( ));
REGISTER_FUNC_NO_ARGS(get_stack, false, Variant::ARRAY); REGISTER_FUNC( print_stack, false, RET(NIL), NOARGS, false, varray( ));
REGISTER_FUNC(len, true, Variant::INT, VARARG("var")); REGISTER_FUNC( get_stack, false, RET(ARRAY), NOARGS, false, varray( ));
REGISTER_FUNC(is_instance_of, true, Variant::BOOL, VARARG("value"), VARARG("type")); REGISTER_FUNC( len, true, RET(INT), ARGS( ARGVAR("var") ), false, varray( ));
REGISTER_FUNC( is_instance_of, true, RET(BOOL), ARGS( ARGVAR("value"), ARGVAR("type") ), false, varray( ));
/* clang-format on */
} }
void GDScriptUtilityFunctions::unregister_functions() { void GDScriptUtilityFunctions::unregister_functions() {

View File

@ -0,0 +1,12 @@
func test():
const COLOR = Color8(255, 0.0, false)
var false_value := false
@warning_ignore("narrowing_conversion")
var color = Color8(255, 0.0, false_value)
print(var_to_str(COLOR))
print(var_to_str(color))
var string := "Node"
var string_name := &"Node"
print(type_exists(string))
print(type_exists(string_name))

View File

@ -0,0 +1,5 @@
GDTEST_OK
Color(1, 0, 0, 1)
Color(1, 0, 0, 1)
true
true