From 35af4ba4fc21a4b2357c50e6b02a2e3e4b236e88 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Wed, 21 Apr 2021 15:07:16 +0200 Subject: treewide: rework internal data type system Instead of relying on json_object values internally, use custom types to represent the different ucode value types which brings a number of advantages compared to the previous approach: - Due to the use of tagged pointers, small integer, string and bool values can be stored directly in the pointer addresses, vastly reducing required heap memory - Ability to create circular data structures such as `let o; o = { test: o };` - Ability to register custom `tostring()` function through prototypes - Initial mark/sweep GC implementation to tear down circular object graphs on VM deinit The change also paves the way for possible future extensions such as constant variables and meta methods for custom ressource types. Signed-off-by: Jo-Philipp Wich --- lib/uci.c | 431 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 215 insertions(+), 216 deletions(-) (limited to 'lib/uci.c') diff --git a/lib/uci.c b/lib/uci.c index be98214..fba7dad 100644 --- a/lib/uci.c +++ b/lib/uci.c @@ -22,7 +22,7 @@ #define err_return(err) do { last_error = err; return NULL; } while(0) static int last_error = 0; -static uc_ressource_type *cursor_type; +static uc_ressource_type_t *cursor_type; enum pkg_cmd { CMD_SAVE, @@ -30,11 +30,11 @@ enum pkg_cmd { CMD_REVERT }; -static json_object * +static uc_value_t * uc_uci_error(uc_vm *vm, size_t nargs) { char buf[sizeof("Unknown error: -9223372036854775808")]; - json_object *errmsg; + uc_value_t *errmsg; const char *errstr[] = { [UCI_ERR_MEM] = "Out of memory", @@ -49,12 +49,12 @@ uc_uci_error(uc_vm *vm, size_t nargs) if (last_error == 0) return NULL; - if (last_error >= 0 && last_error < ARRAY_SIZE(errstr)) { - errmsg = json_object_new_string(errstr[last_error]); + if (last_error >= 0 && (unsigned)last_error < ARRAY_SIZE(errstr)) { + errmsg = ucv_string_new(errstr[last_error]); } else { snprintf(buf, sizeof(buf), "Unknown error: %d", last_error); - errmsg = json_object_new_string(buf); + errmsg = ucv_string_new(buf); } last_error = 0; @@ -63,16 +63,16 @@ uc_uci_error(uc_vm *vm, size_t nargs) } -static json_object * +static uc_value_t * uc_uci_cursor(uc_vm *vm, size_t nargs) { - json_object *cdir = uc_get_arg(0); - json_object *sdir = uc_get_arg(1); + uc_value_t *cdir = uc_get_arg(0); + uc_value_t *sdir = uc_get_arg(1); struct uci_context *c; int rv; - if ((cdir && !json_object_is_type(cdir, json_type_string)) || - (sdir && !json_object_is_type(sdir, json_type_string))) + if ((cdir && ucv_type(cdir) != UC_STRING) || + (sdir && ucv_type(sdir) != UC_STRING)) err_return(UCI_ERR_INVAL); c = uci_alloc_context(); @@ -81,14 +81,14 @@ uc_uci_cursor(uc_vm *vm, size_t nargs) err_return(UCI_ERR_MEM); if (cdir) { - rv = uci_set_confdir(c, json_object_get_string(cdir)); + rv = uci_set_confdir(c, ucv_string_get(cdir)); if (rv) err_return(rv); } if (sdir) { - rv = uci_set_savedir(c, json_object_get_string(sdir)); + rv = uci_set_savedir(c, ucv_string_get(sdir)); if (rv) err_return(rv); @@ -98,54 +98,57 @@ uc_uci_cursor(uc_vm *vm, size_t nargs) } -static json_object * +static uc_value_t * uc_uci_load(uc_vm *vm, size_t nargs) { struct uci_context **c = uc_get_self("uci.cursor"); - json_object *conf = uc_get_arg(0); + uc_value_t *conf = uc_get_arg(0); struct uci_element *e; + char *s; if (!c || !*c) err_return(UCI_ERR_INVAL); - if (!json_object_is_type(conf, json_type_string)) + if (ucv_type(conf) != UC_STRING) err_return(UCI_ERR_INVAL); + s = ucv_string_get(conf); + uci_foreach_element(&(*c)->root, e) { - if (!strcmp(e->name, json_object_get_string(conf))) { + if (!strcmp(e->name, s)) { uci_unload(*c, uci_to_package(e)); break; } } - if (uci_load(*c, json_object_get_string(conf), NULL)) + if (uci_load(*c, s, NULL)) err_return((*c)->err); - return json_object_new_boolean(true); + return ucv_boolean_new(true); } -static json_object * +static uc_value_t * uc_uci_unload(uc_vm *vm, size_t nargs) { struct uci_context **c = uc_get_self("uci.cursor"); - json_object *conf = uc_get_arg(0); + uc_value_t *conf = uc_get_arg(0); struct uci_element *e; if (!c || !*c) err_return(UCI_ERR_INVAL); - if (!json_object_is_type(conf, json_type_string)) + if (ucv_type(conf) != UC_STRING) err_return(UCI_ERR_INVAL); uci_foreach_element(&(*c)->root, e) { - if (!strcmp(e->name, json_object_get_string(conf))) { + if (!strcmp(e->name, ucv_string_get(conf))) { uci_unload(*c, uci_to_package(e)); - return json_object_new_boolean(true); + return ucv_boolean_new(true); } } - return json_object_new_boolean(false); + return ucv_boolean_new(false); } static int @@ -177,22 +180,22 @@ lookup_ptr(struct uci_context *ctx, struct uci_ptr *ptr, bool extended) return uci_lookup_ptr(ctx, ptr, NULL, extended); } -static json_object * -option_to_json(struct uci_option *o) +static uc_value_t * +option_to_uval(uc_vm *vm, struct uci_option *o) { - json_object *arr; struct uci_element *e; + uc_value_t *arr; switch (o->type) { case UCI_TYPE_STRING: - return json_object_new_string(o->v.string); + return ucv_string_new(o->v.string); case UCI_TYPE_LIST: - arr = json_object_new_array(); + arr = ucv_array_new(vm); if (arr) uci_foreach_element(&o->v.list, e) - json_object_array_add(arr, json_object_new_string(e->name)); + ucv_array_push(arr, ucv_string_new(e->name)); return arr; @@ -201,36 +204,36 @@ option_to_json(struct uci_option *o) } } -static json_object * -section_to_json(struct uci_section *s, int index) +static uc_value_t * +section_to_uval(uc_vm *vm, struct uci_section *s, int index) { - json_object *so = json_object_new_object(); + uc_value_t *so = ucv_object_new(vm); struct uci_element *e; struct uci_option *o; if (!so) return NULL; - json_object_object_add(so, ".anonymous", json_object_new_boolean(s->anonymous)); - json_object_object_add(so, ".type", json_object_new_string(s->type)); - json_object_object_add(so, ".name", json_object_new_string(s->e.name)); + ucv_object_add(so, ".anonymous", ucv_boolean_new(s->anonymous)); + ucv_object_add(so, ".type", ucv_string_new(s->type)); + ucv_object_add(so, ".name", ucv_string_new(s->e.name)); if (index >= 0) - json_object_object_add(so, ".index", json_object_new_int64(index)); + ucv_object_add(so, ".index", ucv_int64_new(index)); uci_foreach_element(&s->options, e) { o = uci_to_option(e); - json_object_object_add(so, o->e.name, option_to_json(o)); + ucv_object_add(so, o->e.name, option_to_uval(vm, o)); } return so; } -static json_object * -package_to_json(struct uci_package *p) +static uc_value_t * +package_to_uval(uc_vm *vm, struct uci_package *p) { - json_object *po = json_object_new_object(); - json_object *so; + uc_value_t *po = ucv_object_new(vm); + uc_value_t *so; struct uci_element *e; int i = 0; @@ -238,37 +241,37 @@ package_to_json(struct uci_package *p) return NULL; uci_foreach_element(&p->sections, e) { - so = section_to_json(uci_to_section(e), i++); - json_object_object_add(po, e->name, so); + so = section_to_uval(vm, uci_to_section(e), i++); + ucv_object_add(po, e->name, so); } return po; } -static json_object * +static uc_value_t * uc_uci_get_any(uc_vm *vm, size_t nargs, bool all) { struct uci_context **c = uc_get_self("uci.cursor"); - json_object *conf = uc_get_arg(0); - json_object *sect = uc_get_arg(1); - json_object *opt = uc_get_arg(2); + uc_value_t *conf = uc_get_arg(0); + uc_value_t *sect = uc_get_arg(1); + uc_value_t *opt = uc_get_arg(2); struct uci_ptr ptr = {}; int rv; if (!c || !*c) err_return(UCI_ERR_INVAL); - if (!json_object_is_type(conf, json_type_string) || - (sect && !json_object_is_type(sect, json_type_string)) || - (opt && !json_object_is_type(opt, json_type_string))) + if ((ucv_type(conf) != UC_STRING) || + (sect && ucv_type(sect) != UC_STRING) || + (opt && ucv_type(opt) != UC_STRING)) err_return(UCI_ERR_INVAL); if ((!sect && !all) || (opt && all)) err_return(UCI_ERR_INVAL); - ptr.package = json_object_get_string(conf); - ptr.section = sect ? json_object_get_string(sect) : NULL; - ptr.option = opt ? json_object_get_string(opt) : NULL; + ptr.package = ucv_string_get(conf); + ptr.section = sect ? ucv_string_get(sect) : NULL; + ptr.option = opt ? ucv_string_get(opt) : NULL; rv = lookup_ptr(*c, &ptr, true); @@ -283,60 +286,60 @@ uc_uci_get_any(uc_vm *vm, size_t nargs, bool all) if (!ptr.s) err_return(UCI_ERR_NOTFOUND); - return section_to_json(ptr.s, -1); + return section_to_uval(vm, ptr.s, -1); } if (!ptr.p) err_return(UCI_ERR_NOTFOUND); - return package_to_json(ptr.p); + return package_to_uval(vm, ptr.p); } if (ptr.option) { if (!ptr.o) err_return(UCI_ERR_NOTFOUND); - return option_to_json(ptr.o); + return option_to_uval(vm, ptr.o); } if (!ptr.s) err_return(UCI_ERR_NOTFOUND); - return json_object_new_string(ptr.s->type); + return ucv_string_new(ptr.s->type); } -static json_object * +static uc_value_t * uc_uci_get(uc_vm *vm, size_t nargs) { return uc_uci_get_any(vm, nargs, false); } -static json_object * +static uc_value_t * uc_uci_get_all(uc_vm *vm, size_t nargs) { return uc_uci_get_any(vm, nargs, true); } -static json_object * +static uc_value_t * uc_uci_get_first(uc_vm *vm, size_t nargs) { struct uci_context **c = uc_get_self("uci.cursor"); - json_object *conf = uc_get_arg(0); - json_object *type = uc_get_arg(1); - json_object *opt = uc_get_arg(2); + uc_value_t *conf = uc_get_arg(0); + uc_value_t *type = uc_get_arg(1); + uc_value_t *opt = uc_get_arg(2); struct uci_package *p = NULL; struct uci_section *sc; struct uci_element *e; struct uci_ptr ptr = {}; int rv; - if (!json_object_is_type(conf, json_type_string) || - !json_object_is_type(type, json_type_string) || - (opt && !json_object_is_type(opt, json_type_string))) + if (ucv_type(conf) != UC_STRING || + ucv_type(type) != UC_STRING || + (opt && ucv_type(opt) != UC_STRING)) err_return(UCI_ERR_INVAL); uci_foreach_element(&(*c)->root, e) { - if (strcmp(e->name, json_object_get_string(conf))) + if (strcmp(e->name, ucv_string_get(conf))) continue; p = uci_to_package(e); @@ -349,15 +352,15 @@ uc_uci_get_first(uc_vm *vm, size_t nargs) uci_foreach_element(&p->sections, e) { sc = uci_to_section(e); - if (strcmp(sc->type, json_object_get_string(type))) + if (strcmp(sc->type, ucv_string_get(type))) continue; if (!opt) - return json_object_new_string(sc->e.name); + return ucv_string_new(sc->e.name); - ptr.package = json_object_get_string(conf); + ptr.package = ucv_string_get(conf); ptr.section = sc->e.name; - ptr.option = json_object_get_string(opt); + ptr.option = ucv_string_get(opt); ptr.p = p; ptr.s = sc; @@ -369,29 +372,29 @@ uc_uci_get_first(uc_vm *vm, size_t nargs) if (!(ptr.flags & UCI_LOOKUP_COMPLETE)) err_return(UCI_ERR_NOTFOUND); - return option_to_json(ptr.o); + return option_to_uval(vm, ptr.o); } err_return(UCI_ERR_NOTFOUND); } -static json_object * +static uc_value_t * uc_uci_add(uc_vm *vm, size_t nargs) { struct uci_context **c = uc_get_self("uci.cursor"); - json_object *conf = uc_get_arg(0); - json_object *type = uc_get_arg(1); + uc_value_t *conf = uc_get_arg(0); + uc_value_t *type = uc_get_arg(1); struct uci_element *e = NULL; struct uci_package *p = NULL; struct uci_section *sc = NULL; int rv; - if (!json_object_is_type(conf, json_type_string) || - !json_object_is_type(type, json_type_string)) + if (ucv_type(conf) != UC_STRING || + ucv_type(type) != UC_STRING) err_return(UCI_ERR_INVAL); uci_foreach_element(&(*c)->root, e) { - if (!strcmp(e->name, json_object_get_string(conf))) { + if (!strcmp(e->name, ucv_string_get(conf))) { p = uci_to_package(e); break; } @@ -400,73 +403,75 @@ uc_uci_add(uc_vm *vm, size_t nargs) if (!p) err_return(UCI_ERR_NOTFOUND); - rv = uci_add_section(*c, p, json_object_get_string(type), &sc); + rv = uci_add_section(*c, p, ucv_string_get(type), &sc); if (rv != UCI_OK) err_return(rv); else if (!sc) err_return(UCI_ERR_NOTFOUND); - return json_object_new_string(sc->e.name); + return ucv_string_new(sc->e.name); } static bool -json_to_value(json_object *val, const char **p, bool *is_list) +uval_to_uci(uc_vm *vm, uc_value_t *val, const char **p, bool *is_list) { - json_object *item; + uc_value_t *item; *p = NULL; if (is_list) *is_list = false; - switch (json_object_get_type(val)) { - case json_type_object: - return false; - - case json_type_array: - if (json_object_array_length(val) == 0) + switch (ucv_type(val)) { + case UC_ARRAY: + if (ucv_array_length(val) == 0) return false; - item = json_object_array_get_idx(val, 0); + item = ucv_array_get(val, 0); /* don't recurse */ - if (json_object_is_type(item, json_type_array)) + if (ucv_type(item) == UC_ARRAY) return false; if (is_list) *is_list = true; - return json_to_value(item, p, NULL); + return uval_to_uci(vm, item, p, NULL); - case json_type_boolean: - *p = json_object_get_boolean(val) ? "1" : "0"; + case UC_BOOLEAN: + *p = xstrdup(ucv_boolean_get(val) ? "1" : "0"); return true; - case json_type_null: + case UC_DOUBLE: + case UC_INTEGER: + case UC_STRING: + *p = ucv_to_string(vm, val); + /* fall through */ + + case UC_NULL: return true; default: - *p = json_object_get_string(val); - - return true; + return false; } } -static json_object * +static uc_value_t * uc_uci_set(uc_vm *vm, size_t nargs) { struct uci_context **c = uc_get_self("uci.cursor"); - json_object *conf = uc_get_arg(0); - json_object *sect = uc_get_arg(1); - json_object *opt = NULL, *val = NULL; + uc_value_t *conf = uc_get_arg(0); + uc_value_t *sect = uc_get_arg(1); + uc_value_t *opt = NULL, *val = NULL; struct uci_ptr ptr = {}; bool is_list = false; - int rv, i; + size_t i; + int rv; - if (!json_object_is_type(conf, json_type_string) || - !json_object_is_type(sect, json_type_string)) + if (ucv_type(conf) != UC_STRING || + ucv_type(sect) != UC_STRING) err_return(UCI_ERR_INVAL); switch (nargs) { @@ -475,7 +480,7 @@ uc_uci_set(uc_vm *vm, size_t nargs) opt = uc_get_arg(2); val = uc_get_arg(3); - if (!json_object_is_type(opt, json_type_string)) + if (ucv_type(opt) != UC_STRING) err_return(UCI_ERR_INVAL); break; @@ -484,7 +489,7 @@ uc_uci_set(uc_vm *vm, size_t nargs) case 3: val = uc_get_arg(2); - if (!json_object_is_type(val, json_type_string)) + if (ucv_type(val) != UC_STRING) err_return(UCI_ERR_INVAL); break; @@ -493,9 +498,9 @@ uc_uci_set(uc_vm *vm, size_t nargs) err_return(UCI_ERR_INVAL); } - ptr.package = json_object_get_string(conf); - ptr.section = json_object_get_string(sect); - ptr.option = opt ? json_object_get_string(opt) : NULL; + ptr.package = ucv_string_get(conf); + ptr.section = ucv_string_get(sect); + ptr.option = opt ? ucv_string_get(opt) : NULL; rv = lookup_ptr(*c, &ptr, true); @@ -505,18 +510,19 @@ uc_uci_set(uc_vm *vm, size_t nargs) if (!ptr.s && ptr.option) err_return(UCI_ERR_NOTFOUND); - if (!json_to_value(val, &ptr.value, &is_list)) + if (!uval_to_uci(vm, val, &ptr.value, &is_list)) err_return(UCI_ERR_INVAL); if (is_list) { /* if we got a one-element array, delete existing option (if any) * and iterate array at offset 0 */ - if (json_object_array_length(val) == 1) { + if (ucv_array_length(val) == 1) { i = 0; - if (ptr.o) { - ptr.value = NULL; + free((char *)ptr.value); + ptr.value = NULL; + if (ptr.o) { rv = uci_delete(*c, &ptr); if (rv != UCI_OK) @@ -529,16 +535,18 @@ uc_uci_set(uc_vm *vm, size_t nargs) i = 1; rv = uci_set(*c, &ptr); + free((char *)ptr.value); if (rv != UCI_OK) err_return(rv); } - for (; i < json_object_array_length(val); i++) { - if (!json_to_value(json_object_array_get_idx(val, i), &ptr.value, NULL)) + for (; i < ucv_array_length(val); i++) { + if (!uval_to_uci(vm, ucv_array_get(val, i), &ptr.value, NULL)) continue; rv = uci_add_list(*c, &ptr); + free((char *)ptr.value); if (rv != UCI_OK) err_return(rv); @@ -546,32 +554,33 @@ uc_uci_set(uc_vm *vm, size_t nargs) } else { rv = uci_set(*c, &ptr); + free((char *)ptr.value); if (rv != UCI_OK) err_return(rv); } - return json_object_new_boolean(true); + return ucv_boolean_new(true); } -static json_object * +static uc_value_t * uc_uci_delete(uc_vm *vm, size_t nargs) { struct uci_context **c = uc_get_self("uci.cursor"); - json_object *conf = uc_get_arg(0); - json_object *sect = uc_get_arg(1); - json_object *opt = uc_get_arg(2); + uc_value_t *conf = uc_get_arg(0); + uc_value_t *sect = uc_get_arg(1); + uc_value_t *opt = uc_get_arg(2); struct uci_ptr ptr = {}; int rv; - if (!json_object_is_type(conf, json_type_string) || - !json_object_is_type(sect, json_type_string) || - (opt && !json_object_is_type(opt, json_type_string))) + if (ucv_type(conf) != UC_STRING || + ucv_type(sect) != UC_STRING || + (opt && ucv_type(opt) != UC_STRING)) err_return(UCI_ERR_INVAL); - ptr.package = json_object_get_string(conf); - ptr.section = json_object_get_string(sect); - ptr.option = opt ? json_object_get_string(opt) : NULL; + ptr.package = ucv_string_get(conf); + ptr.section = ucv_string_get(sect); + ptr.option = opt ? ucv_string_get(opt) : NULL; rv = lookup_ptr(*c, &ptr, true); @@ -586,21 +595,21 @@ uc_uci_delete(uc_vm *vm, size_t nargs) if (rv != UCI_OK) err_return(rv); - return json_object_new_boolean(true); + return ucv_boolean_new(true); } -static json_object * +static uc_value_t * uc_uci_rename(uc_vm *vm, size_t nargs) { struct uci_context **c = uc_get_self("uci.cursor"); - json_object *conf = uc_get_arg(0); - json_object *sect = uc_get_arg(1); - json_object *opt = NULL, *val = NULL; + uc_value_t *conf = uc_get_arg(0); + uc_value_t *sect = uc_get_arg(1); + uc_value_t *opt = NULL, *val = NULL; struct uci_ptr ptr = {}; int rv; - if (!json_object_is_type(conf, json_type_string) || - !json_object_is_type(sect, json_type_string)) + if (ucv_type(conf) != UC_STRING || + ucv_type(sect) != UC_STRING) err_return(UCI_ERR_INVAL); switch (nargs) { @@ -609,8 +618,8 @@ uc_uci_rename(uc_vm *vm, size_t nargs) opt = uc_get_arg(2); val = uc_get_arg(3); - if (!json_object_is_type(opt, json_type_string) || - !json_object_is_type(val, json_type_string)) + if (ucv_type(opt) != UC_STRING || + ucv_type(val) != UC_STRING) err_return(UCI_ERR_INVAL); break; @@ -619,7 +628,7 @@ uc_uci_rename(uc_vm *vm, size_t nargs) case 3: val = uc_get_arg(2); - if (!json_object_is_type(val, json_type_string)) + if (ucv_type(val) != UC_STRING) err_return(UCI_ERR_INVAL); break; @@ -628,10 +637,10 @@ uc_uci_rename(uc_vm *vm, size_t nargs) err_return(UCI_ERR_INVAL); } - ptr.package = json_object_get_string(conf); - ptr.section = json_object_get_string(sect); - ptr.option = opt ? json_object_get_string(opt) : NULL; - ptr.value = json_object_get_string(val); + ptr.package = ucv_string_get(conf); + ptr.section = ucv_string_get(sect); + ptr.option = opt ? ucv_string_get(opt) : NULL; + ptr.value = ucv_string_get(val); rv = lookup_ptr(*c, &ptr, true); @@ -646,32 +655,32 @@ uc_uci_rename(uc_vm *vm, size_t nargs) if (rv != UCI_OK) err_return(rv); - return json_object_new_boolean(true); + return ucv_boolean_new(true); } -static json_object * +static uc_value_t * uc_uci_reorder(uc_vm *vm, size_t nargs) { struct uci_context **c = uc_get_self("uci.cursor"); - json_object *conf = uc_get_arg(0); - json_object *sect = uc_get_arg(1); - json_object *val = uc_get_arg(2); + uc_value_t *conf = uc_get_arg(0); + uc_value_t *sect = uc_get_arg(1); + uc_value_t *val = uc_get_arg(2); struct uci_ptr ptr = {}; int64_t n; int rv; - if (!json_object_is_type(conf, json_type_string) || - !json_object_is_type(sect, json_type_string) || - !json_object_is_type(val, json_type_int)) + if (ucv_type(conf) != UC_STRING || + ucv_type(sect) != UC_STRING || + ucv_type(val) != UC_INTEGER) err_return(UCI_ERR_INVAL); - n = json_object_get_int64(val); + n = ucv_int64_get(val); if (n < 0) err_return(UCI_ERR_INVAL); - ptr.package = json_object_get_string(conf); - ptr.section = json_object_get_string(sect); + ptr.package = ucv_string_get(conf); + ptr.section = ucv_string_get(sect); rv = lookup_ptr(*c, &ptr, true); @@ -686,14 +695,14 @@ uc_uci_reorder(uc_vm *vm, size_t nargs) if (rv != UCI_OK) err_return(rv); - return json_object_new_boolean(true); + return ucv_boolean_new(true); } -static json_object * +static uc_value_t * uc_uci_pkg_command(uc_vm *vm, size_t nargs, enum pkg_cmd cmd) { struct uci_context **c = uc_get_self("uci.cursor"); - json_object *conf = uc_get_arg(0); + uc_value_t *conf = uc_get_arg(0); struct uci_element *e, *tmp; struct uci_package *p; struct uci_ptr ptr = {}; @@ -702,13 +711,13 @@ uc_uci_pkg_command(uc_vm *vm, size_t nargs, enum pkg_cmd cmd) if (cmd != CMD_REVERT && conf) err_return(UCI_ERR_INVAL); - if (conf && !json_object_is_type(conf, json_type_string)) + if (conf && ucv_type(conf) != UC_STRING) err_return(UCI_ERR_INVAL); uci_foreach_element_safe(&(*c)->root, tmp, e) { p = uci_to_package(e); - if (conf && strcmp(e->name, json_object_get_string(conf))) + if (conf && strcmp(e->name, ucv_string_get(conf))) continue; switch (cmd) { @@ -736,29 +745,29 @@ uc_uci_pkg_command(uc_vm *vm, size_t nargs, enum pkg_cmd cmd) if (res != UCI_OK) err_return(res); - return json_object_new_boolean(true); + return ucv_boolean_new(true); } -static json_object * +static uc_value_t * uc_uci_save(uc_vm *vm, size_t nargs) { return uc_uci_pkg_command(vm, nargs, CMD_SAVE); } -static json_object * +static uc_value_t * uc_uci_commit(uc_vm *vm, size_t nargs) { return uc_uci_pkg_command(vm, nargs, CMD_COMMIT); } -static json_object * +static uc_value_t * uc_uci_revert(uc_vm *vm, size_t nargs) { return uc_uci_pkg_command(vm, nargs, CMD_REVERT); } -static json_object * -change_to_json(struct uci_delta *d) +static uc_value_t * +change_to_uval(uc_vm *vm, struct uci_delta *d) { const char *types[] = { [UCI_CMD_REORDER] = "order", @@ -770,36 +779,36 @@ change_to_json(struct uci_delta *d) [UCI_CMD_CHANGE] = "set", }; - json_object *a; + uc_value_t *a; if (!d->section) return NULL; - a = json_object_new_array(); + a = ucv_array_new(vm); if (!a) return NULL; - json_object_array_add(a, json_object_new_string(types[d->cmd])); - json_object_array_add(a, json_object_new_string(d->section)); + ucv_array_push(a, ucv_string_new(types[d->cmd])); + ucv_array_push(a, ucv_string_new(d->section)); if (d->e.name) - json_object_array_add(a, json_object_new_string(d->e.name)); + ucv_array_push(a, ucv_string_new(d->e.name)); if (d->value) { if (d->cmd == UCI_CMD_REORDER) - json_object_array_add(a, json_object_new_int64(strtoul(d->value, NULL, 10))); + ucv_array_push(a, ucv_int64_new(strtoul(d->value, NULL, 10))); else - json_object_array_add(a, json_object_new_string(d->value)); + ucv_array_push(a, ucv_string_new(d->value)); } return a; } -static json_object * -changes_to_json(struct uci_context *ctx, const char *package) +static uc_value_t * +changes_to_uval(uc_vm *vm, struct uci_context *ctx, const char *package) { - json_object *a = NULL, *c; + uc_value_t *a = NULL, *c; struct uci_package *p = NULL; struct uci_element *e; bool unload = false; @@ -820,23 +829,23 @@ changes_to_json(struct uci_context *ctx, const char *package) return NULL; if (!uci_list_empty(&p->delta) || !uci_list_empty(&p->saved_delta)) { - a = json_object_new_array(); + a = ucv_array_new(vm); if (!a) err_return(UCI_ERR_MEM); uci_foreach_element(&p->saved_delta, e) { - c = change_to_json(uci_to_delta(e)); + c = change_to_uval(vm, uci_to_delta(e)); if (c) - json_object_array_add(a, c); + ucv_array_push(a, c); } uci_foreach_element(&p->delta, e) { - c = change_to_json(uci_to_delta(e)); + c = change_to_uval(vm, uci_to_delta(e)); if (c) - json_object_array_add(a, c); + ucv_array_push(a, c); } } @@ -846,16 +855,16 @@ changes_to_json(struct uci_context *ctx, const char *package) return a; } -static json_object * +static uc_value_t * uc_uci_changes(uc_vm *vm, size_t nargs) { struct uci_context **c = uc_get_self("uci.cursor"); - json_object *conf = uc_get_arg(0); - json_object *res, *chg; + uc_value_t *conf = uc_get_arg(0); + uc_value_t *res, *chg; char **configs; int rv, i; - if (conf && !json_object_is_type(conf, json_type_string)) + if (conf && ucv_type(conf) != UC_STRING) err_return(UCI_ERR_INVAL); rv = uci_list_configs(*c, &configs); @@ -863,21 +872,16 @@ uc_uci_changes(uc_vm *vm, size_t nargs) if (rv != UCI_OK) err_return(rv); - res = json_object_new_object(); - - if (!res) { - free(configs); - err_return(UCI_ERR_MEM); - } + res = ucv_object_new(vm); for (i = 0; configs[i]; i++) { - if (conf && strcmp(configs[i], json_object_get_string(conf))) + if (conf && strcmp(configs[i], ucv_string_get(conf))) continue; - chg = changes_to_json(*c, configs[i]); + chg = changes_to_uval(vm, *c, configs[i]); if (chg) - json_object_object_add(res, configs[i], chg); + ucv_object_add(res, configs[i], chg); } free(configs); @@ -885,14 +889,14 @@ uc_uci_changes(uc_vm *vm, size_t nargs) return res; } -static json_object * +static uc_value_t * uc_uci_foreach(uc_vm *vm, size_t nargs) { struct uci_context **c = uc_get_self("uci.cursor"); - json_object *conf = uc_get_arg(0); - json_object *type = uc_get_arg(1); - json_object *func = uc_get_arg(2); - json_object *rv = NULL; + uc_value_t *conf = uc_get_arg(0); + uc_value_t *type = uc_get_arg(1); + uc_value_t *func = uc_get_arg(2); + uc_value_t *rv = NULL; struct uci_package *p = NULL; struct uci_element *e, *tmp; struct uci_section *sc; @@ -901,12 +905,12 @@ uc_uci_foreach(uc_vm *vm, size_t nargs) bool ret = false; int i = 0; - if (!json_object_is_type(conf, json_type_string) || - (type && !json_object_is_type(type, json_type_string))) + if (ucv_type(conf) != UC_STRING || + (type && ucv_type(type) != UC_STRING)) err_return(UCI_ERR_INVAL); uci_foreach_element(&(*c)->root, e) { - if (strcmp(e->name, json_object_get_string(conf))) + if (strcmp(e->name, ucv_string_get(conf))) continue; p = uci_to_package(e); @@ -920,11 +924,11 @@ uc_uci_foreach(uc_vm *vm, size_t nargs) sc = uci_to_section(e); i++; - if (type && strcmp(sc->type, json_object_get_string(type))) + if (type && strcmp(sc->type, ucv_string_get(type))) continue; - uc_push_val(uc_value_get(func)); - uc_push_val(section_to_json(sc, i - 1)); + uc_push_val(ucv_get(func)); + uc_push_val(section_to_uval(vm, sc, i - 1)); ex = uc_call(1); @@ -934,9 +938,9 @@ uc_uci_foreach(uc_vm *vm, size_t nargs) ret = true; rv = uc_pop_val(); - stop = (json_object_is_type(rv, json_type_boolean) && !json_object_get_boolean(rv)); + stop = (ucv_type(rv) == UC_BOOLEAN && !ucv_boolean_get(rv)); - json_object_put(rv); + ucv_put(rv); if (stop) break; @@ -944,14 +948,14 @@ uc_uci_foreach(uc_vm *vm, size_t nargs) /* XXX: rethrow */ - return json_object_new_boolean(ret); + return ucv_boolean_new(ret); } -static json_object * +static uc_value_t * uc_uci_configs(uc_vm *vm, size_t nargs) { struct uci_context **c = uc_get_self("uci.cursor"); - json_object *a; + uc_value_t *a; char **configs; int i, rv; @@ -960,15 +964,10 @@ uc_uci_configs(uc_vm *vm, size_t nargs) if (rv != UCI_OK) err_return(rv); - a = json_object_new_array(); - - if (!a) { - free(configs); - err_return(UCI_ERR_MEM); - } + a = ucv_array_new(vm); for (i = 0; configs[i]; i++) - json_object_array_add(a, json_object_new_string(configs[i])); + ucv_array_push(a, ucv_string_new(configs[i])); free(configs); @@ -1006,7 +1005,7 @@ static void close_uci(void *ud) { uci_free_context((struct uci_context *)ud); } -void uc_module_init(uc_prototype *scope) +void uc_module_init(uc_value_t *scope) { uc_add_proto_functions(scope, global_fns); -- cgit v1.2.3 From 64eec7f90e945696572ee076b75d1f35e8f2248a Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Tue, 27 Apr 2021 00:35:20 +0200 Subject: treewide: ISO C / pedantic compliance - Shuffle typedefs to avoid need for non-compliant forward declarations - Fix non-compliant empty struct initializers - Remove use of braced expressions - Remove use of anonymous unions - Avoid `void *` pointer arithmetic - Fix several warnings reported by gcc -pedantic mode and clang 11 compilation Signed-off-by: Jo-Philipp Wich --- CMakeLists.txt | 5 +- chunk.h | 28 +--------- compiler.c | 33 ++++++----- lexer.c | 77 +++++++++++++------------- lexer.h | 6 -- lib.c | 52 +++++++++++------ lib/uci.c | 14 ++--- main.c | 9 ++- source.h | 10 +--- tests/cram/test_basic.t | 5 +- types.c | 11 ++-- types.h | 144 +++++++++++++++++++++++++++++++++++++++++++----- util.h | 2 + value.c | 2 +- value.h | 15 +---- vm.c | 37 +++++++------ vm.h | 49 +--------------- 17 files changed, 278 insertions(+), 221 deletions(-) (limited to 'lib/uci.c') diff --git a/CMakeLists.txt b/CMakeLists.txt index c6bfec9..0d5fe54 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,8 +107,9 @@ IF(UNIT_TESTING) LIST(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure") IF(CMAKE_C_COMPILER_ID STREQUAL "Clang") - ADD_EXECUTABLE(ucode-san ${UCODE_SOURCES}) - TARGET_LINK_LIBRARIES(ucode-san ${json}) + ADD_EXECUTABLE(ucode-san ${UCODE_SOURCES}) + SET_PROPERTY(TARGET ucode-san PROPERTY ENABLE_EXPORTS 1) + TARGET_LINK_LIBRARIES(ucode-san ${json}) TARGET_COMPILE_OPTIONS(ucode-san PRIVATE -g -fno-omit-frame-pointer -fsanitize=undefined,address,leak -fno-sanitize-recover=all) TARGET_LINK_OPTIONS(ucode-san PRIVATE -fsanitize=undefined,address,leak) ENDIF() diff --git a/chunk.h b/chunk.h index cad00f7..2a8fa3a 100644 --- a/chunk.h +++ b/chunk.h @@ -22,38 +22,12 @@ #include "value.h" #include "util.h" - - -typedef struct { - size_t from, to, target, slot; -} uc_ehrange; - -typedef struct { - size_t from, to, slot, nameidx; -} uc_varrange; - -uc_declare_vector(uc_ehranges, uc_ehrange); -uc_declare_vector(uc_variables, uc_varrange); -uc_declare_vector(uc_offsetinfo, uint8_t); - -typedef struct { - size_t count; - uint8_t *entries; - uc_value_list constants; - uc_ehranges ehranges; - struct { - uc_variables variables; - uc_value_list varnames; - uc_offsetinfo offsets; - } debuginfo; -} uc_chunk; +#include "types.h" void uc_chunk_init(uc_chunk *chunk); void uc_chunk_free(uc_chunk *chunk); size_t uc_chunk_add(uc_chunk *chunk, uint8_t byte, size_t line); -typedef struct uc_value_t uc_value_t; - ssize_t uc_chunk_add_constant(uc_chunk *chunk, uc_value_t *value); uc_value_t *uc_chunk_get_constant(uc_chunk *chunk, size_t idx); void uc_chunk_pop(uc_chunk *chunk); diff --git a/compiler.c b/compiler.c index 04ff8ee..fd5b3af 100644 --- a/compiler.c +++ b/compiler.c @@ -468,7 +468,12 @@ uc_compiler_emit_s32(uc_compiler *compiler, size_t srcpos, int32_t n) { uc_chunk *chunk = uc_compiler_current_chunk(compiler); size_t lineoff = uc_compiler_set_srcpos(compiler, srcpos); - uint32_t v = n + 0x7fffffff; + uint32_t v; + + if (n <= 0) + v = n + 0x7fffffff; + else + v = (uint32_t)n + 0x7fffffff; uc_chunk_add(chunk, v / 0x1000000, lineoff); uc_chunk_add(chunk, (v / 0x10000) % 0x100, 0); @@ -555,9 +560,9 @@ uc_compiler_get_jmpaddr(uc_compiler *compiler, size_t off) assert(off + 4 < chunk->count); return ( - chunk->entries[off + 1] * 0x1000000 + - chunk->entries[off + 2] * 0x10000 + - chunk->entries[off + 3] * 0x100 + + chunk->entries[off + 1] * 0x1000000UL + + chunk->entries[off + 2] * 0x10000UL + + chunk->entries[off + 3] * 0x100UL + chunk->entries[off + 4] ) - 0x7fffffff; } @@ -1065,7 +1070,7 @@ static bool uc_compiler_compile_arrowfn(uc_compiler *compiler, uc_value_t *args, bool restarg) { bool array = (ucv_type(args) == UC_ARRAY); - uc_compiler fncompiler = {}; + uc_compiler fncompiler = { 0 }; size_t i, pos, load_off; uc_function_t *fn; ssize_t slot; @@ -1283,7 +1288,7 @@ static void uc_compiler_compile_call(uc_compiler *compiler, bool assignable) { uc_chunk *chunk = uc_compiler_current_chunk(compiler); - uc_jmplist spreads = {}; + uc_jmplist spreads = { 0 }; enum insn_type type; size_t i, nargs = 0; @@ -1422,7 +1427,7 @@ uc_compiler_compile_delimitted_block(uc_compiler *compiler, uc_tokentype_t endty static void uc_compiler_compile_function(uc_compiler *compiler, bool assignable) { - uc_compiler fncompiler = {}; + uc_compiler fncompiler = { 0 }; uc_value_t *name = NULL; ssize_t slot = -1, pos; uc_tokentype_t type; @@ -1891,7 +1896,7 @@ uc_compiler_compile_if(uc_compiler *compiler) uc_chunk *chunk = uc_compiler_current_chunk(compiler); size_t jmpz_off, jmp_off, i; bool expect_endif = false; - uc_jmplist elifs = {}; + uc_jmplist elifs = { 0 }; uc_tokentype_t type; /* parse & compile condition expression */ @@ -1996,7 +2001,7 @@ uc_compiler_compile_while(uc_compiler *compiler) { uc_chunk *chunk = uc_compiler_current_chunk(compiler); size_t cond_off, jmpz_off, end_off; - uc_patchlist p = {}; + uc_patchlist p = { 0 }; p.parent = compiler->patchlist; compiler->patchlist = &p; @@ -2044,7 +2049,7 @@ uc_compiler_compile_for_in(uc_compiler *compiler, bool local, uc_token *kvar, uc { uc_chunk *chunk = uc_compiler_current_chunk(compiler); size_t skip_jmp, test_jmp, key_slot, val_slot; - uc_patchlist p = {}; + uc_patchlist p = { 0 }; p.parent = compiler->patchlist; compiler->patchlist = &p; @@ -2154,7 +2159,7 @@ uc_compiler_compile_for_count(uc_compiler *compiler, bool local, uc_token *var) { uc_chunk *chunk = uc_compiler_current_chunk(compiler); size_t test_off = 0, incr_off, skip_off, cond_off = 0; - uc_patchlist p = {}; + uc_patchlist p = { 0 }; p.parent = compiler->patchlist; compiler->patchlist = &p; @@ -2254,7 +2259,7 @@ uc_compiler_compile_for_count(uc_compiler *compiler, bool local, uc_token *var) static void uc_compiler_compile_for(uc_compiler *compiler) { - uc_token keyvar = {}, valvar = {}; + uc_token keyvar = { 0 }, valvar = { 0 }; bool local; uc_compiler_parse_consume(compiler, TK_LPAREN); @@ -2309,8 +2314,8 @@ uc_compiler_compile_switch(uc_compiler *compiler) size_t i, test_jmp, skip_jmp, next_jmp, value_slot, default_off = 0; uc_chunk *chunk = uc_compiler_current_chunk(compiler); uc_locals *locals = &compiler->locals; - uc_jmplist cases = {}; - uc_patchlist p = {}; + uc_jmplist cases = { 0 }; + uc_patchlist p = { 0 }; p.parent = compiler->patchlist; compiler->patchlist = &p; diff --git a/lexer.c b/lexer.c index 74e0c4d..aaba314 100644 --- a/lexer.c +++ b/lexer.c @@ -38,7 +38,7 @@ struct keyword { union { double d; bool b; - }; + } u; }; struct token { @@ -46,7 +46,7 @@ struct token { union { uint32_t patn; char pat[4]; - }; + } u; unsigned plen; uc_token *(*parse)(uc_lexer *, bool); }; @@ -135,33 +135,33 @@ static const struct token tokens[] = { }; static const struct keyword reserved_words[] = { - { TK_ENDFUNC, "endfunction", 11, {} }, + { TK_ENDFUNC, "endfunction", 11, { 0 } }, { TK_DOUBLE, "Infinity", 8, { .d = INFINITY } }, - { TK_CONTINUE, "continue", 8, {} }, - { TK_ENDWHILE, "endwhile", 8, {} }, - { TK_FUNC, "function", 8, {} }, - { TK_DEFAULT, "default", 7, {} }, - { TK_RETURN, "return", 6, {} }, - { TK_ENDFOR, "endfor", 6, {} }, - { TK_SWITCH, "switch", 6, {} }, - { TK_LOCAL, "local", 5, {} }, - { TK_ENDIF, "endif", 5, {} }, - { TK_WHILE, "while", 5, {} }, - { TK_BREAK, "break", 5, {} }, - { TK_CATCH, "catch", 5, {} }, + { TK_CONTINUE, "continue", 8, { 0 } }, + { TK_ENDWHILE, "endwhile", 8, { 0 } }, + { TK_FUNC, "function", 8, { 0 } }, + { TK_DEFAULT, "default", 7, { 0 } }, + { TK_RETURN, "return", 6, { 0 } }, + { TK_ENDFOR, "endfor", 6, { 0 } }, + { TK_SWITCH, "switch", 6, { 0 } }, + { TK_LOCAL, "local", 5, { 0 } }, + { TK_ENDIF, "endif", 5, { 0 } }, + { TK_WHILE, "while", 5, { 0 } }, + { TK_BREAK, "break", 5, { 0 } }, + { TK_CATCH, "catch", 5, { 0 } }, { TK_BOOL, "false", 5, { .b = false } }, { TK_BOOL, "true", 4, { .b = true } }, - { TK_ELIF, "elif", 4, {} }, - { TK_ELSE, "else", 4, {} }, - { TK_THIS, "this", 4, {} }, - { TK_NULL, "null", 4, {} }, - { TK_CASE, "case", 4, {} }, + { TK_ELIF, "elif", 4, { 0 } }, + { TK_ELSE, "else", 4, { 0 } }, + { TK_THIS, "this", 4, { 0 } }, + { TK_NULL, "null", 4, { 0 } }, + { TK_CASE, "case", 4, { 0 } }, { TK_DOUBLE, "NaN", 3, { .d = NAN } }, - { TK_TRY, "try", 3, {} }, - { TK_FOR, "for", 3, {} }, - { TK_LOCAL, "let", 3, {} }, - { TK_IF, "if", 2, {} }, - { TK_IN, "in", 2, {} }, + { TK_TRY, "try", 3, { 0 } }, + { TK_FOR, "for", 3, { 0 } }, + { TK_LOCAL, "let", 3, { 0 } }, + { TK_IF, "if", 2, { 0 } }, + { TK_IN, "in", 2, { 0 } }, }; @@ -362,7 +362,7 @@ parse_comment(uc_lexer *lex, bool no_regexp) if (!buf_remaining(lex)) return emit_op(lex, lex->lastoff, TK_ERROR, ucv_string_new("Unterminated comment")); - if (!strcmp(tok->pat, "//")) { + if (!strcmp(tok->u.pat, "//")) { end = "\n"; elen = 1; } @@ -400,7 +400,7 @@ static uc_token * parse_string(uc_lexer *lex, bool no_regexp) { const struct token *tok = lex->tok; - char q = tok->pat[0]; + char q = tok->u.pat[0]; char *ptr, *c; uc_token *rv; int code; @@ -438,7 +438,7 @@ parse_string(uc_lexer *lex, bool no_regexp) default: lex->is_escape = false; - c = strchr("a\ab\be\ef\fn\nr\rt\tv\v", *ptr); + c = strchr("a\ab\be\033f\fn\nr\rt\tv\v", *ptr); if (c && *c >= 'a') { lookbehind_append(lex, c + 1, 1); @@ -725,7 +725,7 @@ parse_label(uc_lexer *lex, bool no_regexp) size_t i; if (!lex->lookbehind && tok->plen) - lookbehind_append(lex, tok->pat, tok->plen); + lookbehind_append(lex, tok->u.pat, tok->plen); if (!buf_remaining(lex) || (lex->bufstart[0] != '_' && !isalnum(lex->bufstart[0]))) { for (i = 0, word = &reserved_words[0]; i < ARRAY_SIZE(reserved_words); i++, word = &reserved_words[i]) { @@ -734,11 +734,11 @@ parse_label(uc_lexer *lex, bool no_regexp) switch (word->type) { case TK_DOUBLE: - rv = emit_op(lex, lex->source->off - word->plen, word->type, ucv_double_new(word->d)); + rv = emit_op(lex, lex->source->off - word->plen, word->type, ucv_double_new(word->u.d)); break; case TK_BOOL: - rv = emit_op(lex, lex->source->off - word->plen, word->type, ucv_boolean_new(word->b)); + rv = emit_op(lex, lex->source->off - word->plen, word->type, ucv_boolean_new(word->u.b)); break; default: @@ -803,7 +803,7 @@ parse_number(uc_lexer *lex, bool no_regexp) if (*e == '.' || *e == 'e' || *e == 'E') { d = strtod(lex->lookbehind, &e); - if (tok->pat[0] == '-') + if (tok->u.pat[0] == '-') d = -d; if (e > lex->lookbehind && *e == 0) @@ -812,7 +812,7 @@ parse_number(uc_lexer *lex, bool no_regexp) rv = emit_op(lex, lex->source->off - (lex->lookbehindlen - (e - lex->lookbehind) - 1), TK_ERROR, ucv_string_new("Invalid number literal")); } else if (*e == 0) { - if (tok->pat[0] == '-') + if (tok->u.pat[0] == '-') n = (errno == ERANGE) ? INT64_MIN : -n; rv = emit_op(lex, lex->source->off - (e - lex->lookbehind), TK_NUMBER, ucv_int64_new(n)); @@ -857,7 +857,8 @@ lex_step(uc_lexer *lex, FILE *fp, bool no_regexp) rem = lex->bufend - lex->bufstart; - memcpy(lex->buf, lex->bufstart, rem); + if (rem) + memcpy(lex->buf, lex->bufstart, rem); rlen = fread(lex->buf + rem, 1, lex->buflen - rem, fp); @@ -1042,8 +1043,8 @@ lex_step(uc_lexer *lex, FILE *fp, bool no_regexp) c = buf_remaining(lex) ? lex->bufstart[0] : 0; - if (tok->plen ? ((search.n & masks[tok->plen]) == tok->patn) - : (c >= tok->pat[0] && c <= tok->pat[1])) { + if (tok->plen ? ((search.n & masks[tok->plen]) == tok->u.patn) + : (c >= tok->u.pat[0] && c <= tok->u.pat[1])) { lex->lastoff = lex->source->off; /* token has a parse method, switch state */ @@ -1067,7 +1068,7 @@ lex_step(uc_lexer *lex, FILE *fp, bool no_regexp) else if ((lex->block == STATEMENTS && tok->type == TK_RSTM) || (lex->block == EXPRESSION && tok->type == TK_REXP)) { /* strip whitespace after block */ - if (tok->pat[0] == '-') + if (tok->u.pat[0] == '-') lex->modifier = MINUS; /* strip newline after statement block */ @@ -1207,7 +1208,7 @@ uc_get_tokenname(unsigned type) if (tokens[i].type != type) continue; - snprintf(buf, sizeof(buf), "'%s'", tokens[i].pat); + snprintf(buf, sizeof(buf), "'%s'", tokens[i].u.pat); return buf; } diff --git a/lexer.h b/lexer.h index f023fb6..069e9e0 100644 --- a/lexer.h +++ b/lexer.h @@ -126,12 +126,6 @@ typedef struct { size_t pos; } uc_token; -typedef struct { - bool lstrip_blocks; - bool trim_blocks; - bool strict_declarations; -} uc_parse_config; - typedef struct { uc_lex_state_t state; uc_parse_config *config; diff --git a/lib.c b/lib.c index 80c1386..5d6235d 100644 --- a/lib.c +++ b/lib.c @@ -35,6 +35,7 @@ #include "compiler.h" #include "vm.h" #include "lib.h" +#include "source.h" static void format_context_line(uc_stringbuf_t *buf, const char *line, size_t off, bool compact) @@ -454,9 +455,11 @@ static uc_value_t * uc_delete(uc_vm *vm, size_t nargs) { uc_value_t *obj = uc_get_arg(0); + uc_value_t *key = NULL; uc_value_t *rv = NULL; - const char *key; + bool freeable; size_t i; + char *k; if (ucv_type(obj) != UC_OBJECT) return NULL; @@ -464,10 +467,14 @@ uc_delete(uc_vm *vm, size_t nargs) for (i = 1; i < nargs; i++) { ucv_put(rv); - key = ucv_string_get(uc_get_arg(i)); - rv = ucv_get(ucv_object_get(obj, key ? key : "null", NULL)); + key = uc_get_arg(i); + k = uc_cast_string(vm, &key, &freeable); + rv = ucv_get(ucv_object_get(obj, k, NULL)); + + ucv_object_delete(obj, k); - ucv_object_delete(obj, key ? key : "null"); + if (freeable) + free(k); } return rv; @@ -482,7 +489,7 @@ uc_die(uc_vm *vm, size_t nargs) s = msg ? uc_cast_string(vm, &msg, &freeable) : "Died"; - uc_vm_raise_exception(vm, EXCEPTION_USER, s); + uc_vm_raise_exception(vm, EXCEPTION_USER, "%s", s); if (freeable) free(s); @@ -494,13 +501,19 @@ static uc_value_t * uc_exists(uc_vm *vm, size_t nargs) { uc_value_t *obj = uc_get_arg(0); - const char *key = ucv_string_get(uc_get_arg(1)); - bool found; + uc_value_t *key = uc_get_arg(1); + bool found, freeable; + char *k; if (ucv_type(obj) != UC_OBJECT) return false; - ucv_object_get(obj, key ? key : "null", &found); + k = uc_cast_string(vm, &key, &freeable); + + ucv_object_get(obj, k, &found); + + if (freeable) + free(k); return ucv_boolean_new(found); } @@ -516,8 +529,9 @@ uc_exit(uc_vm *vm, size_t nargs) static uc_value_t * uc_getenv(uc_vm *vm, size_t nargs) { - const char *key = ucv_string_get(uc_get_arg(0)); - char *val = key ? getenv(key) : NULL; + uc_value_t *key = uc_get_arg(0); + char *k = ucv_string_get(key); + char *val = k ? getenv(k) : NULL; return val ? ucv_string_new(val) : NULL; } @@ -562,16 +576,18 @@ uc_filter(uc_vm *vm, size_t nargs) static uc_value_t * uc_hex(uc_vm *vm, size_t nargs) { - const char *val = ucv_string_get(uc_get_arg(0)); + uc_value_t *val = uc_get_arg(0); + char *e, *v; int64_t n; - char *e; - if (!val || !isxdigit(*val)) + v = ucv_string_get(val); + + if (!v || !isxdigit(*v)) return ucv_double_new(NAN); - n = strtoll(val, &e, 16); + n = strtoll(v, &e, 16); - if (e == val || *e) + if (e == v || *e) return ucv_double_new(NAN); return ucv_int64_new(n); @@ -622,8 +638,10 @@ uc_keys(uc_vm *vm, size_t nargs) arr = ucv_array_new(vm); - ucv_object_foreach(obj, key, val) + ucv_object_foreach(obj, key, val) { + (void)val; ucv_array_push(arr, ucv_string_new(key)); + } return arr; } @@ -1423,7 +1441,7 @@ uc_require_so(uc_vm *vm, const char *path, uc_value_t **res) return true; } - init = dlsym(dlh, "uc_module_entry"); + *(void **)(&init) = dlsym(dlh, "uc_module_entry"); if (!init) { uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, diff --git a/lib/uci.c b/lib/uci.c index fba7dad..00a451c 100644 --- a/lib/uci.c +++ b/lib/uci.c @@ -255,7 +255,7 @@ uc_uci_get_any(uc_vm *vm, size_t nargs, bool all) uc_value_t *conf = uc_get_arg(0); uc_value_t *sect = uc_get_arg(1); uc_value_t *opt = uc_get_arg(2); - struct uci_ptr ptr = {}; + struct uci_ptr ptr = { 0 }; int rv; if (!c || !*c) @@ -330,7 +330,7 @@ uc_uci_get_first(uc_vm *vm, size_t nargs) struct uci_package *p = NULL; struct uci_section *sc; struct uci_element *e; - struct uci_ptr ptr = {}; + struct uci_ptr ptr = { 0 }; int rv; if (ucv_type(conf) != UC_STRING || @@ -465,7 +465,7 @@ uc_uci_set(uc_vm *vm, size_t nargs) uc_value_t *conf = uc_get_arg(0); uc_value_t *sect = uc_get_arg(1); uc_value_t *opt = NULL, *val = NULL; - struct uci_ptr ptr = {}; + struct uci_ptr ptr = { 0 }; bool is_list = false; size_t i; int rv; @@ -570,7 +570,7 @@ uc_uci_delete(uc_vm *vm, size_t nargs) uc_value_t *conf = uc_get_arg(0); uc_value_t *sect = uc_get_arg(1); uc_value_t *opt = uc_get_arg(2); - struct uci_ptr ptr = {}; + struct uci_ptr ptr = { 0 }; int rv; if (ucv_type(conf) != UC_STRING || @@ -605,7 +605,7 @@ uc_uci_rename(uc_vm *vm, size_t nargs) uc_value_t *conf = uc_get_arg(0); uc_value_t *sect = uc_get_arg(1); uc_value_t *opt = NULL, *val = NULL; - struct uci_ptr ptr = {}; + struct uci_ptr ptr = { 0 }; int rv; if (ucv_type(conf) != UC_STRING || @@ -665,7 +665,7 @@ uc_uci_reorder(uc_vm *vm, size_t nargs) uc_value_t *conf = uc_get_arg(0); uc_value_t *sect = uc_get_arg(1); uc_value_t *val = uc_get_arg(2); - struct uci_ptr ptr = {}; + struct uci_ptr ptr = { 0 }; int64_t n; int rv; @@ -705,7 +705,7 @@ uc_uci_pkg_command(uc_vm *vm, size_t nargs, enum pkg_cmd cmd) uc_value_t *conf = uc_get_arg(0); struct uci_element *e, *tmp; struct uci_package *p; - struct uci_ptr ptr = {}; + struct uci_ptr ptr = { 0 }; int rv, res = UCI_OK; if (cmd != CMD_REVERT && conf) diff --git a/main.c b/main.c index 618d5e4..d291447 100644 --- a/main.c +++ b/main.c @@ -98,7 +98,7 @@ parse(uc_parse_config *config, uc_source *src, { uc_value_t *globals = NULL; uc_function_t *entry; - uc_vm vm = {}; + uc_vm vm = { 0 }; int c, c2, rc = 0; char *err; @@ -285,7 +285,12 @@ main(int argc, char **argv) if (source) fprintf(stderr, "Options -i and -s are exclusive\n"); - source = uc_source_new_buffer("[-s argument]", xstrdup(optarg), strlen(optarg)); + c = xstrdup(optarg); + source = uc_source_new_buffer("[-s argument]", c, strlen(c)); + + if (!source) + free(c); + break; case 'S': diff --git a/source.h b/source.h index 4ca01b3..6557a48 100644 --- a/source.h +++ b/source.h @@ -22,17 +22,9 @@ #include #include "util.h" +#include "types.h" -uc_declare_vector(uc_lineinfo, uint8_t); - -typedef struct { - char *filename, *buffer; - FILE *fp; - size_t usecount, off; - uc_lineinfo lineinfo; -} uc_source; - uc_source *uc_source_new_file(const char *path); uc_source *uc_source_new_buffer(const char *name, char *buf, size_t len); diff --git a/tests/cram/test_basic.t b/tests/cram/test_basic.t index 982d58a..3d4cd9e 100644 --- a/tests/cram/test_basic.t +++ b/tests/cram/test_basic.t @@ -35,7 +35,7 @@ check that ucode provides proper error messages: One of -i or -s is required [1] - $ ucode -m foo -s '' + $ ucode -m foo -s ' ' Runtime error: No module named 'foo' could be found At start of program @@ -53,6 +53,7 @@ check that ucode can load fs module: One of -i or -s is required [1] - $ ucode -m fs -s '' + $ ucode -m fs -s ' ' + (no-eol) $ touch moo; ucode -m fs -i moo diff --git a/types.c b/types.c index e786fb1..3ed8aaa 100644 --- a/types.c +++ b/types.c @@ -927,7 +927,7 @@ ucv_closure_new(uc_vm *vm, uc_function_t *function, bool arrow_fn) closure->header.refcount = 1; closure->function = function; closure->is_arrow = arrow_fn; - closure->upvals = function->nupvals ? ((void *)closure + ALIGN(sizeof(*closure))) : NULL; + closure->upvals = function->nupvals ? (uc_upvalref_t **)((uintptr_t)closure + ALIGN(sizeof(*closure))) : NULL; if (vm) ucv_ref(&vm->values, &closure->ref); @@ -1590,8 +1590,8 @@ ucv_to_stringbuf(uc_vm *vm, uc_stringbuf_t *pb, uc_value_t *uv, bool json) ucv_stringbuf_printf(pb, "%sfunction%s%s(...) { [native code] }%s", json ? "\"" : "", - cfunction->name ? " " : "", - cfunction->name ? cfunction->name : "", + cfunction->name[0] ? " " : "", + cfunction->name[0] ? cfunction->name : "", json ? "\"" : ""); break; @@ -1739,6 +1739,7 @@ ucv_equal(uc_value_t *uv1, uc_value_t *uv2) } ucv_object_foreach(uv2, key2, val2) { + (void)val2; ucv_object_get(uv1, key2, &b1); if (!b1) @@ -1772,7 +1773,7 @@ ucv_gc(uc_vm *vm, bool final) /* unref unreachable objects */ for (ref = vm->values.next; ref != &vm->values; ref = ref->next) { - val = (void *)ref - offsetof(uc_array_t, ref); + val = (uc_value_t *)((uintptr_t)ref - offsetof(uc_array_t, ref)); if (ucv_is_marked(val)) ucv_clear_mark(val); @@ -1782,7 +1783,7 @@ ucv_gc(uc_vm *vm, bool final) /* free destroyed objects */ for (ref = vm->values.next, tmp = ref->next; ref != &vm->values; ref = tmp, tmp = tmp->next) { - val = (void *)ref - offsetof(uc_array_t, ref); + val = (uc_value_t *)((uintptr_t)ref - offsetof(uc_array_t, ref)); if (val->type == UC_NULL) { ucv_unref(ref); diff --git a/types.h b/types.h index a8962da..9dab246 100644 --- a/types.h +++ b/types.h @@ -22,8 +22,10 @@ #include #include -#include "source.h" -#include "chunk.h" +#include "util.h" + + +/* Value types and generic value header */ typedef enum uc_type_t { UC_NULL, @@ -48,6 +50,58 @@ typedef struct uc_value_t { uint32_t refcount:26; } uc_value_t; + +/* Constant list defintions */ + +typedef struct { + size_t isize; + size_t dsize; + uint64_t *index; + char *data; +} uc_value_list; + + +/* Source buffer defintions */ + +uc_declare_vector(uc_lineinfo, uint8_t); + +typedef struct { + char *filename, *buffer; + FILE *fp; + size_t usecount, off; + uc_lineinfo lineinfo; +} uc_source; + + +/* Bytecode chunk defintions */ + +typedef struct { + size_t from, to, target, slot; +} uc_ehrange; + +typedef struct { + size_t from, to, slot, nameidx; +} uc_varrange; + +uc_declare_vector(uc_ehranges, uc_ehrange); +uc_declare_vector(uc_variables, uc_varrange); +uc_declare_vector(uc_offsetinfo, uint8_t); + +typedef struct { + size_t count; + uint8_t *entries; + uc_value_list constants; + uc_ehranges ehranges; + struct { + uc_variables variables; + uc_value_list varnames; + uc_offsetinfo offsets; + } debuginfo; +} uc_chunk; + + +/* Value type structures */ + typedef struct uc_weakref_t { struct uc_weakref_t *prev; struct uc_weakref_t *next; @@ -144,6 +198,69 @@ typedef struct { uc_declare_vector(uc_ressource_types_t, uc_ressource_type_t); + +/* Parser definitions */ + +typedef struct { + bool lstrip_blocks; + bool trim_blocks; + bool strict_declarations; +} uc_parse_config; + + +/* VM definitions */ + +typedef enum { + EXCEPTION_NONE, + EXCEPTION_SYNTAX, + EXCEPTION_RUNTIME, + EXCEPTION_TYPE, + EXCEPTION_REFERENCE, + EXCEPTION_USER +} uc_exception_type_t; + +typedef struct { + uc_exception_type_t type; + uc_value_t *stacktrace; + char *message; +} uc_exception; + +typedef struct { + uint8_t *ip; + uc_closure_t *closure; + uc_cfunction_t *cfunction; + size_t stackframe; + uc_value_t *ctx; + bool mcall; +} uc_callframe; + +uc_declare_vector(uc_callframes, uc_callframe); +uc_declare_vector(uc_stack, uc_value_t *); + +struct uc_vm { + uc_stack stack; + uc_exception exception; + uc_callframes callframes; + uc_upvalref_t *open_upvals; + uc_parse_config *config; + uc_value_t *globals; + uc_source *sources; + uc_weakref_t values; + union { + uint32_t u32; + int32_t s32; + uint16_t u16; + int16_t s16; + uint8_t u8; + int8_t s8; + } arg; + size_t spread_values; + uint8_t trace; +}; + + +/* Value API */ + typedef struct printbuf uc_stringbuf_t; void ucv_free(uc_value_t *, bool); @@ -162,7 +279,7 @@ uc_value_t *ucv_string_new_length(const char *, size_t); size_t ucv_string_length(uc_value_t *); char *_ucv_string_get(uc_value_t **); -#define ucv_string_get(uv) ({ uc_value_t * volatile p = (uv); _ucv_string_get((uc_value_t **)&p); }) +#define ucv_string_get(uv) _ucv_string_get((uc_value_t **)&uv) uc_stringbuf_t *ucv_stringbuf_new(void); uc_value_t *ucv_stringbuf_finish(uc_stringbuf_t *); @@ -200,16 +317,17 @@ bool ucv_object_add(uc_value_t *, const char *, uc_value_t *); bool ucv_object_delete(uc_value_t *, const char *); size_t ucv_object_length(uc_value_t *); -#define ucv_object_foreach(obj, key, val) \ - char *key; \ - uc_value_t *val __attribute__((__unused__)); \ - for (struct lh_entry *entry ## key = (ucv_type(obj) == UC_OBJECT) ? ((uc_object_t *)obj)->table->head : NULL, *entry_next ## key = NULL; \ - ({ if (entry ## key) { \ - key = (char *)entry ## key->k; \ - val = (uc_value_t *)entry ## key->v; \ - entry_next ## key = entry ## key->next; \ - } ; entry ## key; }); \ - entry ## key = entry_next ## key) +#define ucv_object_foreach(obj, key, val) \ + char *key = NULL; \ + uc_value_t *val = NULL; \ + struct lh_entry *entry##key; \ + struct lh_entry *entry_next##key = NULL; \ + for (entry##key = (ucv_type(obj) == UC_OBJECT) ? ((uc_object_t *)obj)->table->head : NULL; \ + (entry##key ? (key = (char *)lh_entry_k(entry##key), \ + val = (uc_value_t *)lh_entry_v(entry##key), \ + entry_next##key = entry##key->next, entry##key) \ + : 0); \ + entry##key = entry_next##key) uc_value_t *ucv_function_new(const char *, size_t, uc_source *); size_t ucv_function_srcpos(uc_value_t *, size_t); diff --git a/util.h b/util.h index a62ccc4..c4ea087 100644 --- a/util.h +++ b/util.h @@ -218,6 +218,7 @@ static inline struct json_tokener *xjs_new_tokener(void) { return tok; } +__attribute__((format(printf, 2, 0))) static inline int xasprintf(char **strp, const char *fmt, ...) { va_list ap; int len; @@ -234,6 +235,7 @@ static inline int xasprintf(char **strp, const char *fmt, ...) { return len; } +__attribute__((format(printf, 2, 0))) static inline int xvasprintf(char **strp, const char *fmt, va_list ap) { int len = vasprintf(strp, fmt, ap); diff --git a/value.c b/value.c index e53e317..0827038 100644 --- a/value.c +++ b/value.c @@ -260,7 +260,7 @@ uc_cmp(int how, uc_value_t *v1, uc_value_t *v2) } else { if (t1 == t2 && !ucv_is_scalar(v1)) { - delta = (void *)v1 - (void *)v2; + delta = (intptr_t)v1 - (intptr_t)v2; } else { t1 = uc_cast_number(v1, &n1, &d1); diff --git a/value.h b/value.h index 770279e..a3abfc6 100644 --- a/value.h +++ b/value.h @@ -29,6 +29,8 @@ #include +#include "types.h" + typedef enum { TAG_INVAL = 0, TAG_NUM = 1, @@ -39,24 +41,11 @@ typedef enum { TAG_PTR = 6 } uc_value_type_t; -typedef struct { - size_t isize; - size_t dsize; - uint64_t *index; - char *data; -} uc_value_list; - -typedef struct uc_value_t uc_value_t; - bool uc_cmp(int how, uc_value_t *v1, uc_value_t *v2); bool uc_val_is_truish(uc_value_t *val); -typedef enum uc_type_t uc_type_t; -typedef struct uc_value_t uc_value_t; uc_type_t uc_cast_number(uc_value_t *v, int64_t *n, double *d); -typedef struct uc_vm uc_vm; - uc_value_t *uc_getval(uc_vm *, uc_value_t *scope, uc_value_t *key); uc_value_t *uc_setval(uc_vm *, uc_value_t *scope, uc_value_t *key, uc_value_t *val); diff --git a/vm.c b/vm.c index 7d60805..ee73265 100644 --- a/vm.c +++ b/vm.c @@ -232,9 +232,9 @@ uc_vm_decode_insn(uc_vm *vm, uc_callframe *frame, uc_chunk *chunk) case -4: vm->arg.s32 = ( - frame->ip[0] * 0x1000000 + - frame->ip[1] * 0x10000 + - frame->ip[2] * 0x100 + + frame->ip[0] * 0x1000000UL + + frame->ip[1] * 0x10000UL + + frame->ip[2] * 0x100UL + frame->ip[3] ) - 0x7fffffff; frame->ip += 4; @@ -247,9 +247,9 @@ uc_vm_decode_insn(uc_vm *vm, uc_callframe *frame, uc_chunk *chunk) case 4: vm->arg.u32 = ( - frame->ip[0] * 0x1000000 + - frame->ip[1] * 0x10000 + - frame->ip[2] * 0x100 + + frame->ip[0] * 0x1000000UL + + frame->ip[1] * 0x10000UL + + frame->ip[2] * 0x100UL + frame->ip[3] ); frame->ip += 4; @@ -307,7 +307,7 @@ uc_vm_frame_dump(uc_vm *vm, uc_callframe *frame) ref = closure->upvals[i]; v = uc_chunk_debug_get_variable(chunk, 0, i, true); s = ucv_to_string(NULL, v); - fprintf(stderr, " [%zu] <%p> %s ", i, ref, s); + fprintf(stderr, " [%zu] <%p> %s ", i, (void *)ref, s); free(s); if (ref->closed) { @@ -588,13 +588,13 @@ uc_dump_insn(uc_vm *vm, uint8_t *pos, enum insn_type insn) case -2: fprintf(stderr, " {%c0x%hx}", vm->arg.s16 < 0 ? '-' : '+', - vm->arg.s16 < 0 ? -(unsigned)vm->arg.s16 : (unsigned)vm->arg.s16); + (uint16_t)(vm->arg.s16 < 0 ? -vm->arg.s16 : vm->arg.s16)); break; case -4: fprintf(stderr, " {%c0x%x}", vm->arg.s32 < 0 ? '-' : '+', - vm->arg.s32 < 0 ? -(unsigned)vm->arg.s32 : (unsigned)vm->arg.s32); + (uint32_t)(vm->arg.s32 < 0 ? -vm->arg.s32 : vm->arg.s32)); break; case 1: @@ -1035,7 +1035,7 @@ uc_vm_capture_upval(uc_vm *vm, size_t slot) if (curr && curr->slot == slot) { if (vm->trace) { s = ucv_to_string(NULL, vm->stack.entries[slot]); - fprintf(stderr, " {+%zu} <%p> %s\n", slot, curr, s); + fprintf(stderr, " {+%zu} <%p> %s\n", slot, (void *)curr, s); free(s); } @@ -1047,7 +1047,7 @@ uc_vm_capture_upval(uc_vm *vm, size_t slot) if (vm->trace) { s = ucv_to_string(NULL, vm->stack.entries[slot]); - fprintf(stderr, " {*%zu} <%p> %s\n", slot, created, s); + fprintf(stderr, " {*%zu} <%p> %s\n", slot, (void *)created, s); free(s); } @@ -1072,7 +1072,7 @@ uc_vm_close_upvals(uc_vm *vm, size_t slot) if (vm->trace) { s = ucv_to_string(NULL, ref->value); - fprintf(stderr, " {!%zu} <%p> %s\n", ref->slot, ref, s); + fprintf(stderr, " {!%zu} <%p> %s\n", ref->slot, (void *)ref, s); free(s); } @@ -1528,11 +1528,13 @@ static void uc_vm_insn_sobj(uc_vm *vm, enum insn_type insn) { uc_value_t *obj = uc_vm_stack_peek(vm, vm->arg.u32); + uc_value_t *val; size_t idx; for (idx = 0; idx < vm->arg.u32; idx += 2) { + val = uc_vm_stack_peek(vm, vm->arg.u32 - idx - 1); ucv_object_add(obj, - ucv_string_get(uc_vm_stack_peek(vm, vm->arg.u32 - idx - 1)), + ucv_string_get(val), ucv_get(uc_vm_stack_peek(vm, vm->arg.u32 - idx - 2))); } @@ -1972,6 +1974,8 @@ uc_vm_callframe_pop(uc_vm *vm) static void uc_vm_output_exception(uc_vm *vm) { + uc_value_t *ctx; + if (vm->exception.type == EXCEPTION_USER) fprintf(stderr, "%s\n", vm->exception.message); else @@ -1979,10 +1983,9 @@ uc_vm_output_exception(uc_vm *vm) exception_type_strings[vm->exception.type] ? exception_type_strings[vm->exception.type] : "Error", vm->exception.message); - fprintf(stderr, "%s\n\n", - ucv_string_get( - ucv_object_get( - ucv_array_get(vm->exception.stacktrace, 0), "context", NULL))); + ctx = ucv_object_get(ucv_array_get(vm->exception.stacktrace, 0), "context", NULL); + + fprintf(stderr, "%s\n\n", ucv_string_get(ctx)); } static uc_vm_status_t diff --git a/vm.h b/vm.h index e4de4f9..0f7b805 100644 --- a/vm.h +++ b/vm.h @@ -23,6 +23,7 @@ #include "chunk.h" #include "util.h" #include "lexer.h" +#include "types.h" #define __insns \ __insn(NOOP) \ @@ -103,54 +104,6 @@ typedef struct { int8_t operand_bytes; } uc_insn_definition; -typedef enum { - EXCEPTION_NONE, - EXCEPTION_SYNTAX, - EXCEPTION_RUNTIME, - EXCEPTION_TYPE, - EXCEPTION_REFERENCE, - EXCEPTION_USER -} uc_exception_type_t; - -typedef struct { - uc_exception_type_t type; - uc_value_t *stacktrace; - char *message; -} uc_exception; - -typedef struct { - uint8_t *ip; - uc_closure_t *closure; - uc_cfunction_t *cfunction; - size_t stackframe; - uc_value_t *ctx; - bool mcall; -} uc_callframe; - -uc_declare_vector(uc_callframes, uc_callframe); -uc_declare_vector(uc_stack, uc_value_t *); - -typedef struct uc_vm { - uc_stack stack; - uc_exception exception; - uc_callframes callframes; - uc_upvalref_t *open_upvals; - uc_parse_config *config; - uc_value_t *globals; - uc_source *sources; - uc_weakref_t values; - union { - uint32_t u32; - int32_t s32; - uint16_t u16; - int16_t s16; - uint8_t u8; - int8_t s8; - } arg; - size_t spread_values; - uint8_t trace; -} uc_vm; - typedef enum { STATUS_OK, ERROR_COMPILE, -- cgit v1.2.3