1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
%//////////////////////////////////////////////////////////////////////////////
%
% 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}
|