【发布时间】:2014-07-24 12:37:40
【问题描述】:
我正在评估要嵌入到 C++ 应用程序中的脚本语言解释器。 TCL/cpptcl 和 Lua 现在是我关注的焦点。 TCL 有一个很好的功能,使我能够“跟踪变量访问”。所以每当我读取一个定义的变量时,我的读取回调函数就会被触发:
int read_trace(const int& v, void *) {
cout << "read trace triggered" << endl;
return v;
}
...
void tclInterpreter() {
std::cout << ": Starting TCL interpreter!" << std::endl;
// new TCL
Tcl::interpreter i;
i.def_read_trace("tracedVar", "read", read_trace);
// load the script
ifstream script("helloworld.tcl");
// run the script with the given arguments
i.eval(script);
}
如果我现在从我的 C++ 应用程序中执行以下 TCL:
set tracedVar 10
for { set i 0 } { $i <= 5 } { incr i } {
puts $tracedVar
}
我收到输出:
read trace triggered
10
read trace triggered
10
read trace triggered
10
read trace triggered
10
read trace triggered
10
read trace triggered
10
所以我执行了我的变量读取回调,然后执行了 var 的 puts-value。
问题:我可以用 Lua 做到这一点吗?如果可以,怎么做?我没有找到任何直接的话题。唯一引起我注意的是调试 Lua,调试器(调试 API)会观察变量的值。但我不想关注价值变化,而是关注价值获取。
【问题讨论】:
-
欢迎来到 Stack Overflow!请不要混淆Lua 和LUA。两种语言是不同的。 “Lua”是一个名字,在葡萄牙语中的意思是“月亮”。请不要写它为“LUA”,这样既丑陋又令人困惑,因为那样它就变成了different meanings的首字母缩写,代表不同的人。