]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
[script] Use access functions when accessing variables
authorCharlie Brej <cbrej@cs.man.ac.uk>
Tue, 1 Sep 2009 20:19:51 +0000 (21:19 +0100)
committerCharlie Brej <cbrej@cs.man.ac.uk>
Tue, 1 Sep 2009 20:19:51 +0000 (21:19 +0100)
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.

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

index 37a027a5492ccfc914c982e492dc0b64f01b4416..6eef8342f6b110e2dc6a9969157f3d2ac2a9dc49 100644 (file)
@@ -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;
 }
 
index a7ce5144e9eab7a5e9ce33aec91c260dc3c8b71b..e37a2f83dc65a8f1df2a713cc8acf7a5a1958d2c 100644 (file)
@@ -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,
index af166825d606d5915d5efc23bdb433c2db47d3ae..526372ef54df803bb4101b710375417510500e68 100644 (file)
@@ -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,