I want to implement a C++ function working like console.log. I need to know javascript caller's source line position in C++. I search MDN JSAPI/JS Debugger API documents but no result.
A concept usage in javascript.
console.log("blahblahblah");
And expected logic in C++.
JSBool consoleLog(JSContext *cx, unsigned argc, jsval *vp) {
// expect to get caller info including filename, lineno.
// write "blahblahblah" and caller info in my log system.
return JS_TRUE;
}
==============
UPDATE
I finally find a way to get filename and lineno. Error handling code is omitted.
#include "jsdbgapi.h"
JSBool consoleLog(JSContext *cx, unsigned argc, jsval *vp) {
JSScript *script;
unsigned int lineno;
JS_DescribeScriptedCaller(cx, &script, &lineno);
const char *filename = JS_GetScriptFilename(cx, script);
// use filename and lineno to write log...
return JS_TRUE;
}