C++/Tcl 调用 Tcl_CreateCommand 记录的函数时,我可以检索 Tcl 文件行号吗

标签 c++ tcl

我使用 Tcl_EvalFile 从 C++ 程序运行 Tcl 脚本。我定义了一些自定义命令(使用 Tcl_CreateCommand ),因此当在文件中找到它们时,会调用回调,然后我可以运行一些 C++ 代码( TclInvokeStringCommand 调用给定给 Tcl_CreateCommand 的回调,回调类型为 int (*executeCmd)( ClientData data, Tcl_Interp *interp, int argc, const char *argv[] ) )。
我想知道被调用的回调函数中的脚本文件名和行号。
我可以使用 ((Interp*) interp)->scriptFile 获取脚本文件名。
但是,我无法获得脚本文件的行号。有没有办法检索它(或以任何方式计算它)?

最佳答案

该信息仅通过 info frame 在 Tcl 级别公开。命令。 (它的内部 C API 非常可怕,以至于它从未被公开。)这意味着你需要用 Tcl_Eval() 做这样的事情。 :

// These can be cached safely per thread; reference management is a thing for another question
Tcl_Obj *scriptNameHandle = Tcl_NewStringObj("file", -1);
Tcl_Obj *lineNumberHandle = Tcl_NewStringObj("line", -1);

// How to actually get the information; I'm omitting error handling
Tcl_Eval(interp, "info frame -1");
Tcl_Obj *frameDict = Tcl_GetObjResult(interp);
Tcl_Obj *scriptNameObj = nullptr, *lineNumberObj = nullptr;
Tcl_DictObjGet(nullptr, frameDict, scriptNameHandle, &scriptNameObj);
Tcl_DictObjGet(nullptr, frameDict, lineNumberHandle, &lineNumberObj);

// Now we have to unbox the information
if (scriptNameObj != nullptr) {
    const char *filename = Tcl_GetString(scriptNameObj);
    // Do something with this info; COPY IT if you want to keep past the result reset
}
if (lineNumberObj != nullptr) {
    int lineNumber = -1;
    Tcl_GetIntFromObj(nullptr, lineNumberObj, &lineNumber);
    // Do something with this info
}

// Release the result memory, i.e., the dictionary and its contents
Tcl_ResetResult(interp);
请注意,file也不是 line保证存在 key ; line key 通常在那里,但 file key 仅在运行可以追溯到文件的代码时才存在,即返回到 Tcl_EvalFile()和相关的。

关于C++/Tcl 调用 Tcl_CreateCommand 记录的函数时,我可以检索 Tcl 文件行号吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64331927/

相关文章:

sqlite - tcl数据库的插入和检索速度慢

linux - 期望 ping 脚本跳过发送步骤

tcl - 处理来自不受信任的套接字(和其他流)的 DoS

arguments - 如何将参数传递给 TCL 源

c++ - 对于单元测试,是否有一种骇人听闻的方法来更改 const 变量的值?

c++ - 从 QML ListView 操作 QAbstractListModel 中的数据

c++ - 在 Windows XP 中引用 _wsystem() 中的 exe 路径时出现问题

c++ - 成员变量继承

c++ - 如何在 alt-tab 切换回来后恢复 ClipCursor 收缩

linux - 将 puts 的输出同时发送到日志文件和标准输出?