Skip to content

Commit

Permalink
Merge pull request #87294 from vnen/allow-free-callable
Browse files Browse the repository at this point in the history
Allow `free()` to be used as Callable
  • Loading branch information
akien-mga committed Jan 18, 2024
2 parents 33f3511 + b4e08eb commit baf87e2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions core/object/class_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "class_db.h"

#include "core/config/engine.h"
#include "core/core_string_names.h"
#include "core/io/resource_loader.h"
#include "core/object/script_language.h"
#include "core/os/mutex.h"
Expand Down Expand Up @@ -1299,6 +1300,12 @@ bool ClassDB::get_property(Object *p_object, const StringName &p_property, Varia
check = check->inherits_ptr;
}

// The "free()" method is special, so we assume it exists and return a Callable.
if (p_property == CoreStringNames::get_singleton()->_free) {
r_value = Callable(p_object, p_property);
return true;
}

return false;
}

Expand Down
3 changes: 2 additions & 1 deletion modules/gdscript/gdscript_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

#include "core/config/engine.h"
#include "core/config/project_settings.h"
#include "core/core_string_names.h"

bool GDScriptCompiler::_is_class_member_property(CodeGen &codegen, const StringName &p_name) {
if (codegen.function_node && codegen.function_node->is_static) {
Expand Down Expand Up @@ -345,7 +346,7 @@ GDScriptCodeGenerator::Address GDScriptCompiler::_parse_expression(CodeGen &code
scr = scr->_base;
}

if (nc && (ClassDB::has_signal(nc->get_name(), identifier) || ClassDB::has_method(nc->get_name(), identifier))) {
if (nc && (identifier == CoreStringNames::get_singleton()->_free || ClassDB::has_signal(nc->get_name(), identifier) || ClassDB::has_method(nc->get_name(), identifier))) {
// Get like it was a property.
GDScriptCodeGenerator::Address temp = codegen.add_temporary(); // TODO: Get type here.
GDScriptCodeGenerator::Address self(GDScriptCodeGenerator::Address::SELF);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
func test():
var node := Node.new()
var callable: Callable = node.free
callable.call()
print(node)

node = Node.new()
callable = node["free"]
callable.call()
print(node)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
GDTEST_OK
<Freed Object>
<Freed Object>

0 comments on commit baf87e2

Please sign in to comment.