%//////////////////////////////////////////////////////////////////////////////
%
% Copyright (c) 2007,2009 Daniel Adler <dadler@uni-goettingen.de>, 
%                         Tassilo Philipp <tphilipp@potion-studios.com>
%
% Permission to use, copy, modify, and distribute this software for any
% purpose with or without fee is hereby granted, provided that the above
% copyright notice and this permission notice appear in all copies.
%
% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
%
%//////////////////////////////////////////////////////////////////////////////

\newpage
\section{Bindings to programming languages}

Through binding of the \product{dyncall} library into a scripting environment,
the scripting language can gain system programming status to a certain degree.\\
The \product{dyncall} library provides bindings to Java\cite{Java},
Lua\cite{Lua}, Python\cite{Python}, R\cite{R}, Ruby\cite{Ruby} and the command line.\\
However, please note that some of these bindings are work-in-progress and not
automatically tested, meaning it might require some additional work to make them
work.

\subsection{Common Architecture}

The binding interfaces of the \product{dyncall} library to various scripting
languages share a common set of functionality to invoke a function call.

\subsubsection{Dynamic loading of code}

The helper library \emph{dynload} which accompanies the \product{dyncall}
library provides an abstract interface to operating-system specific mechanisms
for loading and accessing executable code out of, but not limited to, shared
libraries.

\subsubsection{Functions}

All bindings are based on a common interface convention providing a common set
of the following 4 functions (exact spelling depending on the binding's scripting
environment):
\begin{description}
\item [load] - load a module of compiled code
\item [free] - unload a module of compiled code
\item [find] - find function pointer by symbolic names
\item [call] - invoke a function call
\end{description}

\pagebreak

\subsubsection{Signatures}

A signature is a character string that represents a function's arguments and
return value types. It is used in the scripting language bindings invoke
functions to perform automatic type-conversion of the languages' types to the
low-level C/C++ data types. This is an essential part of mapping the more flexible
and often abstract data types provided in scripting languages conveniently to the
strict machine-level data types used by C-libraries.
The high-level C interface functions \capi{dcCallF()} and \capi{dcCallFV()}
of the \product{dyncall} library also make use of this signature string format.\\
\\
The format of a \product{dyncall} signature string is as depicted below:


\paragraph{\product{dyncall} signature string format}

\begin{center}
\group{input parameter type signature character}* \sigchar{)} \group{return
type signature character} \\
\end{center}

The \group{input parameter type signature character} sequence left to the
\sigchar{)} is in left-to-right order of the corresponding C function
parameter type list.\\
The special \group{return type signature character} \sigchar{v} specifies
that the function does not return a value and corresponds to \capi{void}
functions in C.

\begin{table}[h]
\begin{center}
\begin{tabular*}{0.75\textwidth}{cl}
\hline
Signature character & C/C++ data type \\
\hline
\sigchar{B} & \_Bool, bool \\
\sigchar{c} & char \\
\sigchar{C} & unsigned char \\
\sigchar{s} & short \\
\sigchar{S} & unsigned short \\
\sigchar{i} & int \\
\sigchar{I} & unsigned int \\
\sigchar{j} & long \\
\sigchar{J} & unsigned long \\
\sigchar{l} & long long, int64\_t \\
\sigchar{L} & unsigned long long, uint64\_t \\
\sigchar{f} & float \\
\sigchar{d} & double \\
\sigchar{p} & void* \\
\sigchar{Z} & const char* (pointing to C string) \\
\sigchar{v} & void \\
\hline
\end{tabular*}
\caption{Type signature encoding for function call data types}
\label{sigchar}
\end{center}
\end{table}

Please note that using a \sigchar{(} at the beginning of a signature string is possible,
although not required. The character doesn't have any meaning and will simply be
ignored. However, using it prevents annoying syntax highlighting problems with some code
editors.

\pagebreak

\paragraph{Examples of C function prototypes}

\begin{table}[h]
\begin{center}
\begin{tabular*}{0.75\textwidth}{rll}
\hline
& C function prototype & dyncall signature \\
\hline
void      & f1();                                     & \sigstr{)v}\\
int       & f2(int, int);                             & \sigstr{ii)i}\\
long long & f3(void*);                                & \sigstr{p)L}\\
void      & f3(int**);                                & \sigstr{p)v}\\
double    & f4(int, bool, char, double, const char*); & \sigstr{iBcdZ)d}\\
\hline
\end{tabular*}
\caption{Type signature examples of C function prototypes}
\label{sigex}
\end{center}
\end{table}



