如何在SpiderMonkey JSNative回调中获取javascript调用程序源行号? 我想实现一个C++函数,像COUNSOL.LoG一样工作。我需要知道C++中的JavaScript调用方的源代码行位置。我搜索了MDN JSAPI/JS调试器API文档,但没有结果
javascript中的概念用法如何在SpiderMonkey JSNative回调中获取javascript调用程序源行号? 我想实现一个C++函数,像COUNSOL.LoG一样工作。我需要知道C++中的JavaScript调用方的源代码行位置。我搜索了MDN JSAPI/JS调试器API文档,但没有结果,javascript,debugging,spidermonkey,Javascript,Debugging,Spidermonkey,javascript中的概念用法 console.log("blahblahblah"); C++中的P>和期望逻辑 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.
console.log("blahblahblah");
C++中的P>和期望逻辑
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;
}
==============
更新
我终于找到了获取文件名和行号的方法。错误处理代码被省略
#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堆栈上当前活动的函数框架,可以通过
cx
访问,这将告诉您脚本和当前执行的字节码。虽然我记不清确切的API了,但是有一个函数可以从这些数据中生成lineno(不要避免放弃文档,开始阅读jscntxt.h
和朋友,因为文档可能已经过时了——它们不是自动生成的)
还有一个警告是,JSNative在概念上是在调用方的堆栈框架中执行的,因此从另一个JSNative调用consoleLog不会说类似于
的话。当从JS代码调用方调用时,它只能有效地执行您想要的操作。谢谢您提供的信息。最后,我使用JS_descriptedcaller
和JS_GetScriptFilename
成功获取脚本文件名和行号。