improved logging
This commit is contained in:
@@ -8143,6 +8143,56 @@ static JSValue js_stacktrace (JSContext *ctx, JSValue this_val, int argc, JSValu
|
||||
return JS_NULL;
|
||||
}
|
||||
|
||||
static JSValue js_caller_info (JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
|
||||
int depth = 0;
|
||||
if (argc > 0) JS_ToInt32(ctx, &depth, argv[0]);
|
||||
|
||||
/* Save frame pointer — JS_GetStack clears it */
|
||||
JSValue saved_frame = ctx->reg_current_frame;
|
||||
uint32_t saved_pc = ctx->current_register_pc;
|
||||
|
||||
cJSON *stack = JS_GetStack(ctx);
|
||||
|
||||
/* Restore so other callers still see the frame */
|
||||
ctx->reg_current_frame = saved_frame;
|
||||
ctx->current_register_pc = saved_pc;
|
||||
|
||||
const char *fn_str = "<anonymous>";
|
||||
const char *file_str = "<unknown>";
|
||||
int line = 0, col = 0;
|
||||
|
||||
if (stack) {
|
||||
int n = cJSON_GetArraySize(stack);
|
||||
/* depth 0 = immediate caller of caller_info, which is frame index 1
|
||||
(frame 0 is caller_info itself) */
|
||||
int idx = depth + 1;
|
||||
if (idx >= n) idx = n - 1;
|
||||
if (idx < 0) idx = 0;
|
||||
|
||||
cJSON *fr = cJSON_GetArrayItem(stack, idx);
|
||||
const char *v;
|
||||
v = cJSON_GetStringValue(cJSON_GetObjectItemCaseSensitive(fr, "function"));
|
||||
if (v) fn_str = v;
|
||||
v = cJSON_GetStringValue(cJSON_GetObjectItemCaseSensitive(fr, "file"));
|
||||
if (v) file_str = v;
|
||||
line = (int)cJSON_GetNumberValue(cJSON_GetObjectItemCaseSensitive(fr, "line"));
|
||||
col = (int)cJSON_GetNumberValue(cJSON_GetObjectItemCaseSensitive(fr, "column"));
|
||||
}
|
||||
|
||||
JSGCRef obj;
|
||||
JS_PushGCRef(ctx, &obj);
|
||||
obj.val = JS_NewObject(ctx);
|
||||
JS_SetPropertyStr(ctx, obj.val, "file", JS_NewString(ctx, file_str));
|
||||
JS_SetPropertyStr(ctx, obj.val, "line", JS_NewInt32(ctx, line));
|
||||
JS_SetPropertyStr(ctx, obj.val, "column", JS_NewInt32(ctx, col));
|
||||
JS_SetPropertyStr(ctx, obj.val, "function", JS_NewString(ctx, fn_str));
|
||||
JSValue result = obj.val;
|
||||
JS_PopGCRef(ctx, &obj);
|
||||
|
||||
if (stack) cJSON_Delete(stack);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------------------
|
||||
* array function and sub-functions
|
||||
* ----------------------------------------------------------------------------
|
||||
@@ -11452,6 +11502,7 @@ static void JS_AddIntrinsicBaseObjects (JSContext *ctx) {
|
||||
/* I/O functions */
|
||||
js_set_global_cfunc(ctx, "print", js_print, -1); /* variadic: length < 0 means no arg limit */
|
||||
js_set_global_cfunc(ctx, "stacktrace", js_stacktrace, 0);
|
||||
js_set_global_cfunc(ctx, "caller_info", js_caller_info, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user