raz / commit 1ea1e717

commit
1ea1e717cf450353595bc598205349da09cc73e5
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
ae1e08ef
site: the tours for v0.16.0

Records: a constant typed []Planet takes its literal's length.
Collections: Map.done and Vec's done deferred; Str.free with Str.copy.
Files: Files.load says why. Processes: signals, catch and handle.

patch · log

9 files changed, +63 -8

site/content/advanced/03-collections.txt (history) modified +3 -1

@@ -121,6 +121,7 @@
121121 w, rest: []U8; key: []U8; v: INT; all: []Entry;
122122 begin
123123 Count.init(ADR(m), Heap.heap);
124+ defer Count.done(ADR(m));
124125 w, rest := Str.field(Text);
125126 while LEN(w) > 0 do
126127 k, _ := Count.get(ADR(m), w);
@@ -128,6 +129,7 @@
128129 w, rest := Str.field(rest);
129130 end;
130131 Entries.init(ADR(entries), Heap.heap);
132+ defer Entries.done(ADR(entries));
131133 found, key, v := Count.next(ADR(m), 0);
132134 while found >= 0 do
133135 Entries.push(ADR(entries), {word: key, n: v});
@@ -143,7 +145,7 @@
143145 end Freq.
144146 ```
145147
146-The keys here are slices of the constant text, so they outlive the map; a program whose keys come from a buffer it will reuse copies them first with `Str.copy`.
148+The keys here are slices of the constant text, so they outlive the map; a program whose keys come from a buffer it will reuse copies them first with `Str.copy`, and gives each back with `Str.free` before `Count.done` gives back the map's own memory.
147149
148150 ## Choosing
149151

site/content/advanced/04-files.txt (history) modified +3 -0

@@ -25,10 +25,13 @@
2525 end;
2626 ok := Files.read("missing.txt", ADR(b));
2727 if not ok then Out.str("missing.txt: not there\n"); end;
28+ if Files.load("missing.txt", ADR(b)) = -2 then Out.str("missing.txt: ENOENT\n"); end;
2829 end;
2930
3031 end Notes.
3132 ```
33+
34+When the reason matters, `Files.load` says it: 0, or minus the errno of what failed, -2 (ENOENT) for no such file, -13 (EACCES) for one not allowed.
3235
3336 ## Descriptors
3437

site/content/advanced/05-processes.txt (history) modified +24 -1

@@ -1,6 +1,6 @@
11 title: Time, randomness and other programs
22 part: The library
3-summary: Clocks, sleeping, random bytes, and running a program with its output piped back.
3+summary: Clocks, sleeping, random bytes, running a program with its output piped back, and signals.
44 ---
55 Everything here is in `Sys`, and is the same procedures on every target raz supports: through system calls on Linux, through the C library elsewhere.
66
@@ -153,4 +153,27 @@
153153 end;
154154
155155 end Where.
156+```
157+
158+## Signals
159+
160+A signal, Ctrl-C's `sigInt` or `sigTerm` from `kill`, ends a program unless it says otherwise. `Sys.catch` takes signals away from what they would do and gives an fd that is ready to read when one comes, so a program that waits on fds anyway, with `Sys.readyOf`, waits for signals the same way; `Sys.caught` gives each in turn. `Sys.handle` is for a program that waits inside a system call, as a shell waits for its program: the signal is noted for `Sys.came`, and the call it comes during fails with -4 (EINTR) rather than going on. Nothing of the program runs when the signal comes; it looks after the call returns.
161+
162+```raz Signals run
163+module Signals;
164+import Out, Sys;
165+
166+proc main*;
167+var fd: INT;
168+begin
169+ fd := Sys.catch([Sys.sigUsr1]);
170+ _ := Sys.kill(Sys.pid(), Sys.sigUsr1);
171+ if Sys.ready(fd, 1000) and (Sys.caught(fd) = Sys.sigUsr1) then Out.str("caught sigUsr1\n"); end;
172+ _ := Sys.handle(Sys.sigUsr2);
173+ _ := Sys.kill(Sys.pid(), Sys.sigUsr2);
174+ if Sys.came(Sys.sigUsr2) then Out.str("sigUsr2 came\n"); end;
175+ if not Sys.came(Sys.sigUsr2) then Out.str("and was seen once\n"); end;
176+end;
177+
178+end Signals.
156179 ```

site/content/tour/07-records.txt (history) modified +2 -2

@@ -51,7 +51,7 @@
5151
5252 type Planet = record name: []U8; moons: INT; water: BOOL; end;
5353
54-const Inner: [4]Planet = [
54+const Inner: []Planet = [
5555 {name: "Mercury"},
5656 {name: "Venus"},
5757 {name: "Earth", moons: 1, water: true},
@@ -70,7 +70,7 @@
7070 end Planets.
7171 ```
7272
73-Fields left out are zero, as in any literal: Mercury's `moons` is 0 and its `water` is `false`.
73+Fields left out are zero, as in any literal: Mercury's `moons` is 0 and its `water` is `false`. A constant typed `[]Planet` takes its length from its literal, here 4, so a planet added to the list needs no count changed.
7474
7575 ## Layout
7676

