Skip to content

Commit

Permalink
Use custom callables for GDScript static methods
Browse files Browse the repository at this point in the history
Fixes #79521
  • Loading branch information
maiself committed Oct 3, 2023
1 parent f5696c3 commit f3566bc
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 1 deletion.
3 changes: 2 additions & 1 deletion modules/gdscript/gdscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "gdscript_compiler.h"
#include "gdscript_parser.h"
#include "gdscript_rpc_callable.h"
#include "gdscript_static_callable.h"
#include "gdscript_warning.h"

#ifdef TOOLS_ENABLED
Expand Down Expand Up @@ -886,7 +887,7 @@ bool GDScript::_get(const StringName &p_name, Variant &r_ret) const {
if (top->rpc_config.has(p_name)) {
r_ret = Callable(memnew(GDScriptRPCCallable(const_cast<GDScript *>(top), E->key)));
} else {
r_ret = Callable(const_cast<GDScript *>(top), E->key);
r_ret = Callable(memnew(GDScriptStaticCallable(const_cast<GDScript *>(top), E->key)));
}
return true;
}
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class GDScript : public Script {
friend class GDScriptCompiler;
friend class GDScriptDocGen;
friend class GDScriptLanguage;
friend class GDScriptStaticCallable;
friend struct GDScriptUtilityFunctionsDefinitions;

Ref<GDScriptNativeClass> native;
Expand Down
86 changes: 86 additions & 0 deletions modules/gdscript/gdscript_static_callable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**************************************************************************/
/* gdscript_static_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 "gdscript_static_callable.h"

#include "gdscript.h"

#include "core/templates/hashfuncs.h"

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

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

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

String GDScriptStaticCallable::get_as_text() const {
String class_name = script->get_class();
if (script->get_path().is_resource_file()) {
class_name += "(" + script->get_path().get_file() + ")";
}
return class_name + "::" + String(method);
}

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

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

bool GDScriptStaticCallable::is_valid() const {
return script.is_valid() && script->has_method(method);
}

ObjectID GDScriptStaticCallable::get_object() const {
return script->get_instance_id();
}

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

void GDScriptStaticCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
r_return_value = script->callp(method, p_arguments, p_argcount, r_call_error);
}

GDScriptStaticCallable::GDScriptStaticCallable(const Ref<GDScript> &p_script, const StringName &p_method) {
script = p_script;
method = p_method;
h = method.hash();
h = hash_murmur3_one_64(script->get_instance_id(), h);
}
62 changes: 62 additions & 0 deletions modules/gdscript/gdscript_static_callable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**************************************************************************/
/* gdscript_static_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 GDSCRIPT_STATIC_CALLABLE_H
#define GDSCRIPT_STATIC_CALLABLE_H

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

class GDScript;

class GDScriptStaticCallable : public CallableCustom {
Ref<GDScript> script;
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;
ObjectID get_object() const override;
StringName get_method() const override;
void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override;

GDScriptStaticCallable(const Ref<GDScript> &p_script, const StringName &p_method);
virtual ~GDScriptStaticCallable() = default;
};

#endif // GDSCRIPT_STATIC_CALLABLE_H

0 comments on commit f3566bc

Please sign in to comment.