1
0
Fork 0
cl-sites/guile.html_node/Vtables.html

133 lines
6.2 KiB
HTML
Raw Normal View History

2024-12-17 12:49:28 +01:00
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<!-- This manual documents Guile version 3.0.10.
Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
Inc.
Copyright (C) 2021 Maxime Devos
Copyright (C) 2024 Tomas Volf
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
copy of the license is included in the section entitled "GNU Free
Documentation License." -->
<title>Vtables (Guile Reference Manual)</title>
<meta name="description" content="Vtables (Guile Reference Manual)">
<meta name="keywords" content="Vtables (Guile Reference Manual)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content=".texi2any-real">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Structures.html" rel="up" title="Structures">
<link href="Structure-Basics.html" rel="next" title="Structure Basics">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
ul.mark-bullet {list-style-type: disc}
-->
</style>
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
</head>
<body lang="en">
<div class="subsubsection-level-extent" id="Vtables">
<div class="nav-panel">
<p>
Next: <a href="Structure-Basics.html" accesskey="n" rel="next">Structure Basics</a>, Up: <a href="Structures.html" accesskey="u" rel="up">Structures</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsubsection" id="Vtables-1"><span>6.6.18.1 Vtables<a class="copiable-link" href="#Vtables-1"> &para;</a></span></h4>
<p>A vtable is a structure type, specifying its layout, and other
information. A vtable is actually itself a structure, but there&rsquo;s no
need to worry about that initially (see <a class="pxref" href="Vtable-Contents.html">Vtable Contents</a>.)
</p>
<dl class="first-deffn">
<dt class="deffn" id="index-make_002dvtable"><span class="category-def">Scheme Procedure: </span><span><strong class="def-name">make-vtable</strong> <var class="def-var-arguments">fields [print]</var><a class="copiable-link" href="#index-make_002dvtable"> &para;</a></span></dt>
<dd><p>Create a new vtable.
</p>
<p><var class="var">fields</var> is a string describing the fields in the structures to be
created. Each field is represented by two characters, a type letter
and a permissions letter, for example <code class="code">&quot;pw&quot;</code>. The types are as
follows.
</p>
<ul class="itemize mark-bullet">
<li><code class="code">p</code> &ndash; a Scheme value. &ldquo;p&rdquo; stands for &ldquo;protected&rdquo; meaning
it&rsquo;s protected against garbage collection.
</li><li><code class="code">u</code> &ndash; an arbitrary word of data (an <code class="code">scm_t_bits</code>). At the
Scheme level it&rsquo;s read and written as an unsigned integer. &ldquo;u&rdquo; stands
for &ldquo;unboxed&rdquo;, as it&rsquo;s stored as a raw value without additional type
annotations.
</li></ul>
<p>It used to be that the second letter for each field was a permission
code, such as <code class="code">w</code> for writable or <code class="code">r</code> for read-only. However
over time structs have become more of a raw low-level facility; access
control is better implemented as a layer on top. After all,
<code class="code">struct-set!</code> is a cross-cutting operator that can bypass
abstractions made by higher-level record facilities; it&rsquo;s not generally
safe (in the sense of abstraction-preserving) to expose
<code class="code">struct-set!</code> to &ldquo;untrusted&rdquo; code, even if the fields happen to
be writable. Additionally, permission checks added overhead to every
structure access in a way that couldn&rsquo;t be optimized out, hampering the
ability of structs to act as a low-level building block. For all of
these reasons, all fields in Guile structs are now writable; attempting
to make a read-only field will now issue a deprecation warning, and the
field will be writable regardless.
</p>
<div class="example">
<pre class="example-preformatted">(make-vtable &quot;pw&quot;) ;; one scheme field
(make-vtable &quot;pwuwuw&quot;) ;; one scheme and two unboxed fields
</pre></div>
<p>The optional <var class="var">print</var> argument is a function called by
<code class="code">display</code> and <code class="code">write</code> (etc) to give a printed representation
of a structure created from this vtable. It&rsquo;s called
<code class="code">(<var class="var">print</var> struct port)</code> and should look at <var class="var">struct</var> and
write to <var class="var">port</var>. The default print merely gives a form like
&lsquo;<samp class="samp">#&lt;struct ADDR:ADDR&gt;</samp>&rsquo; with a pair of machine addresses.
</p>
<p>The following print function for example shows the two fields of its
structure.
</p>
<div class="example">
<pre class="example-preformatted">(make-vtable &quot;pwpw&quot;
(lambda (struct port)
(format port &quot;#&lt;~a and ~a&gt;&quot;
(struct-ref struct 0)
(struct-ref struct 1))))
</pre></div>
</dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="Structure-Basics.html">Structure Basics</a>, Up: <a href="Structures.html">Structures</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>