From 79aa56c704ff4117511cd33be3efd0321683d89a Mon Sep 17 00:00:00 2001 From: Kpy! Date: Tue, 21 Apr 2015 13:48:25 +0200 Subject: Fix script function return always removing references from NPC variables. Bugreport:8642 http://hercules.ws/board/tracker/issue-8642-script-engine-issue-quo/ --- npc/dev/test.txt | 18 ++++++++++++++++++ src/map/script.c | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/npc/dev/test.txt b/npc/dev/test.txt index 7b498e922..8e94d6ee7 100644 --- a/npc/dev/test.txt +++ b/npc/dev/test.txt @@ -86,6 +86,15 @@ function script F_TestNPCArrays { return getarraysize(.y); } +function script F_TestVarOfAnotherNPC { + return getvariableofnpc(.x, getarg(0)); +} + +- script TestVarOfAnotherNPC -1,{ + // Used to test getvariableofnpc() + end; +} + function script HerculesSelfTestHelper { if (.once > 0) return .errors; @@ -618,6 +627,9 @@ function script HerculesSelfTestHelper { callsub(OnCheck, "Callsub (parent array vars isolation)", getarraysize(.@x), .@z); deletearray .@x; deletearray .@y; + .x = 2; + set getvariableofnpc(.x, "TestVarOfAnotherNPC"), 1; + callsub(OnCheck, "Callsub (return NPC variables from another NPC)", callsub(OnTestVarOfAnotherNPC, "TestVarOfAnotherNPC"), 1); // Callfunc callsub(OnCheck, "Callfunc return value", callfunc("F_TestReturnValue", 1)); @@ -663,6 +675,9 @@ function script HerculesSelfTestHelper { callsub(OnCheck, "Callfunc (parent array NPC vars isolation)", getarraysize(.@x), .@z); deletearray .x; deletearray .y; + .x = 2; + set getvariableofnpc(.x, "TestVarOfAnotherNPC"), 1; + callsub(OnCheck, "Callfunc (return NPC variables from another NPC)", callfunc("F_TestVarOfAnotherNPC", "TestVarOfAnotherNPC"), 1); if (.errors) { debugmes "Script engine self-test [ \033[0;31mFAILED\033[0m ]"; @@ -704,6 +719,9 @@ OnTestScopeArrays: setarray .@x, 1, 2, 3, 4; copyarray .@y, getarg(0), getarraysize(getarg(0)); return getarraysize(.@y); + +OnTestVarOfAnotherNPC: + return getvariableofnpc(.x, getarg(0)); OnReportError: .@msg$ = getarg(0,"Unknown Error"); diff --git a/src/map/script.c b/src/map/script.c index ed9345678..b355dfa9e 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -5703,7 +5703,7 @@ BUILDIN(return) { if( !st->script->local.arrays ) st->script->local.arrays = idb_alloc(DB_OPT_BASE); data->ref->arrays = st->script->local.arrays; - } else if ( name[0] == '.' /* && data->ref != NULL */ ) { + } else if ( name[0] == '.' && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->script->local.vars ) { data->ref = NULL; // Reference to the parent scope's script, remove reference pointer. } } -- cgit v1.2.3-60-g2f50 From 7e76bbb730679d4fe310198891499edeec241c31 Mon Sep 17 00:00:00 2001 From: Kpy! Date: Wed, 22 Apr 2015 17:36:17 +0200 Subject: Fix a crash condition when incorrectly using return outside the scope of a function or subroutine. --- src/map/script.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/map/script.c b/src/map/script.c index b355dfa9e..335f45509 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -5682,6 +5682,13 @@ BUILDIN(getarg) /// return; /// return ; BUILDIN(return) { + st->state = RETFUNC; + + if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO ) { + // Incorrect usage of return outside the scope of a function or subroutine. + return true; // No need for further processing, running script is about to be aborted. + } + if( script_hasdata(st,2) ) {// return value struct script_data* data; @@ -5712,7 +5719,7 @@ BUILDIN(return) { {// no return value script_pushnil(st); } - st->state = RETFUNC; + return true; } -- cgit v1.2.3-60-g2f50 From 2970847b879821f738bb892e9079c0ae2a5d08c7 Mon Sep 17 00:00:00 2001 From: Kpy! Date: Wed, 22 Apr 2015 17:53:32 +0200 Subject: Minor refactoring of script function return. --- src/map/script.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/map/script.c b/src/map/script.c index 335f45509..265c4a549 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -5702,16 +5702,19 @@ BUILDIN(return) { script->get_val(st, data);// current scope, convert to value if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars ) data->ref = NULL; // Reference to the parent scope, remove reference pointer - } else if( name[0] == '.' && !data->ref ) { - // script variable without a reference set, link to current script - data->ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1); - script->add_pending_ref(st, data->ref); - data->ref->vars = st->script->local.vars; - if( !st->script->local.arrays ) - st->script->local.arrays = idb_alloc(DB_OPT_BASE); - data->ref->arrays = st->script->local.arrays; - } else if ( name[0] == '.' && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->script->local.vars ) { - data->ref = NULL; // Reference to the parent scope's script, remove reference pointer. + } else if( name[0] == '.' ) { + // npc variable + if( !data->ref ) { + // npc variable without a reference set, link to current script + data->ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1); + script->add_pending_ref(st, data->ref); + data->ref->vars = st->script->local.vars; + if( !st->script->local.arrays ) + st->script->local.arrays = idb_alloc(DB_OPT_BASE); + data->ref->arrays = st->script->local.arrays; + } else if( data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->script->local.vars ) { + data->ref = NULL; // Reference to the parent scope's script, remove reference pointer. + } } } } -- cgit v1.2.3-60-g2f50