rm some functions
This commit is contained in:
327
source/runtime.c
327
source/runtime.c
@@ -2462,11 +2462,6 @@ JSValue js_new_function (JSContext *ctx, JSFunctionKind kind) {
|
||||
func->kind = kind;
|
||||
func->name = JS_NULL;
|
||||
func->length = 0;
|
||||
/* Initialize closure fields for bytecode functions */
|
||||
if (kind == JS_FUNC_KIND_BYTECODE) {
|
||||
func->u.func.outer_frame = JS_NULL;
|
||||
func->u.func.env_record = JS_NULL;
|
||||
}
|
||||
return JS_MKPTR (func);
|
||||
}
|
||||
|
||||
@@ -2494,66 +2489,10 @@ JSValue JS_GetException (JSContext *ctx) {
|
||||
return val;
|
||||
}
|
||||
|
||||
JS_BOOL
|
||||
JS_HasException (JSContext *ctx) {
|
||||
JS_BOOL JS_HasException (JSContext *ctx) {
|
||||
return !JS_IsNull (ctx->current_exception);
|
||||
}
|
||||
|
||||
/* get_line_col — compute line and column from a byte offset */
|
||||
int get_line_col (int *pcol_num, const uint8_t *buf, size_t len) {
|
||||
int line_num, col_num, c;
|
||||
size_t i;
|
||||
|
||||
line_num = 0;
|
||||
col_num = 0;
|
||||
for (i = 0; i < len; i++) {
|
||||
c = buf[i];
|
||||
if (c == '\n') {
|
||||
line_num++;
|
||||
col_num = 0;
|
||||
} else if (c < 0x80 || c >= 0xc0) {
|
||||
col_num++;
|
||||
}
|
||||
}
|
||||
*pcol_num = col_num;
|
||||
return line_num;
|
||||
}
|
||||
|
||||
int get_line_col_cached (GetLineColCache *s, int *pcol_num, const uint8_t *ptr) {
|
||||
int line_num, col_num;
|
||||
if (ptr >= s->ptr) {
|
||||
line_num = get_line_col (&col_num, s->ptr, ptr - s->ptr);
|
||||
if (line_num == 0) {
|
||||
s->col_num += col_num;
|
||||
} else {
|
||||
s->line_num += line_num;
|
||||
s->col_num = col_num;
|
||||
}
|
||||
} else {
|
||||
line_num = get_line_col (&col_num, ptr, s->ptr - ptr);
|
||||
if (line_num == 0) {
|
||||
s->col_num -= col_num;
|
||||
} else {
|
||||
const uint8_t *p;
|
||||
s->line_num -= line_num;
|
||||
/* find the absolute column position */
|
||||
col_num = 0;
|
||||
for (p = ptr - 1; p >= s->buf_start; p--) {
|
||||
if (*p == '\n') {
|
||||
break;
|
||||
} else if (*p < 0x80 || *p >= 0xc0) {
|
||||
col_num++;
|
||||
}
|
||||
}
|
||||
s->col_num = col_num;
|
||||
}
|
||||
}
|
||||
s->ptr = ptr;
|
||||
*pcol_num = s->col_num;
|
||||
return s->line_num;
|
||||
}
|
||||
|
||||
|
||||
/* in order to avoid executing arbitrary code during the stack trace
|
||||
generation, we only look at simple 'name' properties containing a
|
||||
string. */
|
||||
@@ -6563,22 +6502,13 @@ static JSValue js_cell_text (JSContext *ctx, JSValue this_val, int argc, JSValue
|
||||
/* Handle function - return source or native stub */
|
||||
if (JS_IsFunction (arg)) {
|
||||
JSFunction *fn = JS_VALUE_GET_FUNCTION (arg);
|
||||
if (fn->kind == JS_FUNC_KIND_BYTECODE) {
|
||||
JSFunctionBytecode *b = fn->u.func.function_bytecode;
|
||||
if (b->has_debug && b->debug.source)
|
||||
return JS_NewStringLen (ctx, b->debug.source, b->debug.source_len);
|
||||
}
|
||||
|
||||
const char *pref = "function ";
|
||||
const char *suff = "() {\n [native code]\n}";
|
||||
const char *name = "";
|
||||
const char *name_cstr = NULL;
|
||||
|
||||
if (fn->kind == JS_FUNC_KIND_BYTECODE) {
|
||||
JSFunctionBytecode *fb = fn->u.func.function_bytecode;
|
||||
name_cstr = JS_ToCString (ctx, fb->func_name);
|
||||
if (name_cstr) name = name_cstr;
|
||||
} else if (!JS_IsNull (fn->name)) {
|
||||
if (!JS_IsNull (fn->name)) {
|
||||
name_cstr = JS_ToCString (ctx, fn->name);
|
||||
if (name_cstr) name = name_cstr;
|
||||
}
|
||||
@@ -7628,259 +7558,6 @@ static JSValue js_stacktrace (JSContext *ctx, JSValue this_val, int argc, JSValu
|
||||
return JS_NULL;
|
||||
}
|
||||
|
||||
|
||||
/* dump_bytecode_opcodes and JS_DumpFunctionBytecode removed — old stack VM deleted */
|
||||
|
||||
#if 0 /* dead code — old bytecode dump */
|
||||
static void dump_bytecode_opcodes_DEAD (JSContext *ctx, JSFunctionBytecode *b) {
|
||||
const uint8_t *tab = b->byte_code_buf;
|
||||
int len = b->byte_code_len;
|
||||
const JSValue *cpool = b->cpool;
|
||||
uint32_t cpool_count = b->cpool_count;
|
||||
const JSVarDef *vars = b->vardefs ? b->vardefs + b->arg_count : NULL;
|
||||
int var_count = b->var_count;
|
||||
int pos = 0;
|
||||
|
||||
while (pos < len) {
|
||||
int op = tab[pos];
|
||||
if (op >= OP_COUNT) {
|
||||
printf (" %5d: <invalid opcode 0x%02x>\n", pos, op);
|
||||
pos++;
|
||||
continue;
|
||||
}
|
||||
const JSOpCode *oi = &short_opcode_info (op);
|
||||
int size = oi->size;
|
||||
if (pos + size > len) {
|
||||
printf (" %5d: <truncated opcode 0x%02x>\n", pos, op);
|
||||
break;
|
||||
}
|
||||
|
||||
printf (" %5d: %s", pos, dump_opcode_names[op]);
|
||||
pos++;
|
||||
|
||||
switch (oi->fmt) {
|
||||
case OP_FMT_none_int:
|
||||
printf (" %d", op - OP_push_0);
|
||||
break;
|
||||
case OP_FMT_npopx:
|
||||
printf (" %d", op - OP_call0);
|
||||
break;
|
||||
case OP_FMT_u8:
|
||||
printf (" %u", get_u8 (tab + pos));
|
||||
break;
|
||||
case OP_FMT_i8:
|
||||
printf (" %d", get_i8 (tab + pos));
|
||||
break;
|
||||
case OP_FMT_u16:
|
||||
case OP_FMT_npop:
|
||||
printf (" %u", get_u16 (tab + pos));
|
||||
break;
|
||||
case OP_FMT_npop_u16:
|
||||
printf (" %u,%u", get_u16 (tab + pos), get_u16 (tab + pos + 2));
|
||||
break;
|
||||
case OP_FMT_i16:
|
||||
printf (" %d", get_i16 (tab + pos));
|
||||
break;
|
||||
case OP_FMT_i32:
|
||||
printf (" %d", get_i32 (tab + pos));
|
||||
break;
|
||||
case OP_FMT_u32:
|
||||
printf (" %u", get_u32 (tab + pos));
|
||||
break;
|
||||
case OP_FMT_label8:
|
||||
printf (" ->%d", pos + get_i8 (tab + pos));
|
||||
break;
|
||||
case OP_FMT_label16:
|
||||
printf (" ->%d", pos + get_i16 (tab + pos));
|
||||
break;
|
||||
case OP_FMT_label:
|
||||
printf (" ->%u", pos + get_u32 (tab + pos));
|
||||
break;
|
||||
case OP_FMT_label_u16:
|
||||
printf (" ->%u,%u", pos + get_u32 (tab + pos), get_u16 (tab + pos + 4));
|
||||
break;
|
||||
case OP_FMT_const8: {
|
||||
uint32_t idx = get_u8 (tab + pos);
|
||||
printf (" [%u]", idx);
|
||||
if (idx < cpool_count) {
|
||||
printf (": ");
|
||||
JS_PrintValue (ctx, js_dump_value_write, stdout, cpool[idx], NULL);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_FMT_const: {
|
||||
uint32_t idx = get_u32 (tab + pos);
|
||||
printf (" [%u]", idx);
|
||||
if (idx < cpool_count) {
|
||||
printf (": ");
|
||||
JS_PrintValue (ctx, js_dump_value_write, stdout, cpool[idx], NULL);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_FMT_key: {
|
||||
uint32_t idx = get_u32 (tab + pos);
|
||||
printf (" [%u]", idx);
|
||||
if (idx < cpool_count) {
|
||||
printf (": ");
|
||||
JS_PrintValue (ctx, js_dump_value_write, stdout, cpool[idx], NULL);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_FMT_key_u8: {
|
||||
uint32_t idx = get_u32 (tab + pos);
|
||||
printf (" [%u],%d", idx, get_u8 (tab + pos + 4));
|
||||
if (idx < cpool_count) {
|
||||
printf (": ");
|
||||
JS_PrintValue (ctx, js_dump_value_write, stdout, cpool[idx], NULL);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_FMT_key_u16: {
|
||||
uint32_t idx = get_u32 (tab + pos);
|
||||
printf (" [%u],%d", idx, get_u16 (tab + pos + 4));
|
||||
if (idx < cpool_count) {
|
||||
printf (": ");
|
||||
JS_PrintValue (ctx, js_dump_value_write, stdout, cpool[idx], NULL);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_FMT_none_loc:
|
||||
printf (" loc%d", (op - OP_get_loc0) % 4);
|
||||
break;
|
||||
case OP_FMT_loc8: {
|
||||
int idx = get_u8 (tab + pos);
|
||||
printf (" loc%d", idx);
|
||||
if (vars && idx < var_count) {
|
||||
char buf[KEY_GET_STR_BUF_SIZE];
|
||||
printf (": %s", JS_KeyGetStr (ctx, buf, sizeof(buf), vars[idx].var_name));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_FMT_loc: {
|
||||
int idx = get_u16 (tab + pos);
|
||||
printf (" loc%d", idx);
|
||||
if (vars && idx < var_count) {
|
||||
char buf[KEY_GET_STR_BUF_SIZE];
|
||||
printf (": %s", JS_KeyGetStr (ctx, buf, sizeof(buf), vars[idx].var_name));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case OP_FMT_none_arg:
|
||||
printf (" arg%d", (op - OP_get_arg0) % 4);
|
||||
break;
|
||||
case OP_FMT_arg:
|
||||
printf (" arg%d", get_u16 (tab + pos));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
printf ("\n");
|
||||
pos += size - 1; /* -1 because we already incremented pos after reading opcode */
|
||||
}
|
||||
}
|
||||
|
||||
void JS_DumpFunctionBytecode (JSContext *ctx, JSValue func_val) {
|
||||
JSFunctionBytecode *b = NULL;
|
||||
|
||||
if (!JS_IsPtr (func_val)) {
|
||||
printf ("JS_DumpFunctionBytecode: not a pointer value\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Get the object header to check type */
|
||||
void *ptr = JS_VALUE_GET_PTR (func_val);
|
||||
objhdr_t hdr = *(objhdr_t *)ptr;
|
||||
uint8_t type = objhdr_type (hdr);
|
||||
|
||||
if (type == OBJ_FUNCTION) {
|
||||
/* It's a JSFunction - extract bytecode */
|
||||
JSFunction *fn = (JSFunction *)ptr;
|
||||
if (fn->kind != JS_FUNC_KIND_BYTECODE) {
|
||||
printf ("JS_DumpFunctionBytecode: not a bytecode function (kind=%d)\n", fn->kind);
|
||||
return;
|
||||
}
|
||||
b = fn->u.func.function_bytecode;
|
||||
} else if (type == OBJ_CODE) {
|
||||
/* It's raw bytecode from js_create_function */
|
||||
b = (JSFunctionBytecode *)ptr;
|
||||
} else {
|
||||
printf ("JS_DumpFunctionBytecode: not a function or bytecode (type=%d)\n", type);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!b) {
|
||||
printf ("JS_DumpFunctionBytecode: no bytecode\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char buf[KEY_GET_STR_BUF_SIZE];
|
||||
|
||||
printf ("=== Bytecode Dump ===\n");
|
||||
|
||||
/* Function name */
|
||||
const char *fname = JS_KeyGetStr (ctx, buf, sizeof(buf), b->func_name);
|
||||
printf ("Function: %s\n", fname ? fname : "<anonymous>");
|
||||
|
||||
/* Debug info */
|
||||
if (b->has_debug && !JS_IsNull (b->debug.filename)) {
|
||||
printf ("File: %s\n", JS_KeyGetStr (ctx, buf, sizeof(buf), b->debug.filename));
|
||||
}
|
||||
|
||||
/* Basic stats */
|
||||
printf ("Args: %d, Vars: %d, Stack: %d\n", b->arg_count, b->var_count, b->stack_size);
|
||||
printf ("Bytecode length: %d bytes\n", b->byte_code_len);
|
||||
|
||||
/* Arguments */
|
||||
if (b->arg_count > 0 && b->vardefs) {
|
||||
printf ("\nArguments:\n");
|
||||
for (int i = 0; i < b->arg_count; i++) {
|
||||
printf (" %d: %s\n", i, JS_KeyGetStr (ctx, buf, sizeof(buf), b->vardefs[i].var_name));
|
||||
}
|
||||
}
|
||||
|
||||
/* Local variables */
|
||||
if (b->var_count > 0 && b->vardefs) {
|
||||
printf ("\nLocal variables:\n");
|
||||
for (int i = 0; i < b->var_count; i++) {
|
||||
JSVarDef *vd = &b->vardefs[b->arg_count + i];
|
||||
const char *kind = vd->is_const ? "const" : vd->is_lexical ? "let" : "var";
|
||||
printf (" %d: %s %s", i, kind, JS_KeyGetStr (ctx, buf, sizeof(buf), vd->var_name));
|
||||
if (vd->scope_level) printf (" [scope:%d]", vd->scope_level);
|
||||
printf ("\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* Closure variables */
|
||||
if (b->closure_var_count > 0) {
|
||||
printf ("\nClosure variables:\n");
|
||||
for (int i = 0; i < b->closure_var_count; i++) {
|
||||
JSClosureVar *cv = &b->closure_var[i];
|
||||
printf (" %d: %s (%s:%s%d)\n", i,
|
||||
JS_KeyGetStr (ctx, buf, sizeof(buf), cv->var_name),
|
||||
cv->is_local ? "local" : "parent",
|
||||
cv->is_arg ? "arg" : "loc",
|
||||
cv->var_idx);
|
||||
}
|
||||
}
|
||||
|
||||
/* Constant pool */
|
||||
if (b->cpool_count > 0) {
|
||||
printf ("\nConstant pool (%d entries):\n", b->cpool_count);
|
||||
for (uint32_t i = 0; i < b->cpool_count; i++) {
|
||||
printf (" [%u]: ", i);
|
||||
JS_PrintValue (ctx, js_dump_value_write, stdout, b->cpool[i], NULL);
|
||||
printf ("\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* Bytecode instructions */
|
||||
printf ("\nBytecode:\n");
|
||||
dump_bytecode_opcodes (ctx, b);
|
||||
|
||||
printf ("=== End Bytecode Dump ===\n");
|
||||
}
|
||||
#endif /* dead code */
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* array function and sub-functions
|
||||
* ----------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user