我想实现像console.log一样的C++函数.我需要知道javascript调用者在C++中的源代码行位置.我搜索MDN JSAPI/JS Debugger API文档但没有结果.
javascript中的概念用法.
console.log("blahblahblah");
Run Code Online (Sandbox Code Playgroud)
和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;
}
Run Code Online (Sandbox Code Playgroud)
==============
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;
}
Run Code Online (Sandbox Code Playgroud) 我现在正在使用 Jekyll-Bootstrap。在我的本地环境中,我可以在 jekyll 的 _config.yml 中启用 CodeRay 并在我的帖子中使用它,没有任何问题。但是同样的代码不能在github上运行。
我的_config.yml(省略了 jekyll-bootstrap 配置):
# Use kramdown as markdown parser engine.
markdown: kramdown
markdown_ext: markdown,mkd,mkdn,md
textile_ext: textile
kramdown:
auto_ids: true
footnote_nr: 1
entity_output: as_char
toc_levels: 1..6
smart_quotes: lsquo,rsquo,ldquo,rdquo
use_coderay: true
coderay:
coderay_wrap: div
coderay_line_numbers: nil
coderay_tab_width: 4
coderay_bold_every: 10
coderay_css: class
Run Code Online (Sandbox Code Playgroud)