@typesol/cli 0.42.0 → 0.43.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.
Files changed (44) hide show
  1. package/CHANGELOG.md +227 -0
  2. package/README.md +10 -7
  3. package/dist/compiler/analyze.js +443 -12
  4. package/dist/compiler/diagnostics.d.ts +1 -0
  5. package/dist/compiler/diagnostics.js +13 -0
  6. package/dist/compiler/emit/alias.d.ts +44 -0
  7. package/dist/compiler/emit/alias.js +135 -4
  8. package/dist/compiler/emit/base.d.ts +25 -0
  9. package/dist/compiler/emit/base.js +58 -7
  10. package/dist/compiler/emit/calls.d.ts +67 -0
  11. package/dist/compiler/emit/calls.js +521 -46
  12. package/dist/compiler/emit/cpi-core.d.ts +20 -3
  13. package/dist/compiler/emit/cpi-core.js +226 -5
  14. package/dist/compiler/emit/cpi.js +8 -2
  15. package/dist/compiler/emit/expressions.js +38 -7
  16. package/dist/compiler/emit/program.js +30 -1
  17. package/dist/compiler/emit/rbtree.d.ts +7 -0
  18. package/dist/compiler/emit/rbtree.js +142 -0
  19. package/dist/compiler/emit/shared.d.ts +8 -0
  20. package/dist/compiler/emit/shared.js +9 -1
  21. package/dist/compiler/emit/statements.d.ts +27 -1
  22. package/dist/compiler/emit/statements.js +288 -19
  23. package/dist/compiler/emit/storage.d.ts +23 -2
  24. package/dist/compiler/emit/storage.js +100 -56
  25. package/dist/compiler/emit/token.js +2 -2
  26. package/dist/compiler/emit/views.d.ts +60 -0
  27. package/dist/compiler/emit/views.js +523 -0
  28. package/dist/compiler/idl.js +2 -1
  29. package/dist/compiler/model.d.ts +65 -4
  30. package/dist/compiler/model.js +14 -0
  31. package/dist/compiler/toolchain.d.ts +15 -0
  32. package/dist/compiler/toolchain.js +146 -11
  33. package/docs/architecture.md +9 -2
  34. package/docs/case-study-phoenix.md +226 -0
  35. package/docs/language.md +139 -8
  36. package/docs/migrate-from-anchor.md +1 -0
  37. package/docs/runtime.md +1 -1
  38. package/docs/stability.md +21 -2
  39. package/docs/troubleshooting.md +5 -2
  40. package/lib/typesol.d.ts +221 -4
  41. package/llms-full.txt +169 -14
  42. package/llms.txt +1 -1
  43. package/package.json +2 -2
  44. package/runtime/typesol_rt.h +407 -0
