static void parseFloat_global_method (JSVirtualMachine *vm, JSBuiltinInfo *builtin_info, void *instance_context, JSNode *result_return, JSNode *args) { char *cp, *end; result_return->type = JS_FLOAT; if (args->u.vinteger != 1) { sprintf_P (vm->error, b_core_string_1); js_vm_error (vm); } if (args[1].type == JS_STRING) cp = js_string_to_c_string (vm, &args[1]); else { JSNode input; /* Convert the input to string. */ js_vm_to_string (vm, &args[1], &input); cp = js_string_to_c_string (vm, &input); } result_return->u.vfloat = strtod (cp, &end); js_free (cp); if (cp == end) /* Couldn't parse, return NaN. */ result_return->type = JS_NAN; }
static void parseInt_global_method (JSVirtualMachine *vm, JSBuiltinInfo *builtin_info, void *instance_context, JSNode *result_return, JSNode *args) { JSInt32 base = 0; char *cp, *end; result_return->type = JS_INTEGER; if (args->u.vinteger != 1 && args->u.vinteger != 2) { sprintf_P (vm->error, b_core_string_0); js_vm_error (vm); } if (args[1].type == JS_STRING) cp = js_string_to_c_string (vm, &args[1]); else { JSNode input; /* Convert the input to string. */ js_vm_to_string (vm, &args[1], &input); cp = js_string_to_c_string (vm, &input); } if (args->u.vinteger == 2) { if (args[2].type == JS_INTEGER) base = args[2].u.vinteger; else base = js_vm_to_int32 (vm, &args[2]); } result_return->u.vinteger = strtol (cp, &end, base); js_free (cp); if (cp == end) result_return->type = JS_NAN; }
static void load_global_method (JSVirtualMachine *vm, JSBuiltinInfo *builtin_info, void *instance_context, JSNode *result_return, JSNode *args) { JSInterpPtr interp = instance_context; int i; int result; if (args->u.vinteger == 0) { sprintf (vm->error, "load: no arguments given"); js_vm_error (vm); } for (i = 1; i <= args->u.vinteger; i++) { char *cp; if (args[i].type != JS_STRING) { sprintf (vm->error, "load: illegal argument"); js_vm_error (vm); } cp = js_string_to_c_string (vm, &args[i]); result = js_eval_file (interp, cp); js_free (cp); if (!result) js_vm_error (vm); } result_return->type = JS_BOOLEAN; result_return->u.vboolean = 1; }
/* Method proc. */ static int method (JSVirtualMachine *vm, JSBuiltinInfo *builtin_info, void *instance_context, JSSymbol method, JSNode *result_return, JSNode *args) { CursesCtx *ctx = builtin_info->obj_context; char *cp; /* The default result. */ result_return->type = JS_BOOLEAN; result_return->u.vboolean = 1; if (method == ctx->s_addstr) { if (args->u.vinteger != 1) goto argument_error; if (args[1].type != JS_STRING) goto argument_type_error; cp = js_string_to_c_string (vm, &args[1]); addstr (cp); js_free (cp); } /* ********************************************************************** */ else if (method == ctx->s_attron) { if (args->u.vinteger != 1) goto argument_error; if (args[1].type != JS_INTEGER) goto argument_type_error; attron (args[1].u.vinteger); } /* ********************************************************************** */ else if (method == ctx->s_attroff) { if (args->u.vinteger != 1) goto argument_error; if (args[1].type != JS_INTEGER) goto argument_type_error; attroff (args[1].u.vinteger); } /* ********************************************************************** */ else if (method == ctx->s_beep) { if (args->u.vinteger != 0) goto argument_error; beep (); } /* ********************************************************************** */ else if (method == ctx->s_cbreak) { if (args->u.vinteger != 0) goto argument_error; cbreak (); } /* ********************************************************************** */ else if (method == ctx->s_clear) { if (args->u.vinteger != 0) goto argument_error; clear (); } /* ********************************************************************** */ else if (method == ctx->s_clrtobot) { if (args->u.vinteger != 0) goto argument_error; clrtobot (); } /* ********************************************************************** */ else if (method == ctx->s_clrtoeol) { if (args->u.vinteger != 0) goto argument_error; clrtoeol (); } /* ********************************************************************** */ else if (method == ctx->s_echo) { if (args->u.vinteger != 0) goto argument_error; echo (); } /* ********************************************************************** */ else if (method == ctx->s_endwin) { if (args->u.vinteger != 0) goto argument_error; endwin (); } /* ********************************************************************** */ else if (method == ctx->s_getch) { if (args->u.vinteger != 0) goto argument_error; result_return->type = JS_INTEGER; result_return->u.vinteger = getch (); } /* ********************************************************************** */ else if (method == ctx->s_initscr) { if (args->u.vinteger != 0) goto argument_error; if (initscr () == (WINDOW *) ERR) result_return->u.vboolean = 0; } /* ********************************************************************** */ else if (method == ctx->s_keypad) { if (args->u.vinteger != 1) goto argument_error; if (args[1].type != JS_BOOLEAN) goto argument_type_error; keypad (stdscr, args->u.vboolean); } /* ********************************************************************** */ else if (method == ctx->s_move) { if (args->u.vinteger != 2) goto argument_error; if (args[1].type != JS_INTEGER || args[2].type != JS_INTEGER) goto argument_type_error; move (args[1].u.vinteger, args[2].u.vinteger); } /* ********************************************************************** */ else if (method == ctx->s_mvaddstr) { if (args->u.vinteger != 3) goto argument_error; if (args[1].type != JS_INTEGER || args[2].type != JS_INTEGER || args[3].type != JS_STRING) goto argument_type_error; cp = js_string_to_c_string (vm, &args[3]); mvaddstr (args[1].u.vinteger, args[2].u.vinteger, cp); js_free (cp); } /* ********************************************************************** */ else if (method == ctx->s_mvaddsubstr) { int start, length; if (args->u.vinteger != 4 && args->u.vinteger != 5) goto argument_error; if (args[1].type != JS_INTEGER || args[2].type != JS_INTEGER || args[3].type != JS_STRING || args[4].type != JS_INTEGER) goto argument_type_error; start = args[4].u.vinteger; if (args->u.vinteger == 5) { if (args[5].type != JS_INTEGER) goto argument_type_error; length = args[5].u.vinteger; if (length < 0) length = 0; } else length = args[3].u.vstring->len; if (start < 0) start += args[3].u.vstring->len; if (start < 0) start = 0; if (start > args[3].u.vstring->len) start = args[3].u.vstring->len; if (start + length > args[3].u.vstring->len) length = args[3].u.vstring->len - start; cp = js_malloc (vm, length + 1); memcpy (cp, args[3].u.vstring->data + start, length); cp[length] = '\0'; mvaddstr (args[1].u.vinteger, args[2].u.vinteger, cp); js_free (cp); } /* ********************************************************************** */ else if (method == ctx->s_mvgetch) { if (args->u.vinteger != 2) goto argument_error; if (args[1].type != JS_INTEGER || args[2].type != JS_INTEGER) goto argument_type_error; result_return->type = JS_INTEGER; result_return->u.vinteger = mvgetch (args[1].u.vinteger, args[2].u.vinteger); } /* ********************************************************************** */ else if (method == ctx->s_nocbreak) { if (args->u.vinteger != 0) goto argument_error; nocbreak (); } /* ********************************************************************** */ else if (method == ctx->s_noecho) { if (args->u.vinteger != 0) goto argument_error; noecho (); } /* ********************************************************************** */ else if (method == ctx->s_refresh) { if (args->u.vinteger != 0) goto argument_error; refresh (); } /* ********************************************************************** */ else if (method == ctx->s_standend) { if (args->u.vinteger != 0) goto argument_error; standend (); } /* ********************************************************************** */ else if (method == ctx->s_standout) { if (args->u.vinteger != 0) goto argument_error; standout (); } /* ********************************************************************** */ else if (method == vm->syms.s_toString) { if (args->u.vinteger != 0) goto argument_error; js_vm_make_static_string (vm, result_return, "Curses", 6); } /* ********************************************************************** */ else return JS_PROPERTY_UNKNOWN; return JS_PROPERTY_FOUND; /* * Error handling. */ argument_error: sprintf (vm->error, "Curses.%s(): illegal amount of arguments", js_vm_symname (vm, method)); js_vm_error (vm); argument_type_error: sprintf (vm->error, "Curses.%s(): illegal argument", js_vm_symname (vm, method)); js_vm_error (vm); /* NOTREACHED. */ return 0; }
/* Method proc. */ static int method (JSVirtualMachine *vm, JSBuiltinInfo *builtin_info, void *instance_context, JSSymbol method, JSNode *result_return, JSNode *args) { ArrayCtx *ctx = builtin_info->obj_context; JSNode *n = instance_context; int i; /* Set the default result type. */ result_return->type = JS_UNDEFINED; if (method == ctx->s_concat) { if (args->u.vinteger != 1) goto argument_error; if (args[1].type != JS_ARRAY) goto argument_type_error; js_vm_make_array (vm, result_return, n->u.varray->length + args[1].u.varray->length); memcpy (result_return->u.varray->data, n->u.varray->data, n->u.varray->length * sizeof (JSNode)); memcpy (&result_return->u.varray->data[n->u.varray->length], args[1].u.varray->data, args[1].u.varray->length * sizeof (JSNode)); } /* ********************************************************************** */ else if (method == ctx->s_join || method == vm->syms.s_toString) { char *glue = NULL; if (method == vm->syms.s_toString) { if (args->u.vinteger != 0) goto argument_error; } else { if (args->u.vinteger == 0) ; else if (args->u.vinteger == 1) { JSNode glue_result; js_vm_to_string (vm, &args[1], &glue_result); glue = js_string_to_c_string (vm, &glue_result); } else goto argument_error; } /* Ok, ready to run. */ if (n->u.varray->length == 0) js_vm_make_static_string (vm, result_return, "", 0); else { int len; int glue_len = glue ? strlen (glue) : 1; /* Estimate the result length. */ len = (n->u.varray->length * 5 + (n->u.varray->length - 1) * glue_len); js_vm_make_string (vm, result_return, NULL, len); result_return->u.vstring->len = 0; /* Do the join. */ for (i = 0; i < n->u.varray->length; i++) { JSNode sitem; int delta; js_vm_to_string (vm, &n->u.varray->data[i], &sitem); delta = sitem.u.vstring->len; if (i + 1 < n->u.varray->length) delta += glue_len; result_return->u.vstring->data = js_vm_realloc (vm, result_return->u.vstring->data, result_return->u.vstring->len + delta); memcpy (result_return->u.vstring->data + result_return->u.vstring->len, sitem.u.vstring->data, sitem.u.vstring->len); result_return->u.vstring->len += sitem.u.vstring->len; if (i + 1 < n->u.varray->length) { if (glue) { memcpy (result_return->u.vstring->data + result_return->u.vstring->len, glue, glue_len); result_return->u.vstring->len += glue_len; } else result_return->u.vstring->data [result_return->u.vstring->len++] = ','; } } } if (glue) js_free (glue); } /* ********************************************************************** */ else if (method == ctx->s_pop) { if (args->u.vinteger != 0) goto argument_error; if (n->u.varray->length == 0) result_return->type = JS_UNDEFINED; else { JS_COPY (result_return, &n->u.varray->data[n->u.varray->length - 1]); n->u.varray->length--; } } /* ********************************************************************** */ else if (method == ctx->s_push) { int old_len; if (args->u.vinteger == 0) goto argument_error; old_len = n->u.varray->length; js_vm_expand_array (vm, n, n->u.varray->length + args->u.vinteger); for (i = 0; i < args->u.vinteger; i++) JS_COPY (&n->u.varray->data[old_len + i], &args[i + 1]); JS_COPY (result_return, &args[i]); } /* ********************************************************************** */ else if (method == ctx->s_reverse) { if (args->u.vinteger != 0) goto argument_error; for (i = 0; i < n->u.varray->length / 2; i++) { JSNode tmp; JS_COPY (&tmp, &n->u.varray->data[i]); JS_COPY (&n->u.varray->data[i], &n->u.varray->data[n->u.varray->length - i - 1]); JS_COPY (&n->u.varray->data[n->u.varray->length - i - 1], &tmp); } } /* ********************************************************************** */ else if (method == ctx->s_shift) { if (args->u.vinteger != 0) goto argument_error; if (n->u.varray->length == 0) result_return->type = JS_UNDEFINED; else { JS_COPY (result_return, &n->u.varray->data[0]); memmove (&n->u.varray->data[0], &n->u.varray->data[1], (n->u.varray->length - 1) * sizeof (JSNode)); n->u.varray->length--; } } /* ********************************************************************** */ else if (method == ctx->s_slice) { int start, end; if (args->u.vinteger < 1 || args->u.vinteger > 2) goto argument_error; if (args[1].type != JS_INTEGER) goto argument_type_error; start = args[1].u.vinteger; if (args->u.vinteger == 2) { if (args[2].type != JS_INTEGER) goto argument_type_error; end = args[2].u.vinteger; } else end = n->u.varray->length; if (end < 0) end += n->u.varray->length; if (end < 0) end = start; js_vm_make_array (vm, result_return, end - start); /* Copy items. */ for (i = 0; i < end - start; i++) JS_COPY (&result_return->u.varray->data[i], &n->u.varray->data[start + i]); } /* ********************************************************************** */ else if (method == ctx->s_splice) { unsigned int new_length; unsigned int old_length; int delta; if (args->u.vinteger < 2) goto argument_error; if (args[1].type != JS_INTEGER || args[2].type != JS_INTEGER) goto argument_type_error; if (args[2].u.vinteger == 0 && args->u.vinteger == 2) /* No deletions: must specify at least one item to insert. */ goto argument_error; old_length = new_length = n->u.varray->length; if (args[1].u.vinteger < new_length) { if (args[2].u.vinteger > new_length - args[1].u.vinteger) { args[2].u.vinteger = new_length - args[1].u.vinteger; new_length = args[1].u.vinteger; } else new_length -= args[2].u.vinteger; } else { new_length = args[1].u.vinteger; args[2].u.vinteger = 0; } new_length += args->u.vinteger - 2; if (new_length > n->u.varray->length) js_vm_expand_array (vm, n, new_length); else /* Cut the array. */ n->u.varray->length = new_length; /* Do the stuffs we must do. */ /* Create the result array. */ if (args[2].u.vinteger == 0) result_return->type = JS_UNDEFINED; else { js_vm_make_array (vm, result_return, args[2].u.vinteger); for (i = 0; i < args[2].u.vinteger; i++) JS_COPY (&result_return->u.varray->data[i], &n->u.varray->data[args[1].u.vinteger + i]); } /* Delete and move. */ delta = args->u.vinteger - 2 - args[2].u.vinteger; memmove (&n->u.varray->data[args[1].u.vinteger + args[2].u.vinteger + delta], &n->u.varray->data[args[1].u.vinteger + args[2].u.vinteger], (old_length - (args[1].u.vinteger + args[2].u.vinteger)) * sizeof (JSNode)); /* Insert. */ for (i = 0; i < args->u.vinteger - 2; i++) JS_COPY (&n->u.varray->data[args[1].u.vinteger + i], &args[i + 3]); } /* ********************************************************************** */ else if (method == ctx->s_sort) { MergesortCompFunc func; ArraySortCtx array_sort_ctx; void *func_ctx = NULL; /* Initialized to keep compiler quiet. */ if (args->u.vinteger == 0) { func = sort_default_cmp_func; func_ctx = vm; } else if (args->u.vinteger == 1) { if (args[1].type != JS_FUNC && args[1].type != JS_BUILTIN) goto argument_type_error; func = sort_js_cmp_func; /* Init context. */ array_sort_ctx.vm = vm; array_sort_ctx.func = &args[1]; /* Init the argc part of the argument vector here. */ array_sort_ctx.argv[0].type = JS_INTEGER; array_sort_ctx.argv[0].u.vinteger = 3; func_ctx = &array_sort_ctx; } else goto argument_error; mergesort_r (n->u.varray->data, n->u.varray->length, sizeof (JSNode), func, func_ctx); } /* ********************************************************************** */ else if (method == ctx->s_unshift) { int old_len; if (args->u.vinteger == 0) goto argument_error; old_len = n->u.varray->length; js_vm_expand_array (vm, n, n->u.varray->length + args->u.vinteger); memmove (&n->u.varray->data[args->u.vinteger], n->u.varray->data, old_len * sizeof (JSNode)); for (i = 0; i < args->u.vinteger; i++) JS_COPY (&n->u.varray->data[i], &args[args->u.vinteger - i]); result_return->type = JS_INTEGER; result_return->u.vinteger = n->u.varray->length; } /* ********************************************************************** */ else return JS_PROPERTY_UNKNOWN; return JS_PROPERTY_FOUND; /* * Error handling. */ argument_error: sprintf (vm->error, "Array.%s(): illegal amount of arguments", js_vm_symname (vm, method)); js_vm_error (vm); argument_type_error: sprintf (vm->error, "Array.%s(): illegal argument", js_vm_symname (vm, method)); js_vm_error (vm); /* NOTREACHED */ return 0; }
/* Method proc. */ static int method (JSVirtualMachine *vm, JSBuiltinInfo *builtin_info, void *instance_context, JSSymbol method, JSNode *result_return, JSNode *args) { XJSCtx *ctx = builtin_info->obj_context; XJSInstanceCtx *instance = instance_context; /* The default result is false. */ result_return->type = JS_BOOLEAN; result_return->u.vboolean = 0; /* * Static methods. */ if (method == vm->syms.s_toString) { if (args->u.vinteger != 0) goto argument_error; if (instance) js_vm_make_static_string (vm, result_return, "JSInterp", 8); else js_vm_make_static_string (vm, result_return, "JS", 2); } /* ********************************************************************** */ else if (instance) { /* * Instance methods. */ if (method == ctx->s_compile) { if (args->u.vinteger != 3) goto argument_error; if (args[1].type != JS_STRING || (args[2].type != JS_NULL && args[2].type != JS_STRING) || (args[3].type != JS_NULL && args[3].type != JS_STRING)) goto argument_type_error; } /* ***************************************************************** */ else if (method == ctx->s_eval) { if (args->u.vinteger != 1) goto argument_error; if (args[1].type != JS_STRING) goto argument_type_error; result_return->type = JS_BOOLEAN; result_return->u.vboolean = (js_eval_data (instance->interp, args[1].u.vstring->data, args[1].u.vstring->len) != 0); } /* ***************************************************************** */ else if (method == ctx->s_evalFile) { char *path; if (args->u.vinteger != 1) goto argument_error; if (args[1].type != JS_STRING) goto argument_type_error; path = js_string_to_c_string (vm, &args[1]); result_return->type = JS_BOOLEAN; result_return->u.vboolean = (js_eval_file (instance->interp, path) != 0); js_free (path); } /* ***************************************************************** */ else if (method == ctx->s_evalJavaScriptFile) { char *path; if (args->u.vinteger != 1) goto argument_error; if (args[1].type != JS_STRING) goto argument_type_error; path = js_string_to_c_string (vm, &args[1]); result_return->type = JS_BOOLEAN; result_return->u.vboolean = (js_eval_javascript_file (instance->interp, path) != 0); js_free (path); } /* ***************************************************************** */ else if (method == ctx->s_executeByteCodeFile) { char *path; if (args->u.vinteger != 1) goto argument_error; if (args[1].type != JS_STRING) goto argument_type_error; path = js_string_to_c_string (vm, &args[1]); result_return->type = JS_BOOLEAN; result_return->u.vboolean = (js_execute_byte_code_file (instance->interp, path) != 0); js_free (path); } /* ***************************************************************** */ else if (method == ctx->s_getVar) { char *cp; JSType value; if (args->u.vinteger != 1) goto argument_error; if (args[1].type != JS_STRING) goto argument_type_error; cp = js_string_to_c_string (vm, &args[1]); js_get_var (instance->interp, cp, &value); js_free (cp); copy_from_type_to_node (vm, result_return, &value); } /* ***************************************************************** */ else if (method == ctx->s_setVar) { char *cp; JSType value; if (args->u.vinteger != 2) goto argument_error; if (args[1].type != JS_STRING) goto argument_type_error; copy_from_node_to_type (instance->interp, &value, &args[2]); cp = js_string_to_c_string (vm, &args[1]); js_set_var (instance->interp, cp, &value); js_free (cp); result_return->type = JS_UNDEFINED; } /* ***************************************************************** */ else return JS_PROPERTY_UNKNOWN; } /* ********************************************************************** */ else return JS_PROPERTY_UNKNOWN; return JS_PROPERTY_FOUND; /* * Error handling. */ argument_error: sprintf (vm->error, "JS.%s(): illegal amount of arguments", js_vm_symname (vm, method)); js_vm_error (vm); argument_type_error: sprintf (vm->error, "JS.%s(): illegal argument", js_vm_symname (vm, method)); js_vm_error (vm); /* NOTREACHED */ return 0; }