From: Charlie Brej Date: Tue, 1 Sep 2009 20:19:51 +0000 (+0100) Subject: [script] Use access functions when accessing variables X-Git-Tag: 0.7.2~24^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=082d52060504392cf116fa18027e26569c5866e4;p=thirdparty%2Fplymouth.git [script] Use access functions when accessing variables Rather than directly accessing hash tables when accessing variables, use the abstraction functions. Adds a peek function which does not create a new object is one has not been defined already. --- diff --git a/src/plugins/splash/script/script-execute.c b/src/plugins/splash/script/script-execute.c index 37a027a5..6eef8342 100644 --- a/src/plugins/splash/script/script-execute.c +++ b/src/plugins/splash/script/script-execute.c @@ -114,31 +114,11 @@ static script_obj_t *script_evaluate_var (script_state_t *state, script_exp_t *exp) { char *name = exp->data.string; - script_obj_t *obj; - - script_obj_deref (&state->global); - script_obj_deref (&state->local); - assert (state->global->type == SCRIPT_OBJ_TYPE_HASH); /*FIXME use script-object functions */ - assert (state->local->type == SCRIPT_OBJ_TYPE_HASH); - - script_variable_t *variable = ply_hashtable_lookup (state->local->data.hash, - name); - if (!variable) - variable = ply_hashtable_lookup (state->global->data.hash, name); - if (variable) - { - obj = variable->object; - script_obj_ref (obj); - return obj; - } - obj = script_obj_new_null (); - - variable = malloc (sizeof (script_variable_t)); - variable->name = strdup (name); - variable->object = obj; - - ply_hashtable_insert (state->local->data.hash, variable->name, variable); - script_obj_ref (obj); + script_obj_t *obj = script_obj_hash_peek_element (state->local, name); + if (obj) return obj; + obj = script_obj_hash_peek_element (state->global, name); + if (obj) return obj; + obj = script_obj_hash_get_element (state->local, name); return obj; } diff --git a/src/plugins/splash/script/script-object.c b/src/plugins/splash/script/script-object.c index a7ce5144..e37a2f83 100644 --- a/src/plugins/splash/script/script-object.c +++ b/src/plugins/splash/script/script-object.c @@ -395,27 +395,33 @@ void script_obj_assign (script_obj_t *obj_a, } } -script_obj_t *script_obj_hash_get_element (script_obj_t *hash, - const char *name) +script_obj_t *script_obj_hash_peek_element (script_obj_t *hash, + const char *name) { hash = script_obj_deref_direct (hash); assert (hash->type == SCRIPT_OBJ_TYPE_HASH); script_variable_t *variable = ply_hashtable_lookup (hash->data.hash, (void *) name); - script_obj_t *obj; + if (!variable) return NULL; + + script_obj_ref (variable->object); + return variable->object; +} - if (variable) - obj = variable->object; - else - { - obj = script_obj_new_null (); - variable = malloc (sizeof (script_variable_t)); - variable->name = strdup (name); - variable->object = obj; - ply_hashtable_insert (hash->data.hash, variable->name, variable); - } - script_obj_ref (obj); - return obj; +script_obj_t *script_obj_hash_get_element (script_obj_t *hash, + const char *name) +{ + script_obj_t *obj = script_obj_hash_peek_element (hash, name); + if (obj) return obj; + + hash = script_obj_deref_direct (hash); + script_variable_t *variable = malloc (sizeof (script_variable_t)); + variable->name = strdup (name); + variable->object = script_obj_new_null (); + ply_hashtable_insert (hash->data.hash, variable->name, variable); + + script_obj_ref (variable->object); + return variable->object; } script_number_t script_obj_hash_get_number (script_obj_t *hash, diff --git a/src/plugins/splash/script/script-object.h b/src/plugins/splash/script/script-object.h index af166825..526372ef 100644 --- a/src/plugins/splash/script/script-object.h +++ b/src/plugins/splash/script/script-object.h @@ -69,6 +69,8 @@ bool script_obj_is_native_of_class_name (script_obj_t *obj, const char *class_name); void script_obj_assign (script_obj_t *obj_a, script_obj_t *obj_b); +script_obj_t *script_obj_hash_peek_element (script_obj_t *hash, + const char *name); script_obj_t *script_obj_hash_get_element (script_obj_t *hash, const char *name); script_number_t script_obj_hash_get_number (script_obj_t *hash,