package/lib/typesol.d.ts CHANGED
@@ -124,6 +124,12 @@ export interface Vec<T> extends ListMethods<T> {
124
124
  * grows one `push` at a time, so calling it on a Vec is a compile error.
125
125
  */
126
126
  pushAll(items: IndexedList<T>): void;
127
+ /** A `FixedVec<u8, N>`'s byte builders (declared here for the same reason; a compile error on a Vec). */
128
+ pushLE<I extends bigint = bigint>(this: Vec<u8>, value: I): void;
129
+ pushKey(this: Vec<u8>, key: Pubkey): void;
130
+ pushBorsh<V = unknown>(this: Vec<u8>, value: V): void;
131
+ setLE<I extends bigint = bigint>(this: Vec<u8>, offset: u64, value: I): void;
132
+ truncate(this: Vec<u8>, length: u64): void;
127
133
  /** Remove and return the last element, shrinking the account. */
128
134
  pop(): T;
129
135
  /** Remove every element, shrinking the account. */
@@ -150,6 +156,28 @@ export interface FixedVec<T, N extends number> extends ListMethods<T> {
150
156
  pushAll(items: IndexedList<T>): void;
151
157
  pop(): T;
152
158
  clear(): void;
159
+ /**
160
+ * A `FixedVec<u8, N>` as a byte buffer: append an integer's little-endian bytes at its width (`buf.pushLE(count)`
161
+ * with `count: u16`; a literal needs one: `buf.pushLE<u16>(1n)` or `buf.pushLE(u16(1n))`). Every byte builder
162
+ * fails (5104) and appends nothing when the bytes do not fit in the room left.
163
+ */
164
+ pushLE<I extends bigint = bigint>(this: FixedVec<u8, N>, value: I): void;
165
+ /** Append a Pubkey's 32 bytes. */
166
+ pushKey(this: FixedVec<u8, N>, key: Pubkey): void;
167
+ /**
168
+ * Append a value's Borsh encoding, as invoke data, events and storeBorsh encode it: a struct (an object literal
169
+ * takes its type: `buf.pushBorsh<Header>({ … })`), a data enum (a tag byte, then the variant's fields), a
170
+ * FixedArray, a list (u32 count, then the elements), an integer at its width, a boolean, a Pubkey, a string or an
171
+ * Option.
172
+ */
173
+ pushBorsh<V = unknown>(this: FixedVec<u8, N>, value: V): void;
174
+ /**
175
+ * Overwrite the integer at byte `offset` with `value`'s little-endian bytes (`buf.setLE<u16>(91n, count)`): within the
176
+ * length, 5104 past it, as Rust's `data[a..b].copy_from_slice(…)` panics there.
177
+ */
178
+ setLE<I extends bigint = bigint>(this: FixedVec<u8, N>, offset: u64, value: I): void;
179
+ /** Keep the first `length` bytes (5104 when `length` is past the current length). */
180
+ truncate(this: FixedVec<u8, N>, length: u64): void;
153
181
  [Symbol.iterator](): Iterator<T>;
154
182
  readonly __capacity?: N;
155
183
  }
@@ -256,6 +284,125 @@ export declare function loadAccountMut<T>(account: RemainingAccount | AccountInf
256
284
  */
257
285
  export declare function loadRaw<T>(account: RemainingAccount | AccountInfo): Account<T>;
258
286
  export declare function loadRawMut<T>(account: RemainingAccount | AccountInfo): Mut<T>;
287
+ /**
288
+ * `loadRaw` / `loadRawMut` at a byte offset known only at run time (a native account whose parts start where its header
289
+ * says: Phoenix's market at byte 576). Owned by this program (3007); `offset + size(T)` within the data, checked once
290
+ * here (ProgramError::AccountDataTooSmall); `loadRawMutAt` writable and claimed as `loadRawMut` is (2040; neither with
291
+ * `NATIVE_ACCOUNT_WRITES`). Bind it with const: `const market = loadRawMutAt<FifoMarket>(acct, 576n)`. A view (this one,
292
+ * `rawArrayAt`, `rbTreeAt` and what is read through them) is not used after `closeRaw` or `resizeAccount` of an account
293
+ * it may view, since its bounds were checked against the old length: take it again after (a compile error says so).
294
+ */
295
+ export declare function loadRawAt<T>(account: RemainingAccount | AccountInfo, offset: u64): Account<T>;
296
+ export declare function loadRawMutAt<T>(account: RemainingAccount | AccountInfo, offset: u64): Mut<T>;
297
+ /**
298
+ * `capacity` elements of a fixed-size type packed in place from a run-time byte offset, the count known only at run
299
+ * time (a slab sized by its header). Bounds checked once when it is made (`offset + capacity × size(T)` within the
300
+ * data, overflow included: ProgramError::AccountDataTooSmall), then `get` / `set` check the index against the count
301
+ * (5104). Elements are references into the account (structs) or values. `.length` is the capacity.
302
+ */
303
+ export interface RawArray<T> extends ListMethods<T> {
304
+ readonly length: u64;
305
+ get(index: bigint): T;
306
+ set(index: bigint, value: T): void;
307
+ [Symbol.iterator](): Iterator<T>;
308
+ }
309
+ /** A RawArray of a read-only view (`rawArrayAt`): elements are read, not written. */
310
+ export interface ReadonlyRawArray<T> extends ListMethods<T> {
311
+ readonly length: u64;
312
+ get(index: bigint): Readonly<T>;
313
+ [Symbol.iterator](): Iterator<Readonly<T>>;
314
+ }
315
+ /** `const slab = rawArrayMutAt<Order>(acct, offset, capacity)`: see RawArray. */
316
+ export declare function rawArrayAt<T>(account: RemainingAccount | AccountInfo, offset: u64, capacity: u64): ReadonlyRawArray<T>;
317
+ export declare function rawArrayMutAt<T>(account: RemainingAccount | AccountInfo, offset: u64, capacity: u64): RawArray<T>;
318
+
319
+ /** A node of a RedBlackTree: its key (read-only), its value (in place) and its index in the tree's node array (1-based). */
320
+ export interface RbEntry<K, V> {
321
+ readonly key: Readonly<K>;
322
+ value: V;
323
+ readonly index: u32;
324
+ }
325
+ /**
326
+ * lib-sokoban 0.3's `RedBlackTree<K, V, N>` in account data, byte for byte (Phoenix's order books and trader seats):
327
+ * at the offset, root u32, padding, the allocator (size u64, bump index u32, free list head u32), then `capacity` nodes
328
+ * of 16 register bytes + K + V, 1-based. Every operation makes the writes sokoban makes, so a port's account bytes
329
+ * match the original's after every instruction (freed nodes keep their key and value bytes; the free list is LIFO).
330
+ * Keys are ordered by the comparator given to `rbTreeMutAt`, called as `compare(searchedKey, nodeKey)`, or by default
331
+ * as Rust derives Ord (fields in order, integers by value, Pubkeys by their bytes).
332
+ */
333
+ export interface RedBlackTree<K, V> {
334
+ /** The number of keys (sokoban's `len()`). */
335
+ readonly length: u64;
336
+ /** The number of nodes it was made with (sokoban's `capacity()`, its `N`). */
337
+ readonly capacity: u64;
338
+ /** NodeAllocator::initialize on zeroed bytes; anything else panics "Cannot reinitialize NodeAllocator", as sokoban. */
339
+ initialize(): void;
340
+ /** The value stored for `key`, in place (a reference: writes go to the tree), or null. */
341
+ get(key: K): V | null;
342
+ /**
343
+ * Insert, or replace the value of an equal key (keeping its node). The node index, or null when a new key finds the
344
+ * tree full (nothing written); a tree that is empty takes the key without that check, as sokoban's does.
345
+ * References into the tree's nodes (`get`, `min`, entries, however bound: `c ? t.min() : t.max()`, `t.get(k)!`)
346
+ * cannot be used after an insert (a freed node may be reused).
347
+ */
348
+ insert(key: K, value: V): u32 | null;
349
+ /** Remove `key`: its value (a struct in place in the freed node, which keeps its bytes until reused), or null. */
350
+ remove(key: K): V | null;
351
+ /**
352
+ * Remove the node at `index` (from `addrOf`, a node of `min` / `max` / `get`'s entry or a walk): the same writes as
353
+ * `remove(node.key)`, without searching the tree again. Its value, or null for index 0 or a node that holds no key
354
+ * (a freed one); past the capacity panics as `nodeAt` does.
355
+ */
356
+ removeAt(index: u32): V | null;
357
+ /** The node with the smallest key (sokoban's get_min_index), or null when empty. */
358
+ min(): RbEntry<K, V> | null;
359
+ /** The node with the largest key, or null. */
360
+ max(): RbEntry<K, V> | null;
361
+ /** The index of the node holding `key` (sokoban's get_addr), 0 when absent. */
362
+ addrOf(key: K): u32;
363
+ /** Node `index` as stored (sokoban's get_node), whether or not it holds a key; 0 or past the capacity panics as Rust's indexing. */
364
+ nodeAt(index: u32): RbEntry<K, V>;
365
+ /** `for (const e of tree.reversed())`: from the largest key down. */
366
+ reversed(): Iterable<RbEntry<K, V>>;
367
+ /**
368
+ * `for (const e of tree)`: from the smallest key up, `break` and `continue` as in any loop. The body may change values
369
+ * but not insert, remove or initialize on this tree or on any tree view of an account that may be this one (another
370
+ * view of the same tree), nor call a helper that takes accounts and makes an `rbTreeMutAt` view.
371
+ */
372
+ [Symbol.iterator](): Iterator<RbEntry<K, V>>;
373
+ }
374
+ /** A RedBlackTree of a read-only view (`rbTreeAt`): read, never changed. */
375
+ export interface ReadonlyRedBlackTree<K, V> {
376
+ readonly length: u64;
377
+ readonly capacity: u64;
378
+ get(key: K): Readonly<V> | null;
379
+ min(): Readonly<RbEntry<K, Readonly<V>>> | null;
380
+ max(): Readonly<RbEntry<K, Readonly<V>>> | null;
381
+ addrOf(key: K): u32;
382
+ nodeAt(index: u32): Readonly<RbEntry<K, Readonly<V>>>;
383
+ reversed(): Iterable<Readonly<RbEntry<K, Readonly<V>>>>;
384
+ [Symbol.iterator](): Iterator<Readonly<RbEntry<K, Readonly<V>>>>;
385
+ }
386
+ /**
387
+ * lib-sokoban 0.3's RedBlackTree<K, V, capacity> at a run-time byte offset (see RedBlackTree): owned by this program
388
+ * (3007), its 32 + capacity × (16 + size(K) + size(V)) bytes within the data (ProgramError::AccountDataTooSmall),
389
+ * `rbTreeMutAt` writable and claimed as `loadRawMut` is. `compare`, a function of the program `(a: K, b: K) => i8`
390
+ * (negative: a first; any signed integer type, an unsigned result is refused as it could never say "less"), orders the keys; it is called as compare(searchedKey, nodeKey), so an order that depends on the
391
+ * left key (Phoenix's FIFOOrderId: bids descending, asks ascending, by the sequence number's top bit) works as in
392
+ * Rust. Without it, keys are ordered as Rust's derived Ord orders them.
393
+ */
394
+ export declare function rbTreeAt<K, V>(
395
+ account: RemainingAccount | AccountInfo,
396
+ offset: u64,
397
+ capacity: u64,
398
+ compare?: (a: K, b: K) => bigint,
399
+ ): ReadonlyRedBlackTree<K, V>;
400
+ export declare function rbTreeMutAt<K, V>(
401
+ account: RemainingAccount | AccountInfo,
402
+ offset: u64,
403
+ capacity: u64,
404
+ compare?: (a: K, b: K) => bigint,
405
+ ): RedBlackTree<K, V>;
259
406
  /**
260
407
  * A native program's Borsh account, as Rust's `try_from_slice_unchecked::<T>(&data)` reads it: the data from byte 0
261
408
  * decoded into a local `T` (a copy; write it back with `storeBorsh`). No owner check and no discriminator; bytes after
@@ -717,6 +864,12 @@ export declare function createProgramAddress(seeds: Seed[], program: Pubkey): Pu
717
864
  export declare function logBase64(prefix: string, value: object): void;
718
865
  /** The bump of that canonical program address (`Pubkey::find_program_address(seeds, program).1`), to sign with it. */
719
866
  export declare function findProgramAddressBump(seeds: Seed[], program: Pubkey): u8;
867
+ /**
868
+ * The canonical program address and its bump from one search (Rust's `let (address, bump) =
869
+ * Pubkey::find_program_address(seeds, program)`), where `findProgramAddress` and `findProgramAddressBump` would search
870
+ * twice: bind both, `const [address, bump] = findProgramAddressWithBump(["seat", market, trader], PHOENIX_ID)`.
871
+ */
872
+ export declare function findProgramAddressWithBump(seeds: Seed[], program: Pubkey): [address: Pubkey, bump: u8];
720
873
  /** This program's address (`crate::ID`). */
721
874
  export declare function programId(): Pubkey;
722
875
  /**
@@ -820,11 +973,30 @@ export declare const AuthorityType: {
820
973
  */
821
974
  export declare function realloc(account: AccountMeta, newSize: u64, payer: Mut<Signer>): void;
822
975
  /**
823
- * Resize a `Mut<AccountInfo>` of this program to `newSize` bytes (Rust's `AccountInfo::resize`): the data length only,
824
- * the new bytes zeroed. No rent moves: fund the account first (`rentExempt(newSize)`), the runtime refuses an account
825
- * left short. At most 10 KiB past its size when the instruction started (3016).
976
+ * Resize a `Mut<AccountInfo>` or a remaining account of this program to `newSize` bytes (Rust's `AccountInfo::resize` /
977
+ * `realloc(newSize, true)`): the data length only, the new bytes zeroed. No rent moves: fund the account first
978
+ * (`rentExempt(newSize)`), the runtime refuses an account left short. At most 10 KiB past its size when the
979
+ * instruction started (3016). A remaining account must be writable (3006) unless the program declares
980
+ * `NATIVE_ACCOUNT_WRITES`, which also lets an `AccountInfo` parameter be resized without `Mut<…>`. A view of the
981
+ * account made before (`loadRawMutAt`, `rbTreeMutAt` …) is not used after it: take the view again.
826
982
  */
827
- export declare function resizeAccount(account: Mut<AccountInfo>, newSize: u64): void;
983
+ export declare function resizeAccount(account: RemainingAccount | Mut<AccountInfo>, newSize: u64): void;
984
+ /**
985
+ * Close an account of this program as a native program does it by hand (Phoenix's Tombstone), in this order:
986
+ * `receiver.lamports += account.lamports`, `account.lamports = 0`, the owner set to the System Program, the data length
987
+ * set to 0 (`realloc(0, false)`: the bytes are not zeroed, the runtime drops them with the length). Unlike `close` it
988
+ * takes remaining accounts and `AccountInfo` parameters and leaves the data unwiped. Both accounts are checked writable
989
+ * (3006; `Mut<AccountInfo>` parameters) and passed once (2040), the account owned by this program (3007) and not the
990
+ * receiver itself; with `NATIVE_ACCOUNT_WRITES` nothing is checked and the runtime judges the result, as for the
991
+ * original. A view of the account made before is not used after it.
992
+ */
993
+ export declare function closeRaw(account: RemainingAccount | Mut<AccountInfo>, receiver: RemainingAccount | Mut<AccountInfo>): void;
994
+ /**
995
+ * Set an account's owner in place (Rust's `AccountInfo::assign`). The runtime accepts the change, at the end of the
996
+ * instruction or at the next CPI, only for a writable account of this program whose data is empty or zeroed. Checked
997
+ * as `closeRaw` checks its account (writable, passed once, this program's), or not at all under `NATIVE_ACCOUNT_WRITES`.
998
+ */
999
+ export declare function assignOwner(account: RemainingAccount | Mut<AccountInfo>, owner: Pubkey): void;
828
1000
 
829
1001
  /**
830
1002
  * Token-2022 extensions of a mint (any account works; legacy SPL Token mints have none).
@@ -895,6 +1067,20 @@ export type Account<T> = Readonly<T> & AccountMeta;
895
1067
  */
896
1068
  export type Mut<T, E = never> = T & AccountMeta;
897
1069
 
1070
+ /**
1071
+ * A helper parameter the helper may change in place (Rust's `&mut T`): `function addEvent(rec: Inout<Recorder>, e:
1072
+ * Event): void`. The helper gets the caller's place itself, not a copy, and the caller sees its writes after the call,
1073
+ * as JS shares an object passed to a function. `T` is a fixed-size struct, a `FixedArray<T, N>` or a `FixedVec<T, N>`
1074
+ * (whose `push`, `pop` and `clear` the caller sees too). The caller passes a writable place: a local struct or list,
1075
+ * a `loadBorsh` local, a field of a `Mut<T>` account or of a `loadRawMut` view, a list element (`book.levels.get(i)`)
1076
+ * or another `Inout` parameter. Two arguments of one call may not overlap: places that may be the same bytes only at
1077
+ * run time (two `Dup` accounts, a writable and a read-only account, two native views under `NATIVE_ACCOUNT_WRITES`) are
1078
+ * compared before the call, an Inout one with every other argument passed in place (2040 when they overlap). Other
1079
+ * struct parameters stay read-only.
1080
+ * Integers are values in JS too: return the new value, or wrap it in a struct.
1081
+ */
1082
+ export type Inout<T> = T;
1083
+
898
1084
  /** A PDA seed: a string, a Pubkey, a Hash (its 32 bytes), an integer (at its width) or a unit enum value (one byte). */
899
1085
  export type Seed = string | Pubkey | Hash | bigint | number;
900
1086
 
@@ -2232,8 +2418,22 @@ export declare enum ProgramError {
2232
2418
  * `location` ("program/src/instruction.rs:482:17") is the place the runtime reports, as the original's panic gives it;
2233
2419
  * by default this source line. A `null` message logs nothing, for a program whose panic handler writes no line of its
2234
2420
  * own: `panic(null, "src/lib.rs:105:19")` leaves only the runtime's "SBF program Panicked in src/lib.rs at 105:19".
2421
+ * Under `export const PANIC_STYLE = "solana-1.14"` (a program built with solana-program 1.14 to 1.17) it logs
2422
+ * `panicked at '<message>', <location>` and aborts, so the runtime reports "SBF program panicked", as that panic
2423
+ * handler does; a message is then required.
2235
2424
  */
2236
2425
  export declare function panic(message?: string | null, location?: string): never;
2426
+ /**
2427
+ * `value`, with its checked arithmetic panicking as Rust's overflow checks do at `location` ("file:line:column", where
2428
+ * the original's expression is): `panicsAt("src/quantities.rs:233:1", a + b)` logs "attempt to add with overflow"
2429
+ * there (also "attempt to subtract / multiply with overflow", "attempt to divide by zero", "attempt to calculate the
2430
+ * remainder with a divisor of zero", "attempt to negate with overflow", "attempt to divide with overflow", "attempt to
2431
+ * shift left / right with overflow" for a shift amount past the width), as `panic` logs (see `PANIC_STYLE`), instead
2432
+ * of failing with 5100 / 5101. Only the operators written inside it, not the helpers it calls; u256 arithmetic, `**`,
2433
+ * `mulDiv`, casts and the bits `<<` loses keep TypeSOL's failures (Rust checks them otherwise, or with other messages); an inner `panicsAt`
2434
+ * gives its own operators their own location. As a statement it takes an assignment: `panicsAt(loc, total += fee);`.
2435
+ */
2436
+ export declare function panicsAt<T>(location: string, value: T): T;
2237
2437
  /**
2238
2438
  * Stop the program as Rust's `abort` does on SBF (the allocator's out-of-memory error, `std::process::abort`): the
2239
2439
  * runtime's `abort` syscall, which fails the instruction with ProgramFailedToComplete and logs "SBF program panicked"
@@ -2299,6 +2499,23 @@ export declare function emit<T>(event: T): void;
2299
2499
  */
2300
2500
  export declare function returnData<T extends bigint | boolean | Pubkey | Hash | object>(program: AccountMeta): T;
2301
2501
 
2502
+ /**
2503
+ * Set this instruction's return data to `bytes` as they are (Rust's `set_return_data(&bytes)`): a list of u8 such as a
2504
+ * local `FixedVec<u8, N>`, a Vec or FixedVec in account data, or the instruction data or a slice of it. For a native
2505
+ * port's `@fallback` entry and helpers (`set_return_data(order_ids.try_to_vec()?.as_ref())`), and void instructions.
2506
+ * The last call wins, and a CPI made afterwards replaces it with the callee's (call it after the last CPI, as Rust
2507
+ * must). More than 1024 bytes fails as Rust's call does: the runtime refuses it ("Return data too large").
2508
+ * An instruction that returns a value cannot call it (its return value is its return data); a helper it calls may, and
2509
+ * the instruction's return value, set last, replaces what the helper set.
2510
+ */
2511
+ export declare function setReturnData(bytes: IndexedList<u8>): void;
2512
+
2513
+ /**
2514
+ * Log the compute units left (Rust's `sol_log_compute_units()`): the runtime writes "Program consumption: <N> units
2515
+ * remaining", as the original's line, at the same place.
2516
+ */
2517
+ export declare function logComputeUnits(): void;
2518
+
2302
2519
  /**
2303
2520
  * Verify `account.key` is the PDA for `seeds` + `bump` under this program. Any bump that gives a PDA passes (Anchor's
2304
2521
  * `bump = expr`): use a stored bump, not one from the arguments, or the caller picks among up to 256 addresses.