From d5b25f942147b09511d77d5470cd38a1e1643fb9 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Sun, 11 Jul 2021 07:18:37 +0200 Subject: treewide: harmonize function naming - Ensure that most functions follow the subject_verb naming schema - Move type related function from value.c to types.c - Rename value.c to vallist.c Signed-off-by: Jo-Philipp Wich --- include/ucode/chunk.h | 2 +- include/ucode/lexer.h | 2 +- include/ucode/lib.h | 70 ++++++++++++++++--------------------------------- include/ucode/types.h | 33 +++++++++++++++++++++-- include/ucode/vallist.h | 51 +++++++++++++++++++++++++++++++++++ include/ucode/value.h | 60 ------------------------------------------ 6 files changed, 106 insertions(+), 112 deletions(-) create mode 100644 include/ucode/vallist.h delete mode 100644 include/ucode/value.h (limited to 'include') diff --git a/include/ucode/chunk.h b/include/ucode/chunk.h index 458af1f..0005e3c 100644 --- a/include/ucode/chunk.h +++ b/include/ucode/chunk.h @@ -20,7 +20,7 @@ #include #include -#include "value.h" +#include "vallist.h" #include "util.h" #include "types.h" diff --git a/include/ucode/lexer.h b/include/ucode/lexer.h index 1b19b60..ee8a0a5 100644 --- a/include/ucode/lexer.h +++ b/include/ucode/lexer.h @@ -170,6 +170,6 @@ uc_token_t *uc_lexer_next_token(uc_lexer_t *lex); bool utf8enc(char **out, int *rem, int code); const char * -uc_get_tokenname(unsigned type); +uc_tokenname(unsigned type); #endif /* __LEXER_H_ */ diff --git a/include/ucode/lib.h b/include/ucode/lib.h index 7eeae9f..1f30cba 100644 --- a/include/ucode/lib.h +++ b/include/ucode/lib.h @@ -20,31 +20,32 @@ #include "vm.h" #include "lexer.h" + typedef struct { const char *name; uc_cfn_ptr_t func; -} uc_cfunction_list_t; +} uc_function_list_t; -extern const uc_cfunction_list_t uc_stdlib_functions[]; +extern const uc_function_list_t uc_stdlib_functions[]; -void uc_load_stdlib(uc_value_t *scope); +void uc_stdlib_load(uc_value_t *scope); -bool format_source_context(uc_stringbuf_t *buf, uc_source_t *src, size_t off, bool compact); -bool format_error_context(uc_stringbuf_t *buf, uc_source_t *src, uc_value_t *stacktrace, size_t off); +bool uc_source_context_format(uc_stringbuf_t *buf, uc_source_t *src, size_t off, bool compact); +bool uc_error_context_format(uc_stringbuf_t *buf, uc_source_t *src, uc_value_t *stacktrace, size_t off); /* vm helper */ static inline void * -_uc_get_self(uc_vm_t *vm, const char *expected_type) +_uc_fn_this(uc_vm_t *vm, const char *expected_type) { return ucv_ressource_dataptr(vm->callframes.entries[vm->callframes.count - 1].ctx, expected_type); } -#define uc_get_self(...) _uc_get_self(vm, __VA_ARGS__) +#define uc_fn_this(...) _uc_fn_this(vm, __VA_ARGS__) static inline uc_value_t * -_uc_get_arg(uc_vm_t *vm, size_t nargs, size_t n) +_uc_fn_arg(uc_vm_t *vm, size_t nargs, size_t n) { if (n >= nargs) return NULL; @@ -52,50 +53,23 @@ _uc_get_arg(uc_vm_t *vm, size_t nargs, size_t n) return uc_vm_stack_peek(vm, nargs - n - 1); } -#define uc_get_arg(...) _uc_get_arg(vm, nargs, __VA_ARGS__) +#define uc_fn_arg(...) _uc_fn_arg(vm, nargs, __VA_ARGS__) #define uc_call(nargs) uc_vm_call(vm, false, nargs) -#define uc_push_val(val) uc_vm_stack_push(vm, val) -#define uc_pop_val() uc_vm_stack_pop(vm) +#define uc_value_push(val) uc_vm_stack_push(vm, val) +#define uc_value_pop() uc_vm_stack_pop(vm) -/* value helper */ +/* ressource type helper */ static inline uc_value_t * -uc_alloc_ressource(uc_ressource_type_t *type, void *data) +uc_ressource_new(uc_ressource_type_t *type, void *data) { return ucv_ressource_new(type, data); } -static inline uc_type_t -uc_to_number(uc_value_t *v, int64_t *n, double *d) -{ - return uc_cast_number(v, n, d); -} - -static inline double -uc_to_double(uc_value_t *v) -{ - int64_t n; - double d; - - return (uc_cast_number(v, &n, &d) == UC_DOUBLE) ? d : (double)n; -} - -static inline int64_t -uc_to_int64(uc_value_t *v) -{ - int64_t n; - double d; - - return (uc_cast_number(v, &n, &d) == UC_DOUBLE) ? (int64_t)d : n; -} - - -/* ressource type helper */ - static inline uc_ressource_type_t * -_uc_declare_type(uc_vm_t *vm, const char *name, const uc_cfunction_list_t *list, size_t len, void (*freefn)(void *)) +_uc_type_declare(uc_vm_t *vm, const char *name, const uc_function_list_t *list, size_t len, void (*freefn)(void *)) { uc_value_t *proto = ucv_object_new(NULL); @@ -106,27 +80,27 @@ _uc_declare_type(uc_vm_t *vm, const char *name, const uc_cfunction_list_t *list, return ucv_ressource_type_add(vm, name, proto, freefn); } -#define uc_declare_type(vm, name, functions, freefn) \ - _uc_declare_type(vm, name, functions, ARRAY_SIZE(functions), freefn) +#define uc_type_declare(vm, name, functions, freefn) \ + _uc_type_declare(vm, name, functions, ARRAY_SIZE(functions), freefn) /* function helper */ -#define uc_add_function(object, name, function) \ +#define uc_function_register(object, name, function) \ ucv_object_add(object, name, ucv_cfunction_new(name, function)) static inline bool -_uc_add_functions(uc_value_t *object, const uc_cfunction_list_t *list, size_t len) +_uc_function_list_register(uc_value_t *object, const uc_function_list_t *list, size_t len) { bool rv = true; while (len-- > 0) - rv &= uc_add_function(object, list[len].name, list[len].func); + rv &= uc_function_register(object, list[len].name, list[len].func); return rv; } -#define uc_add_functions(object, functions) \ - _uc_add_functions(object, functions, ARRAY_SIZE(functions)) +#define uc_function_list_register(object, functions) \ + _uc_function_list_register(object, functions, ARRAY_SIZE(functions)) #endif /* __LIB_H_ */ diff --git a/include/ucode/types.h b/include/ucode/types.h index da19f9b..7473f22 100644 --- a/include/ucode/types.h +++ b/include/ucode/types.h @@ -369,6 +369,27 @@ void ucv_to_stringbuf_formatted(uc_vm_t *, uc_stringbuf_t *, uc_value_t *, size_ #define ucv_to_jsonstring(vm, val) ucv_to_jsonstring_formatted(vm, val, '\1', 0) #define ucv_to_stringbuf(vm, buf, val, json) ucv_to_stringbuf_formatted(vm, buf, val, 0, json ? '\1' : '\0', 0) +uc_type_t ucv_cast_number(uc_value_t *, int64_t *, double *); + +static inline double +ucv_to_double(uc_value_t *v) +{ + int64_t n; + double d; + + return (ucv_cast_number(v, &n, &d) == UC_DOUBLE) ? d : (double)n; +} + +static inline int64_t +ucv_to_integer(uc_value_t *v) +{ + int64_t n; + double d; + + return (ucv_cast_number(v, &n, &d) == UC_DOUBLE) ? (int64_t)d : n; +} + + static inline bool ucv_is_callable(uc_value_t *uv) { @@ -412,6 +433,16 @@ ucv_is_scalar(uc_value_t *uv) } } +bool ucv_is_equal(uc_value_t *, uc_value_t *); +bool ucv_is_truish(uc_value_t *); + +bool ucv_compare(int, uc_value_t *, uc_value_t *); + +uc_value_t *ucv_key_get(uc_vm_t *, uc_value_t *, uc_value_t *); +uc_value_t *ucv_key_set(uc_vm_t *, uc_value_t *, uc_value_t *, uc_value_t *); +bool ucv_key_delete(uc_vm_t *, uc_value_t *, uc_value_t *); + + static inline bool ucv_is_marked(uc_value_t *uv) { @@ -432,8 +463,6 @@ ucv_clear_mark(uc_value_t *uv) uv->mark = false; } -bool ucv_equal(uc_value_t *, uc_value_t *); - void ucv_gc(uc_vm_t *); void ucv_freeall(uc_vm_t *); diff --git a/include/ucode/vallist.h b/include/ucode/vallist.h new file mode 100644 index 0000000..a3d94e8 --- /dev/null +++ b/include/ucode/vallist.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2020-2021 Jo-Philipp Wich + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef __VALUE_H_ +#define __VALUE_H_ + +#include +#include +#include + +#ifdef JSONC + #include +#else + #include +#endif + +#include + +#include "types.h" + +typedef enum { + TAG_INVAL = 0, + TAG_NUM = 1, + TAG_LNUM = 2, + TAG_DBL = 3, + TAG_STR = 4, + TAG_LSTR = 5, + TAG_PTR = 6 +} uc_value_type_t; + +void uc_vallist_init(uc_value_list_t *list); +void uc_vallist_free(uc_value_list_t *list); + +ssize_t uc_vallist_add(uc_value_list_t *list, uc_value_t *value); +uc_value_type_t uc_vallist_type(uc_value_list_t *list, size_t idx); +uc_value_t *uc_vallist_get(uc_value_list_t *list, size_t idx); + +#endif /* __VALUE_H_ */ diff --git a/include/ucode/value.h b/include/ucode/value.h deleted file mode 100644 index 04d37a9..0000000 --- a/include/ucode/value.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (C) 2020-2021 Jo-Philipp Wich - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef __VALUE_H_ -#define __VALUE_H_ - -#include -#include -#include - -#ifdef JSONC - #include -#else - #include -#endif - -#include - -#include "types.h" - -typedef enum { - TAG_INVAL = 0, - TAG_NUM = 1, - TAG_LNUM = 2, - TAG_DBL = 3, - TAG_STR = 4, - TAG_LSTR = 5, - TAG_PTR = 6 -} uc_value_type_t; - -bool uc_cmp(int how, uc_value_t *v1, uc_value_t *v2); -bool uc_val_is_truish(uc_value_t *val); - -uc_type_t uc_cast_number(uc_value_t *v, int64_t *n, double *d); - -uc_value_t *uc_getval(uc_vm_t *, uc_value_t *scope, uc_value_t *key); -uc_value_t *uc_setval(uc_vm_t *, uc_value_t *scope, uc_value_t *key, uc_value_t *val); -bool uc_delval(uc_vm_t *, uc_value_t *scope, uc_value_t *key); - -void uc_vallist_init(uc_value_list_t *list); -void uc_vallist_free(uc_value_list_t *list); - -ssize_t uc_vallist_add(uc_value_list_t *list, uc_value_t *value); -uc_value_type_t uc_vallist_type(uc_value_list_t *list, size_t idx); -uc_value_t *uc_vallist_get(uc_value_list_t *list, size_t idx); - -#endif /* __VALUE_H_ */ -- cgit v1.2.3