site/public/advanced/collections.html (history) modified +3 -1

@@ -152,6 +152,7 @@
152152 w, rest: []<span class="ty">U8</span>; key: []<span class="ty">U8</span>; v: <span class="ty">INT</span>; all: []Entry;
153153 <span class="kw">begin</span>
154154 Count.init(<span class="ty">ADR</span>(m), Heap.heap);
155+ <span class="kw">defer</span> Count.done(<span class="ty">ADR</span>(m));
155156 w, rest := Str.field(Text);
156157 <span class="kw">while</span> <span class="ty">LEN</span>(w) &gt; <span class="number">0</span> <span class="kw">do</span>
157158 k, _ := Count.get(<span class="ty">ADR</span>(m), w);
@@ -159,6 +160,7 @@
159160 w, rest := Str.field(rest);
160161 <span class="kw">end</span>;
161162 Entries.init(<span class="ty">ADR</span>(entries), Heap.heap);
163+ <span class="kw">defer</span> Entries.done(<span class="ty">ADR</span>(entries));
162164 found, key, v := Count.next(<span class="ty">ADR</span>(m), <span class="number">0</span>);
163165 <span class="kw">while</span> found &gt;= <span class="number">0</span> <span class="kw">do</span>
164166 Entries.push(<span class="ty">ADR</span>(entries), {word: key, n: v});
@@ -181,7 +183,7 @@
181183 1 mat
182184 1 on
183185 </code></pre>
184-<p>The keys here are slices of the constant text, so they outlive the map; a program whose keys come from a buffer it will reuse copies them first with <code>Str.copy</code>.</p>
186+<p>The keys here are slices of the constant text, so they outlive the map; a program whose keys come from a buffer it will reuse copies them first with <code>Str.copy</code>, and gives each back with <code>Str.free</code> before <code>Count.done</code> gives back the map's own memory.</p>
185187 <h2>Choosing</h2>
186188 <p>Use a <code>List</code> when you push and pop at one end and walk it in order. Use a <code>Vec</code> when you add at the end and index, and don't know how many will come; an array when you do. Use a <code>Map</code> when you look things up by key. Use <code>Sort</code> on either's slice when you want an order. When none of them fits, they are each under a hundred lines of raz in <code>lib/</code>, and the advanced tour's allocator lesson shows what is underneath.</p><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="bytes.html">&larr; Strings and bytes</a><a href="index.html">Contents</a><a class="next" href="files.html">Files and the command line &rarr;</a></div></article></div>
187189 </main>

site/public/advanced/files.html (history) modified +3 -0

@@ -63,13 +63,16 @@
6363 <span class="kw">end</span>;
6464 ok := Files.read(<span class="string">&quot;missing.txt&quot;</span>, <span class="ty">ADR</span>(b));
6565 <span class="kw">if</span> <span class="kw">not</span> ok <span class="kw">then</span> Out.str(<span class="string">&quot;missing.txt: not there\n&quot;</span>); <span class="kw">end</span>;
66+ <span class="kw">if</span> Files.load(<span class="string">&quot;missing.txt&quot;</span>, <span class="ty">ADR</span>(b)) = -<span class="number">2</span> <span class="kw">then</span> Out.str(<span class="string">&quot;missing.txt: ENOENT\n&quot;</span>); <span class="kw">end</span>;
6667 <span class="kw">end</span>;
6768
6869 <span class="kw">end</span> Notes.</code></pre>
6970 <pre class="output"><code>23 bytes: first line
7071 second line
7172 missing.txt: not there
73+missing.txt: ENOENT
7274 </code></pre>
75+<p>When the reason matters, <code>Files.load</code> says it: 0, or minus the errno of what failed, -2 (ENOENT) for no such file, -13 (EACCES) for one not allowed.</p>
7376 <h2>Descriptors</h2>
7477 <p>Underneath, a file is a descriptor, an <code>INT</code>. <code>Sys.open</code> takes the path and the way to open it as a set; <code>read</code>, <code>write</code>, <code>seek</code> and <code>close</code> do what they say. A result that can fail is a number, and minus an errno if it failed, as on Linux itself. Reading in a loop is the pattern for anything too big to hold at once.</p>
7578 <div class="file"><span>Count.raz</span></div><pre class="code"><code><span class="kw">module</span> Count;

site/public/advanced/index.html (history) modified +1 -1

@@ -33,7 +33,7 @@
3333 <li><a href="bytes.html">Strings and bytes</a> <span>Copying, hashing and ordering strings, searching bytes many at a time, and reading and writing binary formats.</span></li>
3434 <li><a href="collections.html">Lists, arrays, maps and sorting</a> <span>List(T), Vec(T), Map(K, V) and Sort(T), put together in a word-frequency program.</span></li>
3535 <li><a href="files.html">Files and the command line</a> <span>Whole files with Files, descriptors and directories with Sys, and options with Opt.</span></li>
36-<li><a href="processes.html">Time, randomness and other programs</a> <span>Clocks, sleeping, random bytes, and running a program with its output piped back.</span></li>
36+<li><a href="processes.html">Time, randomness and other programs</a> <span>Clocks, sleeping, random bytes, running a program with its output piped back, and signals.</span></li>
3737 <li><a href="allocators.html">Writing an allocator</a> <span>Mem.Allocator is two procedures; make one over a fixed array, and one that counts.</span></li>
3838 </ol>
3939 <h2>Standing alone</h2><ol start='7'>

