I think you can just get “name”property from a JSValue if it’s a function to
get function name, you also get “filename”and “lineNumber”propertys to get
source info.
发件人: <quickjs-devel-bounce@xxxxxxxxxxxxx> 代表 "Eduard Suica (Redacted sender
"rokempes" for DMARC)" <dmarc-noreply@xxxxxxxxxxxxx>
答复: <quickjs-devel@xxxxxxxxxxxxx>
日期: 2020年1月10日 星期五 上午1:47
收件人: <quickjs-devel@xxxxxxxxxxxxx>
主题: [quickjs-devel] Re: Getting filename, calling function name and line number
from a C native function
Thank you, your fork looks interesting. I think I will add your functions to my
code, thank you. Now I'm doing this (ugly thing):
JS_FreeValue(ctx, JS_ThrowTypeError(ctx, "callstack"));
JSValue exception_val = JS_GetException(ctx);
JSValue val = JS_GetPropertyStr(ctx, exception_val, "stack");
int line_number = 1;
char filename_buf[0x100];
filename_buf[0] = 0;
if (!JS_IsUndefined(val)) {
const char *call_stack = JS_ToCString(ctx, val);
if (call_stack) {
const char *line = strchr(call_stack, '\n');
if (line) {
const char *filename = strchr(line, '(');
if (filename) {
filename ++;
const char *line_ptr = strchr(filename, ':');
if (line_ptr) {
int size = (uintptr_t)line_ptr - (uintptr_t)filename;
line_number = atoi(++ line_ptr);
if (size >= sizeof(filename_buf))
size = sizeof(filename_buf) - 1;
memcpy(filename_buf, filename, size);
filename_buf[size] = 0;
} else {
line_ptr = strchr(filename, ')');
if (line_ptr) {
int size = (uintptr_t)line_ptr -
(uintptr_t)filename;
if (size >= sizeof(filename_buf))
size = sizeof(filename_buf) - 1;
memcpy(filename_buf, filename, size);
filename_buf[size] = 0;
}
}
}
}
}
JS_FreeString(ctx, call_stack);
}
JS_FreeValue(ctx, exception_val);
JS_ResetUncatchableError(ctx);
It's pretty ugly, but works.
Thank you,
GE.
On Thursday, January 9, 2020, 9:39:36 AM GMT+2, Koushik Dutta
<koush@xxxxxxxxxxxxxxxx> wrote:
I had to add this to add debugger support. This is available in my quickjs fork.
https://github.com/koush/quickjs/blob/master/quickjs-debugger.h#L82
To get a JSON stack trace:
JSValue stack_trace = js_debugger_build_backtrace(ctx,
ctx->current_stack_frame->cur_pc);
Can also pull that function out and add it to your own build. As far as I know,
it's not natively available, short of parsing an Error stack trace.
On Wed, Jan 8, 2020 at 11:28 PM Eduard Suica <dmarc-noreply@xxxxxxxxxxxxx>
wrote:
Hello,
It is possible to get the current filename, function name and line number from
a C native function? I can get this info from an exception (call stack), but
how can I get this from a native function, without an exception?
I need something like console.log to display something like:
[foo] test.js:4: hello
Thank you,
GE.