5
我想实现一个像console.log一样工作的C++函数。我需要知道C++中的javascript调用者的源代码行位置。我搜索MDN JSAPI/JS调试器API文档,但没有结果。如何在SpiderMonkey JSNative回调中获取javascript调用者源代码行号?
javascript中的概念用法。
console.log("blahblahblah");
在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
我终于找到一个办法让文件名和LINENO。错误处理代码被省略。
#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;
}
感谢您的信息。我最终使用'JS_DescribeScriptedCaller'和'JS_GetScriptFilename'来成功获取脚本文件名和行号。 – huandu 2013-06-13 09:51:50