site/public/advanced/processes.html (history) modified +22 -0

@@ -184,6 +184,28 @@
184184 <span class="kw">end</span> Where.</code></pre>
185185 <pre class="output"><code>unset reads as empty
186186 hello from /tmp
187+</code></pre>
188+<h2>Signals</h2>
189+<p>A signal, Ctrl-C's <code>sigInt</code> or <code>sigTerm</code> from <code>kill</code>, ends a program unless it says otherwise. <code>Sys.catch</code> takes signals away from what they would do and gives an fd that is ready to read when one comes, so a program that waits on fds anyway, with <code>Sys.readyOf</code>, waits for signals the same way; <code>Sys.caught</code> gives each in turn. <code>Sys.handle</code> is for a program that waits inside a system call, as a shell waits for its program: the signal is noted for <code>Sys.came</code>, and the call it comes during fails with -4 (EINTR) rather than going on. Nothing of the program runs when the signal comes; it looks after the call returns.</p>
190+<div class="file"><span>Signals.raz</span></div><pre class="code"><code><span class="kw">module</span> Signals;
191+<span class="kw">import</span> Out, Sys;
192+
193+<span class="kw">proc</span> main*;
194+<span class="kw">var</span> fd: <span class="ty">INT</span>;
195+<span class="kw">begin</span>
196+ fd := Sys.catch([Sys.sigUsr1]);
197+ _ := Sys.kill(Sys.pid(), Sys.sigUsr1);
198+ <span class="kw">if</span> Sys.ready(fd, <span class="number">1000</span>) <span class="kw">and</span> (Sys.caught(fd) = Sys.sigUsr1) <span class="kw">then</span> Out.str(<span class="string">&quot;caught sigUsr1\n&quot;</span>); <span class="kw">end</span>;
199+ _ := Sys.handle(Sys.sigUsr2);
200+ _ := Sys.kill(Sys.pid(), Sys.sigUsr2);
201+ <span class="kw">if</span> Sys.came(Sys.sigUsr2) <span class="kw">then</span> Out.str(<span class="string">&quot;sigUsr2 came\n&quot;</span>); <span class="kw">end</span>;
202+ <span class="kw">if</span> <span class="kw">not</span> Sys.came(Sys.sigUsr2) <span class="kw">then</span> Out.str(<span class="string">&quot;and was seen once\n&quot;</span>); <span class="kw">end</span>;
203+<span class="kw">end</span>;
204+
205+<span class="kw">end</span> Signals.</code></pre>
206+<pre class="output"><code>caught sigUsr1
207+sigUsr2 came
208+and was seen once
187209 </code></pre><p class="tryit">Save a program under its module&rsquo;s name and run it with <code>raz run Name.raz</code>.</p><div class="pager"><a href="files.html">&larr; Files and the command line</a><a href="index.html">Contents</a><a class="next" href="allocators.html">Writing an allocator &rarr;</a></div></article></div>
188210 </main>
189211 <footer>raz is under the ISC license.</footer>

site/public/tour/records.html (history) modified +2 -2

@@ -114,7 +114,7 @@
114114
115115 <span class="kw">type</span> Planet = <span class="kw">record</span> name: []<span class="ty">U8</span>; moons: <span class="ty">INT</span>; water: <span class="ty">BOOL</span>; <span class="kw">end</span>;
116116
117-<span class="kw">const</span> Inner: [<span class="number">4</span>]Planet = [
117+<span class="kw">const</span> Inner: []Planet = [
118118 {name: <span class="string">&quot;Mercury&quot;</span>},
119119 {name: <span class="string">&quot;Venus&quot;</span>},
120120 {name: <span class="string">&quot;Earth&quot;</span>, moons: <span class="number">1</span>, water: <span class="ty">true</span>},
@@ -135,7 +135,7 @@
135135 Venus has no moon
136136 3 moons in all
137137 </code></pre>
138-<p>Fields left out are zero, as in any literal: Mercury's <code>moons</code> is 0 and its <code>water</code> is <code>false</code>.</p>
138+<p>Fields left out are zero, as in any literal: Mercury's <code>moons</code> is 0 and its <code>water</code> is <code>false</code>. A constant typed <code>[]Planet</code> takes its length from its literal, here 4, so a planet added to the list needs no count changed.</p>
139139 <h2>Layout</h2>
140140 <p>A record's layout is C's: each field at the next multiple of its own alignment, and the whole padded to a multiple of its largest alignment. <code>SIZE(T)</code> says how many bytes. A <code>packed record</code> has no padding, which is what a file format or a wire protocol wants.</p>
141141 <div class="file"><span>Layout.raz</span></div><pre class="code"><code><span class="kw">module</span> Layout;