<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE TIP SYSTEM "http://www.tcl.tk/cgi-bin/tct/tip/tipxml.dtd">
<!-- Converted at Thu May 17 03:35:01 GMT 2012 -->
<!-- TIP AutoGenerator - written by Donal K. Fellows -->

<TIP number='227'>
<header><title>Interface to Get and Set the Return Options of an Interpreter</title><author address="mailto:dgp@users.sf.net">Don Porter</author><status type='project' state='final' tclversion="8.5" vote='after'>$Revision: 1.5 $</status><history></history><created day='30' month='oct' year='2004' /><keyword>Tcl</keyword></header>
<abstract>This TIP proposes new public C routines to allow the same access to interpreter return options <tipref type="text" tip="90"/> as are provided at the script level by the <emph style="bold">catch</emph> and <emph style="bold">return</emph> commands.</abstract>
<body><section title="Background">
<para>In Tcl 8.5, the <emph style="bold">return</emph> command has aready been extended to accept all option value pairs passed to it as arguments, to permit extensions to augment any custom return code values with whatever additional data values are appropriate. The <emph style="bold">errorInfo</emph> and <emph style="bold">errorCode</emph> values associated with the <emph style="bold">TCL_ERROR</emph> return code are already converted to this mechanism.</para>
<para>The ability to set custom return options in the interp has been limited to the script level though. For extension commands implemented entirely in C, it is inconvenient and somewhat unreliable to perform a <emph style="italic">Tcl_Eval(&quot;return ...&quot;)</emph> to be able to access this capability.</para>
<para>Likewise, the <emph style="bold">catch</emph> command is able to capture the current set of return options in the interp, but doing so requires both a script level command, and use of a variable. Extension commands implemented in C are better served with a direct interface to fetch the dictionary value.</para>
</section>
<section title="Proposal">
<para>Two new routines will be added to Tcl&apos;s public stub table:</para>
<quote>int <emph style="bold">Tcl_SetReturnOptions</emph>(Tcl_Interp *<emph style="italic">interp</emph>, Tcl_Obj *<emph style="italic">options</emph>)</quote>
<quote>Tcl_Obj *<emph style="bold">Tcl_GetReturnOptions</emph>(Tcl_Interp *<emph style="italic">interp</emph>, int <emph style="italic">result</emph>)</quote>
<para>These routines already exist in the HEAD of Tcl&apos;s development sources, but as private routines. The Tcl source code itself already makes calls to these routines where appropriate, and their functioning is tested by the test suite. This TIP merely proposes making these routines available as part of the public interface.</para>
<para>The <emph style="italic">Tcl_SetReturnOptions</emph> routine is essentially equivalent to the <emph style="bold">return -options</emph> command. The <emph style="italic">int</emph> value returned is the return code that the <emph style="bold">return</emph> command would return given the same options. In fact, a valid implementation for the <emph style="bold">return</emph> command would be:</para>
<verbatim><vline encoding='base64'>IGludCBUY2xfUmV0dXJuT2JqQ21kKENsaWVudERhdGEgY2QsIFRjbF9JbnRlcnAgKmludGVycCw=</vline><vline encoding='base64'>CQkJaW50IG9iamMsIFRjbF9PYmogKmNvbnN0IG9ianZbXSk=</vline><vline encoding='base64'>IHs=</vline><vline encoding='base64'>ICAgICBpbnQgZXhwbGljaXRSZXN1bHQgPSAoMCA9PSAob2JqYyAlMikpOw==</vline><vline encoding='base64'>ICAgICBpbnQgbnVtT3B0aW9ucyA9IG9iamMgLSAxIC0gZXhwbGljaXRSZXN1bHQ7</vline><vline encoding='base64'>ICAgICBpZiAoZXhwbGljaXRSZXN1bHQpIFRjbF9TZXRPYmpSZXN1bHQoaW50ZXJwLCBvYmp2W29iamMtMV0pOw==</vline><vline encoding='base64'>ICAgICByZXR1cm4gVGNsX1NldFJldHVybk9wdGlvbnMoaW50ZXJwLCBUY2xfTmV3TGlzdE9iaihudW1PcHRpb25zLCBvYmp2KzEpKTs=</vline><vline encoding='base64'>IH0=</vline></verbatim>
<para>Note that <emph style="italic">Tcl_SetReturnOptions</emph> is like <emph style="italic">Tcl_SetObjResult</emph> and <emph style="italic">Tcl_SetVar2Ex</emph> in that you can pass it a newly created Tcl_Obj confident that it will be freed later. No refCount manipulation is required.</para>
<para>The <emph style="italic">Tcl_GetReturnOptions</emph> routine is used by <emph style="bold">catch</emph> to fetch the value to be stored in the <emph style="italic">optionsVarName</emph> variable, if any. The <emph style="italic">result</emph> argument should be whatever return code was returned by the script evaluation, or other <emph style="italic">interp</emph> activity whose return options you wish to retrieve.</para>
<para>The <emph style="italic">(Tcl_Obj *)</emph> returned by <emph style="italic">Tcl_GetReturnOptions</emph> points to a newly created, unshared <emph style="bold">Tcl_Obj</emph>. It can be written to as returned; no need to implement copy-on-write checks. In particular, new key value pairs can be added to the dictionary, or existing ones changed or removed. As noted above, such a value is also suitable for passing right back to <emph style="italic">Tcl_SetReturnOptions</emph>.</para>
</section>
<section title="Compatibility">
<para>Some extensions provide commands that offer the ability to evaluate a Tcl script in some other context, and return the complete result of that evaluation. For example, the <emph style="bold">Thread</emph> package provides the <emph style="bold">thread::send</emph> command that evaluates a script in another interp in a different thread. The <emph style="bold">thread::send</emph> command ultimately has a return code and a result that matches those produced by the script evaluation in the other thread. Furthermore, the <emph style="bold">::errorInfo</emph> and <emph style="bold">::errorCode</emph> variables are set according to the script evaluation outcome in the other thread as well. Extensions that accomplish such passing of full evaluation results achieve it now with copies of all portions of the full evaluation results: the return code, the interp result, and when appropriate, the values of <emph style="bold">::errorInfo</emph> and <emph style="bold">::errorCode</emph>. </para>
<para>Such mechanisms will continue to work unchanged in Tcl 8.5. However, they will no longer represent the <emph style="italic">complete</emph> evaluation results of the script. In order to continue to communicate back the full outcome of script evaluation, these extensions will want to call <emph style="italic">Tcl_GetReturnOptions</emph> after the script completes, transport that value back, and call <emph style="italic">Tcl_SetReturnOptions</emph> in the original interp. Note that use of these routines will automatically take care of <emph style="bold">::errorInfo</emph> and <emph style="bold">::errorCode</emph>, so the complete outcome of script evaluation will be able to be communicated by the return code, the interp result, and the dictionary of return options. Because the return options dictionary is itself extensible, this interface will not need to change again.</para>
</section>
<section title="Reference Implementation">
<para>See Tcl Patch 1060579.</para>
</section>
<section title="Comments">
<para>Please make any comments here.</para>
</section>
<section title="Copyright">
<para>This document has been placed in the public domain.</para>
</section>
</body></TIP>

