From 7ab622fb40605a3e944cc18f3ae473d92dacf97a Mon Sep 17 00:00:00 2001 From: Charlie Brej Date: Wed, 2 Sep 2009 21:21:05 +0100 Subject: [PATCH] [script] Set "this" in functions called through an object When functins are called as an element of an object (e.g. obj.func(par) ), the object is passed into the function execution as "this" in the local context. --- src/plugins/splash/script/script-execute.c | 36 +++++++++++++++++-- src/plugins/splash/script/script-execute.h | 1 + .../splash/script/script-lib-plymouth.c | 9 +++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/plugins/splash/script/script-execute.c b/src/plugins/splash/script/script-execute.c index 6eef8342..5814adea 100644 --- a/src/plugins/splash/script/script-execute.c +++ b/src/plugins/splash/script/script-execute.c @@ -42,6 +42,7 @@ static script_obj_t *script_evaluate (script_state_t *state, script_exp_t *exp); static script_return_t script_execute_function_with_parlist (script_state_t *state, script_function_t *function, + script_obj_t *this, ply_list_t *parameter_data); @@ -219,14 +220,35 @@ static script_obj_t *script_evaluate_unary (script_state_t *state, static script_obj_t *script_evaluate_func (script_state_t *state, script_exp_t *exp) { - script_obj_t *func_obj = script_evaluate (state, exp->data.function_exe.name); - script_obj_t *obj = NULL; + script_obj_t *this_obj; + script_obj_t *func_obj; + script_exp_t *name_exp = exp->data.function_exe.name; + + if (name_exp->type == SCRIPT_EXP_TYPE_HASH) + { + script_obj_t *this_key = script_evaluate (state, name_exp->data.dual.sub_b); + this_obj = script_evaluate (state, name_exp->data.dual.sub_a); + char *this_key_name = script_obj_as_string (this_key); + script_obj_unref (this_key); + if (script_obj_is_hash(this_obj)) + { + func_obj = script_obj_hash_get_element (this_obj, this_key_name); + } + free(this_key_name); + } + else + { + func_obj = script_evaluate (state, exp->data.function_exe.name); + this_obj = NULL; + } + script_function_t *function = script_obj_as_function (func_obj); if (!function) { script_execute_error(exp, "Call operated on an object with is not a function"); script_obj_unref (func_obj); + if (this_obj) script_obj_unref (this_obj); return script_obj_new_null (); } ply_list_t *parameter_expressions = exp->data.function_exe.parameters; @@ -243,7 +265,9 @@ static script_obj_t *script_evaluate_func (script_state_t *state, } script_return_t reply = script_execute_function_with_parlist (state, function, + this_obj, parameter_data); + script_obj_t *obj; if (reply.type == SCRIPT_RETURN_TYPE_RETURN) obj = reply.object; else @@ -258,6 +282,7 @@ static script_obj_t *script_evaluate_func (script_state_t *state, ply_list_free (parameter_data); script_obj_unref (func_obj); + if (this_obj) script_obj_unref (this_obj); return obj; } @@ -453,6 +478,7 @@ static script_return_t script_execute_list (script_state_t *state, /* parameter_data list should be freed by caller */ static script_return_t script_execute_function_with_parlist (script_state_t *state, script_function_t *function, + script_obj_t *this, ply_list_t *parameter_data) { script_state_t *sub_state = script_state_init_sub (state); @@ -486,6 +512,9 @@ static script_return_t script_execute_function_with_parlist (script_state_t * script_obj_unref (count_obj); script_obj_unref (arg_obj); + if (this) + script_obj_hash_add_element (sub_state->local, this, "this"); + script_return_t reply; switch (function->type) { @@ -509,6 +538,7 @@ static script_return_t script_execute_function_with_parlist (script_state_t * script_return_t script_execute_function (script_state_t *state, script_function_t *function, + script_obj_t *this, script_obj_t *first_arg, ...) { @@ -526,7 +556,7 @@ script_return_t script_execute_function (script_state_t *state, } va_end (args); - reply = script_execute_function_with_parlist (state, function, parameter_data); + reply = script_execute_function_with_parlist (state, function, this, parameter_data); ply_list_free (parameter_data); return reply; diff --git a/src/plugins/splash/script/script-execute.h b/src/plugins/splash/script/script-execute.h index 1e5b7dd3..8eb1174c 100644 --- a/src/plugins/splash/script/script-execute.h +++ b/src/plugins/splash/script/script-execute.h @@ -28,6 +28,7 @@ script_return_t script_execute (script_state_t *state, script_op_t *op); script_return_t script_execute_function (script_state_t *state, script_function_t *function, + script_obj_t *this, script_obj_t *first_arg, ...); diff --git a/src/plugins/splash/script/script-lib-plymouth.c b/src/plugins/splash/script/script-lib-plymouth.c index a601d434..5e17143c 100644 --- a/src/plugins/splash/script/script-lib-plymouth.c +++ b/src/plugins/splash/script/script-lib-plymouth.c @@ -152,6 +152,7 @@ void script_lib_plymouth_on_refresh (script_state_t *state, { script_return_t ret = script_execute_function (state, function, + NULL, NULL); script_obj_unref (ret.object); } @@ -169,6 +170,7 @@ void script_lib_plymouth_on_boot_progress (script_state_t *state, script_obj_t *progress_obj = script_obj_new_number (progress); script_return_t ret = script_execute_function (state, function, + NULL, duration_obj, progress_obj, NULL); @@ -186,6 +188,7 @@ void script_lib_plymouth_on_root_mounted (script_state_t *state, { script_return_t ret = script_execute_function (state, function, + NULL, NULL); script_obj_unref (ret.object); } @@ -201,6 +204,7 @@ void script_lib_plymouth_on_keyboard_input (script_state_t *state, script_obj_t *keyboard_input_obj = script_obj_new_string (keyboard_input); script_return_t ret = script_execute_function (state, function, + NULL, keyboard_input_obj, NULL); script_obj_unref (keyboard_input_obj); @@ -218,6 +222,7 @@ void script_lib_plymouth_on_update_status (script_state_t *state, script_obj_t *new_status_obj = script_obj_new_string (new_status); script_return_t ret = script_execute_function (state, function, + NULL, new_status_obj, NULL); script_obj_unref (new_status_obj); @@ -233,6 +238,7 @@ void script_lib_plymouth_on_display_normal (script_state_t *state, { script_return_t ret = script_execute_function (state, function, + NULL, NULL); script_obj_unref (ret.object); } @@ -250,6 +256,7 @@ void script_lib_plymouth_on_display_password (script_state_t *state, script_obj_t *bullets_obj = script_obj_new_number (bullets); script_return_t ret = script_execute_function (state, function, + NULL, prompt_obj, bullets_obj, NULL); @@ -271,6 +278,7 @@ void script_lib_plymouth_on_display_question (script_state_t *state, script_obj_t *entry_text_obj = script_obj_new_string (entry_text); script_return_t ret = script_execute_function (state, function, + NULL, prompt_obj, entry_text_obj, NULL); @@ -290,6 +298,7 @@ void script_lib_plymouth_on_message (script_state_t *state, script_obj_t *new_message_obj = script_obj_new_string (message); script_return_t ret = script_execute_function (state, function, + NULL, new_message_obj, NULL); script_obj_unref (new_message_obj); -- 2.47.3