Skip to content

Commit

Permalink
GDScript: transparent funcrefs and _call
Browse files Browse the repository at this point in the history
  • Loading branch information
poke1024 committed Jan 19, 2018
1 parent 9f479f0 commit 65b54bf
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/func_ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ void FuncRef::_bind_methods() {
mi.name = "call_func";
Vector<Variant> defargs;
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "call_func", &FuncRef::call_func, mi, defargs);
ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "_call", &FuncRef::call_func, mi, defargs);
}

ClassDB::bind_method(D_METHOD("set_instance", "instance"), &FuncRef::set_instance);
Expand Down
9 changes: 9 additions & 0 deletions modules/gdscript/gdscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,14 @@ Variant GDScriptInstance::call(const StringName &p_method, const Variant **p_arg
Map<StringName, GDScriptFunction *>::Element *E = sptr->member_functions.find(p_method);
if (E) {
return E->get()->call(this, p_args, p_argcount, r_error);
} else {
Map<StringName, GDScript::MemberInfo>::Element *M = sptr->member_indices.find(p_method);
if (M) {
Variant method_object = members[M->get().index];
if (method_object.get_type() == Variant::OBJECT) {
return method_object.call(GDScriptLanguage::get_singleton()->strings._call, p_args, p_argcount, r_error);
}
}
}
sptr = sptr->_base;
}
Expand Down Expand Up @@ -1785,6 +1793,7 @@ GDScriptLanguage::GDScriptLanguage() {
strings._notification = StaticCString::create("_notification");
strings._set = StaticCString::create("_set");
strings._get = StaticCString::create("_get");
strings._call = StaticCString::create("_call");
strings._get_property_list = StaticCString::create("_get_property_list");
strings._script_source = StaticCString::create("script/source");
_debug_parse_err_line = -1;
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ class GDScriptLanguage : public ScriptLanguage {
StringName _notification;
StringName _set;
StringName _get;
StringName _call;
StringName _get_property_list;
StringName _script_source;

Expand Down
8 changes: 7 additions & 1 deletion modules/gdscript/gdscript_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,13 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
return -1;
}
GDScriptParser::IdentifierNode *id = static_cast<GDScriptParser::IdentifierNode *>(on->arguments[i]);
ret = codegen.get_name_map_pos(id->name);

if (instance->type == GDScriptParser::Node::TYPE_SELF && static_cast<const GDScriptParser::SelfNode *>(instance)->implicit && codegen.stack_identifiers.has(id->name)) {
arguments[0] = codegen.stack_identifiers[id->name] | (GDScriptFunction::ADDR_TYPE_STACK << GDScriptFunction::ADDR_BITS);
ret = codegen.get_name_map_pos("_call");
} else {
ret = codegen.get_name_map_pos(id->name);
}

} else {

Expand Down
15 changes: 15 additions & 0 deletions modules/gdscript/gdscript_function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "gdscript_function.h"

#include "func_ref.h"
#include "gdscript.h"
#include "gdscript_functions.h"
#include "os/os.h"
Expand Down Expand Up @@ -603,6 +604,20 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
#else
*dst = src->get_named(*index, &valid);
#endif
if (!valid && src->get_type() == Variant::OBJECT) {
Object *obj = *src;
if (obj->has_method(*index)) {
Ref<FuncRef> fr = memnew(FuncRef);
fr->set_instance(obj);
fr->set_function(*index);
valid = true;
#ifdef DEBUG_ENABLED
ret = fr;
#else
*dst = fr;
#endif
}
}
#ifdef DEBUG_ENABLED
if (!valid) {
if (src->has_method(*index)) {
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ GDScriptParser::Node *GDScriptParser::_parse_expression(Node *p_parent, bool p_s
} else {

SelfNode *self = alloc_node<SelfNode>();
self->implicit = true;
op->arguments.push_back(self);

StringName identifier;
Expand Down
6 changes: 5 additions & 1 deletion modules/gdscript/gdscript_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ class GDScriptParser {
};

struct SelfNode : public Node {
SelfNode() { type = TYPE_SELF; }
bool implicit;
SelfNode() {
type = TYPE_SELF;
implicit = false;
}
};

struct OperatorNode : public Node {
Expand Down

0 comments on commit 65b54bf

Please sign in to comment.