- Updated the script engine to make use of the DBMap* structure for storing variables (for Ind <3)

- Fixed missing new-lines at the end of various files causing warnings

git-svn-id: https://svn.code.sf.net/p/rathena/svn/trunk@15997 54d463be-8e91-2dee-dedb-b68131a5f0ec
This commit is contained in:
epoque11 2012-04-28 21:50:55 +00:00
parent de96ee0457
commit 02e4daf67d
8 changed files with 72 additions and 92 deletions

View File

@ -8993,4 +8993,4 @@ void do_init_atcommand(void)
void do_final_atcommand(void) void do_final_atcommand(void)
{ {
atcommand_db_clear(); atcommand_db_clear();
} }

View File

@ -343,13 +343,10 @@ void instance_destroy(int instance_id)
} }
if( instance[instance_id].ivar ) if( instance[instance_id].ivar )
linkdb_final( &instance[instance_id].ivar ); // Remove numeric vars db_destroy(instance[instance_id].ivar);
if( instance[instance_id].svar ) if( instance[instance_id].svar )
{ // Remove string vars db_destroy(instance[instance_id].svar);
linkdb_foreach( &instance[instance_id].svar, instance_destroy_freesvar );
linkdb_final( &instance[instance_id].svar );
}
if( instance[instance_id].progress_timer != INVALID_TIMER ) if( instance[instance_id].progress_timer != INVALID_TIMER )
delete_timer( instance[instance_id].progress_timer, instance_destroy_timer); delete_timer( instance[instance_id].progress_timer, instance_destroy_timer);

View File

