@typesol/cli 0.41.0 → 0.42.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 +92 -0
- package/README.md +2 -1
- package/dist/compiler/analyze.js +41 -1
- package/dist/compiler/emit/base.d.ts +40 -7
- package/dist/compiler/emit/base.js +33 -0
- package/dist/compiler/emit/borsh.d.ts +7 -0
- package/dist/compiler/emit/borsh.js +86 -1
- package/dist/compiler/emit/calls.d.ts +102 -2
- package/dist/compiler/emit/calls.js +663 -49
- package/dist/compiler/emit/cpi-core.d.ts +9 -3
- package/dist/compiler/emit/cpi-core.js +12 -7
- package/dist/compiler/emit/cpi.js +8 -14
- package/dist/compiler/emit/create.d.ts +3 -3
- package/dist/compiler/emit/create.js +33 -25
- package/dist/compiler/emit/expressions.js +51 -2
- package/dist/compiler/emit/function.js +2 -2
- package/dist/compiler/emit/outline.d.ts +24 -0
- package/dist/compiler/emit/outline.js +100 -0
- package/dist/compiler/emit/program.js +7 -2
- package/dist/compiler/emit/shared.d.ts +35 -1
- package/dist/compiler/emit/shared.js +9 -1
- package/dist/compiler/emit/statements.d.ts +31 -0
- package/dist/compiler/emit/statements.js +272 -14
- package/dist/compiler/emit/storage.d.ts +27 -1
- package/dist/compiler/emit/storage.js +124 -11
- package/dist/compiler/emit/token.d.ts +11 -8
- package/dist/compiler/emit/token.js +233 -104
- package/dist/compiler/hints.js +6 -0
- package/dist/compiler/model.d.ts +20 -1
- package/dist/compiler/model.js +5 -0
- package/dist/testing-stake.d.ts +216 -0
- package/dist/testing-stake.js +526 -0
- package/dist/testing.d.ts +1 -0
- package/dist/testing.js +1 -0
- package/docs/architecture.md +1 -1
- package/docs/case-study-spl-stake-pool.md +122 -0
- package/docs/errors.md +1 -1
- package/docs/language.md +107 -14
- package/docs/security-model.md +4 -0
- package/docs/stability.md +13 -0
- package/docs/test-your-program.md +47 -0
- package/docs/testing.md +19 -0
- package/lib/typesol.d.ts +241 -40
- package/llms-full.txt +172 -15
- package/llms.txt +1 -1
- package/package.json +1 -1
- package/runtime/typesol_rt.h +69 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
|
-
import { heapVecOf, resolveScalarType } from "../analyze.js";
|
|
2
|
+
import { heapVecOf, resolveScalarType, structNameOf, typesolName } from "../analyze.js";
|
|
3
3
|
import { INT_INFO, isScalar, isWritable, sameType, scalarSize, typeName } from "../model.js";
|
|
4
4
|
import { U64, BOOL, PUBKEY, STRING, optC, fieldC, fixedVecC, storageDecl, vecCount, vecPlace, emptySizeC } from "./shared.js";
|
|
5
5
|
import { AliasEmitter } from "./alias.js";
|
|
@@ -134,22 +134,86 @@ export class StorageEmitter extends AliasEmitter {
|
|
|
134
134
|
const r = this.probe(() => this.ref(node));
|
|
135
135
|
return r?.type.kind === "struct";
|
|
136
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* A local list lives in the function's stack frame (4 KiB on SBPF v0, shared with every other local and the helpers
|
|
139
|
+
* clang inlines): one list may take at most 2 KiB, so the failure is a clear compile error, not clang's frame error.
|
|
140
|
+
*/
|
|
141
|
+
checkLocalListSize(t, at, what = typeName(t)) {
|
|
142
|
+
const bytes = this.fieldBytes(t);
|
|
143
|
+
const smaller = t.kind === "vec" ? "FixedVec<T, N>" : "FixedArray<T, N>";
|
|
144
|
+
if (bytes > 2048)
|
|
145
|
+
this.fail(at, `${what} takes ${bytes} bytes; locals live on the 4 KiB stack, so keep it under 2 KiB (a smaller ${smaller})`);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* `FixedVec<T, N>` / `FixedArray<T, N>` written as a local's type: T an integer, boolean, Pubkey, enum,
|
|
149
|
+
* `FixedString<N>` or fixed-size struct, N a literal. Undefined for other types; a list type with anything else fails.
|
|
150
|
+
*/
|
|
151
|
+
listAnnotation(t) {
|
|
152
|
+
const list = typesolName(t, this.scope.imports);
|
|
153
|
+
if ((list !== "FixedVec" && list !== "FixedArray") || !ts.isTypeReferenceNode(t))
|
|
154
|
+
return undefined;
|
|
155
|
+
const [elemNode, nNode] = t.typeArguments ?? [];
|
|
156
|
+
const n = nNode && ts.isLiteralTypeNode(nNode) && ts.isNumericLiteral(nNode.literal) ? Number(nNode.literal.text) : NaN;
|
|
157
|
+
if (!Number.isInteger(n) || n < 1)
|
|
158
|
+
this.fail(t, `${list}<T, N>: N must be a literal of at least 1`);
|
|
159
|
+
const elem = elemNode && this.localElem(elemNode);
|
|
160
|
+
if (!elem)
|
|
161
|
+
this.fail(elemNode ?? t, `${list}<T, N> as a local: T is an integer, boolean, Pubkey, enum, FixedString<N> or a struct of fixed size`);
|
|
162
|
+
return list === "FixedVec" ? { kind: "vec", elem, cap: n } : { kind: "array", elem, len: n };
|
|
163
|
+
}
|
|
164
|
+
/** A local list's element type: a scalar, a FixedString<N> or a fixed-size struct (no Vec, no heap). */
|
|
165
|
+
localElem(node) {
|
|
166
|
+
const scalar = resolveScalarType(node, this.scope);
|
|
167
|
+
if (scalar)
|
|
168
|
+
return scalar;
|
|
169
|
+
if (typesolName(node, this.scope.imports) === "FixedString") {
|
|
170
|
+
const a = node.typeArguments?.[0];
|
|
171
|
+
const cap = a && ts.isLiteralTypeNode(a) && ts.isNumericLiteral(a.literal) ? Number(a.literal.text) : NaN;
|
|
172
|
+
if (!Number.isInteger(cap) || cap < 1)
|
|
173
|
+
return undefined;
|
|
174
|
+
this.model.fstrings.add(cap);
|
|
175
|
+
return { kind: "fstring", cap };
|
|
176
|
+
}
|
|
177
|
+
const sn = structNameOf(node, this.scope);
|
|
178
|
+
const s = sn ? this.model.structs.get(sn) : undefined;
|
|
179
|
+
if (s && s.size !== undefined && !s.vec && !heapVecOf(s))
|
|
180
|
+
return { kind: "struct", name: s.name };
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
137
183
|
/** `fixedArray<T, N>(fill)` as a local declaration. */
|
|
138
|
-
localArray(init, c) {
|
|
139
|
-
|
|
140
|
-
const
|
|
184
|
+
localArray(init, c, annotation) {
|
|
185
|
+
// The type arguments, or the declaration's `FixedArray<T, N>` when the call has none.
|
|
186
|
+
const fromType = !init.typeArguments?.length && annotation && ts.isTypeReferenceNode(annotation) ? annotation.typeArguments : undefined;
|
|
187
|
+
const [elemNode, lenNode] = init.typeArguments ?? fromType ?? [];
|
|
188
|
+
const scalar = elemNode && resolveScalarType(elemNode, this.scope);
|
|
189
|
+
// A struct (or FixedString) element: the fill is built once and copied into every slot, as JS fills with one value.
|
|
190
|
+
const elem = scalar ?? (elemNode && this.localElem(elemNode));
|
|
141
191
|
if (!elem)
|
|
142
|
-
this.fail(init, "fixedArray<T, N>(fill): T must be an integer, boolean or
|
|
192
|
+
this.fail(init, "fixedArray<T, N>(fill): T must be an integer, boolean, Pubkey, enum, FixedString<N> or a struct of fixed size");
|
|
143
193
|
const len = lenNode && ts.isLiteralTypeNode(lenNode) && ts.isNumericLiteral(lenNode.literal) ? Number(lenNode.literal.text) : NaN;
|
|
144
194
|
if (!Number.isInteger(len) || len < 1 || len > 256)
|
|
145
195
|
this.fail(init, "fixedArray<T, N>: N must be a literal 1..256 (locals live on the 4KB stack)");
|
|
146
196
|
if (init.arguments.length !== 1)
|
|
147
197
|
this.fail(init, "fixedArray<T, N>(fill) takes the fill value");
|
|
148
|
-
const fill = this.expect(this.expr(init.arguments[0], elem), elem, init.arguments[0]);
|
|
149
198
|
const type = { kind: "array", elem, len };
|
|
150
|
-
this.line(`${storageDecl(type, c)};`);
|
|
151
199
|
const i = this.tmp("i");
|
|
152
|
-
|
|
200
|
+
if (scalar) {
|
|
201
|
+
const fill = this.expect(this.expr(init.arguments[0], scalar), scalar, init.arguments[0]);
|
|
202
|
+
this.line(`${storageDecl(type, c)};`);
|
|
203
|
+
this.line(`for (uint64_t ${i} = 0; ${i} < ${len}; ${i}++) ${this.writeStorage({ lv: `${c}[${i}]`, type: elem, writable: true }, fill.c)};`);
|
|
204
|
+
return type;
|
|
205
|
+
}
|
|
206
|
+
this.checkLocalListSize(type, init);
|
|
207
|
+
const one = this.tmp("fill");
|
|
208
|
+
this.line(`${storageDecl(elem, one)};`);
|
|
209
|
+
this.line(`ts_memzero(&${one}, sizeof(${one}));`);
|
|
210
|
+
const vt = this.storageValueType(elem);
|
|
211
|
+
if (vt)
|
|
212
|
+
this.line(this.writeStorage({ lv: one, type: elem, writable: true }, this.expect(this.expr(init.arguments[0], vt), vt, init.arguments[0]).c) + ";");
|
|
213
|
+
else
|
|
214
|
+
this.writeAggregate({ lv: one, type: elem, writable: true }, init.arguments[0], true);
|
|
215
|
+
this.line(`${storageDecl(type, c)};`);
|
|
216
|
+
this.line(`for (uint64_t ${i} = 0; ${i} < ${len}; ${i}++) ts_memcpy(&${c}[${i}], &${one}, sizeof(${one}));`);
|
|
153
217
|
return type;
|
|
154
218
|
}
|
|
155
219
|
/** Resolve an expression to a storage location, if it names one. */
|
|
@@ -179,7 +243,7 @@ export class StorageEmitter extends AliasEmitter {
|
|
|
179
243
|
return { lv: l.c, type: l.type.option, writable: false };
|
|
180
244
|
// A struct loaded from a remaining account.
|
|
181
245
|
if (l && l.type.kind === "structRef")
|
|
182
|
-
return { lv: `(*${l.c})`, type: { kind: "struct", name: l.type.struct }, writable: l.type.writable };
|
|
246
|
+
return { lv: `(*${l.c})`, type: { kind: "struct", name: l.type.struct }, writable: l.type.writable, ...(l.bigCap ? { bigCap: l.bigCap } : {}) };
|
|
183
247
|
if (l && l.type.kind === "enumRef")
|
|
184
248
|
return { lv: `(*${l.c})`, type: { kind: "dataEnum", name: l.type.name }, writable: l.type.writable };
|
|
185
249
|
return undefined;
|
|
@@ -288,7 +352,20 @@ export class StorageEmitter extends AliasEmitter {
|
|
|
288
352
|
if (!f)
|
|
289
353
|
this.fail(node.name, `${s.name} has no field "${field}"`);
|
|
290
354
|
const r = { ...base, lv: `${base.lv}.${f.name}`, type: f.type, path: base.path && [...base.path, f.name], vec: undefined };
|
|
291
|
-
if (f.type.kind === "vec" && f.type.
|
|
355
|
+
if (f.type.kind === "vec" && f.type.big) {
|
|
356
|
+
// A loadRaw view sits at the account's data, so its room is what the account holds past the struct.
|
|
357
|
+
const elemSize = s.bigVec.elemSize;
|
|
358
|
+
r.vec = {
|
|
359
|
+
len: `${base.lv}.${f.name}__len`,
|
|
360
|
+
items: r.lv,
|
|
361
|
+
fixed: "",
|
|
362
|
+
elemSize,
|
|
363
|
+
resizable: false,
|
|
364
|
+
// Computed once at the load for a view bound to a local; for one used in place, here.
|
|
365
|
+
big: base.bigCap ?? `ts_bigvec_cap((const uint8_t *)&${base.lv}, sizeof(TSS_${s.name}), ${elemSize})`,
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
else if (f.type.kind === "vec" && f.type.heap)
|
|
292
369
|
r.vec = this.heapVec(r.lv, f.type.elem);
|
|
293
370
|
// Vecs of loaded accounts can be read and updated in place but not resized (no entry length known).
|
|
294
371
|
else if (f.type.kind === "vec" && f.type.cap === undefined) {
|
|
@@ -301,6 +378,16 @@ export class StorageEmitter extends AliasEmitter {
|
|
|
301
378
|
}
|
|
302
379
|
return undefined;
|
|
303
380
|
}
|
|
381
|
+
// bigVec.slice(start, end): a view of elements start..end-1, for for … of and the list methods.
|
|
382
|
+
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === "slice") {
|
|
383
|
+
const cached = this.sliceViews.get(node);
|
|
384
|
+
if (cached)
|
|
385
|
+
return cached;
|
|
386
|
+
const base = this.ref(node.expression.expression);
|
|
387
|
+
if (base?.vec?.big !== undefined && !base.vec.slice)
|
|
388
|
+
return this.bigVecSlice(node, base);
|
|
389
|
+
return undefined;
|
|
390
|
+
}
|
|
304
391
|
// arr.get(i) / vec.get(i)
|
|
305
392
|
if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === "get") {
|
|
306
393
|
const owner = node.expression.expression;
|
|
@@ -418,6 +505,8 @@ export class StorageEmitter extends AliasEmitter {
|
|
|
418
505
|
this.fail(node, `this is a ${r.type.name}; compare .kind or read a field of the narrowed variant`);
|
|
419
506
|
case "array":
|
|
420
507
|
case "vec":
|
|
508
|
+
if (r.vec?.slice)
|
|
509
|
+
this.fail(node, "a slice is not stored: use it where it is written (for (const x of list.slice(a, b)), list.slice(a, b).forEach(…), .get(i), .length)");
|
|
421
510
|
this.fail(node, "this is a list; use .get(i), .set(i, v) or .length");
|
|
422
511
|
case "struct":
|
|
423
512
|
this.fail(node, `this is a ${r.type.name}; read one of its fields`);
|
|
@@ -637,7 +726,9 @@ export class StorageEmitter extends AliasEmitter {
|
|
|
637
726
|
const heap = target.type.kind === "vec" && !!target.type.heap;
|
|
638
727
|
if (target.type.kind === "vec" && (target.type.cap !== undefined || heap) && target.type.elem.kind !== "string" && target.type.elem.kind !== "fstring") {
|
|
639
728
|
const t = target.type;
|
|
640
|
-
|
|
729
|
+
// A slice of a Vec argument or the instruction data is a view like the list itself.
|
|
730
|
+
const view = !ts.isIdentifier(valueNode) && this.isVecArgView(valueNode) ? this.vecArgView(valueNode) : undefined;
|
|
731
|
+
const l = ts.isIdentifier(valueNode) ? this.lookup(valueNode.text) : view;
|
|
641
732
|
const r = l?.type.kind === "vecArg" ? undefined : this.reporter.guard(() => this.ref(valueNode));
|
|
642
733
|
const from = l?.type.kind === "vecArg" && sameType(l.type.elem, t.elem)
|
|
643
734
|
? { items: `${l.c}.ptr`, count: `((uint64_t)${l.c}.len)` }
|
|
@@ -685,6 +776,28 @@ export class StorageEmitter extends AliasEmitter {
|
|
|
685
776
|
heapVec(lv, elem) {
|
|
686
777
|
return { len: `${lv}.len`, items: `${lv}.items`, fixed: "", elemSize: this.fieldBytes(elem), resizable: false, heap: true };
|
|
687
778
|
}
|
|
779
|
+
/** Slices lowered once before a loop over them (see eachElement): the loop's length and element reads reuse them. */
|
|
780
|
+
sliceViews = new Map();
|
|
781
|
+
/**
|
|
782
|
+
* `bigVec.slice(start, end)`: elements start..end-1 in place, as BigVec::deserialize_mut_slice(start, end - start)
|
|
783
|
+
* gives them. Out of range fails with ProgramError::AccountDataTooSmall, as that does (JS's slice would clamp).
|
|
784
|
+
*/
|
|
785
|
+
bigVecSlice(node, base) {
|
|
786
|
+
const args = node.arguments;
|
|
787
|
+
if (args.length < 1 || args.length > 2)
|
|
788
|
+
this.fail(node, "slice(start, end) takes the first element's index and the index after the last one");
|
|
789
|
+
const info = base.vec;
|
|
790
|
+
const start = this.tmp("sl_start");
|
|
791
|
+
const end = this.tmp("sl_end");
|
|
792
|
+
this.line(`uint64_t ${start} = ${this.expect(this.expr(args[0], U64), U64, args[0]).c};`);
|
|
793
|
+
this.line(`uint64_t ${end} = ${args[1] ? this.expect(this.expr(args[1], U64), U64, args[1]).c : vecCount(info)};`);
|
|
794
|
+
this.line(`if (${end} > ${vecCount(info)} || ${start} > ${end}) ${this.programErrorFail("AccountDataTooSmall", node)};`);
|
|
795
|
+
// Named in scope, so a loop over the slice moved into a function of its own is given them (see outlinedLoop).
|
|
796
|
+
for (const c of [start, end])
|
|
797
|
+
this.scopes.at(-1).set(`__${c}`, { c, type: U64, mutable: false });
|
|
798
|
+
const items = `((${fieldC(base.type.elem)} *)${info.items} + ${start})`;
|
|
799
|
+
return { ...base, lv: items, path: undefined, vec: { ...info, len: `(${end} - ${start})`, items, slice: true } };
|
|
800
|
+
}
|
|
688
801
|
/** A place whose type is a FixedVec (or a heap Vec) gets its Vec view. */
|
|
689
802
|
withFixedVec(r) {
|
|
690
803
|
if (r.type.kind === "vec" && r.type.cap !== undefined)
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
|
-
import { type
|
|
2
|
+
import { type CpiProgram, type Value } from "./shared.js";
|
|
3
3
|
import { CreateEmitter } from "./create.js";
|
|
4
4
|
/** SPL Token and Token-2022 instructions (`tokenProgram.transfer(…)` and the rest) and the ATA program's. */
|
|
5
5
|
export declare abstract class TokenEmitter extends CreateEmitter {
|
|
6
|
-
protected tokenOp(node: ts.CallExpression, program:
|
|
7
|
-
protected ataOp(node: ts.CallExpression, program:
|
|
6
|
+
protected tokenOp(node: ts.CallExpression, program: CpiProgram, method: string): undefined;
|
|
7
|
+
protected ataOp(node: ts.CallExpression, program: CpiProgram, method: string): undefined;
|
|
8
8
|
/** memoProgram.memo(text, signers?, signerSeeds?): SPL Memo v2, the UTF-8 text as data, each signer a signing account. */
|
|
9
|
-
protected memoOp(node: ts.CallExpression, program:
|
|
9
|
+
protected memoOp(node: ts.CallExpression, program: CpiProgram, method: string): undefined;
|
|
10
10
|
/**
|
|
11
|
-
* The Stake program (bincode: a u32 instruction tag
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* The Stake program (bincode: a u32 instruction tag, as solana-stake-interface's StakeInstruction numbers them):
|
|
12
|
+
* initialize, authorize, delegate, split, deactivate, withdraw, merge and getMinimumDelegation, with the account
|
|
13
|
+
* metas of its instruction builders and the sysvar and config accounts it reads passed in. Each authority signs (a
|
|
14
|
+
* PDA of this program with `signerSeeds`).
|
|
14
15
|
*/
|
|
15
|
-
protected stakeOp(node: ts.CallExpression, program:
|
|
16
|
+
protected stakeOp(node: ts.CallExpression, program: CpiProgram, method: string): Value | undefined;
|
|
17
|
+
/** A stake Lockup { unix_timestamp: i64, epoch: u64, custodian: Pubkey } from `{ unixTimestamp, epoch, custodian }`. */
|
|
18
|
+
protected stakeLockup(n: ts.Expression, put: (src: string, n: string, bytes?: number) => void): void;
|
|
16
19
|
}
|