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

<TIP number='356'>
<header><title>NR-enabled Substitutions for Extensions</title><author address="mailto:dgp@users.sf.net">Don Porter</author><status type='project' state='final' tclversion="8.6" vote='after'>$Revision: 1.4 $</status><history></history><created day='17' month='sep' year='2009' /><keyword>Tcl {C API} subst</keyword></header>
<abstract>This TIP proposes the new public routine <emph style="bold">Tcl_NRSubstObj</emph> to provide extension commands that evaluate Tcl substitutions the ability to do so in a non-recursive manner.</abstract>
<body><section title="Background">
<para>Continuing in the path of <tipref type="text" tip="322"/> and <tipref type="text" tip="353"/>, we want extensions to be able to create NR-enabled commands, and any command procedures currently calling the <emph style="bold">Tcl_SubstObj</emph> routine are not NR-enabled. The solution is to provide the NR-enabled counterpart.</para>
</section>
<section title="Proposal">
<para>Add the following routine to Tcl&apos;s public interface:</para>
<quote>int <emph style="bold">Tcl_NRSubstObj</emph>(Tcl_Interp *<emph style="italic">interp</emph>, Tcl_Obj *<emph style="italic">objPtr</emph>, int <emph style="italic">flags</emph>)</quote>
<para>This routine places on the NR stack a request that the Tcl non-recursive trampoline evaluate the <emph style="italic">objPtr</emph> value as a Tcl substitution in interpreter <emph style="italic">interp</emph>, as controlled by the value of <emph style="italic">flags</emph>. The <emph style="italic">flags</emph> value is the same combination of <emph style="bold">TCL_SUBST_BACKSLASHES</emph>, <emph style="bold">TCL_SUBST_COMMANDS</emph>, and <emph style="bold">TCL_SUBST_VARIABLES</emph> that control <emph style="bold">Tcl_SubstObj</emph>. This routine returns the value <emph style="bold">TCL_OK</emph>, since there is (currently) no way this request operation can fail. The proposed interface still provides for an int return value so that future revisions to Tcl&apos;s internals have the freedom to change that without need to change the public interface.</para>
<para>After the trampoline completes the requested substitution, it will pass the return code, either <emph style="bold">TCL_OK</emph> or <emph style="bold">TCL_ERROR</emph>, to the next callback on the NR-stack, and either the result of the substitution or the error message will be stored in the result of <emph style="italic">interp</emph>. The caller of <emph style="bold">Tcl_NRSubstObj</emph> may also call <emph style="bold">Tcl_NRAddCallback</emph> to request a <emph style="bold">Tcl_NRPostProc</emph> callback routine be placed on the NR stack to receive these results, if needed to achieve the task the caller is performing.</para>
</section>
<section title="Implementation">
<para>The proposed routine is already present in the HEAD of Tcl as the internal routine <emph style="bold">TclNRSubstObj</emph>. This proposal would simply promote it to Tcl&apos;s public interface.</para>
</section>
<section title="Compatibility">
<para>There should be no compatibility issues, since at the interface level this is just the addition of a new routine.</para>
</section>
<section title="Migration">
<para>As an example for extensions to follow, consider this template for a <emph style="bold">Tcl_ObjCmdProc</emph> currently calling <emph style="bold">Tcl_SubstObj</emph>.</para>
<verbatim><vline encoding='base64'>aW50IE9iakNtZChDbGllbnREYXRhIGNkLCBUY2xfSW50ZXJwICppbnRlcnAsIGludCBvYmpjLCBUY2xfT2JqICpjb25zdCBvYmp2W10p</vline><vline encoding='base64'>ICAgIFRjbF9PYmogKnJlc3VsdFB0cjs=</vline><vline encoding='base64'>ICAgIC8qIGRldGVybWluZSB0ZXh0IHRvIGJlIHN1YnN0aXR1dGVkLCBvYmpQdHIgKi8=</vline><vline encoding='base64'>ICAgIC8qIGRldGVybWluZSBmbGFncyB2YWx1ZSB0byBjb250cm9sIHN1YnN0aXR1dGlvbiAqLw==</vline><vline encoding='base64'>ICAgIHJlc3VsdFB0ciA9IFRjbF9TdWJzdE9iaihpbnRlcnAsIG9ialB0ciwgZmxhZ3MpOw==</vline><vline encoding='base64'>ICAgIGlmIChyZXN1bHRQdHIgPT0gTlVMTCkge3JldHVybiBUQ0xfRVJST1J9</vline><vline encoding='base64'>ICAgIC8qIHJlc3VsdFB0ciBob2xkcyBzdWJzdGl0dXRpb24gcmVzdWx0OyBjb250aW51ZSAqLw==</vline><vline encoding='base64'>fQ==</vline><vline encoding='base64'></vline><vline encoding='base64'>VGNsX0NyZWF0ZU9iakNvbW1hbmQoaW50ZXJwLCBuYW1lLCBPYmpDbWQsIC8qIC4uLiAqLyk7</vline></verbatim>
<para>To use <emph style="bold">Tcl_NRSubstObj</emph> to NR-enable this command, rewrite along these lines:</para>
<verbatim><vline encoding='base64'>aW50IE9iakNtZChDbGllbnREYXRhIGNkLCBUY2xfSW50ZXJwICppbnRlcnAsIGludCBvYmpjLCBUY2xfT2JqICpjb25zdCBvYmp2W10p</vline><vline encoding='base64'>eyAgIHJldHVybiBUY2xfTlJDYWxsT2JqUHJvYyhpbnRlcnAsIE5ST2JqQ21kLCBjZCwgb2JqYywgb2Jqdik7ICB9</vline><vline encoding='base64'></vline><vline encoding='base64'>aW50IE5ST2JqQ21kKENsaWVudERhdGEgY2QsIFRjbF9JbnRlcnAgKmludGVycCwgaW50IG9iamMsIFRjbF9PYmogKmNvbnN0IG9ianZbXSk=</vline><vline encoding='base64'>eyAgIA==</vline><vline encoding='base64'>ICAgIC8qIGRldGVybWluZSB0ZXh0IHRvIGJlIHN1YnN0aXR1dGVkLCBvYmpQdHIgKi8=</vline><vline encoding='base64'>ICAgIC8qIGRldGVybWluZSBmbGFncyB2YWx1ZSB0byBjb250cm9sIHN1YnN0aXR1dGlvbiAqLw==</vline><vline encoding='base64'>ICAgIFRjbF9OUkFkZENhbGxiYWNrKGludGVycCwgQ2FsbGJhY2ssIC8qLi4uKi8pOw==</vline><vline encoding='base64'>ICAgIHJldHVybiBUY2xfTlJTdWJzdE9iaihpbnRlcnAsIG9ialB0ciwgZmxhZ3MpOw==</vline><vline encoding='base64'>fQ==</vline><vline encoding='base64'></vline><vline encoding='base64'>aW50IENhbGxiYWNrKENsaWVudERhdGEgZGF0YVtdLCBUY2xfSW50ZXJwICppbnRlcnAsIGludCBjb2RlKQ==</vline><vline encoding='base64'>eyAgIFRjbF9PYmogKnJlc3VsdFB0cjs=</vline><vline encoding='base64'>ICAgIGlmIChjb2RlID09IFRDTF9FUlJPUikge3JldHVybiBUQ0xfRVJST1I7fQ==</vline><vline encoding='base64'>ICAgIHJlc3VsdFB0ciA9IFRjbF9HZXRPYmpSZXN1bHQoaW50ZXJwKTs=</vline><vline encoding='base64'>ICAgIC8qIHJlc3VsdFB0ciBob2xkcyBleHByZXNzaW9uIHJlc3VsdDsgY29udGludWUgKi8=</vline><vline encoding='base64'>fQ==</vline><vline encoding='base64'></vline><vline encoding='base64'>VGNsX05SQ3JlYXRlQ29tbWFuZChpbnRlcnAsIG5hbWUsIE9iakNtZCwgTlJPYmpDbWQsIC8qLi4uKi8pOw==</vline></verbatim>
</section>
<section title="Copyright">
<para>This document has been placed in the public domain.</para>
</section>
</body></TIP>
