diff options
Diffstat (limited to 'lib.c')
-rw-r--r-- | lib.c | 412 |
1 files changed, 194 insertions, 218 deletions
@@ -32,11 +32,11 @@ #include <sys/wait.h> #include <fnmatch.h> -#include "lexer.h" -#include "compiler.h" -#include "vm.h" -#include "lib.h" -#include "source.h" +#include "ucode/lexer.h" +#include "ucode/compiler.h" +#include "ucode/vm.h" +#include "ucode/lib.h" +#include "ucode/source.h" static void format_context_line(uc_stringbuf_t *buf, const char *line, size_t off, bool compact) @@ -94,7 +94,7 @@ format_context_line(uc_stringbuf_t *buf, const char *line, size_t off, bool comp } static char * -source_filename(uc_source *src, uint32_t line) +source_filename(uc_source_t *src, uint32_t line) { const char *name = src->filename ? basename(src->filename) : "[?]"; static char buf[sizeof("xxxxxxxxx.uc:0000000000")]; @@ -109,7 +109,7 @@ source_filename(uc_source *src, uint32_t line) } bool -format_source_context(uc_stringbuf_t *buf, uc_source *src, size_t off, bool compact) +uc_source_context_format(uc_stringbuf_t *buf, uc_source_t *src, size_t off, bool compact) { size_t len, rlen; bool truncated; @@ -155,7 +155,7 @@ format_source_context(uc_stringbuf_t *buf, uc_source *src, size_t off, bool comp } bool -format_error_context(uc_stringbuf_t *buf, uc_source *src, uc_value_t *stacktrace, size_t off) +uc_error_context_format(uc_stringbuf_t *buf, uc_source_t *src, uc_value_t *stacktrace, size_t off) { uc_value_t *e, *fn, *file, *line, *byte; const char *path; @@ -201,10 +201,10 @@ format_error_context(uc_stringbuf_t *buf, uc_source *src, uc_value_t *stacktrace } } - return format_source_context(buf, src, off, false); + return uc_source_context_format(buf, src, off, false); } -static char *uc_cast_string(uc_vm *vm, uc_value_t **v, bool *freeable) { +static char *uc_cast_string(uc_vm_t *vm, uc_value_t **v, bool *freeable) { if (ucv_type(*v) == UC_STRING) { *freeable = false; @@ -223,7 +223,7 @@ uc_cast_double(uc_value_t *v) int64_t n; double d; - t = uc_cast_number(v, &n, &d); + t = ucv_cast_number(v, &n, &d); errno = 0; if (t == UC_DOUBLE) { @@ -245,7 +245,7 @@ uc_cast_int64(uc_value_t *v) int64_t n; double d; - t = uc_cast_number(v, &n, &d); + t = ucv_cast_number(v, &n, &d); errno = 0; if (t == UC_DOUBLE) { @@ -263,7 +263,7 @@ uc_cast_int64(uc_value_t *v) } static void -uc_vm_ctx_push(uc_vm *vm) +uc_vm_ctx_push(uc_vm_t *vm) { uc_value_t *ctx = NULL; @@ -274,7 +274,7 @@ uc_vm_ctx_push(uc_vm *vm) } static uc_value_t * -uc_print_common(uc_vm *vm, size_t nargs, FILE *fh) +uc_print_common(uc_vm_t *vm, size_t nargs, FILE *fh) { uc_value_t *item; size_t reslen = 0; @@ -283,7 +283,7 @@ uc_print_common(uc_vm *vm, size_t nargs, FILE *fh) char *p; for (arridx = 0; arridx < nargs; arridx++) { - item = uc_get_arg(arridx); + item = uc_fn_arg(arridx); if (ucv_type(item) == UC_STRING) { len = ucv_string_length(item); @@ -302,15 +302,15 @@ uc_print_common(uc_vm *vm, size_t nargs, FILE *fh) static uc_value_t * -uc_print(uc_vm *vm, size_t nargs) +uc_print(uc_vm_t *vm, size_t nargs) { return uc_print_common(vm, nargs, vm->output); } static uc_value_t * -uc_length(uc_vm *vm, size_t nargs) +uc_length(uc_vm_t *vm, size_t nargs) { - uc_value_t *arg = uc_get_arg(0); + uc_value_t *arg = uc_fn_arg(0); switch (ucv_type(arg)) { case UC_OBJECT: @@ -328,10 +328,10 @@ uc_length(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_index(uc_vm *vm, size_t nargs, bool right) +uc_index(uc_vm_t *vm, size_t nargs, bool right) { - uc_value_t *stack = uc_get_arg(0); - uc_value_t *needle = uc_get_arg(1); + uc_value_t *stack = uc_fn_arg(0); + uc_value_t *needle = uc_fn_arg(1); const char *sstr, *nstr, *p; size_t arridx, len; ssize_t ret = -1; @@ -339,7 +339,7 @@ uc_index(uc_vm *vm, size_t nargs, bool right) switch (ucv_type(stack)) { case UC_ARRAY: for (arridx = 0, len = ucv_array_length(stack); arridx < len; arridx++) { - if (uc_cmp(I_EQ, ucv_array_get(stack, arridx), needle)) { + if (ucv_compare(I_EQ, ucv_array_get(stack, arridx), needle)) { ret = (ssize_t)arridx; if (!right) @@ -371,21 +371,21 @@ uc_index(uc_vm *vm, size_t nargs, bool right) } static uc_value_t * -uc_lindex(uc_vm *vm, size_t nargs) +uc_lindex(uc_vm_t *vm, size_t nargs) { return uc_index(vm, nargs, false); } static uc_value_t * -uc_rindex(uc_vm *vm, size_t nargs) +uc_rindex(uc_vm_t *vm, size_t nargs) { return uc_index(vm, nargs, true); } static uc_value_t * -uc_push(uc_vm *vm, size_t nargs) +uc_push(uc_vm_t *vm, size_t nargs) { - uc_value_t *arr = uc_get_arg(0); + uc_value_t *arr = uc_fn_arg(0); uc_value_t *item = NULL; size_t arridx; @@ -393,7 +393,7 @@ uc_push(uc_vm *vm, size_t nargs) return NULL; for (arridx = 1; arridx < nargs; arridx++) { - item = uc_get_arg(arridx); + item = uc_fn_arg(arridx); ucv_array_push(arr, ucv_get(item)); } @@ -401,25 +401,25 @@ uc_push(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_pop(uc_vm *vm, size_t nargs) +uc_pop(uc_vm_t *vm, size_t nargs) { - uc_value_t *arr = uc_get_arg(0); + uc_value_t *arr = uc_fn_arg(0); return ucv_array_pop(arr); } static uc_value_t * -uc_shift(uc_vm *vm, size_t nargs) +uc_shift(uc_vm_t *vm, size_t nargs) { - uc_value_t *arr = uc_get_arg(0); + uc_value_t *arr = uc_fn_arg(0); return ucv_array_shift(arr); } static uc_value_t * -uc_unshift(uc_vm *vm, size_t nargs) +uc_unshift(uc_vm_t *vm, size_t nargs) { - uc_value_t *arr = uc_get_arg(0); + uc_value_t *arr = uc_fn_arg(0); uc_value_t *item = NULL; size_t i; @@ -427,7 +427,7 @@ uc_unshift(uc_vm *vm, size_t nargs) return NULL; for (i = 1; i < nargs; i++) { - item = uc_get_arg(i); + item = uc_fn_arg(i); ucv_array_unshift(arr, ucv_get(item)); } @@ -435,7 +435,7 @@ uc_unshift(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_chr(uc_vm *vm, size_t nargs) +uc_chr(uc_vm_t *vm, size_t nargs) { uc_value_t *rv = NULL; size_t idx; @@ -448,7 +448,7 @@ uc_chr(uc_vm *vm, size_t nargs) str = xalloc(nargs); for (idx = 0; idx < nargs; idx++) { - n = uc_cast_int64(uc_get_arg(idx)); + n = uc_cast_int64(uc_fn_arg(idx)); if (n < 0) n = 0; @@ -465,9 +465,9 @@ uc_chr(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_die(uc_vm *vm, size_t nargs) +uc_die(uc_vm_t *vm, size_t nargs) { - uc_value_t *msg = uc_get_arg(0); + uc_value_t *msg = uc_fn_arg(0); bool freeable = false; char *s; @@ -482,10 +482,10 @@ uc_die(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_exists(uc_vm *vm, size_t nargs) +uc_exists(uc_vm_t *vm, size_t nargs) { - uc_value_t *obj = uc_get_arg(0); - uc_value_t *key = uc_get_arg(1); + uc_value_t *obj = uc_fn_arg(0); + uc_value_t *key = uc_fn_arg(1); bool found, freeable; char *k; @@ -502,18 +502,21 @@ uc_exists(uc_vm *vm, size_t nargs) return ucv_boolean_new(found); } -__attribute__((noreturn)) static uc_value_t * -uc_exit(uc_vm *vm, size_t nargs) +static uc_value_t * +uc_exit(uc_vm_t *vm, size_t nargs) { - int64_t n = uc_cast_int64(uc_get_arg(0)); + int64_t n = uc_cast_int64(uc_fn_arg(0)); + + vm->arg.s32 = (int32_t)n; + uc_vm_raise_exception(vm, EXCEPTION_EXIT, "Terminated"); - exit(n); + return NULL; } static uc_value_t * -uc_getenv(uc_vm *vm, size_t nargs) +uc_getenv(uc_vm_t *vm, size_t nargs) { - uc_value_t *key = uc_get_arg(0); + uc_value_t *key = uc_fn_arg(0); char *k = ucv_string_get(key); char *val = k ? getenv(k) : NULL; @@ -521,10 +524,10 @@ uc_getenv(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_filter(uc_vm *vm, size_t nargs) +uc_filter(uc_vm_t *vm, size_t nargs) { - uc_value_t *obj = uc_get_arg(0); - uc_value_t *func = uc_get_arg(1); + uc_value_t *obj = uc_fn_arg(0); + uc_value_t *func = uc_fn_arg(1); uc_value_t *rv, *arr; size_t arridx, arrlen; @@ -548,7 +551,7 @@ uc_filter(uc_vm *vm, size_t nargs) rv = uc_vm_stack_pop(vm); - if (uc_val_is_truish(rv)) + if (ucv_is_truish(rv)) ucv_array_push(arr, ucv_get(ucv_array_get(obj, arridx))); ucv_put(rv); @@ -558,9 +561,9 @@ uc_filter(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_hex(uc_vm *vm, size_t nargs) +uc_hex(uc_vm_t *vm, size_t nargs) { - uc_value_t *val = uc_get_arg(0); + uc_value_t *val = uc_fn_arg(0); char *e, *v; int64_t n; @@ -578,9 +581,9 @@ uc_hex(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_int(uc_vm *vm, size_t nargs) +uc_int(uc_vm_t *vm, size_t nargs) { - int64_t n = uc_cast_int64(uc_get_arg(0)); + int64_t n = uc_cast_int64(uc_fn_arg(0)); if (errno == EINVAL || errno == EOVERFLOW) return ucv_double_new(NAN); @@ -589,10 +592,10 @@ uc_int(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_join(uc_vm *vm, size_t nargs) +uc_join(uc_vm_t *vm, size_t nargs) { - uc_value_t *sep = uc_get_arg(0); - uc_value_t *arr = uc_get_arg(1); + uc_value_t *sep = uc_fn_arg(0); + uc_value_t *arr = uc_fn_arg(1); size_t arrlen, arridx; uc_stringbuf_t *buf; @@ -612,9 +615,9 @@ uc_join(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_keys(uc_vm *vm, size_t nargs) +uc_keys(uc_vm_t *vm, size_t nargs) { - uc_value_t *obj = uc_get_arg(0); + uc_value_t *obj = uc_fn_arg(0); uc_value_t *arr = NULL; if (ucv_type(obj) != UC_OBJECT) @@ -631,9 +634,9 @@ uc_keys(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_lc(uc_vm *vm, size_t nargs) +uc_lc(uc_vm_t *vm, size_t nargs) { - char *str = ucv_to_string(vm, uc_get_arg(0)); + char *str = ucv_to_string(vm, uc_fn_arg(0)); uc_value_t *rv = NULL; char *p; @@ -652,10 +655,10 @@ uc_lc(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_map(uc_vm *vm, size_t nargs) +uc_map(uc_vm_t *vm, size_t nargs) { - uc_value_t *obj = uc_get_arg(0); - uc_value_t *func = uc_get_arg(1); + uc_value_t *obj = uc_fn_arg(0); + uc_value_t *func = uc_fn_arg(1); uc_value_t *arr, *rv; size_t arridx, arrlen; @@ -686,9 +689,9 @@ uc_map(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_ord(uc_vm *vm, size_t nargs) +uc_ord(uc_vm_t *vm, size_t nargs) { - uc_value_t *obj = uc_get_arg(0); + uc_value_t *obj = uc_fn_arg(0); uc_value_t *rv, *pos; const char *str; size_t i, len; @@ -706,7 +709,7 @@ uc_ord(uc_vm *vm, size_t nargs) rv = ucv_array_new(vm); for (i = 1; i < nargs; i++) { - pos = uc_get_arg(i); + pos = uc_fn_arg(i); if (ucv_type(pos) == UC_INTEGER) { n = ucv_int64_get(pos); @@ -727,9 +730,9 @@ uc_ord(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_type(uc_vm *vm, size_t nargs) +uc_type(uc_vm_t *vm, size_t nargs) { - uc_value_t *v = uc_get_arg(0); + uc_value_t *v = uc_fn_arg(0); uc_type_t t = ucv_type(v); switch (t) { @@ -753,9 +756,9 @@ uc_type(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_reverse(uc_vm *vm, size_t nargs) +uc_reverse(uc_vm_t *vm, size_t nargs) { - uc_value_t *obj = uc_get_arg(0); + uc_value_t *obj = uc_fn_arg(0); uc_value_t *rv = NULL; size_t len, arridx; const char *str; @@ -785,7 +788,7 @@ uc_reverse(uc_vm *vm, size_t nargs) static struct { - uc_vm *vm; + uc_vm_t *vm; bool ex; uc_value_t *fn; } sort_ctx; @@ -801,8 +804,8 @@ default_cmp(uc_value_t *v1, uc_value_t *v2) if (ucv_type(v1) == UC_INTEGER || ucv_type(v1) == UC_DOUBLE || ucv_type(v2) == UC_INTEGER || ucv_type(v2) == UC_DOUBLE) { - t1 = uc_cast_number(v1, &n1, &d1); - t2 = uc_cast_number(v2, &n2, &d2); + t1 = ucv_cast_number(v1, &n1, &d1); + t2 = ucv_cast_number(v2, &n2, &d2); if (t1 == UC_DOUBLE || t2 == UC_DOUBLE) { d1 = (t1 == UC_DOUBLE) ? d1 : (double)n1; @@ -865,7 +868,7 @@ sort_fn(const void *k1, const void *k2) } rv = uc_vm_stack_pop(sort_ctx.vm); - t = uc_cast_number(rv, &n, &d); + t = ucv_cast_number(rv, &n, &d); if (t == UC_DOUBLE) { if (d < 0) @@ -888,10 +891,10 @@ sort_fn(const void *k1, const void *k2) } static uc_value_t * -uc_sort(uc_vm *vm, size_t nargs) +uc_sort(uc_vm_t *vm, size_t nargs) { - uc_value_t *arr = uc_get_arg(0); - uc_value_t *fn = uc_get_arg(1); + uc_value_t *arr = uc_fn_arg(0); + uc_value_t *fn = uc_fn_arg(1); if (ucv_type(arr) != UC_ARRAY) return NULL; @@ -905,11 +908,11 @@ uc_sort(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_splice(uc_vm *vm, size_t nargs) +uc_splice(uc_vm_t *vm, size_t nargs) { - uc_value_t *arr = uc_get_arg(0); - int64_t ofs = uc_cast_int64(uc_get_arg(1)); - int64_t remlen = uc_cast_int64(uc_get_arg(2)); + uc_value_t *arr = uc_fn_arg(0); + int64_t ofs = uc_cast_int64(uc_fn_arg(1)); + int64_t remlen = uc_cast_int64(uc_fn_arg(2)); size_t arrlen, addlen, idx; if (ucv_type(arr) != UC_ARRAY) @@ -972,16 +975,16 @@ uc_splice(uc_vm *vm, size_t nargs) for (idx = 0; idx < addlen; idx++) ucv_array_set(arr, ofs + idx, - ucv_get(uc_get_arg(3 + idx))); + ucv_get(uc_fn_arg(3 + idx))); return ucv_get(arr); } static uc_value_t * -uc_split(uc_vm *vm, size_t nargs) +uc_split(uc_vm_t *vm, size_t nargs) { - uc_value_t *str = uc_get_arg(0); - uc_value_t *sep = uc_get_arg(1); + uc_value_t *str = uc_fn_arg(0); + uc_value_t *sep = uc_fn_arg(1); uc_value_t *arr = NULL; const char *p, *sepstr, *splitstr; int eflags = 0, res; @@ -1037,11 +1040,11 @@ uc_split(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_substr(uc_vm *vm, size_t nargs) +uc_substr(uc_vm_t *vm, size_t nargs) { - uc_value_t *str = uc_get_arg(0); - int64_t ofs = uc_cast_int64(uc_get_arg(1)); - int64_t sublen = uc_cast_int64(uc_get_arg(2)); + uc_value_t *str = uc_fn_arg(0); + int64_t ofs = uc_cast_int64(uc_fn_arg(1)); + int64_t sublen = uc_cast_int64(uc_fn_arg(2)); const char *p; size_t len; @@ -1101,7 +1104,7 @@ uc_substr(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_time(uc_vm *vm, size_t nargs) +uc_time(uc_vm_t *vm, size_t nargs) { time_t t = time(NULL); @@ -1109,9 +1112,9 @@ uc_time(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_uc(uc_vm *vm, size_t nargs) +uc_uc(uc_vm_t *vm, size_t nargs) { - char *str = ucv_to_string(vm, uc_get_arg(0)); + char *str = ucv_to_string(vm, uc_fn_arg(0)); uc_value_t *rv = NULL; char *p; @@ -1130,7 +1133,7 @@ uc_uc(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_uchr(uc_vm *vm, size_t nargs) +uc_uchr(uc_vm_t *vm, size_t nargs) { uc_value_t *rv = NULL; size_t idx, ulen; @@ -1139,7 +1142,7 @@ uc_uchr(uc_vm *vm, size_t nargs) int rem; for (idx = 0, ulen = 0; idx < nargs; idx++) { - n = uc_cast_int64(uc_get_arg(idx)); + n = uc_cast_int64(uc_fn_arg(idx)); if (errno == EINVAL || errno == EOVERFLOW || n < 0 || n > 0x10FFFF) ulen += 3; @@ -1156,7 +1159,7 @@ uc_uchr(uc_vm *vm, size_t nargs) str = xalloc(ulen); for (idx = 0, p = str, rem = ulen; idx < nargs; idx++) { - n = uc_cast_int64(uc_get_arg(idx)); + n = uc_cast_int64(uc_fn_arg(idx)); if (errno == EINVAL || errno == EOVERFLOW || n < 0 || n > 0x10FFFF) n = 0xFFFD; @@ -1173,9 +1176,9 @@ uc_uchr(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_values(uc_vm *vm, size_t nargs) +uc_values(uc_vm_t *vm, size_t nargs) { - uc_value_t *obj = uc_get_arg(0); + uc_value_t *obj = uc_fn_arg(0); uc_value_t *arr; if (ucv_type(obj) != UC_OBJECT) @@ -1192,10 +1195,10 @@ uc_values(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_trim_common(uc_vm *vm, size_t nargs, bool start, bool end) +uc_trim_common(uc_vm_t *vm, size_t nargs, bool start, bool end) { - uc_value_t *str = uc_get_arg(0); - uc_value_t *chr = uc_get_arg(1); + uc_value_t *str = uc_fn_arg(0); + uc_value_t *chr = uc_fn_arg(1); const char *p, *c; size_t len; @@ -1232,27 +1235,27 @@ uc_trim_common(uc_vm *vm, size_t nargs, bool start, bool end) } static uc_value_t * -uc_trim(uc_vm *vm, size_t nargs) +uc_trim(uc_vm_t *vm, size_t nargs) { return uc_trim_common(vm, nargs, true, true); } static uc_value_t * -uc_ltrim(uc_vm *vm, size_t nargs) +uc_ltrim(uc_vm_t *vm, size_t nargs) { return uc_trim_common(vm, nargs, true, false); } static uc_value_t * -uc_rtrim(uc_vm *vm, size_t nargs) +uc_rtrim(uc_vm_t *vm, size_t nargs) { return uc_trim_common(vm, nargs, false, true); } static void -uc_printf_common(uc_vm *vm, size_t nargs, uc_stringbuf_t *buf) +uc_printf_common(uc_vm_t *vm, size_t nargs, uc_stringbuf_t *buf) { - uc_value_t *fmt = uc_get_arg(0); + uc_value_t *fmt = uc_fn_arg(0); char *fp, sfmt[sizeof("%0- 123456789.123456789%")]; union { char *s; int64_t n; double d; } arg; const char *fstr, *last, *p; @@ -1342,7 +1345,7 @@ uc_printf_common(uc_vm *vm, size_t nargs, uc_stringbuf_t *buf) t = UC_INTEGER; if (argidx < nargs) - arg.n = uc_cast_int64(uc_get_arg(argidx++)); + arg.n = uc_cast_int64(uc_fn_arg(argidx++)); else arg.n = 0; @@ -1357,7 +1360,7 @@ uc_printf_common(uc_vm *vm, size_t nargs, uc_stringbuf_t *buf) t = UC_DOUBLE; if (argidx < nargs) - arg.d = uc_cast_double(uc_get_arg(argidx++)); + arg.d = uc_cast_double(uc_fn_arg(argidx++)); else arg.d = 0; @@ -1367,7 +1370,7 @@ uc_printf_common(uc_vm *vm, size_t nargs, uc_stringbuf_t *buf) t = UC_INTEGER; if (argidx < nargs) - arg.n = uc_cast_int64(uc_get_arg(argidx++)) & 0xff; + arg.n = uc_cast_int64(uc_fn_arg(argidx++)) & 0xff; else arg.n = 0; @@ -1377,7 +1380,7 @@ uc_printf_common(uc_vm *vm, size_t nargs, uc_stringbuf_t *buf) t = UC_STRING; if (argidx < nargs) - arg.s = ucv_to_string(vm, uc_get_arg(argidx++)); + arg.s = ucv_to_string(vm, uc_fn_arg(argidx++)); else arg.s = NULL; @@ -1400,7 +1403,7 @@ uc_printf_common(uc_vm *vm, size_t nargs, uc_stringbuf_t *buf) if (argidx < nargs) { arg.s = ucv_to_jsonstring_formatted(vm, - uc_get_arg(argidx++), + uc_fn_arg(argidx++), pad_size > 0 ? (pad_size > 1 ? ' ' : '\t') : '\0', pad_size > 0 ? (pad_size > 1 ? pad_size - 1 : 1) : 0); } @@ -1463,7 +1466,7 @@ next: } static uc_value_t * -uc_sprintf(uc_vm *vm, size_t nargs) +uc_sprintf(uc_vm_t *vm, size_t nargs) { uc_stringbuf_t *buf = ucv_stringbuf_new(); @@ -1473,7 +1476,7 @@ uc_sprintf(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_printf(uc_vm *vm, size_t nargs) +uc_printf(uc_vm_t *vm, size_t nargs) { uc_stringbuf_t *buf = xprintbuf_new(); size_t len; @@ -1488,9 +1491,9 @@ uc_printf(uc_vm *vm, size_t nargs) } static bool -uc_require_so(uc_vm *vm, const char *path, uc_value_t **res) +uc_require_so(uc_vm_t *vm, const char *path, uc_value_t **res) { - void (*init)(uc_value_t *); + void (*init)(uc_vm_t *, uc_value_t *); uc_value_t *scope; struct stat st; void *dlh; @@ -1519,7 +1522,7 @@ uc_require_so(uc_vm *vm, const char *path, uc_value_t **res) scope = ucv_object_new(vm); - init(scope); + init(vm, scope); *res = scope; @@ -1527,13 +1530,13 @@ uc_require_so(uc_vm *vm, const char *path, uc_value_t **res) } static bool -uc_require_ucode(uc_vm *vm, const char *path, uc_value_t *scope, uc_value_t **res) +uc_require_ucode(uc_vm_t *vm, const char *path, uc_value_t *scope, uc_value_t **res) { uc_exception_type_t extype; uc_function_t *function; uc_value_t *prev_scope; uc_value_t *closure; - uc_source *source; + uc_source_t *source; struct stat st; char *err; @@ -1565,12 +1568,15 @@ uc_require_ucode(uc_vm *vm, const char *path, uc_value_t *scope, uc_value_t **re uc_vm_stack_push(vm, closure); - prev_scope = vm->globals; - vm->globals = scope ? scope : prev_scope; + if (scope) { + prev_scope = ucv_get(uc_vm_scope_get(vm)); + uc_vm_scope_set(vm, ucv_get(scope)); + } extype = uc_vm_call(vm, false, 0); - vm->globals = prev_scope; + if (scope) + uc_vm_scope_set(vm, prev_scope); if (extype == EXCEPTION_NONE) *res = uc_vm_stack_pop(vm); @@ -1581,14 +1587,14 @@ uc_require_ucode(uc_vm *vm, const char *path, uc_value_t *scope, uc_value_t **re } static bool -uc_require_path(uc_vm *vm, const char *path_template, const char *name, uc_value_t **res) +uc_require_path(uc_vm_t *vm, const char *path_template, const char *name, uc_value_t **res) { uc_stringbuf_t *buf = xprintbuf_new(); const char *p, *q, *last; uc_value_t *modtable; bool rv; - modtable = ucv_property_get(vm->globals, "modules"); + modtable = ucv_property_get(uc_vm_scope_get(vm), "modules"); *res = ucv_object_get(modtable, name, &rv); if (rv) @@ -1635,9 +1641,9 @@ out: } static uc_value_t * -uc_require(uc_vm *vm, size_t nargs) +uc_require(uc_vm_t *vm, size_t nargs) { - uc_value_t *val = uc_get_arg(0); + uc_value_t *val = uc_fn_arg(0); uc_value_t *search, *se, *res; size_t arridx, arrlen; const char *name; @@ -1646,7 +1652,7 @@ uc_require(uc_vm *vm, size_t nargs) return NULL; name = ucv_string_get(val); - search = ucv_property_get(vm->globals, "REQUIRE_SEARCH_PATH"); + search = ucv_property_get(uc_vm_scope_get(vm), "REQUIRE_SEARCH_PATH"); if (ucv_type(search) != UC_ARRAY) { uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, @@ -1672,9 +1678,9 @@ uc_require(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_iptoarr(uc_vm *vm, size_t nargs) +uc_iptoarr(uc_vm_t *vm, size_t nargs) { - uc_value_t *ip = uc_get_arg(0); + uc_value_t *ip = uc_fn_arg(0); uc_value_t *res; union { uint8_t u8[4]; @@ -1725,9 +1731,9 @@ check_byte(uc_value_t *v) } static uc_value_t * -uc_arrtoip(uc_vm *vm, size_t nargs) +uc_arrtoip(uc_vm_t *vm, size_t nargs) { - uc_value_t *arr = uc_get_arg(0); + uc_value_t *arr = uc_fn_arg(0); union { uint8_t u8[4]; struct in6_addr in6; @@ -1773,10 +1779,10 @@ uc_arrtoip(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_match(uc_vm *vm, size_t nargs) +uc_match(uc_vm_t *vm, size_t nargs) { - uc_value_t *subject = uc_get_arg(0); - uc_value_t *pattern = uc_get_arg(1); + uc_value_t *subject = uc_fn_arg(0); + uc_value_t *pattern = uc_fn_arg(1); uc_value_t *rv = NULL, *m; regmatch_t pmatch[10]; int eflags = 0, res; @@ -1827,7 +1833,7 @@ uc_match(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_replace_cb(uc_vm *vm, uc_value_t *func, +uc_replace_cb(uc_vm_t *vm, uc_value_t *func, const char *subject, regmatch_t *pmatch, size_t plen, uc_stringbuf_t *resbuf) { @@ -1856,7 +1862,7 @@ uc_replace_cb(uc_vm *vm, uc_value_t *func, } static void -uc_replace_str(uc_vm *vm, uc_value_t *str, +uc_replace_str(uc_vm_t *vm, uc_value_t *str, const char *subject, regmatch_t *pmatch, size_t plen, uc_stringbuf_t *resbuf) { @@ -1930,12 +1936,12 @@ uc_replace_str(uc_vm *vm, uc_value_t *str, } static uc_value_t * -uc_replace(uc_vm *vm, size_t nargs) +uc_replace(uc_vm_t *vm, size_t nargs) { char *sb = NULL, *pt = NULL, *p, *l; - uc_value_t *subject = uc_get_arg(0); - uc_value_t *pattern = uc_get_arg(1); - uc_value_t *replace = uc_get_arg(2); + uc_value_t *subject = uc_fn_arg(0); + uc_value_t *pattern = uc_fn_arg(1); + uc_value_t *replace = uc_fn_arg(2); bool sb_freeable, pt_freeable; uc_value_t *rv = NULL; uc_stringbuf_t *resbuf; @@ -2032,9 +2038,9 @@ uc_replace(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_json(uc_vm *vm, size_t nargs) +uc_json(uc_vm_t *vm, size_t nargs) { - uc_value_t *rv, *src = uc_get_arg(0); + uc_value_t *rv, *src = uc_fn_arg(0); struct json_tokener *tok = NULL; enum json_tokener_error err; json_object *jso; @@ -2124,10 +2130,10 @@ include_path(const char *curpath, const char *incpath) } static uc_value_t * -uc_include(uc_vm *vm, size_t nargs) +uc_include(uc_vm_t *vm, size_t nargs) { - uc_value_t *path = uc_get_arg(0); - uc_value_t *scope = uc_get_arg(1); + uc_value_t *path = uc_fn_arg(0); + uc_value_t *scope = uc_fn_arg(1); uc_value_t *rv = NULL, *sc = NULL; uc_closure_t *closure = NULL; size_t i; @@ -2176,10 +2182,10 @@ uc_include(uc_vm *vm, size_t nargs) ucv_object_foreach(scope, key, val) ucv_object_add(sc, key, ucv_get(val)); - ucv_prototype_set(sc, ucv_get(vm->globals)); + ucv_prototype_set(sc, ucv_get(uc_vm_scope_get(vm))); } else { - sc = ucv_get(vm->globals); + sc = ucv_get(uc_vm_scope_get(vm)); } if (uc_require_ucode(vm, p, sc, &rv)) @@ -2192,7 +2198,7 @@ uc_include(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_render(uc_vm *vm, size_t nargs) +uc_render(uc_vm_t *vm, size_t nargs) { uc_string_t *ustr = NULL; FILE *mem, *prev; @@ -2239,16 +2245,16 @@ out: } static uc_value_t * -uc_warn(uc_vm *vm, size_t nargs) +uc_warn(uc_vm_t *vm, size_t nargs) { return uc_print_common(vm, nargs, stderr); } static uc_value_t * -uc_system(uc_vm *vm, size_t nargs) +uc_system(uc_vm_t *vm, size_t nargs) { - uc_value_t *cmdline = uc_get_arg(0); - uc_value_t *timeout = uc_get_arg(1); + uc_value_t *cmdline = uc_fn_arg(0); + uc_value_t *timeout = uc_fn_arg(1); const char **arglist, *fn; sigset_t sigmask, sigomask; struct timespec ts; @@ -2385,9 +2391,9 @@ fail: } static uc_value_t * -uc_trace(uc_vm *vm, size_t nargs) +uc_trace(uc_vm_t *vm, size_t nargs) { - uc_value_t *level = uc_get_arg(0); + uc_value_t *level = uc_fn_arg(0); uint8_t prev_level; if (ucv_type(level) != UC_INTEGER) { @@ -2403,15 +2409,15 @@ uc_trace(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_proto(uc_vm *vm, size_t nargs) +uc_proto(uc_vm_t *vm, size_t nargs) { - uc_value_t *val = uc_get_arg(0); + uc_value_t *val = uc_fn_arg(0); uc_value_t *proto = NULL; if (nargs < 2) return ucv_get(ucv_prototype_get(val)); - proto = uc_get_arg(1); + proto = uc_fn_arg(1); if (!ucv_prototype_set(val, proto)) uc_vm_raise_exception(vm, EXCEPTION_TYPE, "Passed value is neither a prototype, ressource or object"); @@ -2422,9 +2428,9 @@ uc_proto(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_sleep(uc_vm *vm, size_t nargs) +uc_sleep(uc_vm_t *vm, size_t nargs) { - uc_value_t *duration = uc_get_arg(0); + uc_value_t *duration = uc_fn_arg(0); struct timeval tv; int64_t ms; @@ -2442,14 +2448,14 @@ uc_sleep(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_assert(uc_vm *vm, size_t nargs) +uc_assert(uc_vm_t *vm, size_t nargs) { - uc_value_t *cond = uc_get_arg(0); - uc_value_t *msg = uc_get_arg(1); + uc_value_t *cond = uc_fn_arg(0); + uc_value_t *msg = uc_fn_arg(1); bool freeable = false; char *s; - if (!uc_val_is_truish(cond)) { + if (!ucv_is_truish(cond)) { s = msg ? uc_cast_string(vm, &msg, &freeable) : "Assertion failed"; uc_vm_raise_exception(vm, EXCEPTION_USER, "%s", s); @@ -2464,11 +2470,11 @@ uc_assert(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_regexp(uc_vm *vm, size_t nargs) +uc_regexp(uc_vm_t *vm, size_t nargs) { bool icase = false, newline = false, global = false, freeable; - uc_value_t *source = uc_get_arg(0); - uc_value_t *flags = uc_get_arg(1); + uc_value_t *source = uc_fn_arg(0); + uc_value_t *flags = uc_fn_arg(1); uc_value_t *regex = NULL; char *p, *err = NULL; @@ -2519,11 +2525,11 @@ uc_regexp(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_wildcard(uc_vm *vm, size_t nargs) +uc_wildcard(uc_vm_t *vm, size_t nargs) { - uc_value_t *subject = uc_get_arg(0); - uc_value_t *pattern = uc_get_arg(1); - uc_value_t *icase = uc_get_arg(2); + uc_value_t *subject = uc_fn_arg(0); + uc_value_t *pattern = uc_fn_arg(1); + uc_value_t *icase = uc_fn_arg(2); int flags = 0, rv; bool freeable; char *s; @@ -2531,7 +2537,7 @@ uc_wildcard(uc_vm *vm, size_t nargs) if (!subject || ucv_type(pattern) != UC_STRING) return NULL; - if (uc_val_is_truish(icase)) + if (ucv_is_truish(icase)) flags |= FNM_CASEFOLD; s = uc_cast_string(vm, &subject, &freeable); @@ -2544,12 +2550,12 @@ uc_wildcard(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_sourcepath(uc_vm *vm, size_t nargs) +uc_sourcepath(uc_vm_t *vm, size_t nargs) { - uc_value_t *calldepth = uc_get_arg(0); - uc_value_t *dironly = uc_get_arg(1); + uc_value_t *calldepth = uc_fn_arg(0); + uc_value_t *dironly = uc_fn_arg(1); uc_value_t *rv = NULL; - uc_callframe *frame; + uc_callframe_t *frame; char *path = NULL; int64_t depth; size_t i; @@ -2575,7 +2581,7 @@ uc_sourcepath(uc_vm *vm, size_t nargs) } if (path) { - if (uc_val_is_truish(dironly)) + if (ucv_is_truish(dironly)) rv = ucv_string_new(dirname(path)); else rv = ucv_string_new(path); @@ -2587,16 +2593,16 @@ uc_sourcepath(uc_vm *vm, size_t nargs) } static uc_value_t * -uc_min_max(uc_vm *vm, size_t nargs, int cmp) +uc_min_max(uc_vm_t *vm, size_t nargs, int cmp) { uc_value_t *rv = NULL, *val; bool set = false; size_t i; for (i = 0; i < nargs; i++) { - val = uc_get_arg(i); + val = uc_fn_arg(i); - if (!set || uc_cmp(cmp, val, rv)) { + if (!set || ucv_compare(cmp, val, rv)) { set = true; rv = val; } @@ -2606,13 +2612,13 @@ uc_min_max(uc_vm *vm, size_t nargs, int cmp) } static uc_value_t * -uc_min(uc_vm *vm, size_t nargs) +uc_min(uc_vm_t *vm, size_t nargs) { return uc_min_max(vm, nargs, I_LT); } static uc_value_t * -uc_max(uc_vm *vm, size_t nargs) +uc_max(uc_vm_t *vm, size_t nargs) { return uc_min_max(vm, nargs, I_GT); } @@ -2695,10 +2701,10 @@ uc_max(uc_vm *vm, size_t nargs) */ static uc_value_t * -uc_b64dec(uc_vm *vm, size_t nargs) +uc_b64dec(uc_vm_t *vm, size_t nargs) { enum { BYTE1, BYTE2, BYTE3, BYTE4 } state; - uc_value_t *str = uc_get_arg(0); + uc_value_t *str = uc_fn_arg(0); uc_stringbuf_t *buf; const char *src; unsigned int ch; @@ -2828,9 +2834,9 @@ static const char Base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static uc_value_t * -uc_b64enc(uc_vm *vm, size_t nargs) +uc_b64enc(uc_vm_t *vm, size_t nargs) { - uc_value_t *str = uc_get_arg(0); + uc_value_t *str = uc_fn_arg(0); unsigned char input[3] = {0}; uc_stringbuf_t *buf; const char *src; @@ -2881,7 +2887,7 @@ uc_b64enc(uc_vm *vm, size_t nargs) */ -static const uc_cfunction_list functions[] = { +const uc_function_list_t uc_stdlib_functions[] = { { "chr", uc_chr }, { "die", uc_die }, { "exists", uc_exists }, @@ -2943,37 +2949,7 @@ static const uc_cfunction_list functions[] = { void -uc_load_stdlib(uc_value_t *scope) +uc_stdlib_load(uc_value_t *scope) { - uc_add_proto_functions(scope, functions); -} - -uc_value_t * -uc_alloc_global(uc_vm *vm) -{ - const char *path[] = { LIB_SEARCH_PATH }; - uc_value_t *global, *arr; - size_t i; - - global = ucv_object_new(vm); - - /* build default require() search path */ - arr = ucv_array_new(vm); - - for (i = 0; i < ARRAY_SIZE(path); i++) - ucv_array_push(arr, ucv_string_new(path[i])); - - /* register module related constants */ - ucv_object_add(global, "REQUIRE_SEARCH_PATH", arr); - ucv_object_add(global, "modules", ucv_object_new(vm)); - - /* register global math constants */ - ucv_object_add(global, "NaN", ucv_double_new(NAN)); - ucv_object_add(global, "Infinity", ucv_double_new(INFINITY)); - - /* register global property */ - ucv_object_add(global, "global", ucv_get(global)); - - - return global; + uc_function_list_register(scope, uc_stdlib_functions); } |