Merge pull request #23959 from RandomShaper/fix-dangling-script-fix

Fix dangling script fix
This commit is contained in:
Rémi Verschelde 2018-11-26 10:13:00 +01:00 committed by GitHub
commit 7b2ac28326
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 41 deletions

View File

@ -1603,13 +1603,13 @@ Error GDScriptCompiler::_parse_block(CodeGen &codegen, const GDScriptParser::Blo
return OK; return OK;
} }
Error GDScriptCompiler::_parse_function(Ref<GDScript> p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready) { Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready) {
Vector<int> bytecode; Vector<int> bytecode;
CodeGen codegen; CodeGen codegen;
codegen.class_node = p_class; codegen.class_node = p_class;
codegen.script = p_script.ptr(); codegen.script = p_script;
codegen.function_node = p_func; codegen.function_node = p_func;
codegen.stack_max = 0; codegen.stack_max = 0;
codegen.current_line = 0; codegen.current_line = 0;
@ -1853,7 +1853,7 @@ Error GDScriptCompiler::_parse_function(Ref<GDScript> p_script, const GDScriptPa
return OK; return OK;
} }
Error GDScriptCompiler::_parse_class_level(Ref<GDScript> p_script, Ref<GDScript> p_owner, const GDScriptParser::ClassNode *p_class, bool p_keep_state) { Error GDScriptCompiler::_parse_class_level(GDScript *p_script, GDScript *p_owner, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
if (p_class->owner && p_class->owner->owner) { if (p_class->owner && p_class->owner->owner) {
// Owner is not root // Owner is not root
@ -1887,7 +1887,7 @@ Error GDScriptCompiler::_parse_class_level(Ref<GDScript> p_script, Ref<GDScript>
p_script->initializer = NULL; p_script->initializer = NULL;
p_script->subclasses.clear(); p_script->subclasses.clear();
p_script->_owner = p_owner.ptr(); p_script->_owner = p_owner;
p_script->tool = p_class->tool; p_script->tool = p_class->tool;
p_script->name = p_class->name; p_script->name = p_class->name;
@ -1994,7 +1994,7 @@ Error GDScriptCompiler::_parse_class_level(Ref<GDScript> p_script, Ref<GDScript>
StringName name = p_class->_signals[i].name; StringName name = p_class->_signals[i].name;
GDScript *c = p_script.ptr(); GDScript *c = p_script;
while (c) { while (c) {
@ -2054,7 +2054,7 @@ Error GDScriptCompiler::_parse_class_level(Ref<GDScript> p_script, Ref<GDScript>
return OK; return OK;
} }
Error GDScriptCompiler::_parse_class_blocks(Ref<GDScript> p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) { Error GDScriptCompiler::_parse_class_blocks(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
//parse methods //parse methods
bool has_initializer = false; bool has_initializer = false;
@ -2159,7 +2159,7 @@ Error GDScriptCompiler::_parse_class_blocks(Ref<GDScript> p_script, const GDScri
return OK; return OK;
} }
void GDScriptCompiler::_make_scripts(const Ref<GDScript> p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) { void GDScriptCompiler::_make_scripts(const GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state) {
Map<StringName, Ref<GDScript> > old_subclasses; Map<StringName, Ref<GDScript> > old_subclasses;
@ -2178,20 +2178,20 @@ void GDScriptCompiler::_make_scripts(const Ref<GDScript> p_script, const GDScrip
subclass.instance(); subclass.instance();
} }
subclass->_owner = const_cast<GDScript *>(p_script.ptr()); subclass->_owner = const_cast<GDScript *>(p_script);
class_map.insert(name, subclass); class_map.insert(name, subclass);
_make_scripts(subclass.ptr(), p_class->subclasses[i], p_keep_state); _make_scripts(subclass.ptr(), p_class->subclasses[i], p_keep_state);
} }
} }
Error GDScriptCompiler::compile(const GDScriptParser *p_parser, Ref<GDScript> p_script, bool p_keep_state) { Error GDScriptCompiler::compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state) {
err_line = -1; err_line = -1;
err_column = -1; err_column = -1;
error = ""; error = "";
parser = p_parser; parser = p_parser;
main_script = p_script.ptr(); main_script = p_script;
const GDScriptParser::Node *root = parser->get_parse_tree(); const GDScriptParser::Node *root = parser->get_parse_tree();
ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, ERR_INVALID_DATA); ERR_FAIL_COND_V(root->type != GDScriptParser::Node::TYPE_CLASS, ERR_INVALID_DATA);

View File

@ -148,17 +148,17 @@ class GDScriptCompiler {
int _parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::OperatorNode *p_expression, int p_stack_level); int _parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::OperatorNode *p_expression, int p_stack_level);
int _parse_expression(CodeGen &codegen, const GDScriptParser::Node *p_expression, int p_stack_level, bool p_root = false, bool p_initializer = false); int _parse_expression(CodeGen &codegen, const GDScriptParser::Node *p_expression, int p_stack_level, bool p_root = false, bool p_initializer = false);
Error _parse_block(CodeGen &codegen, const GDScriptParser::BlockNode *p_block, int p_stack_level = 0, int p_break_addr = -1, int p_continue_addr = -1); Error _parse_block(CodeGen &codegen, const GDScriptParser::BlockNode *p_block, int p_stack_level = 0, int p_break_addr = -1, int p_continue_addr = -1);
Error _parse_function(Ref<GDScript> p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready = false); Error _parse_function(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready = false);
Error _parse_class_level(Ref<GDScript> p_script, Ref<GDScript> p_owner, const GDScriptParser::ClassNode *p_class, bool p_keep_state); Error _parse_class_level(GDScript *p_script, GDScript *p_owner, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
Error _parse_class_blocks(Ref<GDScript> p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state); Error _parse_class_blocks(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
void _make_scripts(const Ref<GDScript> p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state); void _make_scripts(const GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
int err_line; int err_line;
int err_column; int err_column;
StringName source; StringName source;
String error; String error;
public: public:
Error compile(const GDScriptParser *p_parser, Ref<GDScript> p_script, bool p_keep_state = false); Error compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state = false);
String get_error() const; String get_error() const;
int get_error_line() const; int get_error_line() const;

View File

@ -370,7 +370,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
} }
script = p_instance->script.ptr(); script = p_instance->script.ptr();
} else { } else {
script = this->_script.ptr(); script = _script;
} }
} }
@ -1182,7 +1182,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
GET_VARIANT_PTR(dst, argc + 3); GET_VARIANT_PTR(dst, argc + 3);
const GDScript *gds = _script.ptr(); const GDScript *gds = _script;
const Map<StringName, GDScriptFunction *>::Element *E = NULL; const Map<StringName, GDScriptFunction *>::Element *E = NULL;
while (gds->base.ptr()) { while (gds->base.ptr()) {
@ -1253,7 +1253,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
gdfs->state.stack_size = _stack_size; gdfs->state.stack_size = _stack_size;
gdfs->state.self = self; gdfs->state.self = self;
gdfs->state.alloca_size = alloca_size; gdfs->state.alloca_size = alloca_size;
gdfs->state.script = _script; gdfs->state.script = Ref<GDScript>(_script);
gdfs->state.ip = ip + ipofs; gdfs->state.ip = ip + ipofs;
gdfs->state.line = line; gdfs->state.line = line;
gdfs->state.instance_id = (p_instance && p_instance->get_owner()) ? p_instance->get_owner()->get_instance_id() : 0; gdfs->state.instance_id = (p_instance && p_instance->get_owner()) ? p_instance->get_owner()->get_instance_id() : 0;
@ -1760,22 +1760,14 @@ GDScriptFunction::~GDScriptFunction() {
Variant GDScriptFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error) { Variant GDScriptFunctionState::_signal_callback(const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
#ifdef DEBUG_ENABLED
// Checking this first since it's faster
if (!state.script.is_valid()) {
ERR_EXPLAIN("Resumed after yield, but script is gone");
ERR_FAIL_V(Variant());
}
if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) { if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
#ifdef DEBUG_ENABLED
ERR_EXPLAIN("Resumed after yield, but class instance is gone"); ERR_EXPLAIN("Resumed after yield, but class instance is gone");
ERR_FAIL_V(Variant()); ERR_FAIL_V(Variant());
}
#else #else
if (!is_valid()) {
return Variant(); return Variant();
}
#endif #endif
}
Variant arg; Variant arg;
r_error.error = Variant::CallError::CALL_OK; r_error.error = Variant::CallError::CALL_OK;
@ -1842,9 +1834,6 @@ bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
return false; return false;
if (p_extended_check) { if (p_extended_check) {
//script gone? (checking this first since it's faster)
if (!state.script.is_valid())
return false;
//class instance gone? //class instance gone?
if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) if (state.instance_id && !ObjectDB::get_instance(state.instance_id))
return false; return false;
@ -1856,18 +1845,14 @@ bool GDScriptFunctionState::is_valid(bool p_extended_check) const {
Variant GDScriptFunctionState::resume(const Variant &p_arg) { Variant GDScriptFunctionState::resume(const Variant &p_arg) {
ERR_FAIL_COND_V(!function, Variant()); ERR_FAIL_COND_V(!function, Variant());
#ifdef DEBUG_ENABLED
// Checking this first since it's faster
if (!state.script.is_valid()) {
ERR_EXPLAIN("Resumed after yield, but script is gone");
ERR_FAIL_V(Variant());
}
if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) { if (state.instance_id && !ObjectDB::get_instance(state.instance_id)) {
#ifdef DEBUG_ENABLED
ERR_EXPLAIN("Resumed after yield, but class instance is gone"); ERR_EXPLAIN("Resumed after yield, but class instance is gone");
ERR_FAIL_V(Variant()); ERR_FAIL_V(Variant());
} #else
return Variant();
#endif #endif
}
state.result = p_arg; state.result = p_arg;
Variant::CallError err; Variant::CallError err;

View File

@ -225,7 +225,7 @@ private:
bool _static; bool _static;
MultiplayerAPI::RPCMode rpc_mode; MultiplayerAPI::RPCMode rpc_mode;
Ref<GDScript> _script; GDScript *_script;
StringName name; StringName name;
Vector<Variant> constants; Vector<Variant> constants;
@ -297,7 +297,7 @@ public:
int get_default_argument_addr(int p_idx) const; int get_default_argument_addr(int p_idx) const;
GDScriptDataType get_return_type() const; GDScriptDataType get_return_type() const;
GDScriptDataType get_argument_type(int p_idx) const; GDScriptDataType get_argument_type(int p_idx) const;
GDScript *get_script() const { return const_cast<GDScript *>(_script.ptr()); } GDScript *get_script() const { return _script; }
StringName get_source() const { return source; } StringName get_source() const { return source; }
void debug_get_stack_member_state(int p_line, List<Pair<StringName, int> > *r_stackvars) const; void debug_get_stack_member_state(int p_line, List<Pair<StringName, int> > *r_stackvars) const;