]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
[script] Add cos, sin, tan and square root functions to the script math library
authorCharlie Brej <cbrej@cs.man.ac.uk>
Sun, 28 Jun 2009 20:39:19 +0000 (21:39 +0100)
committerCharlie Brej <cbrej@cs.man.ac.uk>
Sun, 28 Jun 2009 20:39:19 +0000 (21:39 +0100)
src/plugins/splash/script/script-lib-math.c

index c623e7e42e759c8ceeceb165c3a785acfd6990c6..544b8f34ff74d906b4ce5b049fdeeaffc75c2e25 100644 (file)
@@ -9,6 +9,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <math.h>
 
 #include "config.h"
 
 #include "script-lib-math.string"
 
 
+static script_return script_lib_math_single_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};
+}
 
 script_lib_math_data_t* script_lib_math_setup(script_state *state)
 {
  script_lib_math_data_t* data = malloc(sizeof(script_lib_math_data_t));
  
+ script_add_native_function (state->global, "MathCos", script_lib_math_single_float_function, cosf, "value", NULL);
+ script_add_native_function (state->global, "MathSin", script_lib_math_single_float_function, sinf, "value", NULL);
+ script_add_native_function (state->global, "MathTan", script_lib_math_single_float_function, tanf, "value", NULL);
+ script_add_native_function (state->global, "MathSqrt",script_lib_math_single_float_function, sqrtf,"value", NULL);
  data->script_main_op = script_parse_string (script_lib_math_string);
  script_return ret = script_execute(state, data->script_main_op);
- if (ret.object) script_obj_unref(ret.object);                  // Throw anything sent back away
+ script_obj_unref(ret.object);
 
  return data;
 }