Skip to content

Commit

Permalink
Core: Allow methods of built-in Variant types to be used as Callables
Browse files Browse the repository at this point in the history
  • Loading branch information
dalexeev committed Oct 25, 2023
1 parent 06d5189 commit b042636
Show file tree
Hide file tree
Showing 9 changed files with 230 additions and 72 deletions.
1 change: 1 addition & 0 deletions core/variant/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ class Variant {
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 MethodInfo get_builtin_method_info(Variant::Type p_type, const StringName &p_method);
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);
Expand Down
85 changes: 48 additions & 37 deletions core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,46 @@ struct VariantBuiltInMethodInfo {
Variant::Type return_type;
int argument_count = 0;
Variant::Type (*get_argument_type)(int p_arg) = nullptr;

MethodInfo get_method_info(const StringName &p_name) const {
MethodInfo mi;
mi.name = p_name;

if (has_return_type) {
mi.return_val.type = return_type;
if (mi.return_val.type == Variant::NIL) {
mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
}
}

if (is_const) {
mi.flags |= METHOD_FLAG_CONST;
}
if (is_vararg) {
mi.flags |= METHOD_FLAG_VARARG;
}
if (is_static) {
mi.flags |= METHOD_FLAG_STATIC;
}

for (int i = 0; i < argument_count; i++) {
PropertyInfo pi;
#ifdef DEBUG_METHODS_ENABLED
pi.name = argument_names[i];
#else
pi.name = "arg" + itos(i + 1);
#endif
pi.type = (*get_argument_type)(i);
if (pi.type == Variant::NIL) {
pi.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
}
mi.arguments.push_back(pi);
}

mi.default_arguments = default_arguments;

return mi;
}
};

typedef OAHashMap<StringName, VariantBuiltInMethodInfo> BuiltinMethodMap;
Expand Down Expand Up @@ -1268,6 +1308,13 @@ Variant::PTRBuiltInMethod Variant::get_ptr_builtin_method(Variant::Type p_type,
return method->ptrcall;
}

MethodInfo Variant::get_builtin_method_info(Variant::Type p_type, const StringName &p_method) {
ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, MethodInfo());
const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method);
ERR_FAIL_NULL_V(method, MethodInfo());
return method->get_method_info(p_method);
}

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);
Expand Down Expand Up @@ -1378,43 +1425,7 @@ void Variant::get_method_list(List<MethodInfo> *p_list) const {
for (const StringName &E : builtin_method_names[type]) {
const VariantBuiltInMethodInfo *method = builtin_method_info[type].lookup_ptr(E);
ERR_CONTINUE(!method);

MethodInfo mi;
mi.name = E;

//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;
}
}

