| 1 |
<!DOCTYPE html><html><head> |
| 2 |
<meta charset="UTF-8"> |
| 3 |
<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 4 |
<title>critcl_howto_use - C Runtime In Tcl (CriTcl)</title> |
| 5 |
<style type="text/css"><!-- |
| 6 |
HTML { |
| 7 |
background: #FFFFFF; |
| 8 |
color: black; |
| 9 |
} |
| 10 |
BODY { |
| 11 |
background: #FFFFFF; |
| 12 |
color: black; |
| 13 |
} |
| 14 |
DIV.doctools { |
| 15 |
margin-left: 10%; |
| 16 |
margin-right: 10%; |
| 17 |
} |
| 18 |
DIV.doctools H1,DIV.doctools H2 { |
| 19 |
margin-left: -5%; |
| 20 |
} |
| 21 |
H1, H2, H3, H4 { |
| 22 |
margin-top: 1em; |
| 23 |
font-family: sans-serif; |
| 24 |
font-size: large; |
| 25 |
color: #005A9C; |
| 26 |
background: transparent; |
| 27 |
text-align: left; |
| 28 |
} |
| 29 |
H1.doctools_title { |
| 30 |
text-align: center; |
| 31 |
} |
| 32 |
UL,OL { |
| 33 |
margin-right: 0em; |
| 34 |
margin-top: 3pt; |
| 35 |
margin-bottom: 3pt; |
| 36 |
} |
| 37 |
UL LI { |
| 38 |
list-style: disc; |
| 39 |
} |
| 40 |
OL LI { |
| 41 |
list-style: decimal; |
| 42 |
} |
| 43 |
DT { |
| 44 |
padding-top: 1ex; |
| 45 |
} |
| 46 |
UL.doctools_toc,UL.doctools_toc UL, UL.doctools_toc UL UL { |
| 47 |
font: normal 12pt/14pt sans-serif; |
| 48 |
list-style: none; |
| 49 |
} |
| 50 |
LI.doctools_section, LI.doctools_subsection { |
| 51 |
list-style: none; |
| 52 |
margin-left: 0em; |
| 53 |
text-indent: 0em; |
| 54 |
padding: 0em; |
| 55 |
} |
| 56 |
PRE { |
| 57 |
display: block; |
| 58 |
font-family: monospace; |
| 59 |
white-space: pre; |
| 60 |
margin: 0%; |
| 61 |
padding-top: 0.5ex; |
| 62 |
padding-bottom: 0.5ex; |
| 63 |
padding-left: 1ex; |
| 64 |
padding-right: 1ex; |
| 65 |
width: 100%; |
| 66 |
} |
| 67 |
PRE.doctools_example { |
| 68 |
color: black; |
| 69 |
background: #f5dcb3; |
| 70 |
border: 1px solid black; |
| 71 |
} |
| 72 |
UL.doctools_requirements LI, UL.doctools_syntax LI { |
| 73 |
list-style: none; |
| 74 |
margin-left: 0em; |
| 75 |
text-indent: 0em; |
| 76 |
padding: 0em; |
| 77 |
} |
| 78 |
DIV.doctools_synopsis { |
| 79 |
color: black; |
| 80 |
background: #80ffff; |
| 81 |
border: 1px solid black; |
| 82 |
font-family: serif; |
| 83 |
margin-top: 1em; |
| 84 |
margin-bottom: 1em; |
| 85 |
} |
| 86 |
UL.doctools_syntax { |
| 87 |
margin-top: 1em; |
| 88 |
border-top: 1px solid black; |
| 89 |
} |
| 90 |
UL.doctools_requirements { |
| 91 |
margin-bottom: 1em; |
| 92 |
border-bottom: 1px solid black; |
| 93 |
} |
| 94 |
--></style> |
| 95 |
</head> |
| 96 |
<!-- Generated from file 'critcl_howto_use.man' by tcllib/doctools with format 'html' |
| 97 |
--> |
| 98 |
<!-- Copyright &copy; Jean-Claude Wippler -- Copyright &copy; Steve Landers -- Copyright &copy; 2011-2024 Andreas Kupries |
| 99 |
--> |
| 100 |
<!-- critcl_howto_use.n |
| 101 |
--> |
| 102 |
<body><hr> [ |
| 103 |
<a href="../toc.html">Table Of Contents</a> |
| 104 |
| <a href="../index.html">Keyword Index</a> |
| 105 |
] <hr> |
| 106 |
<div class="doctools"> |
| 107 |
<h1 class="doctools_title">critcl_howto_use(n) 3.3.1 doc "C Runtime In Tcl (CriTcl)"</h1> |
| 108 |
<div id="name" class="doctools_section"><h2><a name="name">Name</a></h2> |
| 109 |
<p>critcl_howto_use - How To Use CriTcl</p> |
| 110 |
</div> |
| 111 |
<div id="toc" class="doctools_section"><h2><a name="toc">Table Of Contents</a></h2> |
| 112 |
<ul class="doctools_toc"> |
| 113 |
<li class="doctools_section"><a href="#toc">Table Of Contents</a></li> |
| 114 |
<li class="doctools_section"><a href="#section1">Description</a></li> |
| 115 |
<li class="doctools_section"><a href="#section2">Basics</a> |
| 116 |
<ul> |
| 117 |
<li class="doctools_subsection"><a href="#subsection1">Simple Arguments</a></li> |
| 118 |
<li class="doctools_subsection"><a href="#subsection2">Simple Results</a></li> |
| 119 |
<li class="doctools_subsection"><a href="#subsection3">Range-limited Simple Arguments</a></li> |
| 120 |
<li class="doctools_subsection"><a href="#subsection4">String Arguments</a></li> |
| 121 |
<li class="doctools_subsection"><a href="#subsection5">String Results</a></li> |
| 122 |
<li class="doctools_subsection"><a href="#subsection6">List Arguments</a></li> |
| 123 |
<li class="doctools_subsection"><a href="#subsection7">Constrained List Arguments</a></li> |
| 124 |
<li class="doctools_subsection"><a href="#subsection8">Raw Tcl_Obj* Arguments</a></li> |
| 125 |
<li class="doctools_subsection"><a href="#subsection9">Raw Tcl_Obj* Results</a></li> |
| 126 |
<li class="doctools_subsection"><a href="#subsection10">Errors & Messages</a></li> |
| 127 |
<li class="doctools_subsection"><a href="#subsection11">Tcl_Interp* Access</a></li> |
| 128 |
<li class="doctools_subsection"><a href="#subsection12">Binary Data Arguments</a></li> |
| 129 |
<li class="doctools_subsection"><a href="#subsection13">Constant Binary Data Results</a></li> |
| 130 |
<li class="doctools_subsection"><a href="#subsection14">Tcl Runtime Version</a></li> |
| 131 |
<li class="doctools_subsection"><a href="#subsection15">Additional Tcl Code</a></li> |
| 132 |
<li class="doctools_subsection"><a href="#subsection16">Debugging Support</a></li> |
| 133 |
<li class="doctools_subsection"><a href="#subsection17">Install The Package</a></li> |
| 134 |
</ul> |
| 135 |
</li> |
| 136 |
<li class="doctools_section"><a href="#section3">Using External Libraries</a> |
| 137 |
<ul> |
| 138 |
<li class="doctools_subsection"><a href="#subsection18">Default Values For Arguments</a></li> |
| 139 |
<li class="doctools_subsection"><a href="#subsection19">Custom Argument Validation</a></li> |
| 140 |
<li class="doctools_subsection"><a href="#subsection20">Separating Local C Sources</a></li> |
| 141 |
<li class="doctools_subsection"><a href="#subsection21">Very Simple Results</a></li> |
| 142 |
<li class="doctools_subsection"><a href="#subsection22">Structure Arguments</a></li> |
| 143 |
<li class="doctools_subsection"><a href="#subsection23">Structure Results</a></li> |
| 144 |
<li class="doctools_subsection"><a href="#subsection24">Structure Types</a></li> |
| 145 |
<li class="doctools_subsection"><a href="#subsection25">Large Structures</a></li> |
| 146 |
<li class="doctools_subsection"><a href="#subsection26">External Structures</a></li> |
| 147 |
<li class="doctools_subsection"><a href="#subsection27">External Enumerations</a></li> |
| 148 |
<li class="doctools_subsection"><a href="#subsection28">External Bitsets/Bitmaps/Flags</a></li> |
| 149 |
<li class="doctools_subsection"><a href="#subsection29">Non-standard header/library locations</a></li> |
| 150 |
<li class="doctools_subsection"><a href="#subsection30">Non-standard compile/link configuration</a></li> |
| 151 |
<li class="doctools_subsection"><a href="#subsection31">Querying the compilation environment</a></li> |
| 152 |
<li class="doctools_subsection"><a href="#subsection32">Shared C Code</a></li> |
| 153 |
</ul> |
| 154 |
</li> |
| 155 |
<li class="doctools_section"><a href="#section4">Various</a> |
| 156 |
<ul> |
| 157 |
<li class="doctools_subsection"><a href="#subsection33">Author, License, Description, Keywords</a></li> |
| 158 |
<li class="doctools_subsection"><a href="#subsection34">Get Critcl Application Help</a></li> |
| 159 |
<li class="doctools_subsection"><a href="#subsection35">Supported Targets & Configurations</a></li> |
| 160 |
<li class="doctools_subsection"><a href="#subsection36">Building A Package</a></li> |
| 161 |
<li class="doctools_subsection"><a href="#subsection37">Building A Package For Debugging</a></li> |
| 162 |
</ul> |
| 163 |
</li> |
| 164 |
<li class="doctools_section"><a href="#section5">Authors</a></li> |
| 165 |
<li class="doctools_section"><a href="#section6">Bugs, Ideas, Feedback</a></li> |
| 166 |
<li class="doctools_section"><a href="#keywords">Keywords</a></li> |
| 167 |
<li class="doctools_section"><a href="#category">Category</a></li> |
| 168 |
<li class="doctools_section"><a href="#copyright">Copyright</a></li> |
| 169 |
</ul> |
| 170 |
</div> |
| 171 |
<div id="section1" class="doctools_section"><h2><a name="section1">Description</a></h2> |
| 172 |
<p>Be welcome to the <i class="term">C Runtime In Tcl</i> (short: <i class="term"><a href="critcl.html">CriTcl</a></i>), a system for embedding and using C |
| 173 |
code from within <a href="http://core.tcl-lang.org/tcl">Tcl</a> scripts.</p> |
| 174 |
<p>This document assumes the presence of a working <i class="term"><a href="critcl.html">CriTcl</a></i> installation.</p> |
| 175 |
<p>If that is missing follow the instructions on <i class="term"><a href="critcl_howto_install.html">How To Install CriTcl</a></i>.</p> |
| 176 |
</div> |
| 177 |
<div id="section2" class="doctools_section"><h2><a name="section2">Basics</a></h2> |
| 178 |
<p>To create a minimal working package</p> |
| 179 |
<ol class="doctools_enumerated"> |
| 180 |
<li><p>Choose a directory to develop in and make it the working directory. This should |
| 181 |
not be a checkout of <i class="term"><a href="critcl.html">CriTcl</a></i> itself.</p></li> |
| 182 |
<li><p>Save the following example to a file. In the following it is assumed that the file |
| 183 |
was named "<b class="file">example.tcl</b>".</p> |
| 184 |
<pre class="doctools_example"> |
| 185 |
# -*- tcl -*- |
| 186 |
# Critcl support, absolutely necessary. |
| 187 |
package require critcl |
| 188 |
# Bail out early if the compile environment is not suitable. |
| 189 |
if {![critcl::compiling]} { |
| 190 |
error "Unable to build project, no proper compiler found." |
| 191 |
} |
| 192 |
# Information for the teapot.txt meta data file put into a generated package. |
| 193 |
# Free form strings. |
| 194 |
critcl::license {Andreas Kupries} {Under a BSD license} |
| 195 |
critcl::summary {The first CriTcl-based package} |
| 196 |
critcl::description { |
| 197 |
This package is the first example of a CriTcl-based package. It contains all the |
| 198 |
necessary and conventionally useful pieces. |
| 199 |
} |
| 200 |
critcl::subject example {critcl package} |
| 201 |
critcl::subject {basic critcl} |
| 202 |
# Minimal Tcl version the package should load into. |
| 203 |
critcl::tcl 8.6 |
| 204 |
# Use to activate Tcl memory debugging |
| 205 |
#critcl::debug memory |
| 206 |
# Use to activate building and linking with symbols (for gdb, etc.) |
| 207 |
#critcl::debug symbols |
| 208 |
# ## #### ######### ################ ######################### |
| 209 |
## A hello world, directly printed to stdout. Bypasses Tcl's channel system. |
| 210 |
critcl::cproc hello {} void { |
| 211 |
printf("hello world\n"); |
| 212 |
} |
| 213 |
# ## #### ######### ################ ######################### |
| 214 |
# Forcing compilation, link, and loading now. |
| 215 |
critcl::msg -nonewline { Building ...} |
| 216 |
if {![critcl::load]} { |
| 217 |
error "Building and loading the project failed." |
| 218 |
} |
| 219 |
# Name and version the package. Just like for every kind of Tcl package. |
| 220 |
package provide critcl-example 1 |
| 221 |
</pre> |
| 222 |
</li> |
| 223 |
<li><p>Invoke the command</p> |
| 224 |
<pre class="doctools_example"> critcl -keep -debug all -pkg example.tcl </pre> |
| 225 |
<p>This compiles the example and installs it into a "<b class="file">lib/</b>" sub directory of the |
| 226 |
working directory, generating output similar to</p> |
| 227 |
<pre class="doctools_example"> |
| 228 |
Config: linux-x86_64-gcc |
| 229 |
Build: linux-x86_64-gcc |
| 230 |
Target: linux-x86_64 |
| 231 |
Source: example.tcl (provide critcl-example 1) Building ... |
| 232 |
Library: example.so |
| 233 |
(tclStubsPtr => const TclStubs *tclStubsPtr;) |
| 234 |
(tclPlatStubsPtr => const TclPlatStubs *tclPlatStubsPtr;) |
| 235 |
Package: lib/example |
| 236 |
Files left in /home/aku/.critcl/pkg2567272.1644845439 |
| 237 |
</pre> |
| 238 |
<p>during operation.</p> |
| 239 |
<p>The <b class="option">-keep</b> option suppressed the cleanup of the generated C files, object |
| 240 |
files, compiler log, etc. normally done at the end of building.</p> |
| 241 |
<pre class="doctools_example"> |
| 242 |
% ls -l /home/aku/.critcl/pkg2567272.1644845439 |
| 243 |
total 36 |
| 244 |
-rw-r--r-- 1 aku aku 1260 Feb 14 18:30 v3118_00000000000000000000000000000004.c |
| 245 |
-rw-r--r-- 1 aku aku 2096 Feb 14 18:30 v3118_00000000000000000000000000000004_pic.o |
| 246 |
-rw-r--r-- 1 aku aku 1728 Feb 14 18:30 v3118_00000000000000000000000000000009.c |
| 247 |
-rw-r--r-- 1 aku aku 2448 Feb 14 18:30 v3118_00000000000000000000000000000009_pic.o |
| 248 |
-rwxr-xr-x 1 aku aku 14424 Feb 14 18:30 v3118_00000000000000000000000000000009.so |
| 249 |
-rw-r--r-- 1 aku aku 1725 Feb 14 18:30 v3118.log |
| 250 |
</pre> |
| 251 |
<p>This enables inspection of the generated C code. |
| 252 |
Simply drop the option from the command if that is not desired.</p> |
| 253 |
<p>The option <b class="option">-debug</b>, with argument <b class="const">all</b> activated Tcl's memory |
| 254 |
debugging and caused the generation of the symbol tables needed by <b class="syscmd">gdb</b> or any |
| 255 |
other debugger. The alternate arguments <b class="const">memory</b> and <b class="const">symbols</b> activate just |
| 256 |
one of the these.</p></li> |
| 257 |
<li><p>Now invoke an interactive <b class="syscmd">tclsh</b> and enter the commands:</p> |
| 258 |
<ul class="doctools_itemized"> |
| 259 |
<li><p><em>lappend auto_path lib</em></p></li> |
| 260 |
<li><p><em>package require critcl-example</em></p></li> |
| 261 |
<li><p><em>info loaded</em></p></li> |
| 262 |
<li><p><em>hello</em></p></li> |
| 263 |
<li><p><em>exit</em></p></li> |
| 264 |
</ul> |
| 265 |
<p>I.e. extend <b class="syscmd">tclsh</b>'s package search path to include the location of the new |
| 266 |
package, load the package, verify that the associated shared library is present, invoke |
| 267 |
the package command, and stop the session.</p> |
| 268 |
<p>When the package command is invoked the terminal will show <b class="const">hello world</b>, |
| 269 |
followed by the prompt.</p></li> |
| 270 |
</ol> |
| 271 |
<p>Commands: <i class="term">critcl::compiling</i>, <i class="term">critcl::cproc</i>, |
| 272 |
<i class="term">critcl::description</i>, <i class="term">critcl::license</i>, <i class="term">critcl::load</i>, |
| 273 |
<i class="term">critcl::msg</i>, <i class="term">critcl::subject</i>, <i class="term">critcl::summary</i>, <i class="term">critcl::tcl</i>.</p> |
| 274 |
<p>Make a copy of "<b class="file">example.tcl</b>" before going through the sub-sections. Keep it as |
| 275 |
a save point to return to from the editing done in the sub-section.</p> |
| 276 |
<div id="subsection1" class="doctools_subsection"><h3><a name="subsection1">Simple Arguments</a></h3> |
| 277 |
<p>A function taking neither arguments nor returning results is not very useful.</p> |
| 278 |
<ol class="doctools_enumerated"> |
| 279 |
<li><p>We are now extending the command to take an argument.</p></li> |
| 280 |
<li><p>Starting from the <span class="sectref"><a href="#section2">Basics</a></span>. |
| 281 |
Edit the file "<b class="file">example.tcl</b>". |
| 282 |
Remove the definition of <b class="cmd">hello</b>. Replace it with</p> |
| 283 |
<pre class="doctools_example"> |
| 284 |
critcl::cproc hello {double x} void { |
| 285 |
/* double x; */ |
| 286 |
printf("hello world, we have %f\n", x); |
| 287 |
} |
| 288 |
</pre> |
| 289 |
<p>and rebuild the package.</p></li> |
| 290 |
<li><p>When testing the package again, entering the simple <b class="cmd">hello</b> will fail.</p> |
| 291 |
<p>The changed command is now expecting an argument, and we gave it none.</p> |
| 292 |
<p>Retry by entering</p> |
| 293 |
<pre class="doctools_example">hello 5</pre> |
| 294 |
<p>instead. |
| 295 |
Now the command behaves as expected and prints the provided value.</p> |
| 296 |
<p>Further try and enter</p> |
| 297 |
<pre class="doctools_example">hello world</pre> |
| 298 |
<p>This will fail again. |
| 299 |
The command expected a real number and we gave it something decidedly not so.</p> |
| 300 |
<p>These checks (argument count, argument type) are implemented in the translation |
| 301 |
layer <i class="term"><a href="critcl.html">CriTcl</a></i> generates for the C function. The function body is never invoked.</p></li> |
| 302 |
</ol> |
| 303 |
</div> |
| 304 |
<div id="subsection2" class="doctools_subsection"><h3><a name="subsection2">Simple Results</a></h3> |
| 305 |
<p>A function taking neither arguments nor returning results is not very useful.</p> |
| 306 |
<ol class="doctools_enumerated"> |
| 307 |
<li><p>We are now extending the command to return a result.</p></li> |
| 308 |
<li><p>Starting from the <span class="sectref"><a href="#section2">Basics</a></span>. |
| 309 |
Edit the file "<b class="file">example.tcl</b>". |
| 310 |
Remove the definition of <b class="cmd">hello</b>. Replace it with</p> |
| 311 |
<pre class="doctools_example"> |
| 312 |
critcl::cproc twice {double x} double { |
| 313 |
return 2*x; |
| 314 |
} |
| 315 |
</pre> |
| 316 |
<p>and rebuild the package.</p></li> |
| 317 |
<li><p>Note that the name of the command changed. Goodbye <b class="cmd">hello</b>, hello <b class="cmd">twice</b>.</p></li> |
| 318 |
<li><p>Invoke</p> |
| 319 |
<pre class="doctools_example"> twice 4 </pre> |
| 320 |
<p>and the <b class="syscmd">tclsh</b> will print the result <b class="const">8</b> |
| 321 |
in the terminal.</p></li> |
| 322 |
</ol> |
| 323 |
<p>An important limitation of the commands implemented so far is that they cannot |
| 324 |
fail. The types used so far (<b class="type">void</b>, <b class="type">double</b>) and related scalar types can |
| 325 |
return only a value of the specified type, and nothing else. They have no ability to |
| 326 |
signal an error to the Tcl script.</p> |
| 327 |
<p>We will come back to this after knowing a bit more about the more complex argument |
| 328 |
and result types.</p> |
| 329 |
<p>Of interest to the eager reader: <i class="term"><a href="critcl_cproc.html">CriTcl cproc Type Reference</a></i></p> |
| 330 |
</div> |
| 331 |
<div id="subsection3" class="doctools_subsection"><h3><a name="subsection3">Range-limited Simple Arguments</a></h3> |
| 332 |
<ol class="doctools_enumerated"> |
| 333 |
<li><p>Starting from the <span class="sectref"><a href="#section2">Basics</a></span>. |
| 334 |
Edit the file "<b class="file">example.tcl</b>". |
| 335 |
Remove the definition of <b class="cmd">hello</b>. Replace it with</p> |
| 336 |
<pre class="doctools_example"> |
| 337 |
critcl::cproc hello {{double > 5 < 22} x} void { |
| 338 |
/* double x, range 6-21; */ |
| 339 |
printf("hello world, we have %f\n", x); |
| 340 |
} |
| 341 |
</pre> |
| 342 |
<p>and rebuild the package.</p></li> |
| 343 |
<li><p>When dealing with simple arguments whose range of legal values is limited to a single |
| 344 |
continuous interval extend the base type with the necessary relations (<b class="const">></b>, <b class="const">>=</b>, |
| 345 |
<b class="const"><</b>, and <b class="const"><=</b>) and limiting values.</p> |
| 346 |
<p><em>Note</em> that the limiting values have to be proper constant numbers acceptable by the |
| 347 |
base type. Symbolic values are not accepted.</p> |
| 348 |
<p>Here the argument <i class="arg">x</i> of the changed function will reject all values outside of the |
| 349 |
interval 6 to 21.</p></li> |
| 350 |
</ol> |
| 351 |
</div> |
| 352 |
<div id="subsection4" class="doctools_subsection"><h3><a name="subsection4">String Arguments</a></h3> |
| 353 |
<p>Tcl prides itself on the fact that <i class="term">Everything Is A String</i>. |
| 354 |
So how are string values passed into C functions ?</p> |
| 355 |
<ol class="doctools_enumerated"> |
| 356 |
<li><p>We are now extending the command with a string argument.</p></li> |
| 357 |
<li><p>Starting from the <span class="sectref"><a href="#section2">Basics</a></span>. |
| 358 |
Edit the file "<b class="file">example.tcl</b>". |
| 359 |
Remove the definition of <b class="cmd">hello</b>. Replace it with</p> |
| 360 |
<pre class="doctools_example"> |
| 361 |
critcl::cproc hello {pstring x} void { |
| 362 |
/* critcl_pstring x (.s, .len, .o); */ |
| 363 |
printf("hello world, from %s (%d bytes)\n", x.s, x.len); |
| 364 |
} |
| 365 |
</pre> |
| 366 |
<p>and rebuild the package.</p></li> |
| 367 |
<li><p>Testing <b class="cmd">hello</b> with any kind of argument the information is printed.</p></li> |
| 368 |
<li><p>Of note here is that the command argument <b class="variable">x</b> is a structure.</p></li> |
| 369 |
<li><p>The example uses only two of the three fields, the pointer to the string data |
| 370 |
(<b class="variable">.s</b>), and the length of the string (<b class="variable">.len</b>). In bytes, <em>not</em> in |
| 371 |
characters, because Tcl's internal representation of strings uses a modified UTF-8 |
| 372 |
encoding. A character consists of between 1 and <b class="const">TCL_UTF_MAX</b> bytes.</p></li> |
| 373 |
<li><p><em>Attention</em> The pointers (<b class="variable">.s</b>) refer into data structures <em>internal</em> |
| 374 |
to and managed by the Tcl interpreter. |
| 375 |
Changing them is highly likely to cause subtle and difficult to track down bugs. |
| 376 |
Any and all complex arguments must be treated as <i class="term">Read-Only</i>. Never modify them.</p></li> |
| 377 |
<li><p>Use the simpler type <b class="type">char*</b> if and only if the length of the string is not |
| 378 |
relevant to the command, i.e. not computed, or not used by any of the functions called |
| 379 |
from the body of the command. |
| 380 |
Its value is essentially just the <b class="variable">.s</b> field of <b class="type">pstring</b>'s structure. |
| 381 |
This then looks like</p> |
| 382 |
<pre class="doctools_example"> |
| 383 |
critcl::cproc hello {char* x} void { |
| 384 |
/* char* x; */ |
| 385 |
printf("hello world, from %s\n", x); |
| 386 |
} |
| 387 |
</pre> |
| 388 |
</li> |
| 389 |
</ol> |
| 390 |
</div> |
| 391 |
<div id="subsection5" class="doctools_subsection"><h3><a name="subsection5">String Results</a></h3> |
| 392 |
<p>Tcl prides itself on the fact that <i class="term">Everything Is A String</i>. |
| 393 |
So how are string values returned from C functions ?</p> |
| 394 |
<ol class="doctools_enumerated"> |
| 395 |
<li><p>We are now giving the command a string result.</p></li> |
| 396 |
<li><p>Starting from the <span class="sectref"><a href="#section2">Basics</a></span>. |
| 397 |
Edit the file "<b class="file">example.tcl</b>". |
| 398 |
Remove the definition of <b class="cmd">hello</b>. Replace it with</p> |
| 399 |
<pre class="doctools_example"> |
| 400 |
critcl::cproc twice {double x} char* { |
| 401 |
char buf [lb]40[rb]; |
| 402 |
sprintf(buf, "%f", 2*x); |
| 403 |
return buf; |
| 404 |
} |
| 405 |
</pre> |
| 406 |
<p>and rebuild the package.</p></li> |
| 407 |
<li><p>Note that the name of the command changed. Goodbye <b class="cmd">hello</b>, hello <b class="cmd">twice</b>.</p></li> |
| 408 |
<li><p>Invoke</p> |
| 409 |
<pre class="doctools_example"> twice 4 </pre> |
| 410 |
<p>and the <b class="syscmd">tclsh</b> will print the result <b class="const">8</b> |
| 411 |
in the terminal.</p></li> |
| 412 |
<li><p><em>Attention</em>. To the translation layer the string pointer is owned by the C |
| 413 |
code. A copy is made to become the result seen by Tcl.</p> |
| 414 |
<p>While the C code is certainly allowed to allocate the string on the heap if it so |
| 415 |
wishes, this comes with the responsibility to free the string as well. Abrogation of that |
| 416 |
responsibility will cause memory leaks.</p> |
| 417 |
<p>The type <b class="type">char*</b> is recommended to be used with static string buffers, string |
| 418 |
constants and the like.</p></li> |
| 419 |
<li><p>Conversely, to return heap-allocated strings it is recommended to use the type |
| 420 |
<b class="type">string</b> instead.</p> |
| 421 |
<p>Replace the definition of <b class="cmd">twice</b> with</p> |
| 422 |
<pre class="doctools_example"> |
| 423 |
critcl::cproc twice {double x} string { |
| 424 |
char* buf = Tcl_Alloc (40); |
| 425 |
sprintf(buf, "%f", 2*x); |
| 426 |
return buf; |
| 427 |
} |
| 428 |
</pre> |
| 429 |
<p>Now the translation layer takes ownership of the string from the C code and |
| 430 |
transfers that ownership to the Tcl interpreter. This means that the string will be |
| 431 |
released when the Tcl interpreter is done with it. The C code has no say in the lifecycle |
| 432 |
of the string any longer, and having the C code releasing the string <em>will</em> cause |
| 433 |
issues. Dangling pointers and associated memory corruption and crashes.</p></li> |
| 434 |
</ol> |
| 435 |
</div> |
| 436 |
<div id="subsection6" class="doctools_subsection"><h3><a name="subsection6">List Arguments</a></h3> |
| 437 |
<p>Even as a string-oriented language Tcl is capable of handling more complex structures. The first of |
| 438 |
it, with Tcl since the beginning are <i class="term">lists</i>. Sets of values indexed by a numeric value.</p> |
| 439 |
<p>In C parlance, <i class="term">arrays</i>.</p> |
| 440 |
<ol class="doctools_enumerated"> |
| 441 |
<li><p>We are now extending the command with a <b class="type">list</b> argument.</p></li> |
| 442 |
<li><p>Starting from the <span class="sectref"><a href="#section2">Basics</a></span>. |
| 443 |
Edit the file "<b class="file">example.tcl</b>". |
| 444 |
Remove the definition of <b class="cmd">hello</b>. Replace it with</p> |
| 445 |
<pre class="doctools_example"> |
| 446 |
critcl::cproc hello {list x} void { |
| 447 |
/* critcl_list x (.o, .v, .c); */ |
| 448 |
printf("hello world, %d elements in (%s)\n", x.c, Tcl_GetString (x.o)); |
| 449 |
} |
| 450 |
</pre> |
| 451 |
<p>and rebuild the package.</p></li> |
| 452 |
<li><p>Testing <b class="cmd">hello</b> with any kind of list argument it will print basic information about it.</p></li> |
| 453 |
<li><p>Of note here is that the command argument <b class="variable">x</b> is a structure.</p></li> |
| 454 |
<li><p>The example uses only two of the three fields, the pointer to the original <b class="type">Tcl_Obj*</b> |
| 455 |
holding the list (<b class="variable">.o</b>), and the length of the list (<b class="variable">.c</b>) in elements.</p> |
| 456 |
<p>The field <b class="variable">.v</b>, not used above, is the C array holding the <b class="type">Tcl_Obj*</b> pointers to |
| 457 |
the list elements.</p></li> |
| 458 |
<li><p><em>Attention</em> The pointers <b class="variable">.o</b> and <b class="variable">.v</b> refer into data structures |
| 459 |
<em>internal</em> to and managed by the Tcl interpreter. |
| 460 |
Changing them is highly likely to cause subtle and difficult to track down bugs. |
| 461 |
Any and all complex arguments must be treated as <i class="term">Read-Only</i>. Never modify them.</p></li> |
| 462 |
<li><p>As a last note, this argument type does not place any constraints on the size of the list, or |
| 463 |
on the type of the elements.</p></li> |
| 464 |
</ol> |
| 465 |
</div> |
| 466 |
<div id="subsection7" class="doctools_subsection"><h3><a name="subsection7">Constrained List Arguments</a></h3> |
| 467 |
<p>As mentioned at the end of section <span class="sectref"><a href="#subsection6">List Arguments</a></span> the basic <b class="type">list</b> type places no |
| 468 |
constraints on the size of the list, nor on the type of the elements.</p> |
| 469 |
<p>Both kind of constraints can be done however, alone or together.</p> |
| 470 |
<ol class="doctools_enumerated"> |
| 471 |
<li><p>We are now extending the command with a length-limited list.</p></li> |
| 472 |
<li><p>Starting from the <span class="sectref"><a href="#section2">Basics</a></span>. |
| 473 |
Edit the file "<b class="file">example.tcl</b>". |
| 474 |
Remove the definition of <b class="cmd">hello</b>. Replace it with</p> |
| 475 |
<pre class="doctools_example"> |
| 476 |
critcl::cproc hello {[5] x} void { |
| 477 |
/* critcl_list x (.o, .v, .c); */ |
| 478 |
printf("hello world, %d elements in (%s)\n", x.c, Tcl_GetString (x.o)); |
| 479 |
} |
| 480 |
</pre> |
| 481 |
<p>and rebuild the package.</p></li> |
| 482 |
<li><p>Testing the new command will show that only lists holding exactly <b class="const">5</b> elements will be |
| 483 |
accepted.</p></li> |
| 484 |
<li><p>To accept lists of any length use <b class="const">[]</b> or <b class="const">[*]</b>. Both forms are actually |
| 485 |
aliases of the base type, i.e. <b class="type">list</b>.</p></li> |
| 486 |
<li><p>To constrain just the type of elements, for example to type <b class="type">int</b>, use</p> |
| 487 |
<pre class="doctools_example">int[]</pre> |
| 488 |
<p>or</p> |
| 489 |
<pre class="doctools_example">[]int</pre> |
| 490 |
</li> |
| 491 |
<li><p>To combine both type and length constraints use the forms</p> |
| 492 |
<pre class="doctools_example">int[5]</pre> |
| 493 |
<p>or</p> |
| 494 |
<pre class="doctools_example">[5]int</pre> |
| 495 |
</li> |
| 496 |
<li><p>The last, most C-like forms of these contraints place the list indicator syntax on the |
| 497 |
argument instead of the type. I.e</p> |
| 498 |
<pre class="doctools_example">int a[]</pre> |
| 499 |
<p>or</p> |
| 500 |
<pre class="doctools_example">int a[5]</pre> |
| 501 |
</li> |
| 502 |
</ol> |
| 503 |
</div> |
| 504 |
<div id="subsection8" class="doctools_subsection"><h3><a name="subsection8">Raw Tcl_Obj* Arguments</a></h3> |
| 505 |
<p>When the set of predefined argument types is not enough the oldest way of handling the |
| 506 |
situation is falling back to the structures used by Tcl to manage values, i.e. |
| 507 |
<b class="type">Tcl_Obj*</b>.</p> |
| 508 |
<ol class="doctools_enumerated"> |
| 509 |
<li><p>Starting from the <span class="sectref"><a href="#section2">Basics</a></span>. |
| 510 |
Edit the file "<b class="file">example.tcl</b>". |
| 511 |
Remove the definition of <b class="cmd">hello</b>. Replace it with</p> |
| 512 |
<pre class="doctools_example"> |
| 513 |
critcl::cproc hello {object x} void { |
| 514 |
/* Tcl_Obj* x */ |
| 515 |
int len; |
| 516 |
char* str = Tcl_GetStringFromObj (x, &len); |
| 517 |
printf("hello world, from %s (%d bytes)\n", str, len); |
| 518 |
} |
| 519 |
</pre> |
| 520 |
<p>and rebuild the package.</p></li> |
| 521 |
<li><p>Having direct access to the raw <b class="type">Tcl_Obj*</b> value all functions of the public |
| 522 |
Tcl API for working with Tcl values become usable. The downside of that is that all the |
| 523 |
considerations for handling them apply as well.</p> |
| 524 |
<p>In other words, the C code becomes responsible for handling the reference counts |
| 525 |
correctly, for duplicating shared <b class="type">Tcl_Obj*</b> structures before modifying them, etc.</p> |
| 526 |
<p>One thing the C code is allowed to do without restriction is to <i class="term">shimmer</i> the |
| 527 |
internal representation of the value as needed, through the associated Tcl API |
| 528 |
functions. For example <b class="function">Tcl_GetWideIntFromObj</b> and the like. |
| 529 |
It actually has to be allowed to do so, as the type checking done as part of such |
| 530 |
conversions is now the responsibility of the C code as well.</p> |
| 531 |
<p>For the predefined types this is all hidden in the translation layer generated by |
| 532 |
<i class="term"><a href="critcl.html">CriTcl</a></i>.</p> |
| 533 |
<p>If more than one command has to perform the same kind of checking and/or conversion |
| 534 |
it is recommended to move the core of the code into proper C functions for proper sharing |
| 535 |
among the commands.</p></li> |
| 536 |
<li><p>This is best done by defining a custom argument type using <i class="term"><a href="critcl.html">CriTcl</a></i> commands. |
| 537 |
This extends the translation layer <i class="term"><a href="critcl.html">CriTcl</a></i> is able to generate. |
| 538 |
The necessary conversions, type checks, etc. are then again hidden from the bulk of the |
| 539 |
application C code.</p> |
| 540 |
<p>We will come back to this.</p></li> |
| 541 |
</ol> |
| 542 |
</div> |
| 543 |
<div id="subsection9" class="doctools_subsection"><h3><a name="subsection9">Raw Tcl_Obj* Results</a></h3> |
| 544 |
<p>When the set of predefined result types is not enough the oldest way of handling the |
| 545 |
situation is falling back to the structures used by Tcl to manage values, i.e. |
| 546 |
<b class="type">Tcl_Obj*</b>.</p> |
| 547 |
<p>Two builtin types are provided for this, to handle different reference counting |
| 548 |
requirements.</p> |
| 549 |
<ol class="doctools_enumerated"> |
| 550 |
<li><p>Starting from the <span class="sectref"><a href="#section2">Basics</a></span>. |
| 551 |
Edit the file "<b class="file">example.tcl</b>". |
| 552 |
Remove the definition of <b class="cmd">hello</b>. Replace it with</p> |
| 553 |
<pre class="doctools_example"> |
| 554 |
critcl::cproc twice {double x} object0 { |
| 555 |
return Tcl_NewDoubleObj(2*x); |
| 556 |
} |
| 557 |
</pre> |
| 558 |
<p>and rebuild the package.</p></li> |
| 559 |
<li><p>With <b class="type">object0</b> the translation layer assumes that the returned <b class="type">Tcl_Obj*</b> |
| 560 |
value has a reference count of <b class="const">0</b>. I.e. a value which is unowned and unshared.</p> |
| 561 |
<p>This value is passed directly to Tcl for its use, without any changes. Tcl |
| 562 |
increments the reference count and thus takes ownership. The value is still unshared.</p> |
| 563 |
<p>It would be extremely detrimental if the translation layer had decremented the |
| 564 |
reference count before passing the value. This action would release the memory and then |
| 565 |
leave Tcl with a dangling pointer and the associated memory corruption bug to come.</p></li> |
| 566 |
<li><p>The situation changes when the C code returns a <b class="type">Tcl_Obj*</b> value with a |
| 567 |
reference count greater than <b class="const">0</b>. I.e. at least owned (by the C code), and possibly |
| 568 |
even shared. |
| 569 |
There are some object constructors and/or mutators in the public Tcl API which do that, |
| 570 |
although I do not recall their names. The example below simulates this situation by |
| 571 |
explicitly incrementing the reference count before returning the value.</p></li> |
| 572 |
<li><p>In this case use the type <b class="type">object</b> (without the trailing <b class="const">0</b>).</p></li> |
| 573 |
<li><p>Edit the file "<b class="file">example.tcl</b>" and replace the definition of <b class="cmd">twice</b> with</p> |
| 574 |
<pre class="doctools_example"> |
| 575 |
critcl::cproc twice {double x} object { |
| 576 |
Tcl_Obj* result = Tcl_NewDoubleObj(2*x); |
| 577 |
Tcl_IncrRefCount (result); |
| 578 |
return result; |
| 579 |
} |
| 580 |
</pre> |
| 581 |
<p>and rebuild the package.</p></li> |
| 582 |
<li><p>After handing the value to Tcl, with the associated incremented reference count, |
| 583 |
the translation layer decrements the reference count, invalidating the C code's ownership |
| 584 |
and leaving the final reference count the same.</p> |
| 585 |
<p>Note, the order matters. If the value has only one reference then decrementing it |
| 586 |
before Tcl increments it would again release the value, and again leave Tcl with a |
| 587 |
dangling pointer.</p> |
| 588 |
<p>Also, not decrementing the reference count at all causes the inverse problem to the |
| 589 |
memory corruption issues of before, memory leaks.</p></li> |
| 590 |
<li><p><em>Note</em> that both types transfer ownership of the value. Their difference is |
| 591 |
just in the reference count of the value coming out of the function, and the (non-)actions |
| 592 |
having to be (not) taken to effect said transfer without causing memory issues.</p></li> |
| 593 |
</ol> |
| 594 |
</div> |
| 595 |
<div id="subsection10" class="doctools_subsection"><h3><a name="subsection10">Errors & Messages</a></h3> |
| 596 |
<ol class="doctools_enumerated"> |
| 597 |
<li><p>Starting from the <span class="sectref"><a href="#section2">Basics</a></span>. |
| 598 |
Edit the file "<b class="file">example.tcl</b>". |
| 599 |
Remove the definition of <b class="cmd">hello</b>. Replace it with</p> |
| 600 |
<pre class="doctools_example"> |
| 601 |
critcl::cproc sqrt { |
| 602 |
Tcl_Interp* interp |
| 603 |
double x |
| 604 |
} object0 { |
| 605 |
if (x < 0) { |
| 606 |
Tcl_SetObjResult (interp, Tcl_ObjPrintf ("Expected double >=0, but got \"%d\"", x)); |
| 607 |
Tcl_SetErrorCode (interp, "EXAMPLE", "BAD", "DOMAIN", NULL); |
| 608 |
return NULL; |
| 609 |
} |
| 610 |
return Tcl_NewDoubleObj(sqrt(x)); |
| 611 |
} |
| 612 |
</pre> |
| 613 |
<p>and rebuild the package.</p></li> |
| 614 |
<li><p>In standard C-based packages commands signal errors by returning <b class="const">TCL_ERROR</b>, |
| 615 |
placing the error message as the interpreter result, and maybe providing an error code via |
| 616 |
<b class="function">Tcl_SetErrorCode</b>.</p></li> |
| 617 |
<li><p>When using <b class="cmd">critcl::cproc</b> this is limited and hidden.</p></li> |
| 618 |
<li><p>The simple and string types for results do not allow failure. The value is returned |
| 619 |
to the translation layer, converted into the interpreter result and then reported as |
| 620 |
success (<b class="const">TCL_OK</b>).</p></li> |
| 621 |
<li><p>The object types on the other hand do allow for failure. |
| 622 |
Return a <b class="const">NULL</b> value to signal failure to the translation layer, which then reports |
| 623 |
this to the interpreter via the standard <b class="const">TCL_ERROR</b>.</p></li> |
| 624 |
<li><p><em>Attention</em> Setting the desired error message and code into the interpreter is |
| 625 |
still the responsibility of the function body.</p></li> |
| 626 |
</ol> |
| 627 |
</div> |
| 628 |
<div id="subsection11" class="doctools_subsection"><h3><a name="subsection11">Tcl_Interp* Access</a></h3> |
| 629 |
<ol class="doctools_enumerated"> |
| 630 |
<li><p>Reread the example in the <b class="sectref">previous</b> section.</p></li> |
| 631 |
<li><p>Note the type <b class="type">Tcl_Interp*</b> used for the first argument.</p></li> |
| 632 |
<li><p>This type is special.</p></li> |
| 633 |
<li><p>An argument of this type has to be the first argument of a function.</p></li> |
| 634 |
<li><p>Using it tells <i class="term"><a href="critcl.html">CriTcl</a></i> that the function needs access to the Tcl interpreter |
| 635 |
calling it. It then arranges for that to happen in the generated C code.</p> |
| 636 |
<p>Using functions from Tcl's public C API taking an interpreter argument in the |
| 637 |
function body is a situation where this is needed.</p></li> |
| 638 |
<li><p><em>This special argument is not visible at the script level</em>.</p></li> |
| 639 |
<li><p><em>This special argument is not an argument of the Tcl command for the function</em>.</p></li> |
| 640 |
<li><p>In our example the <b class="cmd">sqrt</b> command is called with a single argument.</p></li> |
| 641 |
<li><p>The name of the argument can be freely chosen. It is the type which is important |
| 642 |
and triggers the special behaviour. |
| 643 |
My prefered names are <b class="variable">ip</b> and <b class="variable">interp</b>.</p></li> |
| 644 |
</ol> |
| 645 |
</div> |
| 646 |
<div id="subsection12" class="doctools_subsection"><h3><a name="subsection12">Binary Data Arguments</a></h3> |
| 647 |
<ol class="doctools_enumerated"> |
| 648 |
<li><p>Starting from the <span class="sectref"><a href="#section2">Basics</a></span>. |
| 649 |
Edit the file "<b class="file">example.tcl</b>". |
| 650 |
Remove the definition of <b class="cmd">hello</b>. Replace it with</p> |
| 651 |
<pre class="doctools_example"> |
| 652 |
critcl::cproc hello {bytes x} void { |
| 653 |
/* critcl_bytes x (.s, .len, .o); */ |
| 654 |
printf("hello world, with %d bytes \n data: ", x.len); |
| 655 |
for (i = 0; i < x.len; i++) { |
| 656 |
printf(" %02x", x.s[i]); |
| 657 |
if (i % 16 == 15) printf ("\ndata: "); |
| 658 |
} |
| 659 |
if (i % 16 != 0) printf ("\n"); |
| 660 |
} |
| 661 |
</pre> |
| 662 |
<p>and rebuild the package.</p></li> |
| 663 |
<li><p>To deal with strings holding binary data use the type <b class="type">bytes</b>. It ensures that |
| 664 |
the function sees the proper binary data, and not how Tcl is encoding it internally, as |
| 665 |
the string types would.</p></li> |
| 666 |
</ol> |
| 667 |
</div> |
| 668 |
<div id="subsection13" class="doctools_subsection"><h3><a name="subsection13">Constant Binary Data Results</a></h3> |
| 669 |
<ol class="doctools_enumerated"> |
| 670 |
<li><p>Use the command <b class="cmd">critcl::cdata</b> to create a command taking no arguments and |
| 671 |
returning a constant ByteArray value.</p> |
| 672 |
<pre class="doctools_example"> |
| 673 |
# P5 3 3 255 \n ... |
| 674 |
critcl::cdata cross3x3pgm { |
| 675 |
80 52 32 51 32 51 32 50 53 53 10 |
| 676 |
0 255 0 |
| 677 |
255 255 255 |
| 678 |
0 255 0 |
| 679 |
} |
| 680 |
</pre> |
| 681 |
</li> |
| 682 |
</ol> |
| 683 |
</div> |
| 684 |
<div id="subsection14" class="doctools_subsection"><h3><a name="subsection14">Tcl Runtime Version</a></h3> |
| 685 |
<ol class="doctools_enumerated"> |
| 686 |
<li><p>See and reread the <span class="sectref"><a href="#section2">basic package</a></span> for the introduction of the |
| 687 |
commands referenced below.</p></li> |
| 688 |
<li><p>Use the command <b class="cmd">critcl::tcl</b> to tell <i class="term"><a href="critcl.html">CriTcl</a></i> the minimal version of Tcl |
| 689 |
the package is to be used with.</p> |
| 690 |
<p>This determines which Tcl headers all files are compiled against, and what version |
| 691 |
of the public Tcl API is available to the C code.</p> |
| 692 |
<p>Currently <b class="const">8.4</b>, <b class="const">8.5</b> and <b class="const">8.6</b> are supported.</p> |
| 693 |
<p>If not specified <b class="const">8.4</b> is assumed.</p></li> |
| 694 |
</ol> |
| 695 |
</div> |
| 696 |
<div id="subsection15" class="doctools_subsection"><h3><a name="subsection15">Additional Tcl Code</a></h3> |
| 697 |
<ol class="doctools_enumerated"> |
| 698 |
<li><p>Starting from the <span class="sectref"><a href="#section2">Basics</a></span>. |
| 699 |
Edit the file "<b class="file">example.tcl</b>". |
| 700 |
Remove the definition of <b class="cmd">hello</b>. Replace it with</p> |
| 701 |
<pre class="doctools_example"> |
| 702 |
critcl::cproc greetings::hello {} void { |
| 703 |
printf("hello world\n"); |
| 704 |
} |
| 705 |
critcl::cproc greetings::hi {} void { |
| 706 |
printf("hi you\n"); |
| 707 |
} |
| 708 |
</pre> |
| 709 |
<p>and rebuild the package.</p></li> |
| 710 |
<li><p>The command <b class="cmd">hello</b> is now available as <b class="cmd">greetings::hello</b>, and a second |
| 711 |
command <b class="cmd">greetings::hi</b> was added.</p></li> |
| 712 |
<li><p>Tcl has automatically created the namespace <b class="namespace">greetings</b>.</p></li> |
| 713 |
<li><p>Create a file "<b class="file">example-policy.tcl</b>" and enter</p> |
| 714 |
<pre class="doctools_example"> |
| 715 |
namespace eval greetings { |
| 716 |
namespace export hello hi |
| 717 |
namespace ensemble create |
| 718 |
} |
| 719 |
</pre> |
| 720 |
<p>into it.</p></li> |
| 721 |
<li><p>Edit "<b class="file">example.tcl</b>". Add the code</p> |
| 722 |
<pre class="doctools_example"> |
| 723 |
critcl::tsources example-policy.tcl |
| 724 |
</pre> |
| 725 |
<p>and rebuild the package.</p></li> |
| 726 |
<li><p>The added Tcl code makes <b class="namespace">greetings</b> available as an <i class="term">ensemble</i> |
| 727 |
command.</p> |
| 728 |
<p>The commands in the namespace have been registered as methods of the ensemble.</p> |
| 729 |
<p>They can now be invoked as</p> |
| 730 |
<pre class="doctools_example"> |
| 731 |
greetings hello |
| 732 |
greetings hi |
| 733 |
</pre> |
| 734 |
</li> |
| 735 |
<li><p>The Tcl builtin command <b class="cmd">string</b> is an ensemble as well, as is <b class="cmd">clock</b>.</p></li> |
| 736 |
</ol> |
| 737 |
<p>New commands: <i class="term">critcl::tsources</i></p> |
| 738 |
</div> |
| 739 |
<div id="subsection16" class="doctools_subsection"><h3><a name="subsection16">Debugging Support</a></h3> |
| 740 |
<ol class="doctools_enumerated"> |
| 741 |
<li><p>See and reread the <span class="sectref"><a href="#section2">basic package</a></span> for the introduction of the |
| 742 |
commands referenced below.</p></li> |
| 743 |
<li><p>Use the command <b class="cmd">critcl::debug</b> to activate various features supporting debugging.</p> |
| 744 |
<pre class="doctools_example"> |
| 745 |
critcl::debug memory ;# Activate Tcl memory debugging (-DTCL_MEM_DEBUG) |
| 746 |
critcl::debug symbols ;# Activate building and linking with debugger symbols (-g) |
| 747 |
critcl::debug all ;# Shorthand for both `memory` and `symbols`. |
| 748 |
</pre> |
| 749 |
</li> |
| 750 |
</ol> |
| 751 |
</div> |
| 752 |
<div id="subsection17" class="doctools_subsection"><h3><a name="subsection17">Install The Package</a></h3> |
| 753 |
<ol class="doctools_enumerated"> |
| 754 |
<li><p>Starting from the <span class="sectref"><a href="#section2">Basics</a></span>.</p></li> |
| 755 |
<li><p>Use an interactive <b class="syscmd">tclsh</b> seesion to determine the value of |
| 756 |
<b class="cmd">info library</b>.</p> |
| 757 |
<p>For the purpose of this HowTo assume that this path is |
| 758 |
"<b class="file">/home/aku/opt/ActiveTcl/lib/tcl8.6</b>"</p></li> |
| 759 |
<li><p>Invoke the critcl application in a terminal, using</p> |
| 760 |
<pre class="doctools_example"> |
| 761 |
critcl -libdir /home/aku/opt/ActiveTcl/lib/tcl8.6 -pkg example.tcl |
| 762 |
</pre> |
| 763 |
</li> |
| 764 |
<li><p>The package is now build and installed into the chosen directory.</p> |
| 765 |
<pre class="doctools_example"> |
| 766 |
% find /home/aku/opt/ActiveTcl/lib/tcl8.6/example/ |
| 767 |
/home/aku/opt/ActiveTcl/lib/tcl8.6/example/ |
| 768 |
/home/aku/opt/ActiveTcl/lib/tcl8.6/example/pkgIndex.tcl |
| 769 |
/home/aku/opt/ActiveTcl/lib/tcl8.6/example/critcl-rt.tcl |
| 770 |
/home/aku/opt/ActiveTcl/lib/tcl8.6/example/license.terms |
| 771 |
/home/aku/opt/ActiveTcl/lib/tcl8.6/example/linux-x86_64 |
| 772 |
/home/aku/opt/ActiveTcl/lib/tcl8.6/example/linux-x86_64/example.so |
| 773 |
/home/aku/opt/ActiveTcl/lib/tcl8.6/example/teapot.txt |
| 774 |
</pre> |
| 775 |
</li> |
| 776 |
</ol> |
| 777 |
</div> |
| 778 |
</div> |
| 779 |
<div id="section3" class="doctools_section"><h2><a name="section3">Using External Libraries</a></h2> |
| 780 |
<p>To create a minimal package wrapping an external library</p> |
| 781 |
<ol class="doctools_enumerated"> |
| 782 |
<li><p>Choose a directory to develop in and make it the working directory. |
| 783 |
This should not be a checkout of <i class="term"><a href="critcl.html">CriTcl</a></i> itself.</p></li> |
| 784 |
<li><p>Save the following example to a file. In the following it is assumed that the file |
| 785 |
was named "<b class="file">example.tcl</b>".</p> |
| 786 |
<pre class="doctools_example"> |
| 787 |
# -*- tcl -*- |
| 788 |
# Critcl support, absolutely necessary. |
| 789 |
package require critcl |
| 790 |
# Bail out early if the compile environment is not suitable. |
| 791 |
if {![critcl::compiling]} { |
| 792 |
error "Unable to build project, no proper compiler found." |
| 793 |
} |
| 794 |
# Information for the teapot.txt meta data file put into a generated package. |
| 795 |
# Free form strings. |
| 796 |
critcl::license {Andreas Kupries} {Under a BSD license} |
| 797 |
critcl::summary {The second CriTcl-based package} |
| 798 |
critcl::description { |
| 799 |
This package is the second example of a CriTcl-based package. It contains all the |
| 800 |
necessary and conventionally useful pieces for wrapping an external library. |
| 801 |
} |
| 802 |
critcl::subject {external library usage} example {critcl package} |
| 803 |
critcl::subject {wrapping external library} |
| 804 |
# Minimal Tcl version the package should load into. |
| 805 |
critcl::tcl 8.6 |
| 806 |
# Locations for headers and shared library of the library to wrap. |
| 807 |
# Required only for non-standard locations, i.e. where CC is not searching by default. |
| 808 |
critcl::cheaders -I/usr/include |
| 809 |
critcl::clibraries -L/usr/lib/x86_64-linux-gnu |
| 810 |
critcl::clibraries -lzstd |
| 811 |
# Import library API, i.e. headers. |
| 812 |
critcl::include zstd.h |
| 813 |
# ## #### ######### ################ ######################### |
| 814 |
## (De)compression using Zstd |
| 815 |
## Data to (de)compress is passed in and returned as Tcl byte arrays. |
| 816 |
critcl::cproc compress { |
| 817 |
Tcl_Interp* ip |
| 818 |
bytes data |
| 819 |
int {level ZSTD_CLEVEL_DEFAULT} |
| 820 |
} object0 { |
| 821 |
/* critcl_bytes data; (.s, .len, .o) */ |
| 822 |
Tcl_Obj* error_message; |
| 823 |
int max = ZSTD_maxCLevel(); |
| 824 |
if ((level < 1) || (level > max)) { |
| 825 |
error_message = Tcl_ObjPrintf ("level must be integer between 1 and %d", max); |
| 826 |
goto err; |
| 827 |
} |
| 828 |
size_t dest_sz = ZSTD_compressBound (data.len); |
| 829 |
void* dest_buf = Tcl_Alloc(dest_sz); |
| 830 |
if (!dest_buf) { |
| 831 |
error_message = Tcl_NewStringObj ("can't allocate memory to compress data", -1); |
| 832 |
goto err; |
| 833 |
} |
| 834 |
size_t compressed_size = ZSTD_compress (dest_buf, dest_sz, |
| 835 |
data.s, data.len, |
| 836 |
level); |
| 837 |
if (ZSTD_isError (compressed_size)) { |
| 838 |
Tcl_Free(dest_buf); |
| 839 |
error_message = Tcl_ObjPrintf ("zstd encoding error: %s", |
| 840 |
ZSTD_getErrorName (compressed_size)); |
| 841 |
goto err; |
| 842 |
} |
| 843 |
Tcl_Obj* compressed = Tcl_NewByteArrayObj (dest_buf, compressed_size); |
| 844 |
Tcl_Free (dest_buf); |
| 845 |
return compressed; |
| 846 |
err: |
| 847 |
Tcl_SetObjResult (ip, error_message); |
| 848 |
return 0; |
| 849 |
} |
| 850 |
critcl::cproc decompress { |
| 851 |
Tcl_Interp* ip |
| 852 |
bytes data |
| 853 |
} object0 { |
| 854 |
Tcl_Obj* error_message; |
| 855 |
size_t dest_sz = ZSTD_getDecompressedSize (data.s, data.len); |
| 856 |
if (dest_sz == 0) { |
| 857 |
error_message = Tcl_NewStringObj("invalid data", -1); |
| 858 |
goto err; |
| 859 |
} |
| 860 |
void* dest_buf = Tcl_Alloc (dest_sz); |
| 861 |
if (!dest_buf) { |
| 862 |
error_message = Tcl_NewStringObj("failed to allocate decompression buffer", -1); |
| 863 |
goto err; |
| 864 |
} |
| 865 |
size_t decompressed_size = ZSTD_decompress (dest_buf, dest_sz, |
| 866 |
data.s, data.len); |
| 867 |
if (decompressed_size != dest_sz) { |
| 868 |
Tcl_Free (dest_buf); |
| 869 |
error_message = Tcl_ObjPrintf("zstd decoding error: %s", |
| 870 |
ZSTD_getErrorName (decompressed_size)); |
| 871 |
goto err; |
| 872 |
} |
| 873 |
Tcl_Obj* decompressed = Tcl_NewByteArrayObj (dest_buf, dest_sz); |
| 874 |
Tcl_Free (dest_buf); |
| 875 |
return decompressed; |
| 876 |
err: |
| 877 |
Tcl_SetObjResult (ip, error_message); |
| 878 |
return 0; |
| 879 |
} |
| 880 |
# ## #### ######### ################ ######################### |
| 881 |
# Forcing compilation, link, and loading now. |
| 882 |
critcl::msg -nonewline { Building ...} |
| 883 |
if {![critcl::load]} { |
| 884 |
error "Building and loading the project failed." |
| 885 |
} |
| 886 |
# Name and version the package. Just like for every kind of Tcl package. |
| 887 |
package provide critcl-example 1 |
| 888 |
</pre> |
| 889 |
</li> |
| 890 |
<li><p>Build the package. See the <b class="sectref">Basics</b>, if necessary.</p></li> |
| 891 |
<li><p>Load the package and invoke the commands.</p> |
| 892 |
<p><em>Attention</em>. The commands take and return binary data. |
| 893 |
This may look very bad in the terminal.</p></li> |
| 894 |
<li><p>To test the commands enter</p> |
| 895 |
<pre class="doctools_example"> |
| 896 |
set a [compress {hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhello wwwwwwwworld}] |
| 897 |
decompress $a |
| 898 |
</pre> |
| 899 |
<p>in the interactive <b class="syscmd">tclsh</b></p></li> |
| 900 |
</ol> |
| 901 |
<p>New commands: <i class="term">critcl::cheaders</i>, <i class="term">critcl::clibraries</i>, <i class="term">critcl::include</i>.</p> |
| 902 |
<div id="subsection18" class="doctools_subsection"><h3><a name="subsection18">Default Values For Arguments</a></h3> |
| 903 |
<ol class="doctools_enumerated"> |
| 904 |
<li><p>Reread the example of the main section. Note specifically the line</p> |
| 905 |
<pre class="doctools_example"> |
| 906 |
int {level ZSTD_CLEVEL_DEFAULT} |
| 907 |
</pre> |
| 908 |
</li> |
| 909 |
<li><p>This line demonstrates that <b class="cmd">critcl::cproc</b> arguments allowed to have default |
| 910 |
values, in the same vein as <b class="cmd">proc</b> arguments, and using the same syntax.</p></li> |
| 911 |
<li><p><em>Attention</em> Default values have to be legal C rvalues and match the C type of |
| 912 |
the argument.</p> |
| 913 |
<p>They are literally pasted into the generated C code.</p> |
| 914 |
<p>They bypass any argument validation done in the generated translation layer. This |
| 915 |
means that it is possible to use a value an invoker of the command cannot use from Tcl.</p></li> |
| 916 |
<li><p>This kind of in-band signaling of a default versus a regular argument is however |
| 917 |
not necessary.</p> |
| 918 |
<p>Look at</p> |
| 919 |
<pre class="doctools_example"> |
| 920 |
critcl::cproc default_or_not {int {x 0}} void { |
| 921 |
if !has_x { |
| 922 |
printf("called with default\n"); |
| 923 |
return |
| 924 |
} |
| 925 |
printf("called with %d\n", x); |
| 926 |
} |
| 927 |
</pre> |
| 928 |
<p>Any argument <i class="arg">x</i> with a default causes <i class="term"><a href="critcl.html">CriTcl</a></i> to create a hidden |
| 929 |
argument <i class="arg">has_x</i>, of type int (boolean). |
| 930 |
This argument is set to <b class="const">1</b> when <i class="arg">x</i> was filled from defaults, and <b class="const">0</b> |
| 931 |
else.</p></li> |
| 932 |
</ol> |
| 933 |
</div> |
| 934 |
<div id="subsection19" class="doctools_subsection"><h3><a name="subsection19">Custom Argument Validation</a></h3> |
| 935 |
<ol class="doctools_enumerated"> |
| 936 |
<li><p>Starting from the <span class="sectref"><a href="#section3">base wrapper</a></span>. |
| 937 |
Edit the file "<b class="file">example.tcl</b>". Replace the entire <b class="cmd">compress</b> function with</p> |
| 938 |
<pre class="doctools_example"> |
| 939 |
critcl::argtype zstd_compression_level { |
| 940 |
/* argtype: `int` */ |
| 941 |
if (Tcl_GetIntFromObj (interp, @@, &@A) != TCL_OK) return TCL_ERROR; |
| 942 |
/* additional validation */ |
| 943 |
int max = ZSTD_maxCLevel(); |
| 944 |
if ((@A < 1) || (@A > max)) { |
| 945 |
Tcl_SetObjResult (interp, |
| 946 |
Tcl_ObjPrintf ("zstd compression level must be integer between 1 and %d", max)); |
| 947 |
return TCL_ERROR; |
| 948 |
} |
| 949 |
/* @@: current objv[] element |
| 950 |
** @A: name of argument variable for transfer to C function |
| 951 |
** interp: predefined variable, access to current interp - error messages, etc. |
| 952 |
*/ |
| 953 |
} int int ;# C types of transfer variable and function argument. |
| 954 |
critcl::cproc compress { |
| 955 |
Tcl_Interp* ip |
| 956 |
bytes data |
| 957 |
zstd_compression_level {level ZSTD_CLEVEL_DEFAULT} |
| 958 |
} object0 { |
| 959 |
/* critcl_bytes data; (.s, .len, .o) */ |
| 960 |
/* int level; validated to be in range 1...ZSTD_maxCLevel() */ |
| 961 |
Tcl_Obj* error_message; |
| 962 |
size_t dest_sz = ZSTD_compressBound (data.len); |
| 963 |
void* dest_buf = Tcl_Alloc(dest_sz); |
| 964 |
if (!dest_buf) { |
| 965 |
error_message = Tcl_NewStringObj ("can't allocate memory to compress data", -1); |
| 966 |
goto err; |
| 967 |
} |
| 968 |
size_t compressed_size = ZSTD_compress (dest_buf, dest_sz, |
| 969 |
data.s, data.len, |
| 970 |
level); |
| 971 |
if (ZSTD_isError (compressed_size)) { |
| 972 |
Tcl_Free(dest_buf); |
| 973 |
error_message = Tcl_ObjPrintf ("zstd encoding error: %s", |
| 974 |
ZSTD_getErrorName (compressed_size)); |
| 975 |
goto err; |
| 976 |
} |
| 977 |
Tcl_Obj* compressed = Tcl_NewByteArrayObj (dest_buf, compressed_size); |
| 978 |
Tcl_Free (dest_buf); |
| 979 |
return compressed; |
| 980 |
err: |
| 981 |
Tcl_SetObjResult (ip, error_message); |
| 982 |
return 0; |
| 983 |
} |
| 984 |
</pre> |
| 985 |
<p>and rebuild the package.</p> |
| 986 |
<p>In the original example the <i class="arg">level</i> argument of the function was validated in |
| 987 |
the function itself. |
| 988 |
This may detract from the funtionality of interest itself, especially if there are lots of |
| 989 |
arguments requiring validation. |
| 990 |
If the same kind of argument is used in multiple places this causes code duplication in |
| 991 |
the functions as well.</p> |
| 992 |
<p>Use a custom argument type as defined by the modification to move this kind of |
| 993 |
validation out of the function, and enhance readability.</p> |
| 994 |
<p>Code duplication however is only partially adressed. |
| 995 |
While there is no duplication in the visible definitions the C code of the new argument |
| 996 |
type is replicated for each use of the type.</p></li> |
| 997 |
<li><p>Now replace the <b class="cmd">argtype</b> definition with</p> |
| 998 |
<pre class="doctools_example"> |
| 999 |
critcl::code { |
| 1000 |
int GetCompressionLevel (Tcl_Interp* interp, Tcl_Obj* obj, int* level) |
| 1001 |
{ |
| 1002 |
if (Tcl_GetIntFromObj (interp, obj, level) != TCL_OK) return TCL_ERROR; |
| 1003 |
int max = ZSTD_maxCLevel(); |
| 1004 |
if ((*level < 1) || (*level > max)) { |
| 1005 |
Tcl_SetObjResult (interp, |
| 1006 |
Tcl_ObjPrintf ("zstd compression level must be integer between 1 and %d", max)); |
| 1007 |
return TCL_ERROR; |
| 1008 |
} |
| 1009 |
return TCL_OK; |
| 1010 |
} |
| 1011 |
} |
| 1012 |
critcl::argtype zstd_compression_level { |
| 1013 |
if (GetCompressionLevel (@@, &@A) != TCL_OK) return TCL_ERROR; |
| 1014 |
} int int |
| 1015 |
</pre> |
| 1016 |
<p>and rebuild the package.</p> |
| 1017 |
<p>Now only the calls to the new validation function are replicated. |
| 1018 |
The function itself exists only once.</p></li> |
| 1019 |
</ol> |
| 1020 |
</div> |
| 1021 |
<div id="subsection20" class="doctools_subsection"><h3><a name="subsection20">Separating Local C Sources</a></h3> |
| 1022 |
<ol class="doctools_enumerated"> |
| 1023 |
<li><p>Starting from the end of the <span class="sectref"><a href="#subsection19">previous</a></span> section. |
| 1024 |
Edit the file "<b class="file">example.tcl</b>".</p></li> |
| 1025 |
<li><p>Save the contents of the <b class="cmd">critcl::ccode</b> block into a file "<b class="file">example.c</b>" and |
| 1026 |
then replace the entire block with</p> |
| 1027 |
<pre class="doctools_example"> |
| 1028 |
critcl::csources example.c |
| 1029 |
critcl::ccode { |
| 1030 |
extern int GetCompressionLevel (Tcl_Interp* interp, Tcl_Obj* obj, int* level); |
| 1031 |
} |
| 1032 |
</pre> |
| 1033 |
<p>When mixing C and Tcl code the different kind of indentation rules for these |
| 1034 |
languages may come into strong conflict. Further, very large blocks of C code may reduce |
| 1035 |
overall readability.</p></li> |
| 1036 |
<li><p>The examples fixes this by moving the code block into a local C file and then |
| 1037 |
registering this file with <i class="term"><a href="critcl.html">CriTcl</a></i>. |
| 1038 |
When building the package <i class="term"><a href="critcl.html">CriTcl</a></i> arranges to build all such registered C files as |
| 1039 |
well.</p></li> |
| 1040 |
<li><p><em>Attention</em>. The C code is now in a separate compilation unit. |
| 1041 |
The example declares the exported function so that the <b class="cmd">cproc</b>s are again able to see |
| 1042 |
and use it.</p></li> |
| 1043 |
<li><p>Now go a step further. Save the declaration into a file "<b class="file">example.h</b>", and then |
| 1044 |
use</p> |
| 1045 |
<pre class="doctools_example"> |
| 1046 |
critcl::include example.h |
| 1047 |
</pre> |
| 1048 |
<p>to import it. Note that this is just a shorthand for</p> |
| 1049 |
<pre class="doctools_example"> critcl::ccode { |
| 1050 |
#include "example.h" |
| 1051 |
} |
| 1052 |
</pre> |
| 1053 |
</li> |
| 1054 |
<li><p>As an alternative solution, start from the beginning of the section and move the |
| 1055 |
entire original <b class="cmd">critcl::ccode</b> block into a file "<b class="file">example-check.tcl</b>".</p> |
| 1056 |
<p>Then replace it with</p> |
| 1057 |
<pre class="doctools_example"> |
| 1058 |
critcl::source example-check.tcl |
| 1059 |
</pre> |
| 1060 |
<p>to import it into the main code again.</p> |
| 1061 |
<p><em>Attention</em> Tcl's builtin command <b class="cmd">source</b> is <em>not suitable</em> for |
| 1062 |
importing the separate file due to how <i class="term"><a href="critcl.html">CriTcl</a></i> uses the information from |
| 1063 |
<b class="cmd">info script</b> to key various internal datastructures.</p></li> |
| 1064 |
</ol> |
| 1065 |
</div> |
| 1066 |
<div id="subsection21" class="doctools_subsection"><h3><a name="subsection21">Very Simple Results</a></h3> |
| 1067 |
<ol class="doctools_enumerated"> |
| 1068 |
<li><p>Starting from the end of the <span class="sectref"><a href="#subsection19">validation</a></span> section. |
| 1069 |
Edit the file "<b class="file">example.tcl</b>". |
| 1070 |
Add the code below, just before the <b class="cmd">compress</b> command.</p> |
| 1071 |
<pre class="doctools_example"> |
| 1072 |
critcl::cconst version char* ZSTD_VERSION_STRING |
| 1073 |
critcl::cconst min-level int 1 |
| 1074 |
critcl::cconst max-level int ZSTD_maxCLevel() |
| 1075 |
</pre> |
| 1076 |
<p>and rebuild the package.</p></li> |
| 1077 |
<li><p>These declarations create three additional commands, each returning the specified |
| 1078 |
value. A fixed string, an integer, and a function call returning an integer.</p></li> |
| 1079 |
<li><p><em>Attention</em> The values have to be legal C rvalues and match the C type of the |
| 1080 |
result. They are literally pasted into the generated C code.</p></li> |
| 1081 |
<li><p>When using <b class="cmd">critcl::cconst</b> <i class="term"><a href="critcl.html">CriTcl</a></i> is aware that the result of the |
| 1082 |
function does not depend on any parameters and is computed in a single C expression.</p> |
| 1083 |
<p>This enables it do to away with the internal helper function it would need and |
| 1084 |
generate if <b class="cmd">critcl::cproc</b> had been used instead. |
| 1085 |
For example</p> |
| 1086 |
<pre class="doctools_example"> |
| 1087 |
critcl::cproc version {} char* { |
| 1088 |
return ZSTD_VERSION_STRING; |
| 1089 |
} |
| 1090 |
</pre> |
| 1091 |
</li> |
| 1092 |
</ol> |
| 1093 |
</div> |
| 1094 |
<div id="subsection22" class="doctools_subsection"><h3><a name="subsection22">Structure Arguments</a></h3> |
| 1095 |
<ol class="doctools_enumerated"> |
| 1096 |
<li><p>For all that this is a part of how to |
| 1097 |
<span class="sectref"><a href="#section3">Use External Libraries</a></span>, for the demonstratation |
| 1098 |
only the basics are needed.</p></li> |
| 1099 |
<li><p>Starting from the <span class="sectref"><a href="#section2">Basics</a></span>. |
| 1100 |
Edit the file "<b class="file">example.tcl</b>". |
| 1101 |
Remove the definition of <b class="cmd">hello</b>. Replace it with</p> |
| 1102 |
<pre class="doctools_example"> |
| 1103 |
critcl::ccode { |
| 1104 |
typedef struct vec2 { |
| 1105 |
double x; |
| 1106 |
double y; |
| 1107 |
} vec2; |
| 1108 |
typedef vec2* vec2ptr; |
| 1109 |
int |
| 1110 |
GetVecFromObj (Tcl_Interp* interp, Tcl_Obj* obj, vec2ptr* vec) |
| 1111 |
{ |
| 1112 |
int len; |
| 1113 |
if (Tcl_ListObjLength (interp, obj, &len) != TCL_OK) return TCL_ERROR; |
| 1114 |
if (len != 2) { |
| 1115 |
Tcl_SetObjResult (interp, Tcl_ObjPrintf ("Expected 2 elements, got %d", len)); |
| 1116 |
return TCL_ERROR; |
| 1117 |
} |
| 1118 |
Tcl_Obj* lv[2]; |
| 1119 |
if (Tcl_ListObjGetElements (interp, obj, &lv) != TCL_OK) return TCL_ERROR; |
| 1120 |
double x, y; |
| 1121 |
if (Tcl_GetDoubleFromObj (interp, lv[0], &x) != TCL_OK) return TCL_ERROR; |
| 1122 |
if (Tcl_GetDoubleFromObj (interp, lv[1], &y) != TCL_OK) return TCL_ERROR; |
| 1123 |
*vec = Tcl_Alloc (sizeof (vec2)); |
| 1124 |
(*vec)->x = x; |
| 1125 |
(*vec)->y = y; |
| 1126 |
return TCL_OK; |
| 1127 |
} |
| 1128 |
} |
| 1129 |
critcl::argtype vec2 { |
| 1130 |
if (GetVecFromObj (interp, @@, &@A) != TCL_OK) return TCL_ERROR; |
| 1131 |
} vec2ptr vec2ptr |
| 1132 |
critcl::argtyperelease vec2 { |
| 1133 |
/* @A : C variable holding the data to release */ |
| 1134 |
Tcl_Free ((char*) @A); |
| 1135 |
} |
| 1136 |
critcl::cproc norm {vec2 vector} double { |
| 1137 |
double norm = hypot (vector->x, vector->y); |
| 1138 |
return norm; |
| 1139 |
} |
| 1140 |
</pre> |
| 1141 |
<p>and rebuild the package.</p></li> |
| 1142 |
<li><p>The structure to pass as argument is a 2-dimensional vector. It is actually passed |
| 1143 |
in as a pointer to a <b class="type">vec2</b> structure. |
| 1144 |
This pointer is created by the <b class="function">GetVecFromObj</b> function. It allocates and fills the |
| 1145 |
structure from the Tcl value, which has to be a list of 2 doubles. The bulk of the code in |
| 1146 |
<b class="function">GetVecFromObj</b> is for verifying this and extracting the doubles.</p></li> |
| 1147 |
<li><p>The <b class="cmd">argtyperelease</b> code releases the pointer when the C function returns. In |
| 1148 |
other words, the pointer to the structure is owned by the translation layer and exists |
| 1149 |
only while the function is active.</p></li> |
| 1150 |
<li><p>While working this code has two disadvantages. |
| 1151 |
First there is memory churn. Each call of <b class="cmd">norm</b> causes the creation and release of a |
| 1152 |
temporary <b class="type">vec2</b> structure for the argument. |
| 1153 |
Second is the need to always extract the data from the <b class="type">Tcl_Obj*</b> value.</p> |
| 1154 |
<p>Both can be done better.</p> |
| 1155 |
<p>We will come back to this after explaining how to return structures to Tcl.</p></li> |
| 1156 |
</ol> |
| 1157 |
</div> |
| 1158 |
<div id="subsection23" class="doctools_subsection"><h3><a name="subsection23">Structure Results</a></h3> |
| 1159 |
<ol class="doctools_enumerated"> |
| 1160 |
<li><p>Starting from the end of the <span class="sectref"><a href="#subsection22">previous</a></span> section.</p></li> |
| 1161 |
<li><p>Edit the file "<b class="file">example.tcl</b>" and add the following code, just after the |
| 1162 |
definition of the <b class="cmd">norm</b> command.</p> |
| 1163 |
<pre class="doctools_example"> |
| 1164 |
critcl::resulttype vec2 { |
| 1165 |
/* rv: result value of function, interp: current Tcl interpreter */ |
| 1166 |
if (rv == NULL) return TCL_ERROR; |
| 1167 |
Tcl_Obj* lv[2]; |
| 1168 |
lv[0] = Tcl_NewDoubleObj (rv->x); |
| 1169 |
lv[1] = Tcl_NewDoubleObj (rv->y); |
| 1170 |
Tcl_SetObjResult (interp, Tcl_NewListObj (2, lv)); |
| 1171 |
Tcl_Free (rv); |
| 1172 |
return TCL_OK; |
| 1173 |
} vec2ptr ;# C result type |
| 1174 |
critcl::cproc add {vec2 a vec2 b} vec2 { |
| 1175 |
vec2ptr z = Tcl_Alloc (sizeof (vec2)); |
| 1176 |
z->x = a->x + b->x; |
| 1177 |
z->y = a->y + b->y; |
| 1178 |
return z; |
| 1179 |
} |
| 1180 |
</pre> |
| 1181 |
<p>and rebuild the package.</p></li> |
| 1182 |
<li><p>The new command <b class="cmd">add</b> takes two vectors and return the element-wise sum of both |
| 1183 |
as a new vector.</p></li> |
| 1184 |
<li><p>The function allocates and initializes a structure and hands it over to the |
| 1185 |
translation layer. Which in turn constructs a Tcl list of 2 doubles from it, sets that as |
| 1186 |
the command's result and at last discards the allocated structure again.</p></li> |
| 1187 |
<li><p>While working this code has two disadvantages. |
| 1188 |
First there is memory churn. Each call of <b class="cmd">add</b> causes the creation and release of |
| 1189 |
three temporary <b class="type">vec2</b> structures. One per argument, and one for the result. |
| 1190 |
Second is the need to always construct a complex <b class="type">Tcl_Obj*</b> value from the structure.</p> |
| 1191 |
<p>Both can be done better. This is explained in the next section.</p></li> |
| 1192 |
</ol> |
| 1193 |
</div> |
| 1194 |
<div id="subsection24" class="doctools_subsection"><h3><a name="subsection24">Structure Types</a></h3> |
| 1195 |
<ol class="doctools_enumerated"> |
| 1196 |
<li><p>Starting from the end of the previous section.</p></li> |
| 1197 |
<li><p>Edit the file "<b class="file">example.tcl</b>".</p></li> |
| 1198 |
<li><p>Remove the entire functionality (type definitions, related C code, and cprocs). |
| 1199 |
Replace it with</p> |
| 1200 |
<pre class="doctools_example"> |
| 1201 |
critcl::ccode { |
| 1202 |
typedef struct vec2 { |
| 1203 |
double x; |
| 1204 |
double y; |
| 1205 |
} vec2; |
| 1206 |
typedef vec2* vec2ptr; |
| 1207 |
/* -- Core vector structure management -- */ |
| 1208 |
static vec2ptr Vec2New (double x, double y) { |
| 1209 |
vec2ptr vec = Tcl_Alloc (sizeof (vec2)); |
| 1210 |
vec->x = x; |
| 1211 |
vec->y = y; |
| 1212 |
return vec; |
| 1213 |
} |
| 1214 |
static vec2ptr Vec2Copy (vec2ptr src) { |
| 1215 |
vec2ptr vec = Tcl_Alloc (sizeof (vec2)); |
| 1216 |
*vec = *src |
| 1217 |
return vec; |
| 1218 |
} |
| 1219 |
static void Vec2Release (vec2ptr vec) { |
| 1220 |
Tcl_Free ((char*) vec); |
| 1221 |
} |
| 1222 |
/* -- Tcl value type for vec2 -- Tcl_ObjType -- */ |
| 1223 |
static void Vec2Free (Tcl_Obj* obj); |
| 1224 |
static void Vec2StringOf (Tcl_Obj* obj); |
| 1225 |
static void Vec2Dup (Tcl_Obj* obj, Tcl_Obj* dst); |
| 1226 |
static int Vec2FromAny (Tcl_Interp* interp, Tcl_Obj* obj); |
| 1227 |
Tcl_ObjType vec2_objtype = { |
| 1228 |
"vec2", |
| 1229 |
Vec2Free, |
| 1230 |
Vec2Dup, |
| 1231 |
Vec2StringOf, |
| 1232 |
Vec2FromAny |
| 1233 |
}; |
| 1234 |
static void Vec2Free (Tcl_Obj* obj) { |
| 1235 |
Vec2Release ((vec2ptr) obj->internalRep.otherValuePtr); |
| 1236 |
} |
| 1237 |
static void Vec2Dup (Tcl_Obj* obj, Tcl_Obj* dst) { |
| 1238 |
vec2ptr vec = (vec2ptr) obj->internalRep.otherValuePtr; |
| 1239 |
dst->internalRep.otherValuePtr = Vec2Copy (vec); |
| 1240 |
dst->typePtr = &vec2_objtype; |
| 1241 |
} |
| 1242 |
static void Vec2StringOf (Tcl_Obj* obj) { |
| 1243 |
vec2ptr vec = (vec2ptr) obj->internalRep.otherValuePtr; |
| 1244 |
/* Serialize vector data to string (list of two doubles) */ |
| 1245 |
Tcl_DString ds; |
| 1246 |
Tcl_DStringInit (&ds); |
| 1247 |
char buf [TCL_DOUBLE_SPACE]; |
| 1248 |
Tcl_PrintDouble (0, vec->x, buf); Tcl_DStringAppendElement (&ds, buf); |
| 1249 |
Tcl_PrintDouble (0, vec->y, buf); Tcl_DStringAppendElement (&ds, buf); |
| 1250 |
int length = Tcl_DStringLength (ds); |
| 1251 |
/* Set string representation */ |
| 1252 |
obj->length = length; |
| 1253 |
obj->bytes = Tcl_Alloc(length+1); |
| 1254 |
memcpy (obj->bytes, Tcl_DStringValue (ds), length); |
| 1255 |
obj->bytes[length] = '\0'; |
| 1256 |
/* |
| 1257 |
** : package require critcl::cutil ;# get C utilities |
| 1258 |
** : critcl::cutil::alloc ;# Activate allocation utilities |
| 1259 |
** : (Internally cheaders, include) |
| 1260 |
** : Then all of the above can be written as STREP_DS (obj, ds); |
| 1261 |
** : STREP_DS = STRing REP from DString |
| 1262 |
*/ |
| 1263 |
Tcl_DStringFree (&ds); |
| 1264 |
} |
| 1265 |
static int Vec2FromAny (Tcl_Interp* interp, Tcl_Obj* obj) { |
| 1266 |
/* Change intrep of obj to vec2 structure. |
| 1267 |
** A Tcl list of 2 doubles is used as an intermediary intrep. |
| 1268 |
*/ |
| 1269 |
int len; |
| 1270 |
if (Tcl_ListObjLength (interp, obj, &len) != TCL_OK) return TCL_ERROR; |
| 1271 |
if (len != 2) { |
| 1272 |
Tcl_SetObjResult (interp, Tcl_ObjPrintf ("Expected 2 elements, got %d", len)); |
| 1273 |
return TCL_ERROR; |
| 1274 |
} |
| 1275 |
Tcl_Obj* lv[2]; |
| 1276 |
if (Tcl_ListObjGetElements (interp, obj, &lv) != TCL_OK) return TCL_ERROR; |
| 1277 |
double x, y; |
| 1278 |
if (Tcl_GetDoubleFromObj (interp, lv[0], &x) != TCL_OK) return TCL_ERROR; |
| 1279 |
if (Tcl_GetDoubleFromObj (interp, lv[1], &y) != TCL_OK) return TCL_ERROR; |
| 1280 |
obj->internalRep.otherValuePtr = (void*) Vec2New (x, y); |
| 1281 |
obj->typePtr = &vec2_objtype; |
| 1282 |
return TCL_OK; |
| 1283 |
} |
| 1284 |
/* -- (un)packing structures from/into Tcl values -- */ |
| 1285 |
int GetVecFromObj (Tcl_Interp* interp, Tcl_Obj* obj, vec2ptr* vec) |
| 1286 |
{ |
| 1287 |
if (obj->typePtr != &vec2_objtype) { |
| 1288 |
if (Vec2FromAny (interp, obj) != TCL_OK) return TCL_ERROR; |
| 1289 |
} |
| 1290 |
*vec = (vec2ptr) obj->internalRep.otherValuePtr; |
| 1291 |
return TCL_OK; |
| 1292 |
} |
| 1293 |
Tcl_Obj* NewVecObj (vec2ptr vec) { |
| 1294 |
Tcl_Obj* obj = Tcl_NewObj (); |
| 1295 |
Tcl_InvalidateStringRep (obj); |
| 1296 |
obj->internalRep.otherValuePtr = Vec2Copy (vec); |
| 1297 |
obj->typePtr = &vec2_objtype; |
| 1298 |
return obj; |
| 1299 |
} |
| 1300 |
} |
| 1301 |
critcl::argtype vec2 { |
| 1302 |
if (GetVecFromObj (interp, @@, &@A) != TCL_OK) return TCL_ERROR; |
| 1303 |
} vec2ptr vec2ptr |
| 1304 |
critcl::resulttype vec2 { |
| 1305 |
/* rv: result value of function, interp: current Tcl interpreter */ |
| 1306 |
Tcl_SetObjResult (interp, NewVecObj (&rv)); |
| 1307 |
return TCL_OK; |
| 1308 |
} vec2 |
| 1309 |
critcl::cproc norm {vec2 vector} double { |
| 1310 |
double norm = hypot (vector->x, vector->y); |
| 1311 |
return norm; |
| 1312 |
} |
| 1313 |
critcl::cproc add {vec2 a vec2 b} vec2 { |
| 1314 |
vec2 z; |
| 1315 |
z.x = a->x + b->x; |
| 1316 |
z.y = a->y + b->y; |
| 1317 |
return z; |
| 1318 |
} |
| 1319 |
</pre> |
| 1320 |
<p>and rebuild the package.</p></li> |
| 1321 |
<li><p>This implements a new <b class="type">Tcl_ObjType</b> to handle <b class="type">vec2</b> structures. With it |
| 1322 |
<b class="type">vec2</b> structures are become usable as internal representation (<i class="term">intrep</i> of |
| 1323 |
<b class="type">Tcl_Obj*</b> values.</p> |
| 1324 |
<p>The two functions <b class="function">NewVecObj</b> and <b class="function">GetVecFromObj</b> pack and unpack the |
| 1325 |
structures from and into <b class="type">Tcl_Obj*</b> values. |
| 1326 |
The latter performs the complex deserialization into a structure if and only if needed, |
| 1327 |
i.e. when the <b class="type">TclObj*</b> value has no intrep, or the intrep for a different type. |
| 1328 |
This process of changing the intrep of a Tcl value is called <i class="term">shimmering</i>.</p> |
| 1329 |
<p>Intreps cache the interpretation of <b class="type">Tcl_Obj*</b> values as a specific kind of |
| 1330 |
type. Here <b class="type">vec2</b>. This reduces conversion effort and memory churn, as intreps are |
| 1331 |
kept by the Tcl interpreter as long as possible and needed.</p></li> |
| 1332 |
<li><p>The arguments of <b class="cmd">norm</b> and <b class="cmd">add</b> are now converted once on entry, if not |
| 1333 |
yet in the proper type, or not at all, if so.</p></li> |
| 1334 |
<li><p><em>Attention</em>. This example has the issue of passing result structures by value |
| 1335 |
through the stack, and then packing a copy into a <b class="type">Tcl_Obj*</b> value. |
| 1336 |
While this is no trouble for structures as small as <b class="type">vec2</b> larger structures may pose |
| 1337 |
a problem.</p> |
| 1338 |
<p>We will address this in the next section.</p></li> |
| 1339 |
</ol> |
| 1340 |
<p>Packages: <i class="term"><a href="critcl_cutil.html">critcl::cutil</a></i></p> |
| 1341 |
</div> |
| 1342 |
<div id="subsection25" class="doctools_subsection"><h3><a name="subsection25">Large Structures</a></h3> |
| 1343 |
<ol class="doctools_enumerated"> |
| 1344 |
<li><p>Starting from the end of the previous section.</p></li> |
| 1345 |
<li><p>Edit the file "<b class="file">example.tcl</b>".</p></li> |
| 1346 |
<li><p>Describing each individual change is too complex. The following is easier.</p></li> |
| 1347 |
<li><p>Save the file, then replace the entire functionality with the following.</p></li> |
| 1348 |
<li><p>After that use a <b class="syscmd">diff</b> of your choice to compare the files and see the |
| 1349 |
critical changes.</p> |
| 1350 |
<pre class="doctools_example"> |
| 1351 |
critcl::ccode { |
| 1352 |
typedef struct vec2 { |
| 1353 |
unsigned int rc; |
| 1354 |
double x; |
| 1355 |
double y; |
| 1356 |
} vec2; |
| 1357 |
typedef vec2* vec2ptr; |
| 1358 |
/* -- Core vector structure management -- */ |
| 1359 |
static vec2ptr Vec2New (double x, double y) { |
| 1360 |
vec2ptr vec = Tcl_Alloc (sizeof (vec2)); |
| 1361 |
vec->rc = 0; |
| 1362 |
vec->x = x; |
| 1363 |
vec->y = y; |
| 1364 |
return vec; |
| 1365 |
} |
| 1366 |
static vec2ptr Vec2Copy (vec2ptr src) { |
| 1367 |
scr->rc ++; |
| 1368 |
return src; |
| 1369 |
} |
| 1370 |
static void Vec2Release (vec2ptr vec) { |
| 1371 |
if (vec->rc > 1) { |
| 1372 |
vec->rc --; |
| 1373 |
return; |
| 1374 |
} |
| 1375 |
|
| 1376 |
Tcl_Free ((char*) vec); |
| 1377 |
} |
| 1378 |
/* -- Vector obj type -- */ |
| 1379 |
static void Vec2Free (Tcl_Obj* obj); |
| 1380 |
static void Vec2StringOf (Tcl_Obj* obj); |
| 1381 |
static void Vec2Dup (Tcl_Obj* obj, Tcl_Obj* dst); |
| 1382 |
static int Vec2FromAny (Tcl_Interp* interp, Tcl_Obj* obj); |
| 1383 |
Tcl_ObjType vec2_objtype = { |
| 1384 |
"vec2", |
| 1385 |
Vec2Free, |
| 1386 |
Vec2Dup, |
| 1387 |
Vec2StringOf, |
| 1388 |
Vec2FromAny |
| 1389 |
}; |
| 1390 |
static void Vec2Free (Tcl_Obj* obj) { |
| 1391 |
Vec2Release ((vec2ptr) obj->internalRep.otherValuePtr); |
| 1392 |
} |
| 1393 |
static void Vec2Dup (Tcl_Obj* obj, Tcl_Obj* dst) { |
| 1394 |
vec2ptr vec = (vec2ptr) obj->internalRep.otherValuePtr; |
| 1395 |
dst->internalRep.otherValuePtr = Vec2Copy (vec); |
| 1396 |
dst->typePtr = &vec2_objtype; |
| 1397 |
} |
| 1398 |
static void Vec2StringOf (Tcl_Obj* obj) { |
| 1399 |
vec2ptr vec = (vec2ptr) obj->internalRep.otherValuePtr; |
| 1400 |
/* Serialize vector data to string (list of two doubles) */ |
| 1401 |
Tcl_DString ds; |
| 1402 |
Tcl_DStringInit (&ds); |
| 1403 |
char buf [TCL_DOUBLE_SPACE]; |
| 1404 |
Tcl_PrintDouble (0, vec->x, buf); Tcl_DStringAppendElement (&ds, buf); |
| 1405 |
Tcl_PrintDouble (0, vec->y, buf); Tcl_DStringAppendElement (&ds, buf); |
| 1406 |
int length = Tcl_DStringLength (ds); |
| 1407 |
/* Set string representation */ |
| 1408 |
obj->length = length; |
| 1409 |
obj->bytes = Tcl_Alloc(length+1); |
| 1410 |
memcpy (obj->bytes, Tcl_DStringValue (ds), length); |
| 1411 |
obj->bytes[length] = '\0'; |
| 1412 |
/* |
| 1413 |
** : package require critcl::cutil ;# get C utilities |
| 1414 |
** : critcl::cutil::alloc ;# Activate allocation utilities |
| 1415 |
** : (Internally cheaders, include) |
| 1416 |
** : Then all of the above can be written as STREP_DS (obj, ds); |
| 1417 |
** : STREP_DS = STRing REP from DString |
| 1418 |
*/ |
| 1419 |
Tcl_DStringFree (&ds); |
| 1420 |
} |
| 1421 |
static int Vec2FromAny (Tcl_Interp* interp, Tcl_Obj* obj) { |
| 1422 |
/* Change internal rep of obj to vector structure. |
| 1423 |
** A Tcl list of 2 doubles is used as intermediary int rep. |
| 1424 |
*/ |
| 1425 |
int len; |
| 1426 |
if (Tcl_ListObjLength (interp, obj, &len) != TCL_OK) return TCL_ERROR; |
| 1427 |
if (len != 2) { |
| 1428 |
Tcl_SetObjResult (interp, Tcl_ObjPrintf ("Expected 2 elements, got %d", len)); |
| 1429 |
return TCL_ERROR; |
| 1430 |
} |
| 1431 |
Tcl_Obj* lv[2]; |
| 1432 |
if (Tcl_ListObjGetElements (interp, obj, &lv) != TCL_OK) return TCL_ERROR; |
| 1433 |
double x, y; |
| 1434 |
if (Tcl_GetDoubleFromObj (interp, lv[0], &x) != TCL_OK) return TCL_ERROR; |
| 1435 |
if (Tcl_GetDoubleFromObj (interp, lv[1], &y) != TCL_OK) return TCL_ERROR; |
| 1436 |
obj->internalRep.otherValuePtr = (void*) Vec2New (x, y); |
| 1437 |
obj->typePtr = &vec2_objtype; |
| 1438 |
return TCL_OK; |
| 1439 |
} |
| 1440 |
/* (un)packing structures from/into Tcl values -- */ |
| 1441 |
int GetVecFromObj (Tcl_Interp* interp, Tcl_Obj* obj, vec2ptr* vec) |
| 1442 |
{ |
| 1443 |
if (obj->typePtr != &vec2_objtype) { |
| 1444 |
if (VecFromAny (interp, obj) != TCL_OK) return TCL_ERROR; |
| 1445 |
} |
| 1446 |
*vec = (vec2ptr) obj->internalRep.otherValuePtr; |
| 1447 |
return TCL_OK; |
| 1448 |
} |
| 1449 |
Tcl_Obj* NewVecObj (vec2ptr vec) { |
| 1450 |
Tcl_Obj* obj = Tcl_NewObj (); |
| 1451 |
Tcl_InvalidateStringRep (obj); |
| 1452 |
obj->internalRep.otherValuePtr = Vec2Copy (vec); |
| 1453 |
obj->typePtr = &vec2_objtype; |
| 1454 |
return obj; |
| 1455 |
} |
| 1456 |
} |
| 1457 |
critcl::argtype vec2 { |
| 1458 |
if (GetVecFromObj (interp, @@, &@A) != TCL_OK) return TCL_ERROR; |
| 1459 |
} vec2ptr vec2ptr |
| 1460 |
critcl::resulttype vec2 { |
| 1461 |
/* rv: result value of function, interp: current Tcl interpreter */ |
| 1462 |
Tcl_SetObjResult (interp, NewVecObj (rv)); |
| 1463 |
return TCL_OK; |
| 1464 |
} vec2ptr |
| 1465 |
critcl::cproc norm {vec2 vector} double { |
| 1466 |
double norm = hypot (vector->x, vector->y); |
| 1467 |
return norm; |
| 1468 |
} |
| 1469 |
critcl::cproc add {vec2 a vec2 b} vec2 { |
| 1470 |
return Vec2New (a->x + b->x, a->y + b->y); |
| 1471 |
} |
| 1472 |
</pre> |
| 1473 |
</li> |
| 1474 |
<li><p>The <b class="type">vec2</b> structure is now reference counted.</p></li> |
| 1475 |
<li><p>The core management functions, i.e. <b class="function">Vec2New</b>, <b class="function">Vec2Copy</b>, and |
| 1476 |
<b class="function">Vec2Release</b> are changed to maintain that reference count. |
| 1477 |
Starting at <b class="const">0</b> on creation, copies increment, and releases decrement. |
| 1478 |
A structure is actually only freed when its reference count falls to <b class="const">0</b> or below.</p></li> |
| 1479 |
<li><p><b class="type">vec2</b> results are changed to pointers, easily passed back through the stack. |
| 1480 |
The modified translation layer just wraps it into a <b class="type">Tcl_Obj*</b> value.</p></li> |
| 1481 |
<li><p><em>Attention</em>. Duplicating such a <b class="type">Tcl_Obj*</b> does not duplicate the |
| 1482 |
referenced <b class="type">vec2</b> structure anymore, just adds a reference.</p></li> |
| 1483 |
<li><p>Regarding <b class="syscmd">diff</b> commands, I know of two graphical diffs for Tcl/Tk, |
| 1484 |
<a href="https://tkdiff.sourceforge.io">TkDiff</a>, and <a href="http://eskil.tcl.tk">Eskil</a>.</p></li> |
| 1485 |
</ol> |
| 1486 |
<p>Packages: <i class="term"><a href="critcl_cutil.html">critcl::cutil</a></i></p> |
| 1487 |
</div> |
| 1488 |
<div id="subsection26" class="doctools_subsection"><h3><a name="subsection26">External Structures</a></h3> |
| 1489 |
<ol class="doctools_enumerated"> |
| 1490 |
<li><p>Handle structures provided by external libraries using either |
| 1491 |
<span class="sectref"><a href="#subsection24">Structure Types</a></span> or <span class="sectref"><a href="#subsection25">Large Structures</a></span> as template.</p></li> |
| 1492 |
<li><p><em>Attention</em>. The choice is with the developer.</p> |
| 1493 |
<p>This is true even if the external structure is not reference counted by itself.</p> |
| 1494 |
<p>To reference count a structure <b class="type">S</b> without such simply wrap <b class="type">S</b> into a |
| 1495 |
local structure which provides the reference count and has a field for <b class="type">S</b> (pointer |
| 1496 |
or value).</p></li> |
| 1497 |
<li><p><em>Attention</em> Opaque external types, i.e. pointers to structures with hidden |
| 1498 |
fields, can also be handled by the given templates.</p></li> |
| 1499 |
</ol> |
| 1500 |
</div> |
| 1501 |
<div id="subsection27" class="doctools_subsection"><h3><a name="subsection27">External Enumerations</a></h3> |
| 1502 |
<p>This section demonstrates how to convert from any kind of enumeration provided by an |
| 1503 |
external library to Tcl strings, and the converse.</p> |
| 1504 |
<ol class="doctools_enumerated"> |
| 1505 |
<li><p>For all that this is a part of how to |
| 1506 |
<span class="sectref"><a href="#section3">Use External Libraries</a></span>, for the demonstratation |
| 1507 |
only the basics are needed.</p></li> |
| 1508 |
<li><p>Starting from the <span class="sectref"><a href="#section2">Basics</a></span>. |
| 1509 |
Edit the file "<b class="file">example.tcl</b>". |
| 1510 |
Remove the definition of <b class="cmd">hello</b>. Replace it with</p> |
| 1511 |
<pre class="doctools_example"> |
| 1512 |
package require critcl::emap |
| 1513 |
# no header included due to use of literal ints instead of symbolic names |
| 1514 |
critcl::emap::def yaml_sequence_style_t { |
| 1515 |
any 0 |
| 1516 |
block 1 |
| 1517 |
flow 2 |
| 1518 |
} |
| 1519 |
# encode: style to int |
| 1520 |
critcl::cproc encode {yaml_sequence_style_t style} int { |
| 1521 |
return style; |
| 1522 |
} |
| 1523 |
# decode: int to style |
| 1524 |
critcl::cproc decode {int style} yaml_sequence_style_t { |
| 1525 |
return style; |
| 1526 |
} |
| 1527 |
</pre> |
| 1528 |
<p>and rebuild the package.</p></li> |
| 1529 |
<li><p>The map converts between the Tcl level strings listed on the left side to the C |
| 1530 |
values on the right side, and the reverse.</p></li> |
| 1531 |
<li><p>It automatically generates <b class="cmd">critcl::argtype</b> and <b class="cmd">critcl::resulttype</b> |
| 1532 |
definitions.</p></li> |
| 1533 |
<li><p><em>Attention</em> Like the default values for <b class="cmd">cproc</b> arguments, and the results |
| 1534 |
for <b class="cmd">cconst</b> definitions the values on the right side have to be proper C |
| 1535 |
rvalues. They have to match C type <b class="type">int</b>.</p> |
| 1536 |
<p>In other words, it is perfectly ok to use the symbolic names provided by the header |
| 1537 |
file of the external library.</p> |
| 1538 |
<p><em>Attention</em> This however comes at a loss in efficiency. As <i class="term"><a href="critcl.html">CriTcl</a></i> then |
| 1539 |
has no insight into the covered range of ints, gaps, etc. it has to perform a linear |
| 1540 |
search when mapping from C to Tcl. When it knows the exact integer values it can use a |
| 1541 |
table lookup instead.</p> |
| 1542 |
<p><em>Attention</em> It also falls back to a search if a lookup table would contain more |
| 1543 |
than 50 entries.</p></li> |
| 1544 |
</ol> |
| 1545 |
<p>Packages: <i class="term"><a href="critcl_emap.html">critcl::emap</a></i></p> |
| 1546 |
</div> |
| 1547 |
<div id="subsection28" class="doctools_subsection"><h3><a name="subsection28">External Bitsets/Bitmaps/Flags</a></h3> |
| 1548 |
<p>This section demonstrates how to convert from any kind of bit-mapped flags provided by an |
| 1549 |
external library to lists of Tcl strings, and the converse.</p> |
| 1550 |
<ol class="doctools_enumerated"> |
| 1551 |
<li><p>For all that this is a part of how to |
| 1552 |
<span class="sectref"><a href="#section3">Use External Libraries</a></span>, for the demonstratation |
| 1553 |
only the basics are needed.</p></li> |
| 1554 |
<li><p>Starting from the <span class="sectref"><a href="#section2">Basics</a></span>. |
| 1555 |
Edit the file "<b class="file">example.tcl</b>". |
| 1556 |
Remove the definition of <b class="cmd">hello</b>. Replace it with</p> |
| 1557 |
<pre class="doctools_example"> |
| 1558 |
# http://man7.org/linux/man-pages/man7/inotify.7.html |
| 1559 |
|
| 1560 |
package require critcl::bitmap |
| 1561 |
# critcl::cheaders - n/a, header is in system directories |
| 1562 |
critcl::include sys/inotify.h |
| 1563 |
|
| 1564 |
critcl::bitmap::def tcl_inotify_events { |
| 1565 |
accessed IN_ACCESS |
| 1566 |
all IN_ALL_EVENTS |
| 1567 |
attribute IN_ATTRIB |
| 1568 |
closed IN_CLOSE |
| 1569 |
closed-nowrite IN_CLOSE_NOWRITE |
| 1570 |
closed-write IN_CLOSE_WRITE |
| 1571 |
created IN_CREATE |
| 1572 |
deleted IN_DELETE |
| 1573 |
deleted-self IN_DELETE_SELF |
| 1574 |
dir-only IN_ONLYDIR |
| 1575 |
dont-follow IN_DONT_FOLLOW |
| 1576 |
modified IN_MODIFY |
| 1577 |
move IN_MOVE |
| 1578 |
moved-from IN_MOVED_FROM |
| 1579 |
moved-self IN_MOVE_SELF |
| 1580 |
moved-to IN_MOVED_TO |
| 1581 |
oneshot IN_ONESHOT |
| 1582 |
open IN_OPEN |
| 1583 |
overflow IN_Q_OVERFLOW |
| 1584 |
unmount IN_UNMOUNT |
| 1585 |
} { |
| 1586 |
all closed move oneshot |
| 1587 |
} |
| 1588 |
|
| 1589 |
# encode: flag set to int |
| 1590 |
critcl::cproc encode {tcl_inotify_events e} int { |
| 1591 |
return e; |
| 1592 |
} |
| 1593 |
# decode: int to flag set |
| 1594 |
critcl::cproc decode {int e} tcl_inotify_events { |
| 1595 |
return e; |
| 1596 |
} |
| 1597 |
</pre> |
| 1598 |
<p>and rebuild the package.</p></li> |
| 1599 |
<li><p>The map converts between lists of the Tcl level strings listed on the left side to |
| 1600 |
the bit-union of the C values on the right side, and the reverse.</p> |
| 1601 |
<p>It is noted that the four strings <b class="const">all</b>, <b class="const">closed</b>, <b class="const">move</b>, and |
| 1602 |
<b class="const">oneshot</b> cannot be converted from C flags to list of strings, only from list to |
| 1603 |
bits.</p></li> |
| 1604 |
<li><p>It automatically generates <b class="cmd">critcl::argtype</b> and <b class="cmd">critcl::resulttype</b> |
| 1605 |
definitions.</p></li> |
| 1606 |
<li><p><em>Attention</em> Like the default values for <b class="cmd">cproc</b> arguments, and the results |
| 1607 |
for <b class="cmd">cconst</b> definitions the values on the right side have to be proper C |
| 1608 |
rvalues. They have to match C type <b class="type">int</b>.</p> |
| 1609 |
<p>In other words, it is perfectly ok to use the symbolic names provided by the header |
| 1610 |
file of the external library. As shown.</p></li> |
| 1611 |
</ol> |
| 1612 |
<p>Packages: <i class="term"><a href="critcl_bitmap.html">critcl::bitmap</a></i></p> |
| 1613 |
</div> |
| 1614 |
<div id="subsection29" class="doctools_subsection"><h3><a name="subsection29">Non-standard header/library locations</a></h3> |
| 1615 |
<ol class="doctools_enumerated"> |
| 1616 |
<li><p>See and reread the <span class="sectref"><a href="#section3">basic wrapper package</a></span> for |
| 1617 |
the introduction of the commands referenced below.</p></li> |
| 1618 |
<li><p><em>Attention</em> Relative paths will be resolved relative to the location of the |
| 1619 |
"<b class="file">.tcl</b>" file containing the <i class="term"><a href="critcl.html">CriTcl</a></i> commands.</p></li> |
| 1620 |
<li><p>Use the command <b class="cmd">critcl::cheaders</b> to tell <i class="term"><a href="critcl.html">CriTcl</a></i> about non-standard |
| 1621 |
locations for header files.</p> |
| 1622 |
<p>Multiple arguments are allowed, and multiple calls as well. The information |
| 1623 |
accumulates.</p> |
| 1624 |
<p>Arguments of the form "<b class="file">-Idirectory</b>" register the directory directly.</p> |
| 1625 |
<p>For arguments of the form "<b class="file">path</b>" the directory holding the path is |
| 1626 |
registered. In other words, it is assumed to be the full path of a header <em>file</em>, and |
| 1627 |
not a directory.</p> |
| 1628 |
<pre class="doctools_example"> |
| 1629 |
critcl::cheaders -I/usr/local/include |
| 1630 |
critcl::cheaders local/types.h |
| 1631 |
critcl::cheaders other-support/*.h |
| 1632 |
</pre> |
| 1633 |
</li> |
| 1634 |
<li><p>Use the command <b class="cmd">critcl::include</b> to actually use a specific header file.</p></li> |
| 1635 |
<li><p>Use the command <b class="cmd">critcl::clibraries</b> to tell <i class="term"><a href="critcl.html">CriTcl</a></i> about non-standard |
| 1636 |
locations for shared libaries, and about shared libaries to link to</p> |
| 1637 |
<p>Multiple arguments are allowed, and multiple calls as well. The information |
| 1638 |
accumulates.</p> |
| 1639 |
<p>Arguments of the form "<b class="file">-Ldirectory</b>" register a directory.</p> |
| 1640 |
<p>Arguments of the form "<b class="file">-lname</b>" register a shared libary to link to by |
| 1641 |
name. The library will be looked for in both standard and registered directories.</p> |
| 1642 |
<p>Arguments of the form "<b class="file">-path</b>" register a shared libary to link to by full |
| 1643 |
path.</p> |
| 1644 |
<pre class="doctools_example"> |
| 1645 |
critcl::clibraries -L/usr/lib/x86_64-linux-gnu |
| 1646 |
critcl::clibraries -lzstd |
| 1647 |
critcl::clibraries /usr/lib/x86_64-linux-gnu/libzstd.so |
| 1648 |
</pre> |
| 1649 |
</li> |
| 1650 |
<li><p>On Mac OS X use the command <b class="cmd">critcl::framework</b> to name the frameworks to use |
| 1651 |
in the package.</p> |
| 1652 |
<p><em>Attention</em> Using the command on other platforms is ok, and will be ignored.</p></li> |
| 1653 |
<li><p>Not answered in the above is how to find the necessary paths if they are not fixed |
| 1654 |
across machines or platforms.</p> |
| 1655 |
<p>We will come back to this.</p></li> |
| 1656 |
</ol> |
| 1657 |
</div> |
| 1658 |
<div id="subsection30" class="doctools_subsection"><h3><a name="subsection30">Non-standard compile/link configuration</a></h3> |
| 1659 |
<ol class="doctools_enumerated"> |
| 1660 |
<li><p>See and reread the <span class="sectref"><a href="#section3">basic wrapper package</a></span> for |
| 1661 |
the introduction of the commands referenced below.</p></li> |
| 1662 |
<li><p>Use the command <b class="cmd">critcl::cflags</b> to provide additional, non-standard flags to |
| 1663 |
the compiler.</p> |
| 1664 |
<pre class="doctools_example"> |
| 1665 |
critcl::cflags -DBYTE_ORDER=bigendian |
| 1666 |
</pre> |
| 1667 |
</li> |
| 1668 |
<li><p>Use the command <b class="cmd">critcl::ldflags</b> to provide additional, non-standard flags to |
| 1669 |
the linker.</p> |
| 1670 |
<pre class="doctools_example"> |
| 1671 |
critcl::ldflags - |
| 1672 |
</pre> |
| 1673 |
</li> |
| 1674 |
<li><p>Not answered in the above is how to determine such flags if they are not fixed |
| 1675 |
across machines or platforms.</p> |
| 1676 |
<p>This is addressed by the next section.</p></li> |
| 1677 |
</ol> |
| 1678 |
</div> |
| 1679 |
<div id="subsection31" class="doctools_subsection"><h3><a name="subsection31">Querying the compilation environment</a></h3> |
| 1680 |
<ol class="doctools_enumerated"> |
| 1681 |
<li><p>Use the command <b class="cmd">critcl::check</b> to immediately check if a piece of C code can |
| 1682 |
compiled successfully as a means of querying the compiler configuration itself.</p> |
| 1683 |
<pre class="doctools_example"> |
| 1684 |
if {[critcl::check { |
| 1685 |
#include <FOO.h> |
| 1686 |
}]} { |
| 1687 |
Do stuff with FOO.h present. |
| 1688 |
} else { |
| 1689 |
Do stuff without FOO.h |
| 1690 |
} |
| 1691 |
</pre> |
| 1692 |
<p>All header and library paths which were registered with <i class="term"><a href="critcl.html">CriTcl</a></i> before using |
| 1693 |
<b class="cmd">critcl::check</b> take part in the attempted compilation.</p> |
| 1694 |
<p>Use the package <b class="package"><a href="critcl_util.html">critcl::util</a></b> and various convenience commands it |
| 1695 |
provides.</p></li> |
| 1696 |
<li><p>Use the full <i class="term">Power of Tcl (tm)</i> itself.</p></li> |
| 1697 |
</ol> |
| 1698 |
</div> |
| 1699 |
<div id="subsection32" class="doctools_subsection"><h3><a name="subsection32">Shared C Code</a></h3> |
| 1700 |
<ol class="doctools_enumerated"> |
| 1701 |
<li><p>See and reread the <span class="sectref"><a href="#section3">basic wrapper package</a></span> for |
| 1702 |
the introduction of the commands referenced below.</p></li> |
| 1703 |
<li><p>Use the command <b class="cmd">critcl::ccode</b> to write C code residing outside of <b class="cmd">cproc</b> |
| 1704 |
bodies.</p></li> |
| 1705 |
<li><p>Or, alternatively, place the C code into one or more "<b class="file">.c</b>" files and use the |
| 1706 |
command <b class="cmd">critcl::csources</b> to register them with <i class="term"><a href="critcl.html">CriTcl</a></i> for compilation.</p></li> |
| 1707 |
<li><p>This topic is also treated in section <span class="sectref"><a href="#subsection20">Separating Local C Sources</a></span>.</p></li> |
| 1708 |
</ol> |
| 1709 |
</div> |
| 1710 |
</div> |
| 1711 |
<div id="section4" class="doctools_section"><h2><a name="section4">Various</a></h2> |
| 1712 |
<div id="subsection33" class="doctools_subsection"><h3><a name="subsection33">Author, License, Description, Keywords</a></h3> |
| 1713 |
<ol class="doctools_enumerated"> |
| 1714 |
<li><p>See and reread the <span class="sectref"><a href="#section2">basic package</a></span> for the introduction of the |
| 1715 |
commands referenced below.</p></li> |
| 1716 |
<li><p>Use the command <b class="cmd">critcl::license</b> to set the package license.</p> |
| 1717 |
<p>Use the same command to set the package author.</p> |
| 1718 |
<p>Both arguments are free form text.</p></li> |
| 1719 |
<li><p>Use the command <b class="cmd">critcl::summary</b> to set a short package description.</p></li> |
| 1720 |
<li><p>Use the command <b class="cmd">critcl::description</b> to set a longer package description.</p> |
| 1721 |
<p>The arguments of both commands are free form text.</p></li> |
| 1722 |
<li><p>Use the command <b class="cmd">critcl::subject</b> to set one or more keywords.</p> |
| 1723 |
<p><em>Attention</em> Contrary to the other commands the arguments accumulate.</p></li> |
| 1724 |
<li><p>All the commands are optional.</p></li> |
| 1725 |
<li><p>Their information is not placed into the generated C code.</p></li> |
| 1726 |
<li><p>In <i class="term">package mode</i> the information is placed into the file "<b class="file">teapot.txt</b>" |
| 1727 |
of the generated package.</p></li> |
| 1728 |
<li><p>This file serves as integration point for <i class="term">Teapot</i>, the package system of |
| 1729 |
<i class="term">ActiveTcl</i>.</p></li> |
| 1730 |
</ol> |
| 1731 |
</div> |
| 1732 |
<div id="subsection34" class="doctools_subsection"><h3><a name="subsection34">Get Critcl Application Help</a></h3> |
| 1733 |
<ol class="doctools_enumerated"> |
| 1734 |
<li><p>Invoke the command</p> |
| 1735 |
<pre class="doctools_example"> |
| 1736 |
critcl -help |
| 1737 |
</pre> |
| 1738 |
<p>in a terminal to get help about the <b class="syscmd"><a href="critcl.html">critcl</a></b> application.</p></li> |
| 1739 |
</ol> |
| 1740 |
</div> |
| 1741 |
<div id="subsection35" class="doctools_subsection"><h3><a name="subsection35">Supported Targets & Configurations</a></h3> |
| 1742 |
<ol class="doctools_enumerated"> |
| 1743 |
<li><p>Invoke the application as</p> |
| 1744 |
<pre class="doctools_example"> |
| 1745 |
critcl -show |
| 1746 |
</pre> |
| 1747 |
<p>in a terminal to get the detailed configuration of the target for the current platform.</p></li> |
| 1748 |
<li><p>Invoke the application as</p> |
| 1749 |
<pre class="doctools_example"> |
| 1750 |
critcl -show -target NAME |
| 1751 |
</pre> |
| 1752 |
<p>in a terminal to get the detailed configuration of the named target.</p></li> |
| 1753 |
<li><p>Invoke the application as</p> |
| 1754 |
<pre class="doctools_example"> |
| 1755 |
critcl -targets |
| 1756 |
</pre> |
| 1757 |
<p>in a terminal to get a list of the available targets.</p></li> |
| 1758 |
</ol> |
| 1759 |
</div> |
| 1760 |
<div id="subsection36" class="doctools_subsection"><h3><a name="subsection36">Building A Package</a></h3> |
| 1761 |
<ol class="doctools_enumerated"> |
| 1762 |
<li><p>Start at section <span class="sectref"><a href="#section2">Basics</a></span>.</p></li> |
| 1763 |
</ol> |
| 1764 |
</div> |
| 1765 |
<div id="subsection37" class="doctools_subsection"><h3><a name="subsection37">Building A Package For Debugging</a></h3> |
| 1766 |
<ol class="doctools_enumerated"> |
| 1767 |
<li><p>Start at section <span class="sectref"><a href="#section2">Basics</a></span>.</p></li> |
| 1768 |
</ol> |
| 1769 |
</div> |
| 1770 |
</div> |
| 1771 |
<div id="section5" class="doctools_section"><h2><a name="section5">Authors</a></h2> |
| 1772 |
<p>Jean Claude Wippler, Steve Landers, Andreas Kupries</p> |
| 1773 |
</div> |
| 1774 |
<div id="section6" class="doctools_section"><h2><a name="section6">Bugs, Ideas, Feedback</a></h2> |
| 1775 |
<p>This document, and the package it describes, will undoubtedly contain |
| 1776 |
bugs and other problems. |
| 1777 |
Please report them at <a href="https://github.com/andreas-kupries/critcl/issues">https://github.com/andreas-kupries/critcl/issues</a>. |
| 1778 |
Ideas for enhancements you may have for either package, application, |
| 1779 |
and/or the documentation are also very welcome and should be reported |
| 1780 |
at <a href="https://github.com/andreas-kupries/critcl/issues">https://github.com/andreas-kupries/critcl/issues</a> as well.</p> |
| 1781 |
</div> |
| 1782 |
<div id="keywords" class="doctools_section"><h2><a name="keywords">Keywords</a></h2> |
| 1783 |
<p><a href="../index.html#c_code">C code</a>, <a href="../index.html#embedded_c_code">Embedded C Code</a>, <a href="../index.html#calling_c_code_from_tcl">calling C code from Tcl</a>, <a href="../index.html#code_generator">code generator</a>, <a href="../index.html#compile_run">compile & run</a>, <a href="../index.html#compiler">compiler</a>, <a href="../index.html#dynamic_code_generation">dynamic code generation</a>, <a href="../index.html#dynamic_compilation">dynamic compilation</a>, <a href="../index.html#generate_package">generate package</a>, <a href="../index.html#linker">linker</a>, <a href="../index.html#on_demand_compilation">on demand compilation</a>, <a href="../index.html#on_the_fly_compilation">on-the-fly compilation</a></p> |
| 1784 |
</div> |
| 1785 |
<div id="category" class="doctools_section"><h2><a name="category">Category</a></h2> |
| 1786 |
<p>Glueing/Embedded C code</p> |
| 1787 |
</div> |
| 1788 |
<div id="copyright" class="doctools_section"><h2><a name="copyright">Copyright</a></h2> |
| 1789 |
<p>Copyright © Jean-Claude Wippler<br> |
| 1790 |
Copyright © Steve Landers<br> |
| 1791 |
Copyright © 2011-2024 Andreas Kupries</p> |
| 1792 |
</div> |
| 1793 |
</div></body></html> |