【发布时间】:2018-03-31 22:02:56
【问题描述】:
假设我实现了一个用 c 代码编写的新 tcl 命令,我使用 Tcl_CreateObjCommand 注册了该命令,在这个 c 代码内部我调用 Tcl_Eval 来评估包含代码的字符串,以创建关联数组并将其存储在变量 tmp .如何将使用 Tcl_eval() 创建的这个 tmp 变量设置为 c 函数的返回结果对象?
例子:
int MyCommand(
ClientData clientData,
Tcl_Interp* interp,
int argc,
char* argv[])
{
int rc = Tcl_Eval(interp,
"array set tmp [list {key1} {value1} {key2} {value2}]");
if (rc != TCL_OK) {
return rc;
}
//???
Tcl_SetObjResult(interp, ?? tmp variable from eval??);
return TCL_OK;
}
当我用上面的 C 扩展运行 Tcl 解释器时,我希望看到这个结果:
TCL> set x [MyCommand]
TCL> puts "$x(key1)"
value1 # Currently an Error and not set
TCL> puts "$x(key2)"
value2 # Currently and Error and not set
在某种程度上,上述工作。只是不是我想要的方式。例如,如果我输入:
TCL> set x [MyCommand]
TCL> puts "$tmp(key1)"
value1 # Its Works! Except, I didn't want to set a global variable tmp
TCL> puts "$tmp(key2)"
value2 # Its Works! Except, I didn't want to set a global variable tmp
(也许它是一个设置 tmp 的“功能”??)无论如何,我仍然希望它通过使用 proc“返回”机制返回值来以正确的方式工作。
从 c-command-extension 的 Tcl_Eval 内部调用 Tcl_Eval() 应该是合法的,因为“Tcl 库”的文档指出,对于 tcl_eval,进行嵌套调用以评估其他命令是合法的。我只是不知道如何将对象结果从 Tcl_Eval 复制到 c 扩展程序的“返回”对象。
【问题讨论】: