<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://www.tcl.tk/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Wed May 22 16:31:57 GMT 2013 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='79'>
<header><title>Add Deletion Callback to Tcl_CreateObjTrace</title><author address="mailto:kennykb@acm.org">Kevin Kenny</author><status type='project' state='final' tclversion="8.4" vote='after'>$Revision: 1.7 $</status><history></history><created day='3' month='jan' year='2002' /><discussions url='news:comp.lang.tcl'/><keyword>trace Tcl_Obj</keyword></header>
<abstract>This document is a correction to the <emph style="italic">Tcl_CreateObjTrace</emph> API from <tipref type="text" tip="32"/>. It addresses a deficiency that the API provides no deletion callback for its client data.</abstract>
<body><section title="Rationale">
<para>In developing a reference implementation for the changes described in <tipref type="text" tip="32"/>, the author of this TIP discovered an anomaly in the proposed API for <emph style="italic">Tcl_CreateObjTrace.</emph> While the function accepts a <emph style="italic">ClientData</emph> parameter, it provides no deletion callback for the client data, making it difficult to clean up the client data if <emph style="italic">Tcl_DeleteTrace</emph> is called from a point in the code where the client data is not readily available. (The usual pattern in the Tcl library is to provide a deletion callback wherever client data is passed to the Tcl interpreter; <emph style="italic">Tcl_CreateObjCommand</emph> is an example.</para>
</section>
<section title="Specification">
<para>The <emph style="italic">Tcl_CreateObjTrace</emph> function proposed in <tipref type="text" tip="32"/> shall be changed to the following:</para>
<verbatim><vline encoding='base64'>IFRjbF9UcmFjZSBUY2xfQ3JlYXRlT2JqVHJhY2UgKCBUY2xfSW50ZXJwKiAgICAgICAgICAgICAgICBpbnRlcnAs</vline><vline encoding='base64'>ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpbnQgICAgICAgICAgICAgICAgICAgICAgICBsZXZlbCwg</vline><vline encoding='base64'>ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpbnQgICAgICAgICAgICAgICAgICAgICAgICBmbGFncyw=</vline><vline encoding='base64'>ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBUY2xfQ21kT2JqVHJhY2VQcm9jKiAgICAgICBvYmpQcm9jLA==</vline><vline encoding='base64'>ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBDbGllbnREYXRhICAgICAgICAgICAgICAgICBjbGllbnREYXRhLA==</vline><vline encoding='base64'>ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBUY2xfQ21kT2JqVHJhY2VEZWxldGVQcm9jKiBkZWxldGVQcm9jICk7</vline></verbatim>
<para>The <emph style="italic">Tcl_CreateObjTrace</emph> function adds a trace to the Tcl evaluator. The <emph style="italic">interp</emph> argument is the Tcl interpreter for which tracing is being requested. The <emph style="italic">level</emph> argument is the maximum depth of recursive calls; when the execution depth of the interpreter exceeds this number, the trace callback does not execute. The <emph style="italic">objProc</emph> argument is the callback procedure to execute each time a Tcl command is evaluated; it is expected to have arguments and result type that match <emph style="italic">Tcl_CmdObjTraceProc</emph> below. The <emph style="italic">clientData</emph> argument is client data to pass to the <emph style="italic">objProc</emph> callback. The <emph style="italic">deleteProc</emph> argument specifies a function to call when the trace is removed by a call to <emph style="italic">Tcl_DeleteTrace.</emph> This parameter may be a null pointer if no deletion callback is desired. Finally, the <emph style="italic">flags</emph> argument gives flags that control the tracing. Initially, the only flag supported will be <emph style="italic">TCL_ALLOW_INLINE_COMPILE</emph>. If this flag is set, the bytecode compiler is permitted to compile in-line code for the Tcl built-in commands; any command that has been compiled in-line will not be traced.</para>
<para>The trace token returned from <emph style="italic">Tcl_CreateObjTrace</emph> may be passed as a parameter to <emph style="italic">Tcl_DeleteTrace</emph>, which arranges to cancel the tracing. If a non-empty <emph style="italic">deleteProc</emph> argument was supplied to <emph style="italic">Tcl_CreateObjTrace</emph>, it is called at this time. After <emph style="italic">Tcl_DeleteTrace</emph> returns, no further calls to the trace procedure will be made, and the trace token must not be used further in the calling program.</para>
<para>The <emph style="italic">Tcl_CmdObjTraceProc</emph> will have the following type signature.</para>
<verbatim><vline encoding='base64'>ICAgIHR5cGVkZWYgaW50IFRjbF9DbWRPYmpUcmFjZVByb2MoIENsaWVudERhdGEgICAgIGNsaWVudERhdGEs</vline><vline encoding='base64'>ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFRjbF9JbnRlcnAqICAgIGludGVycCw=</vline><vline encoding='base64'>ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGludCAgICAgICAgICAgIGxldmVsLA==</vline><vline encoding='base64'>ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIENPTlNUIGNoYXIqICAgIGNvbW1hbmQs</vline><vline encoding='base64'>ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFRjbF9Db21tYW5kICAgIGNvbW1hbmRJbmZvLA==</vline><vline encoding='base64'>ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGludCAgICAgICAgICAgIG9iamMs</vline><vline encoding='base64'>ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFRjbF9PYmogKkNPTlNUIG9ianZbXSApOw==</vline></verbatim>
<para>The <emph style="italic">clientData</emph> parameter is the client data that was passed to <emph style="italic">Tcl_CreateObjTrace</emph>. The <emph style="italic">interp</emph> parameter designates a Tcl interpreter. The <emph style="italic">level</emph> parameter specifies the execution level. The <emph style="italic">command</emph> parameter gives the raw UTF-8 text of the command being evaluated, before any substitutions have been performed. The <emph style="italic">commandInfo</emph> parameter is an opaque <emph style="italic">Tcl_Command</emph> object that gives information about the command. The <emph style="italic">objc</emph> and <emph style="italic">objv</emph> parameters are the command name and parameter vector after substitution.</para>
<para>The trace procedure is expected to return a standard Tcl status return. If it returns <emph style="italic">TCL_OK</emph>, the command is evaluated normally. If it returns <emph style="italic">TCL_ERROR</emph>, evaluation of the command does not take place. The interpreter result is expected to contain an error message. If it returns any other status, such as <emph style="italic">TCL_BREAK</emph>, <emph style="italic">TCL_CONTINUE</emph> or <emph style="italic">TCL_RETURN</emph>, it is treated as if the command had done so.</para>
<para>The <emph style="italic">Tcl_CmdObjTraceDeleteProc</emph> will have the following type signature.</para>
<verbatim><vline encoding='base64'>ICAgIHR5cGVkZWYgdm9pZCBUY2xfQ21kT2JqVHJhY2VEZWxldGVQcm9jKCBDbGllbnREYXRhIGNsaWVudERhdGEgKTs=</vline></verbatim>
<para>The <emph style="italic">clientData</emph> parameter is the client data that was originally passed into <emph style="italic">Tcl_CreateObjTrace</emph>.</para>
</section>
<section title="Copyright">
<para>Copyright © 2002 by Kevin B. Kenny. Distribution in whole or part, with or without annotations, is unlimited.</para>
</section>
</body></TIP>
