diff options
Diffstat (limited to 'lib/math.c')
-rw-r--r-- | lib/math.c | 155 |
1 files changed, 67 insertions, 88 deletions
@@ -1,5 +1,5 @@ /* - * Copyright (C) 2020 Jo-Philipp Wich <jo@mein.io> + * Copyright (C) 2020-2021 Jo-Philipp Wich <jo@mein.io> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -14,172 +14,151 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#include "../module.h" - #include <math.h> #include <sys/time.h> -static const struct uc_ops *ops; - -static double -to_double(struct json_object *v) -{ - int64_t n; - double d; - - return (ops->cast_number(v, &n, &d) == json_type_double) ? d : (double)n; -} - -static int64_t -to_int64(struct json_object *v) -{ - int64_t n; - double d; +#include "../module.h" - return (ops->cast_number(v, &n, &d) == json_type_double) ? (int64_t)d : n; -} +static bool srand_called = false; -static struct json_object * -uc_abs(struct uc_state *s, uint32_t off, struct json_object *args) +static json_object * +uc_abs(uc_vm *vm, size_t nargs) { - struct json_object *v = json_object_array_get_idx(args, 0); + json_object *v = uc_get_arg(0); enum json_type t; int64_t n; double d; if (json_object_is_type(v, json_type_null)) - return ops->new_double(NAN); + return uc_alloc_double(NAN); - t = ops->cast_number(v, &n, &d); + t = uc_to_number(v, &n, &d); if (t == json_type_double) - return (isnan(d) || d < 0) ? ops->new_double(-d) : json_object_get(v); + return (isnan(d) || d < 0) ? uc_alloc_double(-d) : json_object_get(v); return (n < 0) ? json_object_new_int64(-n) : json_object_get(v); } -static struct json_object * -uc_atan2(struct uc_state *s, uint32_t off, struct json_object *args) +static json_object * +uc_atan2(uc_vm *vm, size_t nargs) { - double d1 = to_double(json_object_array_get_idx(args, 0)); - double d2 = to_double(json_object_array_get_idx(args, 1)); + double d1 = uc_to_double(uc_get_arg(0)); + double d2 = uc_to_double(uc_get_arg(1)); if (isnan(d1) || isnan(d2)) - return ops->new_double(NAN); + return uc_alloc_double(NAN); - return ops->new_double(atan2(d1, d2)); + return uc_alloc_double(atan2(d1, d2)); } -static struct json_object * -uc_cos(struct uc_state *s, uint32_t off, struct json_object *args) +static json_object * +uc_cos(uc_vm *vm, size_t nargs) { - double d = to_double(json_object_array_get_idx(args, 0)); + double d = uc_to_double(uc_get_arg(0)); if (isnan(d)) - return ops->new_double(NAN); + return uc_alloc_double(NAN); - return ops->new_double(cos(d)); + return uc_alloc_double(cos(d)); } -static struct json_object * -uc_exp(struct uc_state *s, uint32_t off, struct json_object *args) +static json_object * +uc_exp(uc_vm *vm, size_t nargs) { - double d = to_double(json_object_array_get_idx(args, 0)); + double d = uc_to_double(uc_get_arg(0)); if (isnan(d)) - return ops->new_double(NAN); + return uc_alloc_double(NAN); - return ops->new_double(exp(d)); + return uc_alloc_double(exp(d)); } -static struct json_object * -uc_log(struct uc_state *s, uint32_t off, struct json_object *args) +static json_object * +uc_log(uc_vm *vm, size_t nargs) { - double d = to_double(json_object_array_get_idx(args, 0)); + double d = uc_to_double(uc_get_arg(0)); if (isnan(d)) - return ops->new_double(NAN); + return uc_alloc_double(NAN); - return ops->new_double(log(d)); + return uc_alloc_double(log(d)); } -static struct json_object * -uc_sin(struct uc_state *s, uint32_t off, struct json_object *args) +static json_object * +uc_sin(uc_vm *vm, size_t nargs) { - double d = to_double(json_object_array_get_idx(args, 0)); + double d = uc_to_double(uc_get_arg(0)); if (isnan(d)) - return ops->new_double(NAN); + return uc_alloc_double(NAN); - return ops->new_double(sin(d)); + return uc_alloc_double(sin(d)); } -static struct json_object * -uc_sqrt(struct uc_state *s, uint32_t off, struct json_object *args) +static json_object * +uc_sqrt(uc_vm *vm, size_t nargs) { - double d = to_double(json_object_array_get_idx(args, 0)); + double d = uc_to_double(uc_get_arg(0)); if (isnan(d)) - return ops->new_double(NAN); + return uc_alloc_double(NAN); - return ops->new_double(sqrt(d)); + return uc_alloc_double(sqrt(d)); } -static struct json_object * -uc_pow(struct uc_state *s, uint32_t off, struct json_object *args) +static json_object * +uc_pow(uc_vm *vm, size_t nargs) { - double x = to_double(json_object_array_get_idx(args, 0)); - double y = to_double(json_object_array_get_idx(args, 1)); + double x = uc_to_double(uc_get_arg(0)); + double y = uc_to_double(uc_get_arg(1)); if (isnan(x) || isnan(y)) - return ops->new_double(NAN); + return uc_alloc_double(NAN); - return ops->new_double(pow(x, y)); + return uc_alloc_double(pow(x, y)); } -static struct json_object * -uc_rand(struct uc_state *s, uint32_t off, struct json_object *args) +static json_object * +uc_rand(uc_vm *vm, size_t nargs) { struct timeval tv; - if (!s->srand_called) { + if (!srand_called) { gettimeofday(&tv, NULL); srand((tv.tv_sec * 1000) + (tv.tv_usec / 1000)); - s->srand_called = true; + srand_called = true; } return json_object_new_int64(rand()); } -static struct json_object * -uc_srand(struct uc_state *s, uint32_t off, struct json_object *args) +static json_object * +uc_srand(uc_vm *vm, size_t nargs) { - - int64_t n = to_int64(json_object_array_get_idx(args, 0)); + int64_t n = uc_to_int64(uc_get_arg(0)); srand((unsigned int)n); - s->srand_called = true; + srand_called = true; return NULL; } -static const struct { const char *name; uc_c_fn *func; } global_fns[] = { - { "abs", uc_abs }, - { "atan2", uc_atan2 }, - { "cos", uc_cos }, - { "exp", uc_exp }, - { "log", uc_log }, - { "sin", uc_sin }, - { "sqrt", uc_sqrt }, - { "pow", uc_pow }, - { "rand", uc_rand }, - { "srand", uc_srand }, +static const uc_cfunction_list math_fns[] = { + { "abs", uc_abs }, + { "atan2", uc_atan2 }, + { "cos", uc_cos }, + { "exp", uc_exp }, + { "log", uc_log }, + { "sin", uc_sin }, + { "sqrt", uc_sqrt }, + { "pow", uc_pow }, + { "rand", uc_rand }, + { "srand", uc_srand }, }; -void uc_module_init(const struct uc_ops *ut, struct uc_state *s, struct json_object *scope) +void uc_module_init(uc_prototype *scope) { - ops = ut; - - register_functions(s, ops, global_fns, scope); + uc_add_proto_functions(scope, math_fns); } |