Debian Codegraph @

embedded/www/files/critcl_tcl9.html

raw
1 <!DOCTYPE html><html><head>
2 <meta charset="UTF-8">
3 <meta name="viewport" content="width=device-width, initial-scale=1">
4 <title>critcl_tcl9 - 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_tcl9.man' by tcllib/doctools with format 'html'
97 -->
98 <!-- Copyright &amp;copy; Jean-Claude Wippler -- Copyright &amp;copy; Steve Landers -- Copyright &amp;copy; 2011-2024 Andreas Kupries
99 -->
100 <!-- critcl_tcl9.n
101 -->
102 <body><hr> [
103 <a href="../toc.html">Table Of Contents</a>
104 &#124; <a href="../index.html">Keyword Index</a>
105 ] <hr>
106 <div class="doctools">
107 <h1 class="doctools_title">critcl_tcl9(n) 3.3.1 doc &quot;C Runtime In Tcl (CriTcl)&quot;</h1>
108 <div id="name" class="doctools_section"><h2><a name="name">Name</a></h2>
109 <p>critcl_tcl9 - How To Adapt Critcl Packages for Tcl 9</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">Additional References</a></li>
116 <li class="doctools_section"><a href="#section3">Authors</a></li>
117 <li class="doctools_section"><a href="#section4">Bugs, Ideas, Feedback</a></li>
118 <li class="doctools_section"><a href="#keywords">Keywords</a></li>
119 <li class="doctools_section"><a href="#category">Category</a></li>
120 <li class="doctools_section"><a href="#copyright">Copyright</a></li>
121 </ul>
122 </div>
123 <div id="section1" class="doctools_section"><h2><a name="section1">Description</a></h2>
124 <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
125 code from within <a href="http://core.tcl-lang.org/tcl">Tcl</a> scripts.</p>
126 <p>This guide contains notes and actions to take by writers of <i class="term"><a href="critcl.html">CriTcl</a></i>-based
127 packages to make their code workable for both Tcl 8.6 and 9.</p>
128 <ol class="doctools_enumerated">
129 <li><p>Generally, if there is no interest in moving to Tcl 9, i.e. Tcl 8.[456]
130 are the only supported runtimes, then just keep using <i class="term"><a href="critcl.html">CriTcl</a></i> <b class="const">3.2</b>.</p>
131 <p>The remainder of this document can be ignored.</p></li>
132 <li><p>Use <i class="term"><a href="critcl.html">CriTcl</a></i> version 3.3.1 <em>if, and only if</em>
133 Tcl 9 support is wanted.</p>
134 <p>With some work this will then also provide backward compatibility with Tcl 8.6.</p></li>
135 <li><p>Header &quot;<b class="file">tcl.h</b>&quot;</p>
136 <p>Replace any inclusion of Tcl's public &quot;<b class="file">tcl.h</b>&quot; header file in the package's
137 C code with the inclusion of <i class="term"><a href="critcl.html">CriTcl</a></i>'s new header file &quot;<b class="file">tclpre9compat.h</b>&quot;.</p>
138 <p>This includes &quot;<b class="file">tcl.h</b>&quot; and further provides a set of compatibility definitions
139 which make supporting both Tcl 8.6 and Tcl 9 in a single code base easier.</p>
140 <p>The following notes assume that this compatibility layer is in place.</p></li>
141 <li><p><b class="cmd">critcl::tcl</b></p>
142 <p>Before <i class="term"><a href="critcl.html">CriTcl</a></i> 3.3.1 a single default (<b class="const">8.4</b>) was used for
143 the minimum Tcl version, to be overriden by an explicit <b class="cmd">critcl::tcl</b> in the
144 package code.</p>
145 <p>Now the default is dynamic, based on the <em>runtime</em> version, i.e.
146 <b class="cmd">package provide Tcl</b>, <i class="term"><a href="critcl.html">CriTcl</a></i> is run with/on.</p>
147 <p>When running on Tcl 9 the new default is version <b class="const">9</b>, and <b class="const">8.6</b> else.
148 <em>Note</em> how this other default was bumped up from <b class="const">8.4</b>.</p>
149 <p>As a consequence it is possible to</p>
150 <ol class="doctools_enumerated">
151 <li><p>Support just Tcl 8.4+, 8.5+, by having an explicit <b class="cmd">critcl::tcl 8.x</b> in
152 the package code.</p>
153 <p><em>Remember however</em>, it is better to simply stick with
154 <i class="term"><a href="critcl.html">CriTcl</a></i> <b class="const">3.2</b> for this.</p></li>
155 <li><p>Support just Tcl 9 by having an explicit <b class="cmd">critcl::tcl 9</b> in the package code.</p></li>
156 <li><p>Support both Tcl 8.6 and Tcl 9 (but not 8.4/8.5) by leaving <b class="cmd">critcl::tcl</b> out of the code
157 and using the proper <b class="syscmd">tclsh</b> version to run <i class="term"><a href="critcl.html">CriTcl</a></i> with.</p></li>
158 </ol>
159 </li>
160 <li><p>Code checking</p>
161 <p><i class="term"><a href="critcl.html">CriTcl</a></i> 3.3.1 comes with a very basic set of code checks pointing
162 out places where compatibility might or will be an issue.</p>
163 <p>The implementation checks all inlined C code declared by <b class="cmd">critcl::ccode</b>,
164 <b class="cmd">critcl::ccommand</b>, <b class="cmd">critcl::cproc</b> (and related/derived commands), as well
165 as the C companion files declared with <b class="cmd">critcl::csources</b>.</p>
166 <p>It is very basic because it simply greps the code line by line for a number
167 of patterns and reports on their presence. The C code is not fully parsed.
168 The check can and will report pattern found in C code comments, for example.</p>
169 <p>The main patterns deal with functions affected by the change to <b class="type">Tcl_Size</b>,
170 the removal of old-style interpreter state handling, and command creation.</p>
171 <p>A warning message is printed for all detections.</p>
172 <p>This is disabled for the <b class="const">Tcl_Size</b>-related pattern if the line also matches
173 the pattern <b class="const">*OK tcl9*</b>.</p>
174 <p>In this way all places in the code already handled can be marked and excluded
175 from the warnings.</p>
176 <ol class="doctools_enumerated">
177 <li><p>Interpreter State handling</p>
178 <p>Tcl 9 removed the type <b class="type">Tcl_SavedResult</b> and its associated functions
179 <b class="function">Tcl_SaveResult</b>, <b class="function">Tcl_RestoreResult</b>, and <b class="function">Tcl_DiscardResult</b>.</p>
180 <p>When a package uses this type and the related functions a rewrite is necessary.</p>
181 <p>With Tcl 9 use of type <b class="type">Tcl_InterpState</b> and its functions
182 <b class="function">Tcl_SaveInterpState</b>, <b class="function">Tcl_RestoreInterpState</b>, and
183 <b class="function">Tcl_DiscardInterpState</b> is now required.</p>
184 <p>As these were introduced with Tcl 8.5 the rewrite gives us compatibility with
185 Tcl 8.6 for free.</p></li>
186 <li><p><b class="type">Tcl_Size</b></p>
187 <p>One of the main changes introduced with Tcl 9 is the breaking of the 2G barrier
188 for the number of bytes in a string, elements in a list, etc.
189 In a lot of interfaces <b class="type">int</b> was replaced with <b class="type">Tcl_Size</b>, which is
190 effectively <b class="type">ptrdiff_t</b> behind the scenes.</p>
191 <p>The &quot;<b class="file">tclpre9compat.h</b>&quot; header mentioned above provides a suitable definition
192 of <b class="type">Tcl_Size</b> for <b class="const">8.6</b>, i.e. maps it to <b class="type">int</b>.
193 This enables the package code to use <b class="type">Tcl_Size</b> everywhere and still have it
194 work for both Tcl 8.6 and 9.</p>
195 <p>It is of course necessary to rewrite the package code to use <b class="type">Tcl_Size</b>.</p>
196 <p>The checker reports all lines in the C code using a function whose signature
197 was changed to use <b class="type">Tcl_Size</b> over <b class="type">int</b>.</p>
198 <p>Note that it is necessary to manually check the package code for places where
199 a <b class="const">%d</b> text formatting specification should be replaced with
200 <b class="const">TCL_SIZE_FMT</b>.</p>
201 <p>I.e. all places where <b class="type">Tcl_Size</b> values are formatted with <b class="function">printf</b>-style
202 functions a formatting string</p>
203 <pre class="doctools_example">&quot;... %d ...&quot;</pre>
204 <p>has to be replaced with</p>
205 <pre class="doctools_example">&quot;... &quot; TCL_SIZE_FMT &quot; ...&quot;</pre>
206 <p>The macro <b class="cmd">TCL_SIZE_FMT</b> is defined by Critcl's compatibility layer, as an
207 extension of the <b class="cmd">TCL_SIZE_MODIFIER</b> macro which only contains the
208 formatting modifier to insert into a plain <b class="const">%d</b> to handle <b class="type">Tcl_Size</b>
209 values.</p>
210 <p><em>Note</em> how the original formatting string is split into multiple strings.
211 The C compiler will fuse these back together into a single string.</p></li>
212 <li><p>Command creation.</p>
213 <p>This is technically a part of the <b class="type">Tcl_Size</b> changes.</p>
214 <p>All places using <b class="function">Tcl_CreateObjCommand</b> have to be rewritten to use
215 <b class="function">Tcl_CreateObjCommand2</b> instead, and the registered command functions
216 to use <b class="type">Tcl_Size</b> for their <i class="arg">objc</i> argument.</p>
217 <p>The &quot;<b class="file">tclpre9compat.h</b>&quot; header maps this back to the old function
218 when compilation is done against Tcl 8.6.</p>
219 <p><i class="term"><a href="critcl.html">CriTcl</a></i> does this itself for the commands created via <b class="cmd">critcl::ccommand</b>,
220 <b class="cmd">critcl::cproc</b>, and derived places (<b class="package"><a href="critcl_class.html">critcl::class</a></b>).</p></li>
221 <li><p>TIP 494. This TIP adds three semantic constants wrapping <b class="const">-1</b> to Tcl 9 to
222 make the meaning of code clearer. As part of this it also casts the constant to the
223 proper type. They are:</p>
224 <ul class="doctools_itemized">
225 <li><p><b class="const">TCL_IO_FAILURE</b></p></li>
226 <li><p><b class="const">TCL_AUTO_LENGTH</b></p></li>
227 <li><p><b class="const">TCL_INDEX_NONE</b></p></li>
228 </ul>
229 <p>Critcl's compatibility layer provides the same constants to Tcl 8.6.</p>
230 <p>Critcl's new checker highlights places where <b class="const">TCL_AUTO_LENGTH</b> is suitable.</p>
231 <p>Doing this for the other two constants looks to require deeper and proper parsing
232 of C code, which the checker does not do.</p></li>
233 </ol>
234 </li>
235 </ol>
236 </div>
237 <div id="section2" class="doctools_section"><h2><a name="section2">Additional References</a></h2>
238 <ol class="doctools_enumerated">
239 <li><p><a href="https://wiki.tcl-lang.org/page/Porting+extensions+to+Tcl+9">https://wiki.tcl-lang.org/page/Porting+extensions+to+Tcl+9</a></p></li>
240 <li><p><a href="https://wiki.tcl-lang.org/page/Tcl+9+functions+using+Tcl%5FSize">https://wiki.tcl-lang.org/page/Tcl+9+functions+using+Tcl%5FSize</a></p></li>
241 <li><p><a href="https://core.tcl-lang.org/tcl/wiki?name=Migrating%20scripts%20to%20Tcl%209">https://core.tcl-lang.org/tcl/wiki?name=Migrating%20scripts%20to%20Tcl%209</a></p></li>
242 <li><p><a href="https://core.tcl-lang.org/tcl/wiki?name=Migrating%20C%20extensions%20to%20Tcl%209">https://core.tcl-lang.org/tcl/wiki?name=Migrating%20C%20extensions%20to%20Tcl%209</a></p></li>
243 </ol>
244 </div>
245 <div id="section3" class="doctools_section"><h2><a name="section3">Authors</a></h2>
246 <p>Jean Claude Wippler, Steve Landers, Andreas Kupries</p>
247 </div>
248 <div id="section4" class="doctools_section"><h2><a name="section4">Bugs, Ideas, Feedback</a></h2>
249 <p>This document, and the package it describes, will undoubtedly contain
250 bugs and other problems.
251 Please report them at <a href="https://github.com/andreas-kupries/critcl/issues">https://github.com/andreas-kupries/critcl/issues</a>.
252 Ideas for enhancements you may have for either package, application,
253 and/or the documentation are also very welcome and should be reported
254 at <a href="https://github.com/andreas-kupries/critcl/issues">https://github.com/andreas-kupries/critcl/issues</a> as well.</p>
255 </div>
256 <div id="keywords" class="doctools_section"><h2><a name="keywords">Keywords</a></h2>
257 <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 &amp; 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>
258 </div>
259 <div id="category" class="doctools_section"><h2><a name="category">Category</a></h2>
260 <p>Glueing/Embedded C code</p>
261 </div>
262 <div id="copyright" class="doctools_section"><h2><a name="copyright">Copyright</a></h2>
263 <p>Copyright &copy; Jean-Claude Wippler<br>
264 Copyright &copy; Steve Landers<br>
265 Copyright &copy; 2011-2024 Andreas Kupries</p>
266 </div>
267 </div></body></html>