/*************************************************************************/ /* gdscript_function.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* 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_function.h" #include "gdscript.h" #include "gdscript_functions.h" #include "os/os.h" Variant *GDScriptFunction::_get_variant(int p_address, GDScriptInstance *p_instance, GDScript *p_script, Variant &self, Variant *p_stack, String &r_error) const { int address = p_address & ADDR_MASK; //sequential table (jump table generated by compiler) switch ((p_address & ADDR_TYPE_MASK) >> ADDR_BITS) { case ADDR_TYPE_SELF: { #ifdef DEBUG_ENABLED if (unlikely(!p_instance)) { r_error = "Cannot access self without instance."; return NULL; } #endif return &self; } break; case ADDR_TYPE_CLASS: { return &p_script->_static_ref; } break; case ADDR_TYPE_MEMBER: { #ifdef DEBUG_ENABLED if (unlikely(!p_instance)) { r_error = "Cannot access member without instance."; return NULL; } #endif //member indexing is O(1) return &p_instance->members.write[address]; } break; case ADDR_TYPE_CLASS_CONSTANT: { //todo change to index! GDScript *o = p_script; #ifdef DEBUG_ENABLED ERR_FAIL_INDEX_V(address, _global_names_count, NULL); #endif const StringName *sn = &_global_names_ptr[address]; while (o) { GDScript *s = o; while (s) { Map::Element *E = s->constants.find(*sn); if (E) { return &E->get(); } s = s->_base; } o = o->_owner; } ERR_EXPLAIN("GDScriptCompiler bug..."); ERR_FAIL_V(NULL); } break; case ADDR_TYPE_LOCAL_CONSTANT: { #ifdef DEBUG_ENABLED ERR_FAIL_INDEX_V(address, _constant_count, NULL); #endif return &_constants_ptr[address]; } break; case ADDR_TYPE_STACK: case ADDR_TYPE_STACK_VARIABLE: { #ifdef DEBUG_ENABLED ERR_FAIL_INDEX_V(address, _stack_size, NULL); #endif return &p_stack[address]; } break; case ADDR_TYPE_GLOBAL: { #ifdef DEBUG_ENABLED ERR_FAIL_INDEX_V(address, GDScriptLanguage::get_singleton()->get_global_array_size(), NULL); #endif return &GDScriptLanguage::get_singleton()->get_global_array()[address]; } break; #ifdef TOOLS_ENABLED case ADDR_TYPE_NAMED_GLOBAL: { #ifdef DEBUG_ENABLED ERR_FAIL_INDEX_V(address, _named_globals_count, NULL); #endif StringName id = _named_globals_ptr[address]; if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(id)) { return (Variant *)&GDScriptLanguage::get_singleton()->get_named_globals_map()[id]; } else { r_error = "Autoload singleton '" + String(id) + "' has been removed."; return NULL; } } break; #endif case ADDR_TYPE_NIL: { return &nil; } break; } ERR_EXPLAIN("Bad Code! (Addressing Mode)"); ERR_FAIL_V(NULL); return NULL; } String GDScriptFunction::_get_call_error(const Variant::CallError &p_err, const String &p_where, const Variant **argptrs) const { String err_text; if (p_err.error == Variant::CallError::CALL_ERROR_INVALID_ARGUMENT) { int errorarg = p_err.argument; err_text = "Invalid type in " + p_where + ". Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(p_err.expected) + "."; } else if (p_err.error == Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) { err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments."; } else if (p_err.error == Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) { err_text = "Invalid call to " + p_where + ". Expected " + itos(p_err.argument) + " arguments."; } else if (p_err.error == Variant::CallError::CALL_ERROR_INVALID_METHOD) { err_text = "Invalid call. Nonexistent " + p_where + "."; } else if (p_err.error == Variant::CallError::CALL_ERROR_INSTANCE_IS_NULL) { err_text = "Attempt to call " + p_where + " on a null instance."; } else { err_text = "Bug, call error: #" + itos(p_err.error); } return err_text; } static String _get_var_type(const Variant *p_type) { String basestr; if (p_type->get_type() == Variant::OBJECT) { Object *bobj = *p_type; if (!bobj) { basestr = "null instance"; } else { #ifdef DEBUG_ENABLED if (ObjectDB::instance_validate(bobj)) { if (bobj->get_script_instance()) basestr = bobj->get_class() + " (" + bobj->get_script_instance()->get_script()->get_path().get_file() + ")"; else basestr = bobj->get_class(); } else { basestr = "previously freed instance"; } #else basestr = "Object"; #endif } } else { basestr = Variant::get_type_name(p_type->get_type()); } return basestr; } #if defined(__GNUC__) #define OPCODES_TABLE \ static const void *switch_table_ops[] = { \ &&OPCODE_OPERATOR, \ &&OPCODE_EXTENDS_TEST, \ &&OPCODE_SET, \ &&OPCODE_GET, \ &&OPCODE_SET_NAMED, \ &&OPCODE_GET_NAMED, \ &&OPCODE_SET_MEMBER, \ &&OPCODE_GET_MEMBER, \ &&OPCODE_ASSIGN, \ &&OPCODE_ASSIGN_TRUE, \ &&OPCODE_ASSIGN_FALSE, \ &&OPCODE_ASSIGN_TYPED_BUILTIN, \ &&OPCODE_ASSIGN_TYPED_NATIVE, \ &&OPCODE_ASSIGN_TYPED_SCRIPT, \ &&OPCODE_CAST_TO_BUILTIN, \ &&OPCODE_CAST_TO_NATIVE, \ &&OPCODE_CAST_TO_SCRIPT, \ &&OPCODE_CONSTRUCT, \ &&OPCODE_CONSTRUCT_ARRAY, \ &&OPCODE_CONSTRUCT_DICTIONARY, \ &&OPCODE_CALL, \ &&OPCODE_CALL_RETURN, \ &&OPCODE_CALL_BUILT_IN, \ &&OPCODE_CALL_SELF, \ &&OPCODE_CALL_SELF_BASE, \ &&OPCODE_YIELD, \ &&OPCODE_YIELD_SIGNAL, \ &&OPCODE_YIELD_RESUME, \ &&OPCODE_JUMP, \ &&OPCODE_JUMP_IF, \ &&OPCODE_JUMP_IF_NOT, \ &&OPCODE_JUMP_TO_DEF_ARGUMENT, \ &&OPCODE_RETURN, \ &&OPCODE_ITERATE_BEGIN, \ &&OPCODE_ITERATE, \ &&OPCODE_ASSERT, \ &&OPCODE_BREAKPOINT, \ &&OPCODE_LINE, \ &&OPCODE_END \ }; #define OPCODE(m_op) \ m_op: #define OPCODE_WHILE(m_test) #define OPCODES_END \ OPSEXIT: #define OPCODES_OUT \ OPSOUT: #define DISPATCH_OPCODE goto *switch_table_ops[_code_ptr[ip]] #define OPCODE_SWITCH(m_test) DISPATCH_OPCODE; #define OPCODE_BREAK goto OPSEXIT #define OPCODE_OUT goto OPSOUT #else #define OPCODES_TABLE #define OPCODE(m_op) case m_op: #define OPCODE_WHILE(m_test) while (m_test) #define OPCODES_END #define OPCODES_OUT #define DISPATCH_OPCODE continue #define OPCODE_SWITCH(m_test) switch (m_test) #define OPCODE_BREAK break #define OPCODE_OUT break #endif Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_args, int p_argcount, Variant::CallError &r_err, CallState *p_state) { OPCODES_TABLE; if (!_code_ptr) { return Variant(); } r_err.error = Variant::CallError::CALL_OK; Variant self; Variant retvalue; Variant *stack = NULL; Variant **call_args; int defarg = 0; #ifdef DEBUG_ENABLED //GDScriptLanguage::get_singleton()->calls++; #endif uint32_t alloca_size = 0; GDScript *_class; int ip = 0; int line = _initial_line; if (p_state) { //use existing (supplied) state (yielded) stack = (Variant *)p_state->stack.ptr(); call_args = (Variant **)&p_state->stack.ptr()[sizeof(Variant) * p_state->stack_size]; //ptr() to avoid bounds check line = p_state->line; ip = p_state->ip; alloca_size = p_state->stack.size(); _class = p_state->_class; p_instance = p_state->instance; defarg = p_state->defarg; self = p_state->self; //stack[p_state->result_pos]=p_state->result; //assign stack with result } else { if (p_argcount != _argument_count) { if (p_argcount > _argument_count) { r_err.error = Variant::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; r_err.argument = _argument_count; return Variant(); } else if (p_argcount < _argument_count - _default_arg_count) { r_err.error = Variant::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; r_err.argument = _argument_count - _default_arg_count; return Variant(); } else { defarg = _argument_count - p_argcount; } } alloca_size = sizeof(Variant *) * _call_size + sizeof(Variant) * _stack_size; if (alloca_size) { uint8_t *aptr = (uint8_t *)alloca(alloca_size); if (_stack_size) { stack = (Variant *)aptr; for (int i = 0; i < p_argcount; i++) { if (!argument_types[i].has_type) { memnew_placement(&stack[i], Variant(*p_args[i])); continue; } if (!argument_types[i].is_type(*p_args[i], true)) { r_err.error = Variant::CallError::CALL_ERROR_INVALID_ARGUMENT; r_err.argument = i; r_err.expected = argument_types[i].kind == GDScriptDataType::BUILTIN ? argument_types[i].builtin_type : Variant::OBJECT; return Variant(); } if (argument_types[i].kind == GDScriptDataType::BUILTIN) { Variant arg = Variant::construct(argument_types[i].builtin_type, &p_args[i], 1, r_err); memnew_placement(&stack[i], Variant(arg)); } else { memnew_placement(&stack[i], Variant(*p_args[i])); } } for (int i = p_argcount; i < _stack_size; i++) { memnew_placement(&stack[i], Variant); } } else { stack = NULL; } if (_call_size) { call_args = (Variant **)&aptr[sizeof(Variant) * _stack_size]; } else { call_args = NULL; } } else { stack = NULL; call_args = NULL; } if (p_instance) { if (p_instance->base_ref && static_cast(p_instance->owner)->is_referenced()) { self = REF(static_cast(p_instance->owner)); } else { self = p_instance->owner; } _class = p_instance->script.ptr(); } else { _class = _script; } } String err_text; #ifdef DEBUG_ENABLED if (ScriptDebugger::get_singleton()) GDScriptLanguage::get_singleton()->enter_function(p_instance, this, stack, &ip, &line); #define GD_ERR_BREAK(m_cond) \ { \ if (unlikely(m_cond)) { \ _err_print_error(FUNCTION_STR, __FILE__, __LINE__, "Condition ' " _STR(m_cond) " ' is true. Breaking..:"); \ OPCODE_BREAK; \ } else \ _err_error_exists = false; \ } #define CHECK_SPACE(m_space) \ GD_ERR_BREAK((ip + m_space) > _code_size) #define GET_VARIANT_PTR(m_v, m_code_ofs) \ Variant *m_v; \ m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, _class, self, stack, err_text); \ if (unlikely(!m_v)) \ OPCODE_BREAK; #else #define GD_ERR_BREAK(m_cond) #define CHECK_SPACE(m_space) #define GET_VARIANT_PTR(m_v, m_code_ofs) \ Variant *m_v; \ m_v = _get_variant(_code_ptr[ip + m_code_ofs], p_instance, _class, self, stack, err_text); #endif #ifdef DEBUG_ENABLED uint64_t function_start_time = 0; uint64_t function_call_time = 0; if (GDScriptLanguage::get_singleton()->profiling) { function_start_time = OS::get_singleton()->get_ticks_usec(); function_call_time = 0; profile.call_count++; profile.frame_call_count++; } #endif bool exit_ok = false; #ifdef DEBUG_ENABLED OPCODE_WHILE(ip < _code_size) { int last_opcode = _code_ptr[ip]; #else OPCODE_WHILE(true) { #endif OPCODE_SWITCH(_code_ptr[ip]) { OPCODE(OPCODE_OPERATOR) { CHECK_SPACE(5); bool valid; Variant::Operator op = (Variant::Operator)_code_ptr[ip + 1]; GD_ERR_BREAK(op >= Variant::OP_MAX); GET_VARIANT_PTR(a, 2); GET_VARIANT_PTR(b, 3); GET_VARIANT_PTR(dst, 4); #ifdef DEBUG_ENABLED Variant ret; Variant::evaluate(op, *a, *b, ret, valid); #else Variant::evaluate(op, *a, *b, *dst, valid); #endif #ifdef DEBUG_ENABLED if (!valid) { if (ret.get_type() == Variant::STRING) { //return a string when invalid with the error err_text = ret; err_text += " in operator '" + Variant::get_operator_name(op) + "'."; } else { err_text = "Invalid operands '" + Variant::get_type_name(a->get_type()) + "' and '" + Variant::get_type_name(b->get_type()) + "' in operator '" + Variant::get_operator_name(op) + "'."; } OPCODE_BREAK; } *dst = ret; #endif ip += 5; } DISPATCH_OPCODE; OPCODE(OPCODE_EXTENDS_TEST) { CHECK_SPACE(4); GET_VARIANT_PTR(a, 1); GET_VARIANT_PTR(b, 2); GET_VARIANT_PTR(dst, 3); #ifdef DEBUG_ENABLED if (a->get_type() != Variant::OBJECT || a->operator Object *() == NULL) { err_text = "Left operand of 'is' is not an instance of anything."; OPCODE_BREAK; } if (b->get_type() != Variant::OBJECT || b->operator Object *() == NULL) { err_text = "Right operand of 'is' is not a class."; OPCODE_BREAK; } #endif Object *obj_A = *a; Object *obj_B = *b; GDScript *scr_B = Object::cast_to(obj_B); bool extends_ok = false; if (scr_B) { //if B is a script, the only valid condition is that A has an instance which inherits from the script //in other situation, this shoul return false. if (obj_A->get_script_instance() && obj_A->get_script_instance()->get_language() == GDScriptLanguage::get_singleton()) { GDScript *cmp = static_cast(obj_A->get_script_instance()->get_script().ptr()); //bool found=false; while (cmp) { if (cmp == scr_B) { //inherits from script, all ok extends_ok = true; break; } cmp = cmp->_base; } } } else { GDScriptNativeClass *nc = Object::cast_to(obj_B); #ifdef DEBUG_ENABLED if (!nc) { err_text = "Right operand of 'is' is not a class (type: '" + obj_B->get_class() + "')."; OPCODE_BREAK; } #endif extends_ok = ClassDB::is_parent_class(obj_A->get_class_name(), nc->get_name()); } *dst = extends_ok; ip += 4; } DISPATCH_OPCODE; OPCODE(OPCODE_SET) { CHECK_SPACE(3); GET_VARIANT_PTR(dst, 1); GET_VARIANT_PTR(index, 2); GET_VARIANT_PTR(value, 3); bool valid; dst->set(*index, *value, &valid); #ifdef DEBUG_ENABLED if (!valid) { String v = index->operator String(); if (v != "") { v = "'" + v + "'"; } else { v = "of type '" + _get_var_type(index) + "'"; } err_text = "Invalid set index " + v + " (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'"; OPCODE_BREAK; } #endif ip += 4; } DISPATCH_OPCODE; OPCODE(OPCODE_GET) { CHECK_SPACE(3); GET_VARIANT_PTR(src, 1); GET_VARIANT_PTR(index, 2); GET_VARIANT_PTR(dst, 3); bool valid; #ifdef DEBUG_ENABLED //allow better error message in cases where src and dst are the same stack position Variant ret = src->get(*index, &valid); #else *dst = src->get(*index, &valid); #endif #ifdef DEBUG_ENABLED if (!valid) { String v = index->operator String(); if (v != "") { v = "'" + v + "'"; } else { v = "of type '" + _get_var_type(index) + "'"; } err_text = "Invalid get index " + v + " (on base: '" + _get_var_type(src) + "')."; OPCODE_BREAK; } *dst = ret; #endif ip += 4; } DISPATCH_OPCODE; OPCODE(OPCODE_SET_NAMED) { CHECK_SPACE(3); GET_VARIANT_PTR(dst, 1); GET_VARIANT_PTR(value, 3); int indexname = _code_ptr[ip + 2]; GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count); const StringName *index = &_global_names_ptr[indexname]; bool valid; dst->set_named(*index, *value, &valid); #ifdef DEBUG_ENABLED if (!valid) { String err_type; err_text = "Invalid set index '" + String(*index) + "' (on base: '" + _get_var_type(dst) + "') with value of type '" + _get_var_type(value) + "'."; OPCODE_BREAK; } #endif ip += 4; } DISPATCH_OPCODE; OPCODE(OPCODE_GET_NAMED) { CHECK_SPACE(4); GET_VARIANT_PTR(src, 1); GET_VARIANT_PTR(dst, 3); int indexname = _code_ptr[ip + 2]; GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count); const StringName *index = &_global_names_ptr[indexname]; bool valid; #ifdef DEBUG_ENABLED //allow better error message in cases where src and dst are the same stack position Variant ret = src->get_named(*index, &valid); #else *dst = src->get_named(*index, &valid); #endif #ifdef DEBUG_ENABLED if (!valid) { if (src->has_method(*index)) { err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "'). Did you mean '." + index->operator String() + "()' or funcref(obj, \"" + index->operator String() + "\") ?"; } else { err_text = "Invalid get index '" + index->operator String() + "' (on base: '" + _get_var_type(src) + "')."; } OPCODE_BREAK; } *dst = ret; #endif ip += 4; } DISPATCH_OPCODE; OPCODE(OPCODE_SET_MEMBER) { CHECK_SPACE(3); int indexname = _code_ptr[ip + 1]; GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count); const StringName *index = &_global_names_ptr[indexname]; GET_VARIANT_PTR(src, 2); bool valid; bool ok = ClassDB::set_property(p_instance->owner, *index, *src, &valid); #ifdef DEBUG_ENABLED if (!ok) { err_text = "Internal error setting property: " + String(*index); OPCODE_BREAK; } else if (!valid) { err_text = "Error setting property '" + String(*index) + "' with value of type " + Variant::get_type_name(src->get_type()) + "."; OPCODE_BREAK; } #endif ip += 3; } DISPATCH_OPCODE; OPCODE(OPCODE_GET_MEMBER) { CHECK_SPACE(3); int indexname = _code_ptr[ip + 1]; GD_ERR_BREAK(indexname < 0 || indexname >= _global_names_count); const StringName *index = &_global_names_ptr[indexname]; GET_VARIANT_PTR(dst, 2); bool ok = ClassDB::get_property(p_instance->owner, *index, *dst); #ifdef DEBUG_ENABLED if (!ok) { err_text = "Internal error getting property: " + String(*index); OPCODE_BREAK; } #endif ip += 3; } DISPATCH_OPCODE; OPCODE(OPCODE_ASSIGN) { CHECK_SPACE(3); GET_VARIANT_PTR(dst, 1); GET_VARIANT_PTR(src, 2); *dst = *src; ip += 3; } DISPATCH_OPCODE; OPCODE(OPCODE_ASSIGN_TRUE) { CHECK_SPACE(2); GET_VARIANT_PTR(dst, 1); *dst = true; ip += 2; } DISPATCH_OPCODE; OPCODE(OPCODE_ASSIGN_FALSE) { CHECK_SPACE(2); GET_VARIANT_PTR(dst, 1); *dst = false; ip += 2; } DISPATCH_OPCODE; OPCODE(OPCODE_ASSIGN_TYPED_BUILTIN) { CHECK_SPACE(4); Variant::Type var_type = (Variant::Type)_code_ptr[ip + 1]; GET_VARIANT_PTR(dst, 2); GET_VARIANT_PTR(src, 3); GD_ERR_BREAK(var_type < 0 || var_type >= Variant::VARIANT_MAX); if (src->get_type() != var_type) { err_text = "Trying to assign value of type '" + Variant::get_type_name(src->get_type()) + "' to a variable of type '" + Variant::get_type_name(var_type) + "'."; OPCODE_BREAK; } *dst = *src; ip += 4; } DISPATCH_OPCODE; OPCODE(OPCODE_ASSIGN_TYPED_NATIVE) { CHECK_SPACE(4); GET_VARIANT_PTR(type, 1); GET_VARIANT_PTR(dst, 2); GET_VARIANT_PTR(src, 3); GDScriptNativeClass *nc = Object::cast_to(type->operator Object *()); GD_ERR_BREAK(!nc); Object *src_obj = src->operator Object *(); GD_ERR_BREAK(!src_obj); if (!ClassDB::is_parent_class(src_obj->get_class_name(), nc->get_name())) { err_text = "Trying to assign value of type '" + src_obj->get_class_name() + "' to a variable of type '" + nc->get_name() + "'."; OPCODE_BREAK; } *dst = *src; ip += 4; } DISPATCH_OPCODE; OPCODE(OPCODE_ASSIGN_TYPED_SCRIPT) { CHECK_SPACE(4); GET_VARIANT_PTR(type, 1); GET_VARIANT_PTR(dst, 2); GET_VARIANT_PTR(src, 3); Script *base_type = Object::cast_to