/branches/closed/gsoc2008-bhy/Doc/Manual/Scripting.html

# · HTML · 474 lines · 382 code · 90 blank · 2 comment · 0 complexity · b2bb257ee2f4ad182d30d5eaea0e852f MD5 · raw file

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>Scripting Languages</title>
  5. <link rel="stylesheet" type="text/css" href="style.css">
  6. </head>
  7. <body bgcolor="#ffffff">
  8. <H1><a name="Scripting"></a>4 Scripting Languages</H1>
  9. <!-- INDEX -->
  10. <div class="sectiontoc">
  11. <ul>
  12. <li><a href="#Scripting_nn2">The two language view of the world</a>
  13. <li><a href="#Scripting_nn3">How does a scripting language talk to C?</a>
  14. <ul>
  15. <li><a href="#Scripting_nn4">Wrapper functions</a>
  16. <li><a href="#Scripting_nn5">Variable linking</a>
  17. <li><a href="#Scripting_nn6">Constants</a>
  18. <li><a href="#Scripting_nn7">Structures and classes</a>
  19. <li><a href="#Scripting_nn8">Proxy classes</a>
  20. </ul>
  21. <li><a href="#Scripting_nn9">Building scripting language extensions</a>
  22. <ul>
  23. <li><a href="#Scripting_nn10">Shared libraries and dynamic loading</a>
  24. <li><a href="#Scripting_nn11">Linking with shared libraries</a>
  25. <li><a href="#Scripting_nn12">Static linking</a>
  26. </ul>
  27. </ul>
  28. </div>
  29. <!-- INDEX -->
  30. <p>
  31. This chapter provides a brief overview of scripting language extension
  32. programming and the mechanisms by which scripting language interpreters
  33. access C and C++ code.
  34. </p>
  35. <H2><a name="Scripting_nn2"></a>4.1 The two language view of the world</H2>
  36. <p>
  37. When a scripting language is used to control a C program, the
  38. resulting system tends to look as follows:
  39. </p>
  40. <center><img src="ch2.1.png" alt="Scripting language input - C/C++ functions output"></center>
  41. <p>
  42. In this programming model, the scripting language interpreter is used
  43. for high level control whereas the underlying functionality of the
  44. C/C++ program is accessed through special scripting language
  45. "commands." If you have ever tried to write your own simple command
  46. interpreter, you might view the scripting language approach
  47. to be a highly advanced implementation of that. Likewise,
  48. If you have ever used a package such as MATLAB or IDL, it is a
  49. very similar model--the interpreter executes user commands and
  50. scripts. However, most of the underlying functionality is written in
  51. a low-level language like C or Fortran.
  52. </p>
  53. <p>
  54. The two-language model of computing is extremely powerful because it
  55. exploits the strengths of each language. C/C++ can be used for maximal
  56. performance and complicated systems programming tasks. Scripting
  57. languages can be used for rapid prototyping, interactive debugging,
  58. scripting, and access to high-level data structures such associative
  59. arrays. </p>
  60. <H2><a name="Scripting_nn3"></a>4.2 How does a scripting language talk to C?</H2>
  61. <p>
  62. Scripting languages are built around a parser that knows how
  63. to execute commands and scripts. Within this parser, there is a
  64. mechanism for executing commands and accessing variables.
  65. Normally, this is used to implement the builtin features
  66. of the language. However, by extending the interpreter, it is usually
  67. possible to add new commands and variables. To do this,
  68. most languages define a special API for adding new commands.
  69. Furthermore, a special foreign function interface defines how these
  70. new commands are supposed to hook into the interpreter.
  71. </p>
  72. <p>
  73. Typically, when you add a new command to a scripting interpreter
  74. you need to do two things; first you need to write a special
  75. "wrapper" function that serves as the glue between the interpreter
  76. and the underlying C function. Then you need to give the interpreter
  77. information about the wrapper by providing details about the name of the
  78. function, arguments, and so forth. The next few sections illustrate
  79. the process.
  80. </p>
  81. <H3><a name="Scripting_nn4"></a>4.2.1 Wrapper functions</H3>
  82. <p>
  83. Suppose you have an ordinary C function like this :</p>
  84. <div class="code"><pre>
  85. int fact(int n) {
  86. if (n &lt;= 1) return 1;
  87. else return n*fact(n-1);
  88. }
  89. </pre></div>
  90. <p>
  91. In order to access this function from a scripting language, it is
  92. necessary to write a special "wrapper" function that serves as the
  93. glue between the scripting language and the underlying C function. A
  94. wrapper function must do three things :</p>
  95. <ul>
  96. <li>Gather function arguments and make sure they are valid.
  97. <li>Call the C function.
  98. <li>Convert the return value into a form recognized by the scripting language.
  99. </ul>
  100. <p>
  101. As an example, the Tcl wrapper function for the <tt>fact()</tt>
  102. function above example might look like the following : </p>
  103. <div class="code"><pre>
  104. int wrap_fact(ClientData clientData, Tcl_Interp *interp,
  105. int argc, char *argv[]) {
  106. int result;
  107. int arg0;
  108. if (argc != 2) {
  109. interp-&gt;result = "wrong # args";
  110. return TCL_ERROR;
  111. }
  112. arg0 = atoi(argv[1]);
  113. result = fact(arg0);
  114. sprintf(interp-&gt;result,"%d", result);
  115. return TCL_OK;
  116. }
  117. </pre></div>
  118. <p>
  119. Once you have created a wrapper function, the final step is to tell the
  120. scripting language about the new function. This is usually done in an
  121. initialization function called by the language when the module is
  122. loaded. For example, adding the above function to the Tcl interpreter
  123. requires code like the following :</p>
  124. <div class="code"><pre>
  125. int Wrap_Init(Tcl_Interp *interp) {
  126. Tcl_CreateCommand(interp, "fact", wrap_fact, (ClientData) NULL,
  127. (Tcl_CmdDeleteProc *) NULL);
  128. return TCL_OK;
  129. }
  130. </pre></div>
  131. <p>
  132. When executed, Tcl will now have a new command called "<tt>fact</tt>"
  133. that you can use like any other Tcl command.</p>
  134. <p>
  135. Although the process of adding a new function to Tcl has been
  136. illustrated, the procedure is almost identical for Perl and
  137. Python. Both require special wrappers to be written and both need
  138. additional initialization code. Only the specific details are
  139. different.</p>
  140. <H3><a name="Scripting_nn5"></a>4.2.2 Variable linking</H3>
  141. <p>
  142. Variable linking refers to the problem of mapping a
  143. C/C++ global variable to a variable in the scripting
  144. language interpreter. For example, suppose you had the following
  145. variable:</p>
  146. <div class="code"><pre>
  147. double Foo = 3.5;
  148. </pre></div>
  149. <p>
  150. It might be nice to access it from a script as follows (shown for Perl):</p>
  151. <div class="targetlang"><pre>
  152. $a = $Foo * 2.3; # Evaluation
  153. $Foo = $a + 2.0; # Assignment
  154. </pre></div>
  155. <p>
  156. To provide such access, variables are commonly manipulated using a
  157. pair of get/set functions. For example, whenever the value of a
  158. variable is read, a "get" function is invoked. Similarly, whenever
  159. the value of a variable is changed, a "set" function is called.
  160. </p>
  161. <p>
  162. In many languages, calls to the get/set functions can be attached to
  163. evaluation and assignment operators. Therefore, evaluating a variable
  164. such as <tt>$Foo</tt> might implicitly call the get function. Similarly,
  165. typing <tt>$Foo = 4</tt> would call the underlying set function to change
  166. the value.
  167. </p>
  168. <H3><a name="Scripting_nn6"></a>4.2.3 Constants</H3>
  169. <p>
  170. In many cases, a C program or library may define a large collection of
  171. constants. For example:
  172. </p>
  173. <div class="code"><pre>
  174. #define RED 0xff0000
  175. #define BLUE 0x0000ff
  176. #define GREEN 0x00ff00
  177. </pre></div>
  178. <p>
  179. To make constants available, their values can be stored in scripting
  180. language variables such as <tt>$RED</tt>, <tt>$BLUE</tt>, and
  181. <tt>$GREEN</tt>. Virtually all scripting languages provide C
  182. functions for creating variables so installing constants is usually
  183. a trivial exercise.
  184. </p>
  185. <H3><a name="Scripting_nn7"></a>4.2.4 Structures and classes</H3>
  186. <p>
  187. Although scripting languages have no trouble accessing simple
  188. functions and variables, accessing C/C++ structures and classes
  189. present a different problem. This is because the implementation
  190. of structures is largely related to the problem of
  191. data representation and layout. Furthermore, certain language features
  192. are difficult to map to an interpreter. For instance, what
  193. does C++ inheritance mean in a Perl interface?
  194. </p>
  195. <p>
  196. The most straightforward technique for handling structures is to
  197. implement a collection of accessor functions that hide the underlying
  198. representation of a structure. For example,
  199. </p>
  200. <div class="code"><pre>
  201. struct Vector {
  202. Vector();
  203. ~Vector();
  204. double x,y,z;
  205. };
  206. </pre></div>
  207. <p>
  208. can be transformed into the following set of functions :
  209. </p>
  210. <div class="code"><pre>
  211. Vector *new_Vector();
  212. void delete_Vector(Vector *v);
  213. double Vector_x_get(Vector *v);
  214. double Vector_y_get(Vector *v);
  215. double Vector_z_get(Vector *v);
  216. void Vector_x_set(Vector *v, double x);
  217. void Vector_y_set(Vector *v, double y);
  218. void Vector_z_set(Vector *v, double z);
  219. </pre></div>
  220. <p>
  221. Now, from an interpreter these function might be used as follows:
  222. </p>
  223. <div class="targetlang"><pre>
  224. % set v [new_Vector]
  225. % Vector_x_set $v 3.5
  226. % Vector_y_get $v
  227. % delete_Vector $v
  228. % ...
  229. </pre></div>
  230. <p>
  231. Since accessor functions provide a mechanism for accessing the
  232. internals of an object, the interpreter does not need to know anything
  233. about the actual representation of a <tt>Vector</tt>.
  234. </p>
  235. <H3><a name="Scripting_nn8"></a>4.2.5 Proxy classes</H3>
  236. <p>
  237. In certain cases, it is possible to use the low-level accessor functions
  238. to create a proxy class, also known as a shadow class.
  239. A proxy class is a special kind of object that gets created
  240. in a scripting language to access a C/C++ class (or struct) in a way
  241. that looks like the original structure (that is, it proxies the real
  242. C++ class). For example, if you
  243. have the following C definition :</p>
  244. <div class="code"><pre>
  245. class Vector {
  246. public:
  247. Vector();
  248. ~Vector();
  249. double x,y,z;
  250. };
  251. </pre></div>
  252. <p>
  253. A proxy classing mechanism would allow you to access the structure in
  254. a more natural manner from the interpreter. For example, in Python, you might want to do this:
  255. </p>
  256. <div class="targetlang"><pre>
  257. &gt;&gt;&gt; v = Vector()
  258. &gt;&gt;&gt; v.x = 3
  259. &gt;&gt;&gt; v.y = 4
  260. &gt;&gt;&gt; v.z = -13
  261. &gt;&gt;&gt; ...
  262. &gt;&gt;&gt; del v
  263. </pre></div>
  264. <p>
  265. Similarly, in Perl5 you may want the interface to work like this:</p>
  266. <div class="targetlang"><pre>
  267. $v = new Vector;
  268. $v-&gt;{x} = 3;
  269. $v-&gt;{y} = 4;
  270. $v-&gt;{z} = -13;
  271. </pre></div>
  272. <p>
  273. Finally, in Tcl :
  274. </p>
  275. <div class="targetlang"><pre>
  276. Vector v
  277. v configure -x 3 -y 4 -z 13
  278. </pre></div>
  279. <p>
  280. When proxy classes are used, two objects are at really work--one in
  281. the scripting language, and an underlying C/C++ object. Operations
  282. affect both objects equally and for all practical purposes, it appears
  283. as if you are simply manipulating a C/C++ object.
  284. </p>
  285. <H2><a name="Scripting_nn9"></a>4.3 Building scripting language extensions</H2>
  286. <p>
  287. The final step in using a scripting language with your C/C++
  288. application is adding your extensions to the scripting language
  289. itself. There are two primary approaches for doing
  290. this. The preferred technique is to build a dynamically loadable
  291. extension in the form a shared library. Alternatively, you can
  292. recompile the scripting language interpreter with your extensions
  293. added to it.
  294. </p>
  295. <H3><a name="Scripting_nn10"></a>4.3.1 Shared libraries and dynamic loading</H3>
  296. <p>
  297. To create a shared library or DLL, you often need to look at the
  298. manual pages for your compiler and linker. However, the procedure
  299. for a few common machines is shown below:</p>
  300. <div class="shell"><pre>
  301. # Build a shared library for Solaris
  302. gcc -c example.c example_wrap.c -I/usr/local/include
  303. ld -G example.o example_wrap.o -o example.so
  304. # Build a shared library for Linux
  305. gcc -fpic -c example.c example_wrap.c -I/usr/local/include
  306. gcc -shared example.o example_wrap.o -o example.so
  307. # Build a shared library for Irix
  308. gcc -c example.c example_wrap.c -I/usr/local/include
  309. ld -shared example.o example_wrap.o -o example.so
  310. </pre></div>
  311. <p>
  312. To use your shared library, you simply use the corresponding command
  313. in the scripting language (load, import, use, etc...). This will
  314. import your module and allow you to start using it. For example:
  315. </p>
  316. <div class="targetlang"><pre>
  317. % load ./example.so
  318. % fact 4
  319. 24
  320. %
  321. </pre></div>
  322. <p>
  323. When working with C++ codes, the process of building shared libraries
  324. may be more complicated--primarily due to the fact that C++ modules may need
  325. additional code in order to operate correctly. On many machines, you
  326. can build a shared C++ module by following the above procedures, but
  327. changing the link line to the following :</p>
  328. <div class="shell"><pre>
  329. c++ -shared example.o example_wrap.o -o example.so
  330. </pre></div>
  331. <H3><a name="Scripting_nn11"></a>4.3.2 Linking with shared libraries</H3>
  332. <p>
  333. When building extensions as shared libraries, it is not uncommon for
  334. your extension to rely upon other shared libraries on your machine. In
  335. order for the extension to work, it needs to be able to find all of
  336. these libraries at run-time. Otherwise, you may get an error such as
  337. the following :</p>
  338. <div class="targetlang"><pre>
  339. &gt;&gt;&gt; import graph
  340. Traceback (innermost last):
  341. File "&lt;stdin&gt;", line 1, in ?
  342. File "/home/sci/data1/beazley/graph/graph.py", line 2, in ?
  343. import graphc
  344. ImportError: 1101:/home/sci/data1/beazley/bin/python: rld: Fatal Error: cannot
  345. successfully map soname 'libgraph.so' under any of the filenames /usr/lib/libgraph.so:/
  346. lib/libgraph.so:/lib/cmplrs/cc/libgraph.so:/usr/lib/cmplrs/cc/libgraph.so:
  347. &gt;&gt;&gt;
  348. </pre></div>
  349. <p>
  350. What this error means is that the extension module created by SWIG
  351. depends upon a shared library called "<tt>libgraph.so</tt>" that the
  352. system was unable to locate. To fix this problem, there are a few
  353. approaches you can take.</p>
  354. <ul>
  355. <li>Link your extension and explicitly tell the linker where the
  356. required libraries are located. Often times, this can be done with a
  357. special linker flag such as <tt>-R</tt>, <tt>-rpath</tt>, etc. This
  358. is not implemented in a standard manner so read the man pages for your
  359. linker to find out more about how to set the search path for shared
  360. libraries.
  361. <li>Put shared libraries in the same directory as the executable. This
  362. technique is sometimes required for correct operation on non-Unix
  363. platforms.
  364. <li>Set the UNIX environment variable <tt>LD_LIBRARY_PATH</tt> to the
  365. directory where shared libraries are located before running Python.
  366. Although this is an easy solution, it is not recommended. Consider setting
  367. the path using linker options instead.
  368. </ul>
  369. <H3><a name="Scripting_nn12"></a>4.3.3 Static linking</H3>
  370. <p>
  371. With static linking, you rebuild the scripting language interpreter
  372. with extensions. The process usually involves compiling a short main
  373. program that adds your customized commands to the language and starts
  374. the interpreter. You then link your program with a library to produce
  375. a new scripting language executable.
  376. </p>
  377. <p>
  378. Although static linking is supported on all platforms, this is not
  379. the preferred technique for building scripting language
  380. extensions. In fact, there are very few practical reasons for doing this--consider
  381. using shared libraries instead.
  382. </p>
  383. </body>
  384. </html>