\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#322: Publish the NRE API}
\date{July 13, 2008}
\author{Miguel Sofer}
\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 \#322}&\textbf{Publish the NRE API}\\\hline
Author&

Miguel Sofer \tipmail{msofer}{users.sourceforge.net}
\\
Created&Sunday, \(\text{13}^{\text{th}}\) July 2008\\
Type&Project\\
Tcl Version&\(8.6\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.9 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This TIP exposes an API to allow extension writers to take advantage of Tcl's Non-Recursive evaluation Engine.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Rationale}

NRE (for \textbf{N}on-\textbf{R}ecursive \textbf{E}ngine) is a trampoline implementation for command evaluation and bytecode execution that massively reduce Tcl's footprint on the C stack. It is conceptually related to stackless Python.

NRE is fully backwards compatible with script and C extensions and has already been committed to HEAD. Extensions that use the normal Tcl API run properly but cannot take advantage of the non-recursivity.

This TIP proposes to publish a small API for extension writers that will allow them to exploit the new possibilities.

\section{Functions to be Exported}

The first two functions permit the creation of NRE-enabled commands. \textbf{Tcl\_NRCreateCommand} creates a command that implements an NRE interface \textit{nreProc}. As every command needs also a regular \textit{objProc}, the function \textbf{Tcl\_NRCallObjProc} is provided as a utility permitting a relatively simple way to generate the \textit{objProc} from the \textit{nreProc}.
\begin{itemize}
\item{}
Tcl\_Command \textbf{Tcl\_NRCreateCommand}(Tcl\_Interp *\textit{interp}, const char *\textit{cmdName}, Tcl\_ObjCmdProc *\textit{proc}, Tcl\_ObjCmdProc *\textit{nreProc}, ClientData \textit{clientData}, Tcl\_CmdDeleteProc *\textit{deleteProc})
\item{}
int \textbf{Tcl\_NRCallObjProc}(Tcl\_Interp *\textit{interp}, Tcl\_ObjCmdProc *\textit{objProc}, ClientData \textit{clientData}, int \textit{objc}, Tcl\_Obj *const *\textit{objv}[])
\end{itemize}

The next three functions provide the API to request an evaluation by the trampoline, after the caller returned:
\begin{itemize}
\item{}
int \textbf{Tcl\_NREvalObj}(Tcl\_Interp *\textit{interp}, Tcl\_Obj *\textit{objPtr}, int \textit{flags})
\item{}
int \textbf{Tcl\_NREvalObjv}(Tcl\_Interp *\textit{interp}, int \textit{objc}, Tcl\_Obj *const \textit{objv}[], int \textit{flags})
\item{}
int \textbf{Tcl\_NRCmdSwap}(Tcl\_Interp *\textit{interp}, Tcl\_Command \textit{cmd}, int \textit{objc}, Tcl\_Obj *const \textit{objv}[])
\end{itemize}

Finally, there is a function to register a callback that the trampoline has to execute right after a requested evaluation, typically used for cleanup.
\begin{itemize}
\item{}
void \textbf{Tcl\_NRAddCallback}(Tcl\_Interp *\textit{interp}, Tcl\_NRPostProc *\textit{postProcPtr}, ClientData \textit{data0}, ClientData \textit{data1}, ClientData \textit{data2}, ClientData \textit{data3})
\end{itemize}

\section{Documentation}

NRE's internal functioning is somewhat documented at \url{http://msofer.com:8080/wiki?name=NRE}

An example of how the API is to be used can be found at \url{http://msofer.com:8080/wiki?name=Exploiting+NRE}

The new API will be documented in a manual page \textit{doc/NRE.3}.

\section{Reference Implementation}

The API is already available in HEAD (to become Tcl8.6a2); a high level description is available at \url{http://msofer.com:8080/wiki?name=NRE+short+explanation}

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\end{document}
