]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
[script] Make "this" (the current object) a part of the status
authorCharlie Brej <cbrej@cs.man.ac.uk>
Wed, 9 Sep 2009 16:28:40 +0000 (17:28 +0100)
committerCharlie Brej <cbrej@cs.man.ac.uk>
Wed, 9 Sep 2009 20:17:08 +0000 (21:17 +0100)
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;

src/plugins/splash/script/script-execute.c
src/plugins/splash/script/script-object.c
src/plugins/splash/script/script-parse.c
src/plugins/splash/script/script.c
src/plugins/splash/script/script.h

index 5814adeac64bd08820b12c1898fc8240c4430c45..731f6714aafa061f24a3ededc95e04a3587a0031 100644 (file)
@@ -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);
index 3e0f3d6a73c2813125f81b7a2d55c4d86df9f454..1cf52636eeb600d860739af3620e3c9df86a431a 100644 (file)
@@ -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);
index 8c3d478e1ed62d77f05b69ff8ffd090edf9a26ff..b2799776dc8fd73f87d577cb7838083561930d03 100644 (file)
@@ -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:
index 525cb083316c4395ec0f6a4711ce639648fcc1fb..635a8b4a47df4d9373b43e145c3dee54edf461b5 100644 (file)
@@ -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);
 }
 
index 0d021ce9d28efc2955dfac13da337a9aab266a9b..1e984f79b996d232910b7235964d9ea32dd38bef 100644 (file)
@@ -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 */