if (method->is_const) {
mi.flags |= METHOD_FLAG_CONST;
}
if (method->is_vararg) {
mi.flags |= METHOD_FLAG_VARARG;
}
if (method->is_static) {
mi.flags |= METHOD_FLAG_STATIC;
}
for (int i = 0; i < method->argument_count; i++) {
PropertyInfo pi;
#ifdef DEBUG_METHODS_ENABLED
pi.name = method->argument_names[i];
#else
pi.name = "arg" + itos(i + 1);
#endif
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 = method->default_arguments;
p_list->push_back(mi);
p_list->push_back(method->get_method_info(E));
}
}
}
Expand Down
81 changes: 81 additions & 0 deletions core/variant/variant_callable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**************************************************************************/
/* variant_callable.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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_callable.h"

#include "core/templates/hashfuncs.h"

bool VariantCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {
return p_a->hash() == p_b->hash();
}

bool VariantCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {
return p_a->hash() < p_b->hash();
}

uint32_t VariantCallable::hash() const {
return h;
}

String VariantCallable::get_as_text() const {
return vformat("%s::%s (Callable)", Variant::get_type_name(variant.get_type()), method);
}

CallableCustom::CompareEqualFunc VariantCallable::get_compare_equal_func() const {
return compare_equal;
}

CallableCustom::CompareLessFunc VariantCallable::get_compare_less_func() const {
return compare_less;
}

bool VariantCallable::is_valid() const {
return Variant::has_builtin_method(variant.get_type(), method);
}

StringName VariantCallable::get_method() const {
return method;
}

ObjectID VariantCallable::get_object() const {
return ObjectID();
}

void VariantCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
Variant v = variant;
v.callp(method, p_arguments, p_argcount, r_return_value, r_call_error);
}

VariantCallable::VariantCallable(const Variant &p_variant, const StringName &p_method) {
variant = p_variant;
method = p_method;
h = variant.hash();
h = hash_murmur3_one_64(Variant::get_builtin_method_hash(variant.get_type(), method), h);
}
58 changes: 58 additions & 0 deletions core/variant/variant_callable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**************************************************************************/
/* variant_callable.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/

#ifndef VARIANT_CALLABLE_H
#define VARIANT_CALLABLE_H

#include "core/variant/callable.h"
#include "core/variant/variant.h"

class VariantCallable : public CallableCustom {
Variant variant;
StringName method;
uint32_t h = 0;

static bool compare_equal(const CallableCustom *p_a, const CallableCustom *p_b);
static bool compare_less(const CallableCustom *p_a, const CallableCustom *p_b);

public:
uint32_t hash() const override;
String get_as_text() const override;
CompareEqualFunc get_compare_equal_func() const override;
CompareLessFunc get_compare_less_func() const override;
bool is_valid() const override;
StringName get_method() const override;
ObjectID get_object() const override;
void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override;

VariantCallable(const Variant &p_variant, const StringName &p_method);
};

#endif // VARIANT_CALLABLE_H
53 changes: 29 additions & 24 deletions core/variant/variant_setget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#include "variant_setget.h"

#include "variant_callable.h"

struct VariantSetterGetterInfo {
void (*setter)(Variant *base, const Variant *value, bool &valid);
void (*getter)(const Variant *base, Variant *value);
Expand Down Expand Up @@ -264,42 +266,45 @@ void Variant::set_named(const StringName &p_member, const Variant &p_value, bool
}

Variant Variant::get_named(const StringName &p_member, bool &r_valid) const {
Variant ret;
uint32_t s = variant_setters_getters[type].size();
if (s) {
for (uint32_t i = 0; i < s; i++) {
if (variant_setters_getters_names[type][i] == p_member) {
Variant ret;
variant_setters_getters[type][i].getter(this, &ret);
r_valid = true;
return ret;
}
}
}

r_valid = false;

} else if (type == Variant::OBJECT) {
Object *obj = get_validated_object();
if (!obj) {
r_valid = false;
return "Instance base is null.";
} else {
return obj->get(p_member, &r_valid);
}
} else if (type == Variant::DICTIONARY) {
const Variant *v = VariantGetInternalPtr<Dictionary>::get_ptr(this)->getptr(p_member);
if (v) {
r_valid = true;

return *v;
} else {
r_valid = false;
}

} else {
r_valid = false;
switch (type) {
case Variant::OBJECT: {
Object *obj = get_validated_object();
if (!obj) {
r_valid = false;
return "Instance base is null.";
} else {
return obj->get(p_member, &r_valid);
}
} break;
case Variant::DICTIONARY: {
const Variant *v = VariantGetInternalPtr<Dictionary>::get_ptr(this)->getptr(p_member);
if (v) {
r_valid = true;
return *v;
}
} break;
default: {
if (Variant::has_builtin_method(type, p_member)) {
r_valid = true;
return Callable(memnew(VariantCallable(*this, p_member)));
}
} break;
}

return ret;
r_valid = false;
return Variant();
}

/**** INDEXED SETTERS AND GETTERS ****/
Expand Down
11 changes: 0 additions & 11 deletions doc/classes/Callable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,6 @@
# Prints "Attack!", when the button_pressed signal is emitted.
button_pressed.connect(func(): print("Attack!"))
[/codeblock]
[b]Note:[/b] Methods of native types such as [Signal], [Array], or [Dictionary] are not of type [Callable] in order to avoid unnecessary overhead. If you need to pass those methods as [Callable], use a lambda function as a wrapper.
[codeblock]
func _init():
var my_dictionary = { "hello": "world" }

# This will not work, `clear` is not a callable.
create_tween().tween_callback(my_dictionary.clear)

# This will work, as lambdas are custom callables.
create_tween().tween_callback(func(): my_dictionary.clear())
[/codeblock]
</description>
<tutorials>
</tutorials>
Expand Down
4 changes: 4 additions & 0 deletions modules/gdscript/gdscript_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3650,6 +3650,10 @@ void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNod
return;
}
}
if (Variant::has_builtin_method(base.builtin_type, name)) {
p_identifier->set_datatype(make_callable_type(Variant::get_builtin_method_info(base.builtin_type, name)));
return;
}
if (base.is_hard_type()) {
#ifdef SUGGEST_GODOT4_RENAMES
String rename_hint = String();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
func test():
var array: Array = [1, 2, 3]
print(array)
var callable: Callable = array.clear
callable.call()
print(array)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GDTEST_OK
[1, 2, 3]
[]

0 comments on commit b042636

Please sign in to comment.