\subsection{Python language bindings}

The python module {\tt pydc} implements the Python language bindings,
namely {\tt load}, {\tt find}, {\tt free}, {\tt call}.

\begin{table}[h]
\begin{center}
\begin{tabular*}{0.75\textwidth}{ll}
\hline
Signature character & accepted Python data types\\
\hline
\sigchar{B} & bool \\
\sigchar{c} & if string, take first item\\
\sigchar{s} & int, check in range\\
\sigchar{i} & int\\
\sigchar{j} & int\\
\sigchar{l} & long, casted to long long\\
\sigchar{f} & float\\
\sigchar{d} & double\\
\sigchar{p} & string or long casted to void*\\
\sigchar{v} & no return type\\
\hline
\end{tabular*}
\caption{Type signature encoding for Python bindings}
\label{Pysigchar}
\end{center}
\end{table}

\subsection{R language bindings}

The R package {\tt rdyncall} implements the R langugae bindings providing the function
{\tt .dyncall() }.

\begin{table}[h]
\begin{center}
\begin{tabular*}{0.75\textwidth}{ll}
\hline
Signature character & accepted R data types\\
\hline
\sigchar{B} & coerced to logical vector, first item\\
\sigchar{c} & coerced to integer vector, first item truncated char\\
\sigchar{C} & coerced to integer vector, first item truncated to unsigned char\\
\sigchar{s} & coerced to integer vector, first item truncated to short\\
\sigchar{S} & coerced to integer vector, first item truncated to unsigned short\\
\sigchar{i} & coerced to integer vector, first item\\
\sigchar{I} & coerced to integer vector, first item casted to unsigned int\\
\sigchar{j} & coerced to integer vector, first item\\
\sigchar{J} & coerced to integer vector, first item casted to unsigned long\\
\sigchar{l} & coerced to numeric, first item casted to long long\\
\sigchar{L} & coerced to numeric, first item casted to unsigned long long\\
\sigchar{f} & coerced to numeric, first item casted to float\\
\sigchar{d} & coerced to numeric, first item\\
\sigchar{p} & external pointer or coerced to string vector, first item\\
\sigchar{Z} & coerced to string vector, first item\\
\sigchar{v} & no return type\\
\hline
\end{tabular*}
\caption{Type signature encoding for R bindings}
\label{Rsigchar}
\end{center}
\end{table}

Some notes on the R Binding:
\begin{itemize}
\item Unsigned 32-bit integers are represented as signed integers in R.
\item 64-bit integer types do not exist in R, therefore we use double floats
to represent 64-bit integers (using only the 52-bit mantissa part).
\end{itemize}

\pagebreak

\subsection{Ruby language bindings}

The Ruby gem {\tt rbdc}  implements the Ruby language bindings.

\begin{table}[h]
\begin{center}
\begin{tabular*}{0.75\textwidth}{ll}
\hline
Signature character & accepted Ruby data types\\
\hline
\sigchar{B} & TrueClass, FalseClass, NilCalss, Fixnum casted to bool\\
\sigchar{c}, \sigchar{C} & Fixnum cast to (unsigned) char\\
\sigchar{s}, \sigchar{S} & Fixnum cast to (unsigned) short\\
\sigchar{i}, \sigchar{I} & Fixnum cast to (unsigned) int\\
\sigchar{j}, \sigchar{J} & Fixnum cast to (unsigned) long\\
\sigchar{l}, \sigchar{L} & Fixnum cast to (unsigned) long long\\
\sigchar{f}              & Float cast to float\\
\sigchar{d}              & Float cast to double\\
\sigchar{p}, \sigchar{Z} & String cast to void*\\
\sigchar{v}              & no return type\\
\hline
\end{tabular*}
\caption{Type signature encoding for Ruby bindings}
\label{Rubysigchar}
\end{center}
\end{table}

