diff options
author | Charlie Brej <cbrej@cs.man.ac.uk> | 2009-07-02 16:41:48 +0100 |
---|---|---|
committer | Charlie Brej <cbrej@cs.man.ac.uk> | 2009-07-02 16:41:48 +0100 |
commit | 51970a443479adece9bf9e098e6a27c5ff334d01 (patch) | |
tree | 66fcae733d825204a5c55bf57e0d4a9c1b66063c | |
parent | f40cacbd7c25d1729bfe0d2e12cb010013d445be (diff) |
[script] Use the helper hash element extraction functions
This should simplify the code a little as it removes the need to play with
objects which must be unreffed.
-rw-r--r-- | src/plugins/splash/script/script-lib-image.c | 20 | ||||
-rw-r--r-- | src/plugins/splash/script/script-lib-math.c | 19 | ||||
-rw-r--r-- | src/plugins/splash/script/script-lib-sprite.c | 30 | ||||
-rw-r--r-- | src/plugins/splash/script/script-object.c | 38 | ||||
-rw-r--r-- | src/plugins/splash/script/script-object.h | 5 |
5 files changed, 63 insertions, 49 deletions
diff --git a/src/plugins/splash/script/script-lib-image.c b/src/plugins/splash/script/script-lib-image.c index bef19cd2..1c501ad4 100644 --- a/src/plugins/splash/script/script-lib-image.c +++ b/src/plugins/splash/script/script-lib-image.c @@ -30,11 +30,8 @@ static script_return image_new (script_state* state, void* user_data) { script_lib_image_data_t* data = user_data; script_obj* reply; - char* filename; char* path_filename; - - script_obj* script_obj_filename = script_obj_hash_get_element (state->local, "filename"); - filename = script_obj_as_string(script_obj_filename); + char* filename = script_obj_hash_get_string (state->local, "filename"); char* test_string = filename; char* prefix_string = "special://"; @@ -52,8 +49,6 @@ static script_return image_new (script_state* state, void* user_data) asprintf(&path_filename, "%s/%s", data->image_dir, filename); - script_obj_unref(script_obj_filename); - ply_image_t *image = ply_image_new (path_filename); if (ply_image_load (image)){ reply = script_obj_new_native (image, data->class); @@ -106,19 +101,18 @@ static script_return image_rotate (script_state* state, void* user_data) { script_lib_image_data_t* data = user_data; script_obj* script_obj_image = script_obj_hash_get_element (state->local, "image"); - script_obj* script_obj_angle = script_obj_hash_get_element (state->local, "angle"); script_obj_deref(&script_obj_image); + float angle = script_obj_hash_get_float (state->local, "angle"); script_obj* reply; if (script_obj_image->type == SCRIPT_OBJ_TYPE_NATIVE && script_obj_image->data.native.class == data->class){ ply_image_t *image = script_obj_image->data.native.object_data; - ply_image_t *new_image = ply_image_rotate (image, ply_image_get_width (image)/2, ply_image_get_height (image)/2, script_obj_as_float (script_obj_angle)); + ply_image_t *new_image = ply_image_rotate (image, ply_image_get_width (image)/2, ply_image_get_height (image)/2, angle); reply = script_obj_new_native (new_image, data->class); } else reply = script_obj_new_null (); script_obj_unref(script_obj_image); - script_obj_unref(script_obj_angle); return (script_return){SCRIPT_RETURN_TYPE_RETURN, reply}; } @@ -128,21 +122,19 @@ static script_return image_scale (script_state* state, void* user_data) { script_lib_image_data_t* data = user_data; script_obj* script_obj_image = script_obj_hash_get_element (state->local, "image"); - script_obj* script_obj_width = script_obj_hash_get_element (state->local, "width"); - script_obj* script_obj_height = script_obj_hash_get_element (state->local, "height"); + int width = script_obj_hash_get_int (state->local, "width"); + int height = script_obj_hash_get_int (state->local, "height"); script_obj_deref(&script_obj_image); script_obj* reply; if (script_obj_image->type == SCRIPT_OBJ_TYPE_NATIVE && script_obj_image->data.native.class == data->class){ ply_image_t *image = script_obj_image->data.native.object_data; - ply_image_t *new_image = ply_image_resize (image, script_obj_as_int (script_obj_width), script_obj_as_int (script_obj_height)); + ply_image_t *new_image = ply_image_resize (image, width, height); reply = script_obj_new_native (new_image, data->class); } else reply = script_obj_new_null (); script_obj_unref(script_obj_image); - script_obj_unref(script_obj_width); - script_obj_unref(script_obj_height); return (script_return){SCRIPT_RETURN_TYPE_RETURN, reply}; } diff --git a/src/plugins/splash/script/script-lib-math.c b/src/plugins/splash/script/script-lib-math.c index f10f5001..02b7bf9a 100644 --- a/src/plugins/splash/script/script-lib-math.c +++ b/src/plugins/splash/script/script-lib-math.c @@ -21,24 +21,17 @@ static script_return script_lib_math_float_from_float_function (script_state* state, void* user_data) { float (*function) (float) = user_data; - script_obj* obj = script_obj_hash_get_element (state->local, "value"); - script_obj* reply; - float reply_float = function(script_obj_as_float(obj)); - script_obj_unref(obj); - if (isnan(reply_float)) reply = script_obj_new_null (); - else reply = script_obj_new_float (reply_float); - return (script_return){SCRIPT_RETURN_TYPE_RETURN, reply}; + float value = script_obj_hash_get_float (state->local, "value"); + float reply_float = function(value); + return (script_return){SCRIPT_RETURN_TYPE_RETURN, script_obj_new_float (reply_float)}; } static script_return script_lib_math_int_from_float_function (script_state* state, void* user_data) { int (*function) (float) = user_data; - script_obj* obj = script_obj_hash_get_element (state->local, "value"); - script_obj* reply; - float reply_int = function(script_obj_as_float(obj)); - script_obj_unref(obj); - reply = script_obj_new_int (reply_int); - return (script_return){SCRIPT_RETURN_TYPE_RETURN, reply}; + float value = script_obj_hash_get_float (state->local, "value"); + float reply_int = function(value); + return (script_return){SCRIPT_RETURN_TYPE_RETURN, script_obj_new_int (reply_int)}; } diff --git a/src/plugins/splash/script/script-lib-sprite.c b/src/plugins/splash/script/script-lib-sprite.c index 520048a6..86c72105 100644 --- a/src/plugins/splash/script/script-lib-sprite.c +++ b/src/plugins/splash/script/script-lib-sprite.c @@ -79,23 +79,19 @@ static script_return sprite_set_image (script_state* state, void* user_data) return (script_return){SCRIPT_RETURN_TYPE_RETURN, script_obj_new_null ()}; } - - static script_return sprite_set_x (script_state* state, void* user_data) { script_lib_sprite_data_t* data = user_data; script_obj* script_obj_sprite = script_obj_hash_get_element (state->local, "sprite"); script_obj_deref(&script_obj_sprite); - script_obj* script_obj_value = script_obj_hash_get_element (state->local, "value"); - script_obj_deref(&script_obj_value); + int value = script_obj_hash_get_int (state->local, "value"); if (script_obj_sprite->type == SCRIPT_OBJ_TYPE_NATIVE && script_obj_sprite->data.native.class == data->class){ sprite_t *sprite = script_obj_sprite->data.native.object_data; - sprite->x = script_obj_as_int(script_obj_value); + sprite->x = value; } script_obj_unref(script_obj_sprite); - script_obj_unref(script_obj_value); return (script_return){SCRIPT_RETURN_TYPE_RETURN, script_obj_new_null ()}; } @@ -104,16 +100,14 @@ static script_return sprite_set_y (script_state* state, void* user_data) script_lib_sprite_data_t* data = user_data; script_obj* script_obj_sprite = script_obj_hash_get_element (state->local, "sprite"); script_obj_deref(&script_obj_sprite); - script_obj* script_obj_value = script_obj_hash_get_element (state->local, "value"); - script_obj_deref(&script_obj_value); + int value = script_obj_hash_get_int (state->local, "value"); if (script_obj_sprite->type == SCRIPT_OBJ_TYPE_NATIVE && script_obj_sprite->data.native.class == data->class){ sprite_t *sprite = script_obj_sprite->data.native.object_data; - sprite->y = script_obj_as_int(script_obj_value); + sprite->y = value; } script_obj_unref(script_obj_sprite); - script_obj_unref(script_obj_value); return (script_return){SCRIPT_RETURN_TYPE_RETURN, script_obj_new_null ()}; } @@ -122,41 +116,33 @@ static script_return sprite_set_z (script_state* state, void* user_data) script_lib_sprite_data_t* data = user_data; script_obj* script_obj_sprite = script_obj_hash_get_element (state->local, "sprite"); script_obj_deref(&script_obj_sprite); - script_obj* script_obj_value = script_obj_hash_get_element (state->local, "value"); - script_obj_deref(&script_obj_value); + int value = script_obj_hash_get_int (state->local, "value"); if (script_obj_sprite->type == SCRIPT_OBJ_TYPE_NATIVE && script_obj_sprite->data.native.class == data->class){ sprite_t *sprite = script_obj_sprite->data.native.object_data; - sprite->z = script_obj_as_int(script_obj_value); + sprite->z = value; } script_obj_unref(script_obj_sprite); - script_obj_unref(script_obj_value); return (script_return){SCRIPT_RETURN_TYPE_RETURN, script_obj_new_null ()}; } - - static script_return sprite_set_opacity (script_state* state, void* user_data) { script_lib_sprite_data_t* data = user_data; script_obj* script_obj_sprite = script_obj_hash_get_element (state->local, "sprite"); script_obj_deref(&script_obj_sprite); - script_obj* script_obj_value = script_obj_hash_get_element (state->local, "value"); - script_obj_deref(&script_obj_value); + float value = script_obj_hash_get_float (state->local, "value"); if (script_obj_sprite->type == SCRIPT_OBJ_TYPE_NATIVE && script_obj_sprite->data.native.class == data->class){ sprite_t *sprite = script_obj_sprite->data.native.object_data; - sprite->opacity = script_obj_as_float(script_obj_value); + sprite->opacity = value; } script_obj_unref(script_obj_sprite); - script_obj_unref(script_obj_value); return (script_return){SCRIPT_RETURN_TYPE_RETURN, script_obj_new_null ()}; } - - static script_return sprite_window_get_width (script_state* state, void* user_data) { script_lib_sprite_data_t* data = user_data; diff --git a/src/plugins/splash/script/script-object.c b/src/plugins/splash/script/script-object.c index 6d8cfd53..d9e45b08 100644 --- a/src/plugins/splash/script/script-object.c +++ b/src/plugins/splash/script/script-object.c @@ -210,6 +210,7 @@ script_obj* script_obj_new_int (int number) script_obj* script_obj_new_float (float number) { + if (isnan(number)) return script_obj_new_null (); script_obj* obj = malloc(sizeof(script_obj)); obj->type = SCRIPT_OBJ_TYPE_FLOAT; obj->refcount = 1; @@ -430,6 +431,12 @@ bool script_obj_is_native_of_class (script_obj* obj, script_obj_native_class* cl return (obj->type == SCRIPT_OBJ_TYPE_NATIVE && obj->data.native.class == class); } +bool script_obj_is_native_of_class_name (script_obj* obj, char* class_name) +{ + obj = script_obj_deref_direct(obj); + return (obj->type == SCRIPT_OBJ_TYPE_NATIVE && obj->data.native.class->name == class_name); +} + @@ -487,6 +494,37 @@ script_obj* script_obj_hash_get_element (script_obj* hash, const char* name) return obj; } +int script_obj_hash_get_int (script_obj* hash, const char* name) +{ + script_obj* obj = script_obj_hash_get_element (hash, name); + int reply = script_obj_as_int (obj); + script_obj_unref (obj); + return reply; +} + +float script_obj_hash_get_float (script_obj* hash, const char* name) +{ + script_obj* obj = script_obj_hash_get_element (hash, name); + float reply = script_obj_as_float (obj); + script_obj_unref (obj); + return reply; +} + +bool script_obj_hash_get_bool (script_obj* hash, const char* name) +{ + script_obj* obj = script_obj_hash_get_element (hash, name); + bool reply = script_obj_as_bool (obj); + script_obj_unref (obj); + return reply; +} + +char* script_obj_hash_get_string (script_obj* hash, const char* name) +{ + script_obj* obj = script_obj_hash_get_element (hash, name); + char* reply = script_obj_as_string (obj); + script_obj_unref (obj); + return reply; +} void script_obj_hash_add_element (script_obj* hash, script_obj* element, const char* name) { diff --git a/src/plugins/splash/script/script-object.h b/src/plugins/splash/script/script-object.h index 31da9433..fb2103c0 100644 --- a/src/plugins/splash/script/script-object.h +++ b/src/plugins/splash/script/script-object.h @@ -30,8 +30,13 @@ bool script_obj_is_hash (script_obj* obj); bool script_obj_is_function (script_obj* obj); bool script_obj_is_native (script_obj* obj); bool script_obj_is_native_of_class (script_obj* obj, script_obj_native_class* class); +bool script_obj_is_native_of_class_name (script_obj* obj, char* class_name); void script_obj_assign (script_obj* obj_a, script_obj* obj_b); script_obj* script_obj_hash_get_element (script_obj* hash, const char* name); +int script_obj_hash_get_int (script_obj* hash, const char* name); +float script_obj_hash_get_float (script_obj* hash, const char* name); +bool script_obj_hash_get_bool (script_obj* hash, const char* name); +char* script_obj_hash_get_string (script_obj* hash, const char* name); void script_obj_hash_add_element (script_obj* hash, script_obj* element, const char* name); script_obj* script_obj_plus (script_obj* script_obj_a_in, script_obj* script_obj_b_in); script_obj* script_obj_minus (script_obj* script_obj_a_in, script_obj* script_obj_b_in); |