-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
312 lines (302 loc) · 17.3 KB
/
Copy pathindex.html
File metadata and controls
312 lines (302 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<!doctype html>
<html lang="en" data-theme="dark">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Bytecode · SxfeScript</title>
<meta name="description" content="Compiling to .sxbc, the compile cache, the measured gains, and the trust boundary.">
<link rel="icon" type="image/png" href="../../logo.png">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;500;600&family=Newsreader:opsz,wght@6..72,400;6..72,500;6..72,600&family=Public+Sans:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="../../site.css">
<style>
.docs-layout {
display: grid; grid-template-columns: 232px minmax(0, 1fr);
gap: 52px; align-items: start; padding: 34px 0 0;
}
@media (max-width: 900px) { .docs-layout { grid-template-columns: 1fr; gap: 24px; } }
/* ---- sidebar ---- */
.sidebar { position: sticky; top: 78px; max-height: calc(100vh - 100px); overflow-y: auto; }
@media (max-width: 900px) {
.sidebar { position: static; max-height: none; border-bottom: 1px solid var(--rule-soft); padding-bottom: 18px; }
}
.sidebar h4 {
font-family: "JetBrains Mono", monospace; font-size: 10.5px; letter-spacing: .12em;
text-transform: uppercase; color: var(--ink-soft); margin: 22px 0 8px; font-weight: 600;
}
.sidebar h4:first-child { margin-top: 0; }
.sidebar ul { list-style: none; margin: 0; padding: 0; }
.sidebar li a {
display: block; padding: 5px 10px; margin-left: -10px; border-radius: 6px;
color: var(--ink-mid); text-decoration: none; font-size: 14px; line-height: 1.45;
}
.sidebar li a:hover { color: var(--ink); background: var(--card-2); }
.sidebar li a.current { color: var(--accent); background: var(--accent-wash); font-weight: 600; }
/* ---- rendered markdown ---- */
.doc { min-width: 0; padding-bottom: 40px; }
.doc > h1:first-child { margin-top: 0; }
.doc h1 { font-size: clamp(30px, 4vw, 40px); font-weight: 600; margin: 0 0 20px; }
.doc h2 {
font-size: 25px; font-weight: 600; margin: 46px 0 12px;
padding-top: 18px; border-top: 1px solid var(--rule-soft);
}
.doc h3 { font-size: 19px; font-weight: 600; margin: 30px 0 8px; }
.doc h4 { font-size: 16px; font-weight: 700; margin: 22px 0 6px; font-family: "Public Sans", sans-serif; }
.doc p { color: var(--ink-mid); margin: 0 0 15px; }
.doc li { color: var(--ink-mid); margin: 4px 0; }
.doc ul, .doc ol { padding-left: 22px; margin: 0 0 15px; }
.doc li > p { margin: 0 0 6px; }
.doc strong { color: var(--ink); font-weight: 600; }
.doc a { text-decoration-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.doc blockquote {
margin: 0 0 15px; padding: 2px 0 2px 18px; border-left: 3px solid var(--rule);
color: var(--ink-soft);
}
.doc hr { border: 0; border-top: 1px solid var(--rule-soft); margin: 34px 0; }
/* Room on the right for the copy button, so a long line scrolls under
nothing but empty padding. */
.doc pre { position: relative; margin: 0 0 18px; padding-right: 52px; }
/* A highlighted fence arrives as .highlight > pre with the tokens as bare
spans and no <code>, so the code-sized type has to be set on the pre. */
.doc .highlight { margin: 0 0 18px; }
.doc .highlight pre { margin: 0; font-size: 13.5px; line-height: 1.6; }
/* A fence with no language is a command or a program's output rather than
source; the renderer leaves those bare, while highlighted code arrives
wrapped in .highlight. Flatten them so the two read differently. */
.doc > pre, .doc li > pre { background: var(--card); box-shadow: none; }
.doc > pre code, .doc li > pre code { color: var(--ink-mid); }
.doc img { max-width: 100%; }
/* Tables come out of the renderer wrapped in this element, which has no
display of its own, so it has to be told to be a scroll container --
otherwise a wide table pushes the whole page sideways. */
.doc .table-scroll {
display: block; overflow-x: auto; margin: 0 0 18px;
border: 1px solid var(--rule); border-radius: 8px; background: var(--card);
}
.doc table { border-collapse: collapse; width: 100%; }
.doc th, .doc td {
text-align: left; padding: 10px 15px; border-bottom: 1px solid var(--rule-soft);
vertical-align: top; font-size: 14px; color: var(--ink-mid);
}
.doc thead th {
font-family: "JetBrains Mono", monospace; font-size: 10.5px; letter-spacing: .09em;
text-transform: uppercase; color: var(--ink-mid); font-weight: 600;
background: var(--card-2); border-bottom: 2px solid var(--rule); white-space: nowrap;
}
.doc tbody tr:last-child td { border-bottom: 0; }
.doc td code { background: none; border: 0; padding: 0; }
/* Anchor links on headings: present for linking, invisible until hovered. */
.doc .anchor {
float: left; margin-left: -22px; padding-right: 8px; text-decoration: none;
color: var(--ink-soft); opacity: 0; font-weight: 400;
}
.doc h2:hover .anchor, .doc h3:hover .anchor, .doc .anchor:focus { opacity: 1; }
/* ---- prev/next ---- */
.doc-nav { display: flex; justify-content: space-between; gap: 14px; margin-top: 46px; flex-wrap: wrap; }
.doc-nav a {
flex: 1 1 220px; border: 1px solid var(--rule); border-radius: 10px; padding: 13px 16px;
background: var(--card); text-decoration: none; color: var(--ink); font-weight: 600; font-size: 14.5px;
}
.doc-nav a:hover { border-color: color-mix(in srgb, var(--accent) 40%, var(--rule)); }
.doc-nav a span {
display: block; font-family: "JetBrains Mono", monospace; font-size: 10.5px;
letter-spacing: .1em; text-transform: uppercase; color: var(--ink-soft);
font-weight: 600; margin-bottom: 3px;
}
.doc-nav a.next { text-align: right; }
.doc-source { margin-top: 26px; font-size: 13px; color: var(--ink-soft); }
</style>
</head>
<body>
<nav class="top">
<div class="row">
<a class="wordmark" href="../../"><img src="../../logo.png" alt="" width="30" height="30"><span>Sxfe<span class="wm-accent">Script</span></span></a>
<ul>
<li><a class="navlink current" href="../../docs/">Docs</a></li>
<li><a class="navlink" href="../../#vs-typescript">vs TypeScript</a></li>
<li><a class="navlink" href="../../#arcsx">ArcSX runtime</a></li>
<li><a class="navlink" href="../../#contributing">Contributing</a></li>
</ul>
<a class="gh-link" href="https://github.com/SxfeScript/sxfescript">GitHub</a>
</div>
</nav>
<div class="wrap">
<div class="docs-layout">
<aside class="sidebar">
<h4>Get started</h4>
<ul>
<li><a href="../../docs/quickstart/">Quick start</a></li>
<li><a href="../../docs/examples/">Examples</a></li>
<li><a href="../../docs/cli/">CLI reference</a></li>
</ul>
<h4>The language</h4>
<ul>
<li><a href="../../docs/language/">Language contract</a></li>
<li><a href="../../docs/abi/">ABI</a></li>
<li><a class="current" href="../../docs/bytecode/">Bytecode</a></li>
</ul>
<h4>The runtime</h4>
<ul>
<li><a href="../../docs/runtime/">Runtime surface</a></li>
<li><a href="../../docs/node/">Node compatibility</a></li>
<li><a href="../../docs/native/">Native code</a></li>
<li><a href="../../docs/network/">Networking</a></li>
</ul>
<h4>Project</h4>
<ul>
<li><a href="../../docs/performance/">Performance</a></li>
<li><a href="../../docs/benchmark-references/">Benchmark references</a></li>
<li><a href="../../docs/implementation/">Implementation ledger</a></li>
<li><a href="../../docs/contributing/">Contributing</a></li>
<li><a href="../../docs/readme/">Project README</a></li>
</ul>
</aside>
<article class="doc">
<div class="markdown-heading"><h1 class="heading-element">Precompiled bytecode: <code>.sxbc</code>
</h1><a id="user-content-precompiled-bytecode-sxbc" class="anchor" aria-label="Permalink: Precompiled bytecode: .sxbc" href="#precompiled-bytecode-sxbc"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p><code>sxn</code> can skip parsing a file entirely and run its already-compiled bytecode
instead. This exists for two different reasons that happen to share one
mechanism:</p>
<ul>
<li>
<strong>Distribution.</strong> <code>sxn compile app.sx</code> produces <code>app.sxbc</code>; ship that
instead of the source and there is nothing left to parse on the machine
that finally runs it. <code>--strip</code> drops the compiling machine's own file
paths from the output, for when the source shouldn't be reconstructible
from a stack trace.</li>
<li>
<strong>Startup, on a large file.</strong> <code>sxn --compile-cache app.sx</code> compiles once,
caches the result next to the source, and reuses it on every later launch
until the source changes. This is the same idea already applied to this
runtime's own bootstrap (see the README's benchmark section), turned into
something a user's own script can opt into.</li>
</ul>
<p>Both produce and consume the same file format, so <code>sxn app.sxbc</code> runs either
one's output directly.</p>
<div class="markdown-heading"><h2 id="is-it-worth-it-for-your-script" class="heading-element"><a class="anchor" href="#is-it-worth-it-for-your-script" aria-hidden="true">#</a>Is it worth it for your script?</h2><a id="user-content-is-it-worth-it-for-your-script" class="anchor" aria-label="Permalink: Is it worth it for your script?" href="#is-it-worth-it-for-your-script"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p>Measured on this runtime's own hardware (Apple M4, Release build), median of
9 runs, whole-process wall clock including startup:</p>
<div class="table-scroll"><table>
<thead>
<tr>
<th>Script</th>
<th align="right">From source</th>
<th align="right">From bytecode</th>
<th align="right">Saved</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>console.log("hi")</code></td>
<td align="right">7.8 ms</td>
<td align="right">6.9 ms</td>
<td align="right">12%</td>
</tr>
<tr>
<td>32k-line generated file (618 KB)</td>
<td align="right">15.2 ms</td>
<td align="right">9.6 ms</td>
<td align="right">37%</td>
</tr>
</tbody>
</table></div>
<p>Skipping the parse always saves something, because there is always a parse to
skip — but it scales with how much there is to parse. A one-line script gets
a small, real win from skipping the tokenizer and AST setup entirely. A
large generated file, a bundled app, or a big TypeScript-emitted script gets
a large one. <code>--compile-cache</code> is the flag to reach for once a script is big
enough, or launched often enough, that the difference shows up in something
you're measuring; for a small script run once, it's not going to move
anything you'd notice.</p>
<div class="markdown-heading"><h2 id="sxn-compile-file--o-outsxbc---strip" class="heading-element"><a class="anchor" href="#sxn-compile-file--o-outsxbc---strip" aria-hidden="true">#</a><code>sxn compile <file> [-o out.sxbc] [--strip]</code></h2><a id="user-content-sxn-compile-file--o-outsxbc---strip" class="anchor" aria-label="Permalink: sxn compile <file> [-o out.sxbc] [--strip]" href="#sxn-compile-file--o-outsxbc---strip"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<div class="highlight highlight-source-shell"><pre>sxn compile app.sx <span class="pl-c"><span class="pl-c">#</span> writes app.sxbc next to it</span>
sxn compile app.sx -o dist/app.sxbc
sxn compile app.sx --strip -o dist/app.sxbc <span class="pl-c"><span class="pl-c">#</span> no local paths in the output</span></pre></div>
<p>Works on anything <code>sxn</code> can run as an entry point: <code>.sx</code>, <code>.ts</code>, <code>.js</code>,
<code>.mjs</code>, <code>.cjs</code>, module or CommonJS, decided the same way running it directly
would decide (<code>spec/NODE.md</code>). The output name defaults to the input's name
with its extension replaced by <code>.sxbc</code>.</p>
<p><code>--strip</code> removes line-number and local-variable debug tables (so a stripped
error reports a bytecode offset, not a source line) and, separately, embeds
the source's bare filename instead of its full path at compile time, so
nothing about the machine or directory the source lived in survives into the
shipped file. Verify what you're about to ship with <code>strings out.sxbc</code> if
that matters to you.</p>
<div class="markdown-heading"><h2 id="sxn---compile-cache-file-args" class="heading-element"><a class="anchor" href="#sxn---compile-cache-file-args" aria-hidden="true">#</a><code>sxn --compile-cache <file> [args...]</code></h2><a id="user-content-sxn---compile-cache-file-args" class="anchor" aria-label="Permalink: sxn --compile-cache <file> [args...]" href="#sxn---compile-cache-file-args"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p>Runs <code>file</code> exactly as <code>sxn file</code> would, except: before running, it checks
for an <code>.sxbc</code> cache next to the source. If the cache is missing or older
than the source (by mtime), it compiles fresh and writes the cache; either
way, execution then runs from bytecode. A script invoked repeatedly parses
once, not on every launch — the common case for a CLI tool people run
often, or a dev server that restarts on every save without its own source
having changed on most of those restarts.</p>
<p>The cache is invisible to the script itself: <code>process.argv</code> and <code>__filename</code>
still show the original source path, not the internal <code>.sxbc</code> file.</p>
<div class="markdown-heading"><h2 id="sxn-appsxbc" class="heading-element"><a class="anchor" href="#sxn-appsxbc" aria-hidden="true">#</a><code>sxn app.sxbc</code></h2><a id="user-content-sxn-appsxbc" class="anchor" aria-label="Permalink: sxn app.sxbc" href="#sxn-appsxbc"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p>Runs a <code>.sxbc</code> file directly, as if it were the source it was compiled from.
<code>require()</code> and <code>import</code> inside it resolve normally, against the directory
the <code>.sxbc</code> file itself sits in.</p>
<div class="markdown-heading"><h2 id="format-and-limits" class="heading-element"><a class="anchor" href="#format-and-limits" aria-hidden="true">#</a>Format and limits</h2><a id="user-content-format-and-limits" class="anchor" aria-label="Permalink: Format and limits" href="#format-and-limits"><span aria-hidden="true" class="octicon octicon-link"></span></a></div>
<p>A <code>.sxbc</code> file is 5 bytes of header — a magic number this runtime checks
before trusting the rest as bytecode, so a corrupt or foreign file fails with
a clear message rather than a confusing one from deep inside the engine —
followed by QuickJS's own serialized bytecode for either a compiled module or
a compiled CommonJS wrapper function. The format is tied to this runtime's
exact build (the same <code>BC_VERSION</code> dependency the lazily-loaded builtins
have, <code>spec/IMPLEMENTATION.md</code>): a <code>.sxbc</code> compiled by one version of <code>sxn</code>
is not guaranteed to load in another, and a version mismatch is reported
rather than misread.</p>
<p><strong>Only compile trusted code.</strong> <code>JS_ReadObject</code> with bytecode enabled is, by
QuickJS's own documentation, not a safe format to parse untrusted input —
unlike source text, a crafted bytecode blob can misdirect the interpreter
directly. Compile your own code, or code you already trust as source; don't
treat a <code>.sxbc</code> from an untrusted party as safer to run than the
<code>.js</code>/<code>.mjs</code>/<code>.cjs</code> it might have come from.</p>
<p>A <code>.sxbc</code>'s dependencies (whatever it <code>import</code>s or <code>require</code>s) still resolve
and load as ordinary source at run time — compiling one file does not pull
its dependency tree into the same blob. Compiling a whole app ahead of time
currently means compiling each of its own files individually; there is no
bundler step here.</p>
<div class="doc-nav">
<a class="prev" href="../../docs/abi/"><span>Previous</span>ABI</a>
<a class="next" href="../../docs/runtime/"><span>Next</span>Runtime surface</a>
</div>
<p class="doc-source">This page is generated from
<a href="https://github.com/SxfeScript/sxfescript/blob/main/spec/BYTECODE.md"><code>spec/BYTECODE.md</code></a>.
Machine-readable copies of the whole set:
<a href="../../llms.txt">llms.txt</a>,
<a href="../../llms-full.txt">llms-full.txt</a>.</p>
</article>
</div>
<footer>
<div class="row">
<p>SxfeScript / ArcSX</p>
<div class="links">
<a href="../../">Home</a>
<a href="../../docs/">Docs</a>
<a href="https://github.com/SxfeScript/sxfescript">Source</a>
<a href="https://github.com/SxfeScript/sxfescript/issues">Issues</a>
</div>
</div>
</footer>
</div>
<script>
(function () {
document.querySelectorAll(".doc pre").forEach(function (pre) {
var btn = document.createElement("button");
btn.type = "button";
btn.className = "copy-btn";
btn.setAttribute("aria-label", "Copy code");
btn.addEventListener("click", function () {
navigator.clipboard.writeText(pre.textContent).then(function () {
btn.classList.add("copied");
setTimeout(function () { btn.classList.remove("copied"); }, 1800);
});
});
pre.appendChild(btn);
});
})();
</script>
</body>
</html>