@typesol/cli 0.50.0 → 0.51.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +144 -0
- package/README.md +12 -10
- package/SECURITY.md +5 -3
- package/dist/cli.js +36 -5
- package/dist/compiler/analyze.d.ts +8 -1
- package/dist/compiler/analyze.js +227 -19
- package/dist/compiler/diagnostics.d.ts +4 -0
- package/dist/compiler/diagnostics.js +5 -1
- package/dist/compiler/emit/base.d.ts +31 -1
- package/dist/compiler/emit/base.js +37 -1
- package/dist/compiler/emit/calls.js +27 -6
- package/dist/compiler/emit/cpi-core.d.ts +3 -0
- package/dist/compiler/emit/cpi-core.js +8 -2
- package/dist/compiler/emit/cpi.js +4 -2
- package/dist/compiler/emit/create.js +9 -9
- package/dist/compiler/emit/expressions.d.ts +8 -1
- package/dist/compiler/emit/expressions.js +65 -0
- package/dist/compiler/emit/function.d.ts +2 -0
- package/dist/compiler/emit/function.js +59 -7
- package/dist/compiler/emit/outline.d.ts +35 -0
- package/dist/compiler/emit/outline.js +547 -0
- package/dist/compiler/emit/program.js +11 -2
- package/dist/compiler/emit/shared.d.ts +57 -2
- package/dist/compiler/emit/shared.js +18 -3
- package/dist/compiler/emit/statements.d.ts +28 -1
- package/dist/compiler/emit/statements.js +279 -24
- package/dist/compiler/emit/storage.d.ts +16 -0
- package/dist/compiler/emit/storage.js +122 -3
- package/dist/compiler/hints.d.ts +1 -1
- package/dist/compiler/hints.js +9 -1
- package/dist/compiler/idl.js +1 -1
- package/dist/compiler/index.d.ts +15 -1
- package/dist/compiler/index.js +47 -10
- package/dist/compiler/model.d.ts +13 -1
- package/dist/compiler/stack.d.ts +71 -0
- package/dist/compiler/stack.js +309 -0
- package/dist/compiler/toolchain.d.ts +87 -5
- package/dist/compiler/toolchain.js +384 -19
- package/dist/workspace.d.ts +1 -0
- package/dist/workspace.js +1 -1
- package/docker/deps/0.51.0.lock.json +1206 -0
- package/docs/architecture.md +1 -1
- package/docs/audit.md +13 -11
- package/docs/case-study-escrow.md +1 -1
- package/docs/cli.md +2 -2
- package/docs/glossary.md +2 -1
- package/docs/language-basics.md +25 -1
- package/docs/language-pdas.md +6 -2
- package/docs/language-statements.md +101 -11
- package/docs/language-types.md +80 -5
- package/docs/runtime.md +1 -1
- package/docs/security-model.md +5 -3
- package/docs/stability.md +5 -1
- package/docs/troubleshooting.md +45 -1
- package/llms-full.txt +271 -27
- package/package.json +1 -1
- package/releases/0.50.0.json +874 -0
- package/runtime/typesol_rt.h +125 -51
package/llms-full.txt
CHANGED
|
@@ -676,10 +676,34 @@ From the source alone, a build writes:
|
|
|
676
676
|
## Memory: stack and heap
|
|
677
677
|
|
|
678
678
|
A program runs with a 4 KiB stack frame per function and a heap of 32 KiB. Locals live on the stack: a list local
|
|
679
|
-
holds at most 2 KiB, and a frame that overflows is a compile error that names its biggest locals.
|
|
679
|
+
holds at most 2 KiB, and a frame that overflows is a compile error that names its biggest locals. Under SBPF v1 and v2
|
|
680
|
+
(`--sbpf v1` / `v2`) each function's frame is sized to fit, so the 4 KiB limit is lifted, but one frame holds at most
|
|
681
|
+
32,767 bytes and all the frames of a call path share 256 KiB; the build refuses a program past either
|
|
682
|
+
([troubleshooting](troubleshooting.md#-needs-n-bytes-of-stack-sbpf-v1-allows-one-function-at-most-32767)). Strings built at run
|
|
680
683
|
time and a few other values live on the heap ([heap values](language-types.md#heap-values)), which is never freed
|
|
681
684
|
during an instruction.
|
|
682
685
|
|
|
686
|
+
A program that does not fit the stack as written is laid out another way by the build before it gives up. It does
|
|
687
|
+
nothing to a program that fits, which builds to the same bytes as before. In order, measuring with clang after each
|
|
688
|
+
step, until the program fits:
|
|
689
|
+
|
|
690
|
+
1. each statement of the function that does not fit, and of the helpers inlined into it, gets a C block of its own, so
|
|
691
|
+
clang can give the temporaries of different statements one slot (a struct literal's, a helper result's); nothing
|
|
692
|
+
else changes;
|
|
693
|
+
2. helpers inlined into it are kept out of line, as if each were [`@noinline`](language-statements.md#inline-and-noinline),
|
|
694
|
+
largest frame first (about 10 compute units per call), except a helper that takes a function or is marked
|
|
695
|
+
`/** @inline */`; nothing else changes;
|
|
696
|
+
3. in an instruction, its largest list and struct locals and the struct results of helpers it holds are kept on the
|
|
697
|
+
heap, as if each were [`@heap`](language-types.md#heap-values) (about 17 compute units each, and their bytes of
|
|
698
|
+
heap), as long as the instruction's heap locals together fit in the heap; never a local marked `/** @stack */`.
|
|
699
|
+
|
|
700
|
+
Each step is printed as a note (`note: instruction f did not fit the stack (4864 bytes; SBPF v0 allows 4096); now 64
|
|
701
|
+
bytes`, `note: helper settle() kept out of line, for instruction trade …`) and recorded in `<name>.build.json` as
|
|
702
|
+
`stack.actions` ([cli.md](cli.md)). The steps depend only on the sources and clang's measurements, so a
|
|
703
|
+
rebuild (`typesol verify`, `--docker`) takes the same ones and gives the same bytes. When none of them is enough, the
|
|
704
|
+
build fails with the error it would have given, and a line saying what it tried. `const STACK_AUTO = false` in the
|
|
705
|
+
program (or `typesol build --stack-auto off` for one build) turns this off: the program then fails to build instead.
|
|
706
|
+
|
|
683
707
|
`const HEAP_SIZE_KIB = 64n` (32 to 256) lets the program use a bigger heap; the client must then add a ComputeBudget
|
|
684
708
|
`requestHeapFrame` instruction with that size to the transaction.
|
|
685
709
|
|
|
@@ -917,7 +941,8 @@ export function report(owner: Pubkey, amount: u64, limit: Option<u64>): void {
|
|
|
917
941
|
or `Hash` prints as base58 and an `Option` of an integer, boolean, `Pubkey` or `Hash` as its value or `null`, as
|
|
918
942
|
JavaScript prints it (after `x !== null`, the value). A template is joined in one allocation; text known when the
|
|
919
943
|
program compiles (literals, string and `Pubkey` constants) is folded in; `s += …` grows the newest heap string in
|
|
920
|
-
place.
|
|
944
|
+
place. The left side of `s += …` is read before the right side runs, as in JavaScript: a right side that assigns `s`
|
|
945
|
+
(a closure it calls) does not change what is appended to. Tagged templates (`` String.raw`…` ``) are refused.
|
|
921
946
|
|
|
922
947
|
## Enums
|
|
923
948
|
|
|
@@ -980,6 +1005,7 @@ export function apply(vault: Mut<Vault>, action: Action): void {
|
|
|
980
1005
|
|
|
981
1006
|
- A data enum is a union of object types, each with a `kind` string literal: Rust's enum with fields, Borsh-encoded as
|
|
982
1007
|
Anchor encodes one. `a.kind === "Deposit"` or `switch (a.kind)`, and TypeScript narrowing picks the variant.
|
|
1008
|
+
Two data enums are not compared with `===` (refused): compare their `kind`, then the fields of the narrowed variants.
|
|
983
1009
|
- Reading a variant's field is checked again at run time: when the value is not that variant, 5112.
|
|
984
1010
|
- In account data: 1 tag byte then room for the largest variant. In arguments and events: the tag, then that
|
|
985
1011
|
variant's fields.
|
|
@@ -1015,7 +1041,10 @@ export function step(player: Mut<Player>, dx: u64): void {
|
|
|
1015
1041
|
```
|
|
1016
1042
|
|
|
1017
1043
|
- An `interface` with fixed-size fields is a struct; exported ones are in the IDL. Fields are stored in declaration
|
|
1018
|
-
order, back to back, with no padding.
|
|
1044
|
+
order, back to back, with no padding. An object type alias is the same struct: `export type Point = { x: u64; y: u64 }`
|
|
1045
|
+
works wherever `export interface Point { … }` does (account data, arguments, results, events, helper parameters,
|
|
1046
|
+
fields of other structs). An object type with `kind: "Name"` is a [data enum](#data-enums) of one variant instead.
|
|
1047
|
+
Account groups are interfaces only.
|
|
1019
1048
|
- A struct literal names every field (`{ x, y: 2n }`); a missing or unknown field is an error. An object literal needs
|
|
1020
1049
|
a known struct type: a typed local (`const p: Point = { … }`), a field, a helper parameter, `emit<T>(…)`.
|
|
1021
1050
|
- `a.home = b.home` copies the whole struct, as Rust and Anchor do; how `const` binds one is in [copies and
|
|
@@ -1028,7 +1057,71 @@ export function step(player: Mut<Player>, dx: u64): void {
|
|
|
1028
1057
|
The same holds wherever TypeSOL copies a whole value itself (Borsh fields, instruction arguments, a helper's copy of
|
|
1029
1058
|
a struct argument, events), and a value it zeroes whole (`x.o = null` on an `Option<struct>`, a data-enum literal)
|
|
1030
1059
|
takes the memset syscall from 192 bytes in account data and 600 in a local.
|
|
1060
|
+
- `===`, `!==`, `==` and `!=` on two structs are refused (`=== on two structs is not supported`): a struct is held by
|
|
1061
|
+
reference and copied where JavaScript would share it, so the comparison could not give JavaScript's answer. Compare
|
|
1062
|
+
the fields you need: `a.x === b.x && a.y === b.y`. The same holds for data enums, lists and Options of a struct
|
|
1063
|
+
([conditions](language-statements.md#conditions)).
|
|
1031
1064
|
- A struct cannot contain itself.
|
|
1065
|
+
- Generic structs (`interface Pair<T>`, `type Pair<T> = { … }`) and intersection types (`type B = A & { … }`) are
|
|
1066
|
+
refused; write the struct out, or extend it ([below](#structs-that-extend-others)).
|
|
1067
|
+
|
|
1068
|
+
### Structs that extend others
|
|
1069
|
+
|
|
1070
|
+
```ts
|
|
1071
|
+
import { type Mut, type Pubkey, type u32, type u64, type u8 } from "typesol";
|
|
1072
|
+
|
|
1073
|
+
export interface Base {
|
|
1074
|
+
id: u32;
|
|
1075
|
+
owner: Pubkey;
|
|
1076
|
+
}
|
|
1077
|
+
|
|
1078
|
+
export type Tagged = { tag: u8 };
|
|
1079
|
+
|
|
1080
|
+
// Fields, in this order: id, owner (Base's), tag (Tagged's), then qty.
|
|
1081
|
+
export interface Item extends Base, Tagged {
|
|
1082
|
+
qty: u64;
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
export interface Shelf {
|
|
1086
|
+
base: Base;
|
|
1087
|
+
item: Item;
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
export function stock(shelf: Mut<Shelf>, qty: u64): void {
|
|
1091
|
+
shelf.item = { ...shelf.base, tag: 1n, qty }; // a parent's fields, then the child's own
|
|
1092
|
+
shelf.base = { ...shelf.item }; // a child's copy of the parent's fields
|
|
1093
|
+
}
|
|
1094
|
+
```
|
|
1095
|
+
|
|
1096
|
+
- `interface B extends A, C { … }` (with `A` and `C` structs of the program, interfaces or object types) has A's fields,
|
|
1097
|
+
then C's, then its own, in that order: that is its layout in account data, arguments and events, and its order in
|
|
1098
|
+
the IDL. In the fixed layout growable `Vec`s still go last ([fixed layout](#structs)); a `BigVec` must stay the last
|
|
1099
|
+
field, so a struct cannot extend one that ends in a `BigVec` and add fields.
|
|
1100
|
+
- A field in two parents is one field when it has the same type in both (`C.a and A.a have different types; B cannot
|
|
1101
|
+
extend both` otherwise). A struct cannot declare an inherited field again, extend a generic type, a data enum or an
|
|
1102
|
+
account group, or extend the same struct twice. Doc tags (`@layout`, `@discriminator`) are each struct's own.
|
|
1103
|
+
- A `B` is not passed or assigned where an `A` is expected (`a Child is not used as its parent Base (TypeSOL copies
|
|
1104
|
+
structs where JavaScript shares the object): …`): JavaScript would pass the object itself, extra fields and all,
|
|
1105
|
+
which TypeSOL, copying structs, could not reproduce. Write the parent's copy as a spread, `{ ...child }`, or field by
|
|
1106
|
+
field.
|
|
1107
|
+
|
|
1108
|
+
### Spreads in struct literals
|
|
1109
|
+
|
|
1110
|
+
- `{ ...p, x: 1n }` and `{ x: 1n, ...p }` build a struct from p's fields and the entries given: entries run left to
|
|
1111
|
+
right, each once, and a later one overwrites an earlier one, as in JavaScript. `...p` reads p's fields at its place,
|
|
1112
|
+
so an entry after it that changes p (a helper writing the account) does not change what was copied, and an entry
|
|
1113
|
+
before it does.
|
|
1114
|
+
- p is a struct place (a local, a field, a list element, a helper's struct parameter) or a struct account; of the
|
|
1115
|
+
literal's type, of a struct the literal's type extends (its fields are copied, the rest are given), or of one that
|
|
1116
|
+
extends it (the literal's fields are copied from it).
|
|
1117
|
+
- A spread copies as assignment does ([copies and references](language-statements.md#copies-and-references)). For
|
|
1118
|
+
scalars, strings and Options of scalars that is JavaScript's result. A field holding a struct, list or data enum
|
|
1119
|
+
would be shared by JavaScript between p and the new value, so it must be given after the spread:
|
|
1120
|
+
`{ ...p, pos: { ...p.pos } }` is a copy in JavaScript too, while `{ ...p }` alone is refused (`...p would copy pos,
|
|
1121
|
+
a Point: JavaScript shares that object between the two values, TypeSOL copies it; give pos after the spread …`).
|
|
1122
|
+
- `acct.p = { ...acct.p, x: 1n }` reads the old fields: the new value is built before it is stored.
|
|
1123
|
+
- Spreads are for struct literals of a known struct type. In `emit<T>(…)`, a data enum literal, a list literal or a
|
|
1124
|
+
built-in's object argument they are refused.
|
|
1032
1125
|
|
|
1033
1126
|
### File-local structs
|
|
1034
1127
|
|
|
@@ -1056,6 +1149,7 @@ export function bid(auction: Mut<Auction>, amount: u64, cap: Option<u64>): void
|
|
|
1056
1149
|
```
|
|
1057
1150
|
|
|
1058
1151
|
- `Option<T>` (or `T | null`) is a value or `null`. Compare with `=== null` / `!== null`; TypeScript narrows it.
|
|
1152
|
+
Two Options of a struct are not compared with `===` (refused): check each against `null`, then compare fields.
|
|
1059
1153
|
- Using an empty option where a value is needed fails with 5108; `x!` is the value, or 5108.
|
|
1060
1154
|
- `a ?? b` takes `b` only when `a` is empty, and evaluates `b` only then; the result is an Option when `b` can be null.
|
|
1061
1155
|
- `===` between options, or with a value, follows JavaScript: `null === null`, and an empty option never equals a
|
|
@@ -1170,8 +1264,8 @@ own PDA must sign, which only its `executeTransaction` does, for a transaction e
|
|
|
1170
1264
|
- Setting one up costs about the same at any size: `fixedVec([])` of 1280 bytes is about 10 CU (the free slots are
|
|
1171
1265
|
zeroed by the memset syscall, the elements written in place, also as a struct literal's field).
|
|
1172
1266
|
- **Size limit.** One list holds at most 2 KiB of elements (the FixedVec's u32 length aside: `FixedVec<u32, 512>`
|
|
1173
|
-
fits). The 4 KiB stack frame is shared with the rest of the function, and
|
|
1174
|
-
its biggest locals; a bigger list is a compile error naming its size. [`@heap`](#heap-values) moves one to the heap.
|
|
1267
|
+
fits). The 4 KiB stack frame (on SBPF v1 and v2, at most 32,767 bytes) is shared with the rest of the function, and
|
|
1268
|
+
a frame that overflows is reported with its biggest locals; a bigger list is a compile error naming its size. [`@heap`](#heap-values) moves one to the heap.
|
|
1175
1269
|
- A `FixedVec<u8, N>` is also a byte buffer: `pushLE`, `pushKey`, `pushBorsh`, `setLE`, `truncate`
|
|
1176
1270
|
([byte builders](language-native.md#byte-strings-built-by-hand)).
|
|
1177
1271
|
|
|
@@ -1307,6 +1401,8 @@ Locals live on the stack. These live on the heap (32 KiB by default;
|
|
|
1307
1401
|
- strings built at run time (5110 when the heap is full);
|
|
1308
1402
|
- a `@layout borsh` struct's `Vec` fields while the instruction runs;
|
|
1309
1403
|
- the CPI scratch lists of a program that declares `MAX_ACCOUNTS`;
|
|
1404
|
+
- a list or struct local of an instruction that the build moved there because the program did not fit the stack
|
|
1405
|
+
([Memory](language-basics.md#memory-stack-and-heap)), as it moves a helper's struct result the instruction holds;
|
|
1310
1406
|
- a list or struct local marked `/** @heap */`:
|
|
1311
1407
|
|
|
1312
1408
|
```ts
|
|
@@ -1322,7 +1418,10 @@ export function collect(n: u8): void {
|
|
|
1322
1418
|
|
|
1323
1419
|
`/** @heap */` on an instruction's list or struct local keeps it on the heap instead of the stack: one allocation per
|
|
1324
1420
|
declaration, reused when a loop declares it again, up to the heap's size, for about 18 CU. Not in helpers, which may
|
|
1325
|
-
run many times while the heap is never freed.
|
|
1421
|
+
run many times while the heap is never freed. A local the build moves to the heap to fit the stack is handled the same
|
|
1422
|
+
way (one allocation per declaration, reused by a loop; for a helper's struct result, one per call site), and only while
|
|
1423
|
+
the instruction's `@heap` and moved locals together fit in the heap; the note gives its bytes. `/** @stack */` on a
|
|
1424
|
+
list or struct local (or on `const r = helper(…)` returning a struct) keeps it off the heap; a local is not both.
|
|
1326
1425
|
|
|
1327
1426
|
**Next:** [Statements and functions](language-statements.md): which TypeScript is TypeSOL.
|
|
1328
1427
|
|
|
@@ -1340,8 +1439,9 @@ In short, a function body may contain:
|
|
|
1340
1439
|
| --- | --- |
|
|
1341
1440
|
| `const`, `let` | one name each, with an initializer ([declarations](#declarations)) |
|
|
1342
1441
|
| assignment, `+=` … `>>=`, `++`, `--` | checked arithmetic ([types](language-types.md#integers)) |
|
|
1442
|
+
| `\|\|=`, `&&=`, `??=` | [logical assignment](#logical-assignment): booleans, Options |
|
|
1343
1443
|
| `if` / `else`, `? :`, `&&`, `\|\|`, `!` | conditions are `boolean` |
|
|
1344
|
-
| `for`, `for … of`, `while`, `do … while`, `break`, `continue` | [loops](#loops) |
|
|
1444
|
+
| `for`, `for … of`, `while`, `do … while`, `break`, `continue` | [loops](#loops), labeled too (`outer: for …`) |
|
|
1345
1445
|
| `switch` | on integers, enums and a data enum's `kind` |
|
|
1346
1446
|
| `return` | |
|
|
1347
1447
|
| calls | helpers, closures, built-ins, account methods |
|
|
@@ -1395,7 +1495,39 @@ You can assign to a local, a field of a writable account (`Mut<T>`), a field of
|
|
|
1395
1495
|
read-only; copy the value into a local first`), and so are helper parameters (see [Inout](#inout-parameters)).
|
|
1396
1496
|
|
|
1397
1497
|
Compound assignments (`+=`, `-=`, `*=`, `/=`, `%=`, `**=`, `&=`, `|=`, `^=`, `<<=`, `>>=`) and `++` / `--` are
|
|
1398
|
-
checked like the operators they stand for.
|
|
1498
|
+
checked like the operators they stand for. All of them are statements: used as a value (`const y = x++`, `f(x += 1n)`)
|
|
1499
|
+
they are refused (`x++ is a statement in TypeSOL, not a value: …`).
|
|
1500
|
+
|
|
1501
|
+
### Logical assignment
|
|
1502
|
+
|
|
1503
|
+
```ts
|
|
1504
|
+
import { type Mut, type Option, type u64 } from "typesol";
|
|
1505
|
+
|
|
1506
|
+
export interface Prefs {
|
|
1507
|
+
seen: boolean;
|
|
1508
|
+
open: boolean;
|
|
1509
|
+
limit: Option<u64>;
|
|
1510
|
+
}
|
|
1511
|
+
|
|
1512
|
+
function fallback(): u64 {
|
|
1513
|
+
return 100n;
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
export function touch(prefs: Mut<Prefs>, now: boolean): void {
|
|
1517
|
+
prefs.seen ||= now; // written only when seen is false
|
|
1518
|
+
prefs.open &&= now; // written only when open is true
|
|
1519
|
+
prefs.limit ??= fallback(); // fallback() runs only when limit is empty
|
|
1520
|
+
}
|
|
1521
|
+
```
|
|
1522
|
+
|
|
1523
|
+
- `x ||= v`, `x &&= v` and `x ??= v` work wherever `x = v` does (a local, a field of a writable account or of a local
|
|
1524
|
+
struct, a list element's field), as JavaScript runs them: the place is found once, its value read, and only when it
|
|
1525
|
+
decides so (false for `||=`, true for `&&=`, empty for `??=`) is `v` evaluated and written. Otherwise `v` does not
|
|
1526
|
+
run and nothing is written.
|
|
1527
|
+
- `||=` and `&&=` take booleans. On an integer, JavaScript would test for `0n`, which TypeSOL does not do implicitly:
|
|
1528
|
+
`||= works on booleans; on u64 JavaScript would test for 0n: write if (…) x = …`.
|
|
1529
|
+
- `??=` takes an `Option` (of an integer, boolean, `Pubkey`, struct or array; `acct.home ??= { x, y }` writes the
|
|
1530
|
+
whole struct). On anything else it is refused (`??= needs an Option on the left (u64 is never null): …`).
|
|
1399
1531
|
|
|
1400
1532
|
### Copies and references
|
|
1401
1533
|
|
|
@@ -1430,6 +1562,10 @@ export function edit(shape: Mut<Shape>): void {
|
|
|
1430
1562
|
refused (`bind struct references with const`).
|
|
1431
1563
|
- **An annotation makes a copy.** `const p: Point = acct.home` (or `= { x, y }`) is a local struct of its own. Lists
|
|
1432
1564
|
likewise: `const ys: FixedVec<u64, 4> = acct.xs` is a copy.
|
|
1565
|
+
- **A spread copies.** `{ ...p, x: 1n }` copies p's fields at its place in the literal, as `field: p.field` entries
|
|
1566
|
+
would ([structs](language-types.md#spreads-in-struct-literals)). A field holding a struct, list or data enum must
|
|
1567
|
+
be given after the spread (`pos: { ...p.pos }`): JavaScript would share that nested object between the two values,
|
|
1568
|
+
which TypeSOL, copying, could not.
|
|
1433
1569
|
- **A reference cannot outlive what it names.** Replacing the referenced place (`book.bids.set(i, …)`, `pop()`,
|
|
1434
1570
|
`clear()`, `acct.p = …`) while the reference is still used afterwards is a compile error, because JavaScript would
|
|
1435
1571
|
keep the old object: `"q" still refers to a.home; replacing it here would change what "q" reads (in JS it keeps the
|
|
@@ -1478,7 +1614,12 @@ A condition (in `if`, `while`, `for`, `? :`, `&&`, `||`, `!`) must be a `boolean
|
|
|
1478
1614
|
truthiness: `if (amount)` is refused (`expected boolean, got u64`).
|
|
1479
1615
|
|
|
1480
1616
|
`==` and `!=` compile like `===` and `!==`: both operands have the same type (`operands have different types` otherwise),
|
|
1481
|
-
so there is no coercion for the loose forms to do.
|
|
1617
|
+
so there is no coercion for the loose forms to do. All four compare integers, booleans, enums, `Pubkey`s, strings and
|
|
1618
|
+
Options of those by value. On two structs, data enums, lists, Options of a struct or accounts they are refused: those
|
|
1619
|
+
are held by reference, so the comparison would test where they are rather than what they hold, and the compiler copies
|
|
1620
|
+
them where JavaScript would pass the same object (a struct argument of a helper, a local declared with its type), so
|
|
1621
|
+
the answer would differ from JavaScript's. Compare the fields (`a.x === b.x && a.y === b.y`), a data enum's `.kind`,
|
|
1622
|
+
a list's `.length` and elements, or two accounts' `.key`. Comparing an Option with `null` is unchanged.
|
|
1482
1623
|
|
|
1483
1624
|
## Loops
|
|
1484
1625
|
|
|
@@ -1517,8 +1658,36 @@ export function tally(book: Mut<Book>, extra: Vec<u64>, n: u64): void {
|
|
|
1517
1658
|
- `for (const f of [dbl, inc])` runs a list of named functions in turn ([functions](#arrow-functions-and-closures)).
|
|
1518
1659
|
- `while`, `do … while`, `break` and `continue`. Inside a `switch`, `break` leaves the switch and `continue` the
|
|
1519
1660
|
enclosing loop, as in JavaScript.
|
|
1520
|
-
-
|
|
1661
|
+
- A label names a loop or a block: `break outer` leaves the labeled statement, `continue outer` goes on with the
|
|
1662
|
+
labeled loop's next pass (a `for` runs its update first), from any depth, as in JavaScript. A label on another
|
|
1663
|
+
statement (`done: if (…)`) is refused, and so is a second label on one statement. A `loadAccountMut` view held by a
|
|
1664
|
+
loop the jump leaves is dropped, as at the end of that loop's pass; a plain `break` drops its loop's views too, so
|
|
1665
|
+
the accounts can be loaded again after the loop, and so does a `return` from a helper or a closure for every loop it
|
|
1666
|
+
leaves.
|
|
1667
|
+
- `for … in` is refused.
|
|
1521
1668
|
- There is no iteration limit: loops are bounded by the transaction's compute budget.
|
|
1669
|
+
|
|
1670
|
+
```ts
|
|
1671
|
+
import { type u64 } from "typesol";
|
|
1672
|
+
|
|
1673
|
+
export function firstPair(n: u64, target: u64): u64 {
|
|
1674
|
+
let found = 0n;
|
|
1675
|
+
search: for (let i = 0n; i < n; i++) {
|
|
1676
|
+
for (let j = i + 1n; j < n; j++) {
|
|
1677
|
+
if (j % 3n === 0n) continue search; // next i
|
|
1678
|
+
if (i + j === target) {
|
|
1679
|
+
found = i * 1000n + j;
|
|
1680
|
+
break search; // leaves both loops
|
|
1681
|
+
}
|
|
1682
|
+
}
|
|
1683
|
+
}
|
|
1684
|
+
done: {
|
|
1685
|
+
if (found !== 0n) break done;
|
|
1686
|
+
found = 1n;
|
|
1687
|
+
}
|
|
1688
|
+
return found;
|
|
1689
|
+
}
|
|
1690
|
+
```
|
|
1522
1691
|
- Remaining accounts are iterated by index: `for (let i = 0n; i < rest.length; i++) rest.get(i)`.
|
|
1523
1692
|
|
|
1524
1693
|
## switch
|
|
@@ -1675,9 +1844,14 @@ while passing large values by pointer:
|
|
|
1675
1844
|
|
|
1676
1845
|
### Inline and @noinline
|
|
1677
1846
|
|
|
1678
|
-
clang inlines most helpers into their callers, so their locals share the caller's 4 KiB stack frame
|
|
1847
|
+
clang inlines most helpers into their callers, so their locals share the caller's 4 KiB stack frame (on SBPF v1 and v2,
|
|
1848
|
+
a frame of at most 32,767 bytes, with 256 KiB for all the frames of a call path).
|
|
1679
1849
|
`/** @noinline */` on a helper keeps it a function of its own with its own frame (a few CU per call), for work with
|
|
1680
|
-
large locals of its own (Phoenix's order ids, built after its event recorder's last CPI)
|
|
1850
|
+
large locals of its own (Phoenix's order ids, built after its event recorder's last CPI). A program that does not fit
|
|
1851
|
+
the stack without it is laid out by the build itself, which keeps helpers out of line as needed
|
|
1852
|
+
([Memory](language-basics.md#memory-stack-and-heap)), so `@noinline` is for compute units (a rarely taken path kept out
|
|
1853
|
+
of its caller) and for explicit control. `/** @inline */` on a helper keeps the build from doing that to it (clang may
|
|
1854
|
+
still keep it out of line on its own); a helper is not both.
|
|
1681
1855
|
|
|
1682
1856
|
```ts
|
|
1683
1857
|
import { type Mut, type u64, fixedArray } from "typesol";
|
|
@@ -1892,15 +2066,17 @@ may gain a hint line.
|
|
|
1892
2066
|
| reassigning a parameter | `n = 3n;` | "n" is const or an argument and cannot be reassigned |
|
|
1893
2067
|
| writing an argument | `p.x = 3n;` (`p: Point` argument) | instruction arguments are read-only; copy the value into a local first |
|
|
1894
2068
|
| `for … in` | `for (const k in b) {}` | for … in is not supported: use for (init; cond; step), for (const x of list), while or do … while |
|
|
1895
|
-
|
|
|
2069
|
+
| a label on another statement | `done: if (n > 1n) { break done; }` | a label goes on a loop or a block: done: for (…) { … } (break / continue done) or done: { … } (break done) |
|
|
2070
|
+
| `\|\|=` / `&&=` on a non-boolean | `b.value \|\|= n;` | \|\|= works on booleans; on u64 JavaScript would test for 0n: write if (…) b.value = … |
|
|
2071
|
+
| `??=` on a non-Option | `b.value ??= n;` | ??= needs an Option on the left (u64 is never null): write b.value = … |
|
|
1896
2072
|
| truthiness | `if (n) log("x");` | expected boolean, got u64 |
|
|
1897
2073
|
| indexing with `[ ]` | `b.value = b.xs[0];` | indexing with [ ] is not supported: use .get(i) and .set(i, value), with i a bigint (0n) |
|
|
1898
2074
|
| array literals as values | `const a = [1n, 2n];` | arrays are only allowed inline as seeds/CPI lists; declare local arrays with fixedArray<T, N>(fill) |
|
|
1899
|
-
| untyped object literals
|
|
1900
|
-
| `new` | `const m = new Map<u64, u64>();` |
|
|
2075
|
+
| untyped object literals | `const o = { a: 1n };` | an object literal needs a struct type here: give it one (const p: Point = { … }), … |
|
|
2076
|
+
| `new` | `const m = new Map<u64, u64>();` | new is not supported: a program has no Map or Set; keep entries in a FixedVec<T, N> of structs (or a FixedArray), or one account per key (a PDA) |
|
|
1901
2077
|
| `typeof` | `const t = typeof n;` | typeof is not supported at run time: the types are known when the program compiles |
|
|
1902
2078
|
| `delete` | `const k = delete b.value;` | The operand of a 'delete' operator must be optional. (TypeScript; on an operand TypeScript accepts: delete is not supported: account fields always exist; set the value instead) |
|
|
1903
|
-
| `void` | `const v = void n;` |
|
|
2079
|
+
| `void` | `const v = void n;` | void is not supported: call the function as a statement (its result is then unused) |
|
|
1904
2080
|
| `instanceof` | `if (b instanceof Object) {}` | operator instanceof is not supported |
|
|
1905
2081
|
| `in` | `if ("value" in b) {}` | operator in is not supported |
|
|
1906
2082
|
| the comma operator | `y = (b.value = 1n, 2n);` | operator , is not supported |
|
|
@@ -1913,8 +2089,18 @@ may gain a hint line.
|
|
|
1913
2089
|
| optional fields | `export interface O { v?: u64 }` | optional fields are not supported; use v: Option<T> (null for none) |
|
|
1914
2090
|
| methods in interfaces | `export interface G { v(): u64 }` | structs may only contain plain fields |
|
|
1915
2091
|
| enum initializers | `export enum E { A = 1, B = 2 }` | value enum members are numbered 0, 1, 2… in order; leave out initializers |
|
|
1916
|
-
| nested `function` declarations | `function inner(x: u64): u64 { return x; }` (in a body) |
|
|
2092
|
+
| nested `function` declarations | `function inner(x: u64): u64 { return x; }` (in a body) | function declarations inside a function are not supported: write const inner = (…): T => { … } (a closure), or declare inner at the top level |
|
|
2093
|
+
| a function called where it is written | `b.value = ((v: u64): u64 => v)(n);` | a function called where it is written is not supported: bind it first (const f = (…) => …) and call f(…) |
|
|
2094
|
+
| `++` / `--` / `+=` as a value | `const y = m++;` | m++ is a statement in TypeSOL, not a value: read m into a const first, then write m++; on its own line |
|
|
2095
|
+
| union types | `export function f(x: u64 \| boolean): void {}` | union types (u64 \| boolean) are not supported: a value has one type; use a data enum (…) for a value of several shapes |
|
|
2096
|
+
| literal types | `type Side = "bid" \| "ask";` | a union of literal types ("bid" \| "ask") is not supported: use an enum (export enum Side { … }) for named choices, or the integer type itself |
|
|
2097
|
+
| intersection types | `export type B = Box & { n: u64 };` | intersection types (Box & { n: u64 }) are not supported: declare an interface that extends the named structs and adds the other fields (…) |
|
|
2098
|
+
| `Readonly`, `Pick`, `keyof`, `typeof` and other type operators | `function h(p: Readonly<Point>): u64 { … }` | Readonly<T> is not supported: write T itself (helper parameters are read-only already) |
|
|
2099
|
+
| a spread that would share a nested object | `const c: Line = { ...l, n };` (`l.a: Point`) | ...l would copy a, a Point: JavaScript shares that object between the two values, TypeSOL copies it; give a after the spread (a: { ...l.a } copies it in JavaScript too) |
|
|
2100
|
+
| a struct passed as its parent | `c.base = c.child;` (`Child extends Base`) | expected a Base: an object literal, another Base; a Child is not used as its parent Base (TypeSOL copies structs where JavaScript shares the object): … |
|
|
1917
2101
|
| recursion | `function a(x: u64): u64 { return x === 0n ? 0n : a(x - 1n); }` | recursion is not supported: a -> a |
|
|
2102
|
+
| `declare function` | `declare function ext(x: u64): u64;` | declare function has no body to compile: a program calls only the helpers it defines and TypeSOL's built-ins (another program is called with a CPI) |
|
|
2103
|
+
| overload signatures | `function h(x: u64): u64;` (before `function h(x: u64): u64 { … }`) | overload signatures are not supported: write one function, with an Option<T> parameter for an argument that may be left out |
|
|
1918
2104
|
| helpers without a return type | `function h(x: u64) { return x; }` | helper functions need an explicit return type (void, an integer, boolean, Pubkey or a struct) |
|
|
1919
2105
|
| namespaces | `namespace N { export const x = 1n; }` | namespaces are not supported: use separate files and import from them |
|
|
1920
2106
|
| npm and Node.js imports | `import { x } from "lodash";` | only imports from "typesol" and relative TypeSOL files ("./math") are allowed, got "lodash" |
|
|
@@ -1931,6 +2117,9 @@ may gain a hint line.
|
|
|
1931
2117
|
| unary minus on unsigned | `b.value = -n;` | unary minus needs a signed integer (i8 … i128) |
|
|
1932
2118
|
| operators on `f64` | `b.value = r.x + r.x;` (`r.x: f64`) | + on an f64 is not supported: an f64 is held as its IEEE 754 bits and there is no float arithmetic (this would compute on the bits); compute in integers and make the result with f64FromBits(bits), or take an f64's bits with f64Bits(x) |
|
|
1933
2119
|
| `===` on `f64` | `if (r.x === r.y) {}` (`f64` fields) | === on an f64 is not supported: an f64 is held as its IEEE 754 bits, and equal bits are not equal numbers (NaN would equal itself, 0 would not equal -0); to compare the bits, write f64Bits(a) === f64Bits(b) |
|
|
2120
|
+
| `===` on structs | `if (p === q) {}` (`p`, `q`: `Point` arguments) | === on two structs is not supported: it would compare where they are held, not their contents, and a struct passed to a helper or declared with its type is a copy; compare the fields you need (a.x === b.x && …) |
|
|
2121
|
+
| `===` on data enums | `if (e === g) {}` (`e`, `g`: data enum arguments) | === on two data enums is not supported: it would compare where they are held, not their contents; compare .kind, then the fields of the narrowed variants |
|
|
2122
|
+
| `===` on lists | `if (u === w) {}` (`u`, `w`: `Vec<u64>` arguments) | === on two lists is not supported: it would compare where they are held, not their contents; compare .length and the elements (.get(i)) |
|
|
1934
2123
|
| an `f64` as an integer | `b.value = r.x;` (`r.x: f64`) | an f64 is not an integer, so it is not converted to u64: it is held as its IEEE 754 bits, and the integer would be those bits; to use the bits, write f64Bits(x) |
|
|
1935
2124
|
| an `f64` as text | ``log(`${r.x}`);`` (`r.x: f64`) | an f64 cannot be written as text: there is no float formatting, and the digits would be its IEEE 754 bits read as an integer; to show the bits, write f64Bits(x) |
|
|
1936
2125
|
| `?.` on a non-optional value | `const v = b?.value;` | ?. works on optional accounts and Option<struct> values (other values are never null) |
|
|
@@ -2519,7 +2708,10 @@ A seed list is an array literal written where the seeds go. Each seed contribute
|
|
|
2519
2708
|
A seed is at most 32 bytes (`a seed can be at most 32 bytes`, when its length is known at compile time). A `string`
|
|
2520
2709
|
argument longer than that is only known at run time: `create`, `createIfNeeded`, `requirePda` and `Seeds<…>` then fail
|
|
2521
2710
|
with 2006, as for any other address that is not the PDA; check `byteLength(s) <= 32n` first to fail with an error of
|
|
2522
|
-
your own.
|
|
2711
|
+
your own. A list whose every seed has a length known at compile time (keys, integers, enums, string literals and
|
|
2712
|
+
constants) cannot exceed the limits, so its derivation skips the run-time check of the seed lengths; a list with a
|
|
2713
|
+
`string` value keeps it, and so do the account-creating calls (`create`, `createIfNeeded`, `createPda` and the token
|
|
2714
|
+
creates) of any program that passes one of them such a list. Solana takes at
|
|
2523
2715
|
most 16 seeds per address, the bump included. A list the derivation adds the bump to (`create`, `requirePda`,
|
|
2524
2716
|
`findProgramAddress`) holds at most 15 entries (`at most 15 seeds: the bump the derivation adds makes 16, Solana's
|
|
2525
2717
|
limit`); a list that ends with its bump (signer seeds, `createProgramAddress`) holds 16: 15 seeds and the bump (`at
|
|
@@ -2598,7 +2790,8 @@ export function locate(book: Mut<Book>, owner: Pubkey, start: u64): void {
|
|
|
2598
2790
|
- `findProgramAddress(seeds, program)` is the canonical PDA of any program (Rust's
|
|
2599
2791
|
`Pubkey::find_program_address(..).0`, e.g. a Bubblegum asset id), and `findProgramAddressBump(seeds, program)` its
|
|
2600
2792
|
bump. `const [address, bump] = findProgramAddressWithBump(seeds, program)` gives both from one search (Rust's
|
|
2601
|
-
`let (address, bump) = …`). Each search costs about 450 CU per bump tried
|
|
2793
|
+
`let (address, bump) = …`). Each search costs about 450 CU per bump tried, and tries 255 down to 1 as Solana's
|
|
2794
|
+
does: bump 0 is never tried.
|
|
2602
2795
|
- `createProgramAddress(seeds, program)` is `Pubkey::create_program_address`: the address for seeds that include the
|
|
2603
2796
|
bump, or `null` when they give none.
|
|
2604
2797
|
- `programId()` is this program's address.
|
|
@@ -4651,7 +4844,8 @@ bytes. See [seeds](language-pdas.md#seeds).
|
|
|
4651
4844
|
## Bump
|
|
4652
4845
|
|
|
4653
4846
|
The one byte added to a PDA's seeds so that the derived address has no private key. The canonical bump is the
|
|
4654
|
-
highest that works; it is found by trying 255, 254, …
|
|
4847
|
+
highest that works; it is found by trying 255, 254, … down to 1, as Solana's `find_program_address` does (bump 0 is
|
|
4848
|
+
never tried), and is usually stored in the account. See [PDAs](language-pdas.md).
|
|
4655
4849
|
|
|
4656
4850
|
## Signer
|
|
4657
4851
|
|
|
@@ -5125,8 +5319,8 @@ error. Exit codes: **0** success, **1** failure (compile errors, failed deploy,
|
|
|
5125
5319
|
| `typesol init [<dir>] [--name <program>] [--template counter\|vault\|escrow] [--no-agent-files]` | Create a project ([below](#typesol-init)). |
|
|
5126
5320
|
| `typesol new <program> [--template counter\|vault\|escrow]` | In a project: add a program, `programs/<program>.ts` and `tests/<program>.test.ts` from the template, and its entry in `typesol.json`. Refuses to overwrite files or reuse a name, and to write through a symbolic link inside the project (a linked `programs/` or `tests/`, or a link where a new file goes). |
|
|
5127
5321
|
| `typesol doctor [--url <rpc>] [--config <cli config>]` | Check what building, testing and deploying need: Node.js, the platform-tools (and a `toolchain` row: the tested ones, or untested with both versions), the Solana CLI, Docker (optional: only `--docker` builds need it; it says when they run emulated), its keypair, the RPC (`--url`, a cluster moniker, or the CLI config) and the keypair's balance there, and the project. Each problem is printed with the command that fixes it. The last line says what is ready (building and testing, or also deploying) and what the warnings concern. Exit code 1 when building is impossible (Node.js or the platform-tools missing); deploy pieces are warnings. |
|
|
5128
|
-
| `typesol build [<program>…] [--sbpf v0\|v1\|v2] [--profile] [--toolchain-strict] [--docker [--typesol-version <v>] [--release-record <file>]] [--quiet]` | In a project: build every program (or those named) into `<out>/<name>/`: `<name>.so`, `<name>.json` (Anchor IDL), `<name>.client.ts` (TypeScript client, see [client.md](client.md)), `<name>.c`, `<name>.build.json` (the TypeSOL, TypeScript, clang and platform-tools versions, the host, the SBPF version, each source's sha256 and the binary's). Each source's sha256 is that of the bytes the compiler read, and the build fails without a manifest when a source or the runtime changes while it runs (an editor saving a file mid-build), so a manifest never pairs a binary with sources that did not make it. With platform-tools other than the tested ones it warns once; `--toolchain-strict` (or `TYPESOL_TOOLCHAIN=strict`) refuses them. The first build creates `<out>/deploy/<name>-keypair.json` and records the address in `typesol.json`; later builds refuse a different keypair. First builds may run at once (several programs, or several processes): the keypair is created exclusively (a process that loses the race uses the winner's key) and `typesol.json` is updated one process at a time and replaced whole, so each address is kept. Without the keypair (a fresh clone, CI) it builds at the recorded address and says so; only `deploy` needs the keypair. `--docker` builds in the pinned container instead ([below](#build---docker-and-verify---docker)). |
|
|
5129
|
-
| `typesol build <file.ts> [--out <dir>] [--program-id <keypair.json>] [--sbpf …] [--profile] [--toolchain-strict] [--docker …] [--quiet]` | Outside a project (or a file that is not one of its programs): build that file. Its keypair is `--program-id`, which must exist, or `<out>/<name>-keypair.json`, created on the first build. A project program's `.ts` builds as that program. |
|
|
5322
|
+
| `typesol build [<program>…] [--sbpf v0\|v1\|v2] [--profile] [--stack-auto on\|off] [--toolchain-strict] [--docker [--typesol-version <v>] [--release-record <file>]] [--quiet]` | In a project: build every program (or those named) into `<out>/<name>/`: `<name>.so`, `<name>.json` (Anchor IDL), `<name>.client.ts` (TypeScript client, see [client.md](client.md)), `<name>.c`, `<name>.build.json` (the TypeSOL, TypeScript, clang and platform-tools versions, the host, the SBPF version, each source's sha256 and the binary's). Each source's sha256 is that of the bytes the compiler read, and the build fails without a manifest when a source or the runtime changes while it runs (an editor saving a file mid-build), so a manifest never pairs a binary with sources that did not make it. A program that does not fit the stack is laid out another way when it can ([Memory](language-basics.md#memory-stack-and-heap)): each step is printed as a `note:` and recorded in `<name>.build.json` under `stack` (`actions`, in order: `scope`, `noinline` or `heap`, with what each was for, and `frames`, the frames of the functions that did not fit before and after); a program that fits has no `stack` field. `--stack-auto off` makes such a build fail instead, as `const STACK_AUTO = false` in the program does (with `--docker`, write the constant). With platform-tools other than the tested ones it warns once; `--toolchain-strict` (or `TYPESOL_TOOLCHAIN=strict`) refuses them. The first build creates `<out>/deploy/<name>-keypair.json` and records the address in `typesol.json`; later builds refuse a different keypair. First builds may run at once (several programs, or several processes): the keypair is created exclusively (a process that loses the race uses the winner's key) and `typesol.json` is updated one process at a time and replaced whole, so each address is kept. Without the keypair (a fresh clone, CI) it builds at the recorded address and says so; only `deploy` needs the keypair. `--docker` builds in the pinned container instead ([below](#build---docker-and-verify---docker)). |
|
|
5323
|
+
| `typesol build <file.ts> [--out <dir>] [--program-id <keypair.json>] [--sbpf …] [--profile] [--stack-auto on\|off] [--toolchain-strict] [--docker …] [--quiet]` | Outside a project (or a file that is not one of its programs): build that file. Its keypair is `--program-id`, which must exist, or `<out>/<name>-keypair.json`, created on the first build. A project program's `.ts` builds as that program. |
|
|
5130
5324
|
| `typesol check [<program>… \| <file.ts>] [--json] [--stdin \| --files]` | Type-check and validate without building. In a project, no argument checks every program (or those named) and exits 1 if any has errors. `--json` prints `{ ok, diagnostics: [{ file, line, column, message }] }`, always (a missing file or unreadable `--files` input is a diagnostic too, exit 1 or 2); `--stdin` checks the text given on stdin as that file (its imports come from disk); `--files` reads `{ "files": { "<absolute path>": "<text>", … } }` from stdin and checks with those texts instead of the disk (an editor's unsaved buffers). |
|
|
5131
5325
|
| `typesol clone [<address>…] [--url <rpc>] [--config <cli config>] [--out <dir>]` | Copy cluster accounts and programs for the tests (Anchor's `[test.validator.clone]`): each account to `<address>.json` (lamports, owner, data), each program to `<address>.so` (its ELF, from the ProgramData account for upgradeable programs), in `tests/accounts` (`"accounts"` in `typesol.json`) or `--out`. Without addresses it clones the project's `"clone"` list. `testProgram` loads that directory into its LiteSVM; commit it so the tests run offline. |
|
|
5132
5326
|
| `typesol emit-c <file.ts> [--out <file.c>] [--profile]` | Print or write the generated C. |
|
|
@@ -5647,7 +5841,8 @@ a ComputeBudget `requestHeapFrame` instruction to the transaction.
|
|
|
5647
5841
|
### "instruction X (with the helpers inlined into it) needs N bytes of stack; SBPF v0 gives each function 4096"
|
|
5648
5842
|
|
|
5649
5843
|
Under SBPF v0 (the default) every function gets a fixed 4 KiB stack frame, and going past it corrupts memory, so the
|
|
5650
|
-
build refuses it. SBPF v1 and v2 (`--sbpf v1` / `v2`)
|
|
5844
|
+
build refuses it. SBPF v1 and v2 (`--sbpf v1` / `v2`) size each function's frame, so the 4 KiB limit is lifted there,
|
|
5845
|
+
but they have limits of their own (the next two entries). Large
|
|
5651
5846
|
structs and arrays copied into typed locals (`const q: Big = acct.big`) are the usual cause: bind them without a type
|
|
5652
5847
|
(`const q = acct.big`, a reference into the account data). The message lists the biggest locals (clang's frame layout: a
|
|
5653
5848
|
local's name and the source line near it; the locals of an inlined helper show as unnamed, with the helpers' locals that
|
|
@@ -5655,6 +5850,49 @@ may be them). Helpers are often inlined into their instruction, so their locals
|
|
|
5655
5850
|
@noinline */` to give it a 4 KiB frame of its own (a few compute units per call), or declare a large local list or
|
|
5656
5851
|
struct of an instruction `/** @heap */` to keep it on the heap (about 18 CU for the allocation).
|
|
5657
5852
|
|
|
5853
|
+
The build tries those itself before it gives this error ([Memory](language-basics.md#memory-stack-and-heap)): when they
|
|
5854
|
+
were not enough, the message ends with "To fit, the build also tried …", and lists the locals the heap could not hold.
|
|
5855
|
+
What is left is usually a helper's own locals (the build keeps a helper's locals on the stack: it may run many times,
|
|
5856
|
+
and the heap is never freed) or one local larger than the heap. With `const STACK_AUTO = false` (or `--stack-auto off`)
|
|
5857
|
+
the build does not try, and gives the error as it is.
|
|
5858
|
+
|
|
5859
|
+
### "note: instruction X did not fit the stack …"
|
|
5860
|
+
|
|
5861
|
+
Not an error: the program did not fit the stack as written, and the build laid it out another way (the notes after it
|
|
5862
|
+
say how, and `<name>.build.json` records it under `stack`). Its binary differs from what the source would give if it
|
|
5863
|
+
fitted, so a port comparing compute units with its original should look at the notes: a helper kept out of line costs
|
|
5864
|
+
about 10 compute units per call, a local on the heap about 17 and its bytes of heap. Declaring the same yourself
|
|
5865
|
+
(`/** @noinline */`, `/** @heap */`) or binding large values as references instead of copies gives the same or better,
|
|
5866
|
+
and `/** @inline */` or `/** @stack */` keeps the build from choosing a helper or a local.
|
|
5867
|
+
|
|
5868
|
+
### "… needs N bytes of stack; SBPF v1 allows one function at most 32767"
|
|
5869
|
+
|
|
5870
|
+
On SBPF v1 and v2 a load or store reaches its stack slot with a signed 16-bit offset, so a frame over 32,767 bytes
|
|
5871
|
+
cannot be addressed: clang's SBF backend builds it anyway, with the large offsets wrapped, and the program reads and
|
|
5872
|
+
writes the wrong memory. The build refuses such a frame. The remedies are the ones above: references instead of copies,
|
|
5873
|
+
`/** @heap */` on a large local of an instruction, or a `/** @noinline */` helper for work with large locals of its
|
|
5874
|
+
own.
|
|
5875
|
+
|
|
5876
|
+
### "the deepest call path needs N bytes of stack; SBPF v1 gives all the frames of a call path together 262144"
|
|
5877
|
+
|
|
5878
|
+
On SBPF v1 and v2 every frame of a call path (the entrypoint, the instruction, each helper it calls in turn) comes out
|
|
5879
|
+
of one 256 KiB stack, and past it the program fails with an access violation. After linking, the build measures the
|
|
5880
|
+
deepest path from the program's own code (its calls and the frame each function reserves, so a helper clang inlined
|
|
5881
|
+
counts in its caller) and refuses a program whose deepest path is over 262,144 bytes. The message lists the path with
|
|
5882
|
+
each function's frame. Shorten it: keep large values in account data or on the heap, or call the helpers with large
|
|
5883
|
+
frames one after another rather than from each other. (An indirect call, which TypeSOL does not generate, cannot be
|
|
5884
|
+
followed: a program with one is refused with a request to report it.)
|
|
5885
|
+
|
|
5886
|
+
### "… a call passes arguments on the stack past the end of the 4096-byte frame of SBPF v0"
|
|
5887
|
+
|
|
5888
|
+
A helper call with more than 5 arguments in C (its parameters, plus one for the program's context and one for a
|
|
5889
|
+
returned value) passes the rest on the stack, after the caller's locals. When the caller's frame is at the limit,
|
|
5890
|
+
those arguments overwrite its own values. clang's SBF backend reports this ("A function call in method … overwrites
|
|
5891
|
+
values in the frame") but still builds, and such a program computed wrong results; the build now refuses it, naming the
|
|
5892
|
+
function and the helpers it calls with arguments on the stack. Give the helper fewer parameters (a struct parameter is
|
|
5893
|
+
passed as one pointer) or make room in the caller as above. The backend's other message, "Stack offset of N exceeded
|
|
5894
|
+
max offset of M", is refused the same way.
|
|
5895
|
+
|
|
5658
5896
|
### "… is missing, but typesol.json records …"
|
|
5659
5897
|
|
|
5660
5898
|
The program keypair under `target/deploy/` was deleted or replaced. Restore it from your backup. Without it you cannot
|
|
@@ -6142,13 +6380,15 @@ exposed to them.
|
|
|
6142
6380
|
The compiler and runtime have had internal security reviews, the latest of 0.44.0 (above); its findings are fixed in
|
|
6143
6381
|
0.44.1. It was an internal review, not an independent audit; it covered the token, Token-2022, Metaplex and Stake
|
|
6144
6382
|
wrappers, the oracles and SBPF v1/v2 builds of the examples that the review of 0.43.0 left out, but not the Metaplex
|
|
6145
|
-
wrappers for unusual argument shapes beyond reading them, the large ports on SBPF v1/v2, or Windows. 0.45.0 to 0.
|
|
6383
|
+
wrappers for unusual argument shapes beyond reading them, the large ports on SBPF v1/v2, or Windows. 0.45.0 to 0.51.0
|
|
6146
6384
|
have had no security review. They add, among others, `runtime/typesol_builtins.c` (the
|
|
6147
6385
|
compiler builtins as TypeSOL's own C, 0.45.0), `transferCheckedWithHook`, `EVENT_AUTHORITY_CHECK` and the reads of
|
|
6148
6386
|
processed sibling instructions (0.48.0), `zeroData`, `log64`,
|
|
6149
6387
|
`PANIC_STYLE = "solana-1.18"`, `executable` on remaining accounts and lamport writes on remaining accounts under
|
|
6150
|
-
`NATIVE_ACCOUNT_WRITES` (0.49.0),
|
|
6151
|
-
copied and zeroed by the memory syscalls (0.50.0)
|
|
6388
|
+
`NATIVE_ACCOUNT_WRITES` (0.49.0), `copyData`, `getReturnData()`, `PANIC_STYLE = "pinocchio"` and whole values
|
|
6389
|
+
copied and zeroed by the memory syscalls (0.50.0), and the base58 text of keys from a table, derivations without
|
|
6390
|
+
the run-time seed-limit check, object-type structs, `extends`, struct spreads, labels, logical assignment and the
|
|
6391
|
+
stack layout the build chooses for a program that does not fit (0.51.0). There has been no external audit: see [audit.md](audit.md).
|
|
6152
6392
|
|
|
6153
6393
|
<!-- docs/ai.md -->
|
|
6154
6394
|
|
|
@@ -6353,7 +6593,11 @@ Useful today, but may still change before 1.0 (each change listed in the CHANGEL
|
|
|
6353
6593
|
- Constant expressions (0.43.0): top-level integer constants folded from literals and earlier constants, a typed
|
|
6354
6594
|
one computed in its type.
|
|
6355
6595
|
- Comma-separated `for` updates and initializers (0.43.0).
|
|
6356
|
-
- Stack room (0.43.0): `/** @noinline */` helpers and `/** @heap */` locals.
|
|
6596
|
+
- Stack room (0.43.0): `/** @noinline */` helpers and `/** @heap */` locals. A program that does not fit the stack is
|
|
6597
|
+
laid out by the build (statements in blocks of their own, helpers kept out of line, an instruction's locals on the
|
|
6598
|
+
heap), with `STACK_AUTO`, `--stack-auto`, `/** @inline */` and `/** @stack */` to turn it off (unreleased). Which
|
|
6599
|
+
steps it takes, and so the bytes of a program that did not fit as written, may change between releases; a program
|
|
6600
|
+
that fits is never laid out.
|
|
6357
6601
|
- Local `FixedVec` / `FixedArray` lists of structs, `FixedVec.pushAll`, the instruction data as a list (`slice`,
|
|
6358
6602
|
`InstructionBytes` helper parameters), `invokeInstruction` in helpers (0.42.0); local FixedVecs given for read-only
|
|
6359
6603
|
`FixedVec<T, N>` / `Vec<T>` helper parameters (0.43.0).
|
package/package.json
CHANGED