Reputation: 87
I defined a tcl command in c++
Tcl_CreateObjCommand(interp, "myproc", myproc_func, nullptr, nullptr);
in tclsh
% rename myproc ""
Does this completely remove myproc? If I define another myproc command, is it possible to associate it with the myproc defined in c++?
Upvotes: 1
Views: 145
Reputation: 137577
When you rename myproc ""
then that command is gone from the current interpreter. No other interpreter is affected.
The internal state of the command may persist for a while however, e.g., if there are some executing instances of the command on the current call stack. So by "gone" I specifically mean that the mapping from the name myproc
to the function myproc_func
with NULL client data is gone; if you had defined a deletion callback it would have been called (handling the consequences of this is what the Tcl_Preserve()
API is for) but as you didn't, it is just a simple deletion, i.e., the removal of a record from a hash table inside a namespace. If you have global state on the C side of your program, it will persist.
Upvotes: 1
Reputation: 4813
When you delete a command (using rename myproc ""
), you lose all references to it. There is no way to get the command back, short of recreating it from scratch.
If you want to temporarily make myproc unavailable, you can rename to some special name, then rename it back to reinstate it:
rename myproc __myproc
# Do some other stuff that may not use myproc
rename __myproc myproc
Of course, the intermediate code can still call __myproc.
Another approach is to hide the command:
interp hide {} myproc
# Do some other stuff that may not use myproc
interp expose {} myproc
This still doesn't completely prevent access to the function, as it can be invoked using: interp invokehidden {} myproc
.
It all depends on why you want to do this. Are you afraid that some code uses myproc when it shouldn't? Or do you want to recover from code that may delete the function? In that case, you may want to run the untrusted code in a (safe) slave interpreter.
Upvotes: 3