\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#353: NR-enabled Expressions for Extensions}
\date{July 29, 2009}
\author{Don Porter}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#353}&\textbf{NR-enabled Expressions for Extensions}\\\hline
Author&

Don Porter \tipmail{dgp}{users.sf.net}
\\
Created&Wednesday, \(\text{29}^{\text{th}}\) July 2009\\
Type&Project\\
Tcl Version&\(8.6\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.7 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP proposes the new public routine \textbf{Tcl\_NRExprObj} to provide extension commands that evaluate Tcl expressions the ability to do so in a non-recursive manner.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Background}

In a few contexts, expressions that contain \textbf{yield} raise the error ``\textit{cannot yield: C stack busy}''; see Tcl Bugs 2823282 [\url{https://sourceforge.net/support/tracker.php?aid=2823282}] and 2823276 [\url{https://sourceforge.net/support/tracker.php?aid=2823276}]. This is because a few little-visited corners of Tcl's implementation call the routine \textbf{Tcl\_ExprObj} and that routine is not NR-enabled.

For extensions wishing to evaluate Tcl expressions, \textbf{Tcl\_ExprObj} is not little-visited. It is the public, supported, recommended tool for the job. Just as \cite{tip322} provided a routine \textbf{Tcl\_NREvalObj} as an NR-enabled replacement for \textbf{Tcl\_EvalObj}, extensions wishing to NR-enable their commands need an analogous replacement for \textbf{Tcl\_ExprObj}.

\section{Rationale}

Tcl has a long history of providing extensions access to the same capabilities available to the built-in command set so that extension commands are on an equal footing, not in a second class status. Keeping with that, we want extensions to be able to create NR-enabled commands, so we need to provide an interface for extensions to evaluate expressions in an NR-enabled manner. This TIP can be seen as filling up a hole in \cite{tip322}.

\section{Scope Limitations}

The Tcl public C interface provides a whole family of variants of \textbf{Tcl\_ExprObj}: \textbf{Tcl\_ExprLongObj}, \textbf{Tcl\_ExprDoubleObj}, \textbf{Tcl\_ExprBooleanObj}, \textbf{Tcl\_ExprLong}, \textbf{Tcl\_ExprDouble}, \textbf{Tcl\_ExprBoolean}, \textbf{Tcl\_ExprString}. NR-enabled counterparts to these routines are \textit{not} proposed. Extensions rewriting their command procedures to use the proposed \textbf{Tcl\_NRExprObj} for sake of NR-enabling can at the same time be expected to convert from these convenience wrappers to more direct use of a single NR-enabled primitive.

\section{Proposal}

Add the following routine to Tcl's public interface:

\begin{quote}
int \textbf{Tcl\_NRExprObj}(Tcl\_Interp *\textit{interp}, Tcl\_Obj *\textit{objPtr}, Tcl\_Obj *\textit{resultPtr})
\end{quote}

This routine places on the NR stack a request that the Tcl non-recursive trampoline evaluate the \textit{objPtr} value as a Tcl expression in interpreter \textit{interp}. This routine returns the value \textbf{TCL\_OK}, 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's internals have the freedom to change that without need to change the public interface.

The \textit{resultPtr} argument must be an unshared Tcl value. When expression evaluation succeeds, the result of the expression is written to \textit{resultPtr} in the same way that \textbf{Tcl\_SetStringObj} would write a string value to an unshared Tcl value. If expression evaluation produces any return code other than \textbf{TCL\_OK}, the value of \textit{resultPtr} is left untouched.

Callers of \textbf{Tcl\_NRExprObj} will also need to call \textbf{Tcl\_NRAddCallback} to request a \textbf{Tcl\_NRPostProc} callback routine be placed on the NR stack which can take care of managing \textit{resultPtr} as appropriate depending on the \textit{result} value.

\section{Implementation}

The patch attached to Tcl Bug 2823282 [\url{https://sourceforge.net/support/tracker.php?aid=2823282}] implements this proposal.

\section{Compatibility}

There should be no compatibility issues, since at the interface level this is just the addition of a new routine. Revisions to the internal implementations of existing routines should be harmless.

\section{Migration}

As an example for extensions to follow, consider this template for a \textbf{Tcl\_ObjCmdProc} currently calling \textbf{Tcl\_ExprObj}.
\begingroup\small\begin{verbatim}
int ObjCmd(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{   int code;
    Tcl_Obj *resultPtr;
    /* determine expression, objPtr */
    code = Tcl_ExprObj(interp, objPtr, &resultPtr);
    if (code != TCL_OK) {return code}
    /* resultPtr holds expression result; continue */
}

Tcl_CreateObjCommand(interp, name, ObjCmd, /* ... */);
\end{verbatim}
\endgroup

To use \textbf{Tcl\_NRExprObj} to NR-enable this command, rewrite along these lines:
\begingroup\small\begin{verbatim}
int ObjCmd(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{   return Tcl_NRCallObjProc(interp, NRObjCmd, cd, objc, objv);  }

int NRObjCmd(ClientData cd, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{   Tcl_Obj *resultPtr = Tcl_NewObj();
    /* determine expression, objPtr */
    Tcl_NRAddCallback(interp, Callback, resultPtr, /*...*/);
    return Tcl_NRExprObj(interp, objPtr, resultPtr);
}

int Callback(ClientData data[], Tcl_Interp *interp, int code)
{   Tcl_Obj *resultPtr = data[0];
    if (code != TCL_OK) {Tcl_DecrRefCount(resultPtr); return code;}
    /* resultPtr holds expression result; continue */
}

Tcl_NRCreateCommand(interp, name, ObjCmd, NRObjCmd, /*...*/);
\end{verbatim}
\endgroup

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#1}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#322]{tip322}
Miguel Sofer,
\emph{Publish the NRE API},
on-line at \url{http://purl.org/tcl/tip/322.html}
\end{thebibliography}
\end{document}