@ -21,7 +21,7 @@ struct s_instance {
int num_map; int num_map;
int users; int users;
struct linkdb_node *ivar, *svar; // Instance Variable for scripts struct DBMap *ivar, *svar; // Instance Variable for scripts
int progress_timer; int progress_timer;
time_t progress_timeout; time_t progress_timeout;

View File

@ -2767,9 +2767,9 @@ static const char* npc_parse_function(char* w1, char* w2, char* w3, char* w4, co
func_db = script_get_userfunc_db(); func_db = script_get_userfunc_db();
if (func_db->put(func_db, db_str2key(w3), db_ptr2data(script), &old_data)) if (func_db->put(func_db, db_str2key(w3), db_ptr2data(script), &old_data))
{ {
struct script_code *oldscript = db_data2ptr(&old_data); struct script_code *oldscript = (struct script_code*)db_data2ptr(&old_data);
ShowInfo("npc_parse_function: Overwriting user function [%s] (%s:%d)\n", w3, filepath, strline(buffer,start-buffer)); ShowInfo("npc_parse_function: Overwriting user function [%s] (%s:%d)\n", w3, filepath, strline(buffer,start-buffer));
script_free_vars(&oldscript->script_vars); script_free_vars(oldscript->script_vars);
aFree(oldscript->script_buf); aFree(oldscript->script_buf);
aFree(oldscript); aFree(oldscript);
} }

View File

@ -443,4 +443,4 @@ void pc_groups_reload(void)
{ {
do_final_pc_groups(); do_final_pc_groups();
do_init_pc_groups(); do_init_pc_groups();
} }

View File

@ -17,4 +17,4 @@ void do_init_pc_groups(void);
void do_final_pc_groups(void); void do_final_pc_groups(void);
void pc_groups_reload(void); void pc_groups_reload(void);
#endif // _PC_GROUPS_H_ #endif // _PC_GROUPS_H_

View File

@ -2533,22 +2533,22 @@ void get_val(struct script_state* st, struct script_data* data)
break; break;
case '.': case '.':
{ {
struct linkdb_node** n = struct DBMap* n =
data->ref ? data->ref: data->ref ? *data->ref:
name[1] == '@' ? st->stack->var_function:// instance/scope variable name[1] == '@' ? st->stack->var_function:// instance/scope variable
&st->script->script_vars;// npc variable st->script->script_vars;// npc variable
data->u.str = (char*)linkdb_search(n, (void*)__64BPRTSIZE(reference_getuid(data))); data->u.str = (char*)idb_get(n,reference_getuid(data));
} }
break; break;
case '\'': case '\'':
{ {
struct linkdb_node** n = NULL; struct DBMap* n = NULL;
if (st->instance_id) { if (st->instance_id) {
n = &instance[st->instance_id].svar; n = instance[st->instance_id].svar;
} else { } else {
ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name); ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
} }
data->u.str = (char*)linkdb_search(n, (void*)__64BPRTSIZE(reference_getuid(data))); data->u.str = (char*)idb_get(n,reference_getuid(data));
} }
break; break;
default: default:
@ -2598,19 +2598,19 @@ void get_val(struct script_state* st, struct script_data* data)
break; break;
case '.': case '.':
{ {
struct linkdb_node** n = struct DBMap* n =
data->ref ? data->ref: data->ref ? *data->ref:
name[1] == '@' ? st->stack->var_function:// instance/scope variable name[1] == '@' ? st->stack->var_function:// instance/scope variable
&st->script->script_vars;// npc variable st->script->script_vars;// npc variable
data->u.num = (int)__64BPRTSIZE(linkdb_search(n, (void*)__64BPRTSIZE(reference_getuid(data)))); data->u.num = (int)idb_iget(n,reference_getuid(data));
} }
break; break;
case '\'': case '\'':
{ {
struct linkdb_node** n = NULL; struct DBMap* n = NULL;
if( st->instance_id ) if( st->instance_id )
n = &instance[st->instance_id].ivar; n = instance[st->instance_id].ivar;
data->u.num = (int)__64BPRTSIZE(linkdb_search(n, (void*)__64BPRTSIZE(reference_getuid(data)))); data->u.num = (int)idb_iget(n,reference_getuid(data));
} }
break; break;
default: default:
@ -2623,11 +2623,11 @@ void get_val(struct script_state* st, struct script_data* data)
return; return;
} }
struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref); struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref);
/// Retrieves the value of a reference identified by uid (variable, constant, param) /// Retrieves the value of a reference identified by uid (variable, constant, param)
/// The value is left in the top of the stack and needs to be removed manually. /// The value is left in the top of the stack and needs to be removed manually.
void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref) void* get_val2(struct script_state* st, int uid, struct DBMap** ref)
{ {
struct script_data* data; struct script_data* data;
push_val2(st->stack, C_NAME, uid, ref); push_val2(st->stack, C_NAME, uid, ref);
@ -2640,7 +2640,7 @@ void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
* Stores the value of a script variable * Stores the value of a script variable
* Return value is 0 on fail, 1 on success. * Return value is 0 on fail, 1 on success.
*------------------------------------------*/ *------------------------------------------*/
static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct linkdb_node** ref) static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct DBMap** ref)
{ {
char prefix = name[0]; char prefix = name[0];
@ -2656,24 +2656,20 @@ static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* nam
return (name[1] == '#') ? return (name[1] == '#') ?
pc_setaccountreg2str(sd, name, str) : pc_setaccountreg2str(sd, name, str) :
pc_setaccountregstr(sd, name, str); pc_setaccountregstr(sd, name, str);
case '.': { case '.':
char* p; {
struct linkdb_node** n; struct DBMap* n;
n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars; n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
p = (char*)linkdb_erase(n, (void*)__64BPRTSIZE(num)); if( n ) {
if (p) aFree(p); idb_remove(n, num);
if (str[0]) linkdb_insert(n, (void*)__64BPRTSIZE(num), aStrdup(str)); if (str[0]) idb_put(n, num, aStrdup(str));
}
} }
return 1; return 1;
case '\'': { case '\'':
char *p; if( st->instance_id ) {
struct linkdb_node** n = NULL; idb_remove(instance[st->instance_id].svar, num);
if( st->instance_id ) if( str[0] ) idb_put(instance[st->instance_id].svar, num, aStrdup(str));
n = &instance[st->instance_id].svar;
p = (char*)linkdb_erase(n, (void*)__64BPRTSIZE(num));
if (p) aFree(p);
if( str[0] ) linkdb_insert(n, (void*)__64BPRTSIZE(num), aStrdup(str));
} }
return 1; return 1;
default: default:
@ -2707,27 +2703,24 @@ static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* nam
return (name[1] == '#') ? return (name[1] == '#') ?
pc_setaccountreg2(sd, name, val) : pc_setaccountreg2(sd, name, val) :
pc_setaccountreg(sd, name, val); pc_setaccountreg(sd, name, val);
case '.': { case '.':
struct linkdb_node** n; {
n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars; struct DBMap* n;
if (val == 0) n = (ref) ? *ref : (name[1] == '@') ? st->stack->var_function : st->script->script_vars;
linkdb_erase(n, (void*)__64BPRTSIZE(num)); if( n ) {
else idb_remove(n, num);
linkdb_replace(n, (void*)__64BPRTSIZE(num), (void*)__64BPRTSIZE(val)); if( val != 0 )
idb_iput(n, num, val);
}
} }
return 1; return 1;
case '\'': case '\'':
{ if( st->instance_id ) {
struct linkdb_node** n = NULL; idb_remove(instance[st->instance_id].ivar, num);
if( st->instance_id ) if( val != 0 )
n = &instance[st->instance_id].ivar; idb_iput(instance[st->instance_id].ivar, num, val);
if( val == 0 )
linkdb_erase(n, (void*)__64BPRTSIZE(num));
else
linkdb_replace(n, (void*)__64BPRTSIZE(num), (void*)__64BPRTSIZE(val));
return 1;
} }
return 1;
default: default:
return pc_setglobalreg(sd, name, val); return pc_setglobalreg(sd, name, val);
} }
@ -2739,7 +2732,7 @@ int set_var(TBL_PC* sd, char* name, void* val)
return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL); return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
} }
void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct linkdb_node **ref) void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct DBMap **ref)
{ {
set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref); set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
} }
@ -2852,7 +2845,7 @@ void stack_expand(struct script_stack* stack)
#define push_val(stack,type,val) push_val2(stack, type, val, NULL) #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
/// Pushes a value into the stack (with reference) /// Pushes a value into the stack (with reference)
struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref) struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct DBMap** ref)
{ {
if( stack->sp >= stack->sp_max ) if( stack->sp >= stack->sp_max )
stack_expand(stack); stack_expand(stack);
@ -2937,10 +2930,7 @@ void pop_stack(struct script_state* st, int start, int end)
{ {
struct script_retinfo* ri = data->u.ri; struct script_retinfo* ri = data->u.ri;
if( ri->var_function ) if( ri->var_function )
{
script_free_vars(ri->var_function); script_free_vars(ri->var_function);
aFree(ri->var_function);
}
aFree(ri); aFree(ri);
} }
data->type = C_NOP; data->type = C_NOP;
@ -2969,22 +2959,17 @@ void pop_stack(struct script_state* st, int start, int end)
/*========================================== /*==========================================
* *
*------------------------------------------*/ *------------------------------------------*/
void script_free_vars(struct linkdb_node **node) void script_free_vars(struct DBMap* storage)
{ {
struct linkdb_node* n = *node; if( storage )
while( n != NULL) {// destroy the storage construct containing the variables
{ db_destroy(storage);
const char* name = get_str((int)__64BPRTSIZE((n->key)&0x00ffffff));
if( is_string_variable(name) )
aFree(n->data); // 文字型変数なので、データ削除
n = n->next;
} }
linkdb_final( node );
} }
void script_free_code(struct script_code* code) void script_free_code(struct script_code* code)
{ {
script_free_vars( &code->script_vars ); script_free_vars( code->script_vars );
aFree( code->script_buf ); aFree( code->script_buf );
aFree( code ); aFree( code );
} }
@ -3005,7 +2990,7 @@ struct script_state* script_alloc_state(struct script_code* script, int pos, int
st->stack->sp_max = 64; st->stack->sp_max = 64;
CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max); CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
st->stack->defsp = st->stack->sp; st->stack->defsp = st->stack->sp;
CREATE(st->stack->var_function, struct linkdb_node*, 1); st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
st->state = RUN; st->state = RUN;
st->script = script; st->script = script;
//st->scriptroot = script; //st->scriptroot = script;
@ -3028,7 +3013,6 @@ void script_free_state(struct script_state* st)
if( st->sleep.timer != INVALID_TIMER ) if( st->sleep.timer != INVALID_TIMER )
delete_timer(st->sleep.timer, run_script_timer); delete_timer(st->sleep.timer, run_script_timer);
script_free_vars(st->stack->var_function); script_free_vars(st->stack->var_function);
aFree(st->stack->var_function);
pop_stack(st, 0, st->stack->sp); pop_stack(st, 0, st->stack->sp);
aFree(st->stack->stack_data); aFree(st->stack->stack_data);
aFree(st->stack); aFree(st->stack);
@ -3465,7 +3449,6 @@ int run_func(struct script_state *st)
return 1; return 1;
} }
script_free_vars( st->stack->var_function ); script_free_vars( st->stack->var_function );
aFree(st->stack->var_function);
ri = st->stack->stack_data[st->stack->defsp-1].u.ri; ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
nargs = ri->nargs; nargs = ri->nargs;
@ -4481,7 +4464,7 @@ BUILDIN_FUNC(callfunc)
{ {
const char* name = reference_getname(data); const char* name = reference_getname(data);
if( name[0] == '.' && name[1] == '@' ) if( name[0] == '.' && name[1] == '@' )
data->ref = st->stack->var_function; data->ref = &st->stack->var_function;
else if( name[0] == '.' ) else if( name[0] == '.' )
data->ref = &st->script->script_vars; data->ref = &st->script->script_vars;
} }
@ -4499,7 +4482,7 @@ BUILDIN_FUNC(callfunc)
st->script = scr; st->script = scr;
st->stack->defsp = st->stack->sp; st->stack->defsp = st->stack->sp;
st->state = GOTO; st->state = GOTO;
st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*)); st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
return 0; return 0;
} }
@ -4527,7 +4510,7 @@ BUILDIN_FUNC(callsub)
{ {
const char* name = reference_getname(data); const char* name = reference_getname(data);
if( name[0] == '.' && name[1] == '@' ) if( name[0] == '.' && name[1] == '@' )
data->ref = st->stack->var_function; data->ref = &st->stack->var_function;
} }
} }
@ -4542,7 +4525,7 @@ BUILDIN_FUNC(callsub)
st->pos = pos; st->pos = pos;
st->stack->defsp = st->stack->sp; st->stack->defsp = st->stack->sp;
st->state = GOTO; st->state = GOTO;
st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*)); st->stack->var_function = idb_alloc(DB_OPT_RELEASE_DATA);
return 0; return 0;
} }
@ -4597,7 +4580,7 @@ BUILDIN_FUNC(return)
const char* name = reference_getname(data); const char* name = reference_getname(data);
if( name[0] == '.' && name[1] == '@' ) if( name[0] == '.' && name[1] == '@' )
{// scope variable {// scope variable
if( !data->ref || data->ref == st->stack->var_function ) if( !data->ref || data->ref == (DBMap**)&st->stack->var_function )
get_val(st, data);// current scope, convert to value get_val(st, data);// current scope, convert to value
} }
else if( name[0] == '.' && !data->ref ) else if( name[0] == '.' && !data->ref )
@ -5178,7 +5161,7 @@ BUILDIN_FUNC(set)
/// ///
/// Returns the size of the specified array /// Returns the size of the specified array
static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref) static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct DBMap** ref)
{ {
int32 ret = idx; int32 ret = idx;

View File

@ -76,7 +76,7 @@ typedef enum c_op {
} c_op; } c_op;
struct script_retinfo { struct script_retinfo {
struct linkdb_node** var_function;// scope variables struct DBMap* var_function;// scope variables
struct script_code* script;// script code struct script_code* script;// script code
int pos;// script location int pos;// script location
int nargs;// argument count int nargs;// argument count
@ -90,7 +90,7 @@ struct script_data {
char *str; char *str;
struct script_retinfo* ri; struct script_retinfo* ri;
} u; } u;
struct linkdb_node** ref; struct DBMap** ref;
}; };
// Moved defsp from script_state to script_stack since // Moved defsp from script_state to script_stack since
@ -98,7 +98,7 @@ struct script_data {
struct script_code { struct script_code {
int script_size; int script_size;
unsigned char* script_buf; unsigned char* script_buf;
struct linkdb_node* script_vars; struct DBMap* script_vars;
}; };
struct script_stack { struct script_stack {
@ -106,7 +106,7 @@ struct script_stack {
int sp_max;// capacity of the stack int sp_max;// capacity of the stack
int defsp; int defsp;
struct script_data *stack_data;// stack struct script_data *stack_data;// stack
struct linkdb_node** var_function;// scope variables struct DBMap* var_function;// scope variables
}; };
@ -164,7 +164,7 @@ void run_script_main(struct script_state *st);
void script_stop_sleeptimers(int id); void script_stop_sleeptimers(int id);
struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n); struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n);
void script_free_code(struct script_code* code); void script_free_code(struct script_code* code);
void script_free_vars(struct linkdb_node **node); void script_free_vars(struct DBMap *storage);
struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid); struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid);
void script_free_state(struct script_state* st); void script_free_state(struct script_state* st);