From: Charlie Brej Date: Wed, 9 Sep 2009 16:28:40 +0000 (+0100) Subject: [script] Make "this" (the current object) a part of the status X-Git-Tag: 0.7.2~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bdc31288c27186f03dfc77e51fb5e6e7bbe7522e;p=thirdparty%2Fplymouth.git [script] Make "this" (the current object) a part of the status This is now also looked up when evaluating vars. Vars are looked for in the local context, then within this (current object) and finally within the global context; --- diff --git a/src/plugins/splash/script/script-execute.c b/src/plugins/splash/script/script-execute.c index 5814adea..731f6714 100644 --- a/src/plugins/splash/script/script-execute.c +++ b/src/plugins/splash/script/script-execute.c @@ -117,6 +117,8 @@ static script_obj_t *script_evaluate_var (script_state_t *state, char *name = exp->data.string; script_obj_t *obj = script_obj_hash_peek_element (state->local, name); if (obj) return obj; + obj = script_obj_hash_peek_element (state->this, 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); @@ -387,6 +389,12 @@ static script_obj_t *script_evaluate (script_state_t *state, return state->global; } + case SCRIPT_EXP_TYPE_TERM_THIS: + { + script_obj_ref (state->this); + return state->this; + } + case SCRIPT_EXP_TYPE_TERM_VAR: { return script_evaluate_var (state, exp); @@ -481,7 +489,7 @@ static script_return_t script_execute_function_with_parlist (script_state_t * script_obj_t *this, ply_list_t *parameter_data) { - script_state_t *sub_state = script_state_init_sub (state); + script_state_t *sub_state = script_state_init_sub (state, this); ply_list_t *parameter_names = function->parameters; ply_list_node_t *node_name = ply_list_get_first_node (parameter_names); ply_list_node_t *node_data = ply_list_get_first_node (parameter_data); diff --git a/src/plugins/splash/script/script-object.c b/src/plugins/splash/script/script-object.c index 3e0f3d6a..1cf52636 100644 --- a/src/plugins/splash/script/script-object.c +++ b/src/plugins/splash/script/script-object.c @@ -392,7 +392,7 @@ 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); + if (hash->type != SCRIPT_OBJ_TYPE_HASH) return NULL; script_variable_t *variable = ply_hashtable_lookup (hash->data.hash, (void *) name); if (!variable) return NULL; @@ -483,7 +483,6 @@ void script_obj_hash_add_element (script_obj_t *hash, script_obj_t *element, const char *name) { - assert (hash->type == SCRIPT_OBJ_TYPE_HASH); script_obj_t *obj = script_obj_hash_get_element (hash, name); script_obj_assign (obj, element); script_obj_unref (obj); diff --git a/src/plugins/splash/script/script-parse.c b/src/plugins/splash/script/script-parse.c index 8c3d478e..b2799776 100644 --- a/src/plugins/splash/script/script-parse.c +++ b/src/plugins/splash/script/script-parse.c @@ -171,7 +171,7 @@ static void script_parse_error (script_debug_location_t *location, } static const script_parse_operator_table_entry_t* /* Only allows 1 or 2 character symbols */ -script_parse_operator_table_entry_lookup (script_scan_t *scan, +script_parse_operator_table_entry_lookup (script_scan_t *scan, const script_parse_operator_table_entry_t *table) { int entry_index; @@ -279,6 +279,8 @@ static script_exp_t *script_parse_exp_tm (script_scan_t *scan) exp = script_parse_new_exp(SCRIPT_EXP_TYPE_TERM_GLOBAL, &curtoken->location); else if (script_scan_token_is_identifier_of_value (curtoken, "local")) exp = script_parse_new_exp(SCRIPT_EXP_TYPE_TERM_LOCAL, &curtoken->location); + else if (script_scan_token_is_identifier_of_value (curtoken, "this")) + exp = script_parse_new_exp(SCRIPT_EXP_TYPE_TERM_THIS, &curtoken->location); else if (script_scan_token_is_identifier_of_value (curtoken, "fun")) { script_debug_location_t location = curtoken->location; @@ -813,6 +815,7 @@ static void script_parse_exp_free (script_exp_t *exp) case SCRIPT_EXP_TYPE_TERM_NULL: case SCRIPT_EXP_TYPE_TERM_LOCAL: case SCRIPT_EXP_TYPE_TERM_GLOBAL: + case SCRIPT_EXP_TYPE_TERM_THIS: break; case SCRIPT_EXP_TYPE_FUNCTION_EXE: diff --git a/src/plugins/splash/script/script.c b/src/plugins/splash/script/script.c index 525cb083..635a8b4a 100644 --- a/src/plugins/splash/script/script.c +++ b/src/plugins/splash/script/script.c @@ -36,9 +36,9 @@ #include "script-parse.h" #include "script-object.h" -script_function_t *script_function_script_new (script_op_t *script, - void *user_data, - ply_list_t *parameter_list) +script_function_t *script_function_script_new (script_op_t *script, + void *user_data, + ply_list_t *parameter_list) { script_function_t *function = malloc (sizeof (script_function_t)); @@ -50,9 +50,9 @@ script_function_t *script_function_script_new (script_op_t *script, return function; } -script_function_t *script_function_native_new (script_native_function_t native_function, - void *user_data, - ply_list_t *parameter_list) +script_function_t *script_function_native_new (script_native_function_t native_function, + void *user_data, + ply_list_t *parameter_list) { script_function_t *function = malloc (sizeof (script_function_t)); @@ -114,21 +114,24 @@ void script_obj_native_class_destroy (script_obj_native_class_t *class) script_state_t *script_state_new (void *user_data) { script_state_t *state = malloc (sizeof (script_state_t)); - - state->global = script_obj_new_hash (); - script_obj_ref (state->global); - state->local = state->global; + script_obj_t *global_hash = script_obj_new_hash (); + state->global = script_obj_new_ref (global_hash); + script_obj_unref(global_hash); + state->local = script_obj_new_ref (global_hash); + state->this = script_obj_new_null(); state->user_data = user_data; return state; } -script_state_t *script_state_init_sub (script_state_t *oldstate) +script_state_t *script_state_init_sub (script_state_t *oldstate, script_obj_t *this) { script_state_t *newstate = malloc (sizeof (script_state_t)); - - newstate->global = oldstate->global; - script_obj_ref (newstate->global); - newstate->local = script_obj_new_hash (); + script_obj_t *local_hash = script_obj_new_hash (); + newstate->local = script_obj_new_ref (local_hash); + script_obj_unref(local_hash); + newstate->global = script_obj_new_ref (oldstate->global); + if (this) newstate->this = script_obj_new_ref (this); + else newstate->this = script_obj_new_ref (oldstate->this); newstate->user_data = oldstate->user_data; return newstate; } @@ -137,6 +140,7 @@ void script_state_destroy (script_state_t *state) { script_obj_unref (state->global); script_obj_unref (state->local); + script_obj_unref (state->this); free (state); } diff --git a/src/plugins/splash/script/script.h b/src/plugins/splash/script/script.h index 0d021ce9..1e984f79 100644 --- a/src/plugins/splash/script/script.h +++ b/src/plugins/splash/script/script.h @@ -47,6 +47,7 @@ typedef struct void *user_data; struct script_obj_t *global; struct script_obj_t *local; + struct script_obj_t *this; } script_state_t; typedef enum @@ -121,6 +122,7 @@ typedef enum SCRIPT_EXP_TYPE_TERM_VAR, SCRIPT_EXP_TYPE_TERM_LOCAL, SCRIPT_EXP_TYPE_TERM_GLOBAL, + SCRIPT_EXP_TYPE_TERM_THIS, SCRIPT_EXP_TYPE_PLUS, SCRIPT_EXP_TYPE_MINUS, SCRIPT_EXP_TYPE_MUL, @@ -233,7 +235,7 @@ script_obj_native_class_t *script_obj_native_class_new (script_obj_function_t fr void script_obj_native_class_destroy (script_obj_native_class_t * class); script_state_t *script_state_new (void *user_data); -script_state_t *script_state_init_sub (script_state_t *oldstate); +script_state_t *script_state_init_sub (script_state_t *oldstate, script_obj_t *this); void script_state_destroy (script_state_t *state); #endif /* SCRIPT_H */