@typesol/cli 0.44.1 → 0.46.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 +343 -0
- package/NOTICE +14 -1
- package/README.md +115 -26
- package/SECURITY.md +10 -8
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +9 -0
- package/dist/cli.js +226 -62
- package/dist/compiler/analyze.js +57 -6
- package/dist/compiler/diagnostics.d.ts +4 -3
- package/dist/compiler/diagnostics.js +10 -6
- package/dist/compiler/emit/base.d.ts +1 -1
- package/dist/compiler/emit/calls.js +18 -5
- package/dist/compiler/emit/cpi-core.d.ts +3 -1
- package/dist/compiler/emit/cpi-core.js +8 -4
- package/dist/compiler/emit/expressions.d.ts +14 -0
- package/dist/compiler/emit/expressions.js +52 -4
- package/dist/compiler/emit/shared.d.ts +8 -1
- package/dist/compiler/emit/shared.js +8 -1
- package/dist/compiler/emit/statements.d.ts +8 -3
- package/dist/compiler/emit/statements.js +68 -3
- package/dist/compiler/emit/storage.js +5 -1
- package/dist/compiler/hints.d.ts +7 -0
- package/dist/compiler/hints.js +58 -9
- package/dist/compiler/index.d.ts +56 -1
- package/dist/compiler/index.js +27 -3
- package/dist/compiler/model.js +3 -2
- package/dist/compiler/toolchain.d.ts +73 -2
- package/dist/compiler/toolchain.js +222 -17
- package/dist/node-version.d.ts +4 -0
- package/dist/node-version.js +12 -0
- package/dist/tools/docker.d.ts +183 -0
- package/dist/tools/docker.js +442 -0
- package/dist/tools/doctor.d.ts +44 -0
- package/dist/tools/doctor.js +108 -14
- package/dist/workspace.d.ts +11 -3
- package/dist/workspace.js +46 -9
- package/docker/Dockerfile +163 -0
- package/docker/deps/0.43.0.lock.json +1206 -0
- package/docker/deps/0.44.0.lock.json +1206 -0
- package/docker/deps/0.44.1.lock.json +1206 -0
- package/docker/deps/0.45.0.lock.json +1206 -0
- package/docker/deps/0.46.0.lock.json +1206 -0
- package/docs/ai.md +2 -1
- package/docs/architecture.md +18 -6
- package/docs/audit.md +15 -7
- package/docs/case-study-phoenix.md +1 -1
- package/docs/case-study-raydium-amm.md +1 -1
- package/docs/case-study-spl-stake-pool.md +1 -1
- package/docs/cli.md +71 -6
- package/docs/errors.md +22 -8
- package/docs/glossary.md +210 -0
- package/docs/language-accounts.md +499 -0
- package/docs/language-basics.md +389 -0
- package/docs/language-builtins.md +411 -0
- package/docs/language-chain.md +198 -0
- package/docs/language-cpi.md +470 -0
- package/docs/language-errors-events.md +184 -0
- package/docs/language-native.md +493 -0
- package/docs/language-pdas.md +198 -0
- package/docs/language-statements.md +620 -0
- package/docs/language-types.md +601 -0
- package/docs/language.md +190 -467
- package/docs/mainnet.md +103 -8
- package/docs/migrate-from-anchor.md +7 -6
- package/docs/security-model.md +4 -4
- package/docs/stability.md +64 -3
- package/docs/test-your-program.md +52 -15
- package/docs/testing.md +49 -2
- package/docs/troubleshooting.md +210 -42
- package/lib/typesol.d.ts +67 -22
- package/llms-full.txt +5084 -591
- package/llms.txt +12 -1
- package/package.json +4 -2
- package/releases/0.43.0.json +669 -0
- package/releases/0.44.0.json +672 -0
- package/releases/0.44.1.json +672 -0
- package/releases/0.45.0.json +732 -0
- package/runtime/LICENSE-compiler-builtins.txt +275 -0
- package/runtime/typesol_builtins.c +484 -0
- package/ts-plugin/index.js +16 -5
|
@@ -14,7 +14,7 @@ const aliasUses = new WeakMap();
|
|
|
14
14
|
export function markAliasUse(copy, use) {
|
|
15
15
|
aliasUses.set(copy, use);
|
|
16
16
|
}
|
|
17
|
-
export function diagnosticAt(node, message) {
|
|
17
|
+
export function diagnosticAt(node, message, hint) {
|
|
18
18
|
// A problem with an aliased type is reported where the alias is used.
|
|
19
19
|
for (let n = node; n; n = n.parent) {
|
|
20
20
|
const use = aliasUses.get(n);
|
|
@@ -26,16 +26,20 @@ export function diagnosticAt(node, message) {
|
|
|
26
26
|
const sf = node.getSourceFile();
|
|
27
27
|
const start = node.getStart(sf);
|
|
28
28
|
const { line, character } = sf.getLineAndCharacterOfPosition(start);
|
|
29
|
-
|
|
29
|
+
const length = Math.max(1, node.getEnd() - start);
|
|
30
|
+
if (!hint)
|
|
31
|
+
return { file: sf.fileName, line: line + 1, column: character + 1, message, length };
|
|
32
|
+
return { file: sf.fileName, line: line + 1, column: character + 1, message: `${message}\nhint: ${hint}`, length, hint };
|
|
30
33
|
}
|
|
31
34
|
/** Collects diagnostics; `fail` records one and throws to unwind the current construct. */
|
|
32
35
|
export class Reporter {
|
|
33
36
|
diagnostics = [];
|
|
34
|
-
|
|
35
|
-
|
|
37
|
+
/** `hint`: what to write instead, shown as the message's last line ("hint: …") and given to tools as `hint`. */
|
|
38
|
+
error(node, message, hint) {
|
|
39
|
+
this.diagnostics.push(diagnosticAt(node, message, hint));
|
|
36
40
|
}
|
|
37
|
-
fail(node, message) {
|
|
38
|
-
this.error(node, message);
|
|
41
|
+
fail(node, message, hint) {
|
|
42
|
+
this.error(node, message, hint);
|
|
39
43
|
throw new Bail();
|
|
40
44
|
}
|
|
41
45
|
/** Run `fn`, swallowing a `fail` so analysis can continue with the next construct. */
|
|
@@ -173,7 +173,7 @@ export declare abstract class EmitterBase {
|
|
|
173
173
|
protected abstract accountChoice(node: ts.ConditionalExpression): Value;
|
|
174
174
|
protected abstract instructionsParam(node: ts.Expression): AccountParam | undefined;
|
|
175
175
|
protected abstract isTxIx(node: ts.Expression): boolean;
|
|
176
|
-
protected abstract seeds(node: ts.Expression, extra: number, bound?: boolean): {
|
|
176
|
+
protected abstract seeds(node: ts.Expression, extra: number, bound?: boolean, withBump?: boolean): {
|
|
177
177
|
name: string;
|
|
178
178
|
count: number;
|
|
179
179
|
idl: IdlSeed[] | undefined;
|
|
@@ -166,6 +166,7 @@ export class CallEmitter extends ExpressionEmitter {
|
|
|
166
166
|
this.line(`sol_log_((const char *)${t}.ptr, ${t}.len);`);
|
|
167
167
|
}
|
|
168
168
|
else {
|
|
169
|
+
this.refuseF64Text(args[0], v.type);
|
|
169
170
|
let value = v.literal !== undefined ? this.expect(v, U64, args[0]) : v;
|
|
170
171
|
// An Option TypeScript has narrowed to its value (after `x === null` was ruled out): log the value.
|
|
171
172
|
if (value.type.kind === "option") {
|
|
@@ -319,6 +320,7 @@ export class CallEmitter extends ExpressionEmitter {
|
|
|
319
320
|
vals[j] = this.settle(vals[j], mark);
|
|
320
321
|
vals.push(v);
|
|
321
322
|
}
|
|
323
|
+
this.refuseF64(node, `${builtin}()`, ...vals);
|
|
322
324
|
const typed = vals.find((v) => v.literal === undefined);
|
|
323
325
|
if (typed?.type.kind === "int" && typed.type.name === "u256") {
|
|
324
326
|
// u256: a 512-bit product in the runtime.
|
|
@@ -344,13 +346,16 @@ export class CallEmitter extends ExpressionEmitter {
|
|
|
344
346
|
if (args.length !== 1)
|
|
345
347
|
this.fail(node, `${builtin}(x) takes one ${builtin === "f64Bits" ? "f64" : "u64"}`);
|
|
346
348
|
const v = this.expr(args[0]);
|
|
347
|
-
const
|
|
348
|
-
|
|
349
|
+
const f64 = { kind: "int", name: "u64", f64: true };
|
|
350
|
+
const from = builtin === "f64Bits" ? f64 : U64;
|
|
351
|
+
const x = v.literal !== undefined && builtin === "f64FromBits" ? this.literal(v.literal, U64, args[0]) : this.expect(v, from, args[0]);
|
|
352
|
+
return { c: x.c, type: builtin === "f64Bits" ? U64 : f64 };
|
|
349
353
|
}
|
|
350
354
|
case "isqrt": {
|
|
351
355
|
if (args.length !== 1)
|
|
352
356
|
this.fail(node, "isqrt(x) takes one unsigned integer");
|
|
353
357
|
const v = this.expr(args[0]);
|
|
358
|
+
this.refuseF64(node, "isqrt()", v);
|
|
354
359
|
const x = v.literal !== undefined ? this.literal(v.literal, U64, args[0]) : v;
|
|
355
360
|
if (x.type.kind !== "int" || INT_INFO[x.type.name].signed)
|
|
356
361
|
this.fail(args[0], "isqrt needs an unsigned integer (u8 … u256)");
|
|
@@ -367,6 +372,7 @@ export class CallEmitter extends ExpressionEmitter {
|
|
|
367
372
|
const mark = this.lines.length;
|
|
368
373
|
let b = this.expr(args[1], a.type.kind === "int" && a.literal === undefined ? a.type : undefined);
|
|
369
374
|
a = this.settle(a, mark);
|
|
375
|
+
this.refuseF64(node, `${builtin}()`, a, b);
|
|
370
376
|
if (a.literal !== undefined && b.literal !== undefined) {
|
|
371
377
|
a = this.literal(a.literal, U64, args[0]);
|
|
372
378
|
b = this.literal(b.literal, U64, args[1]);
|
|
@@ -655,7 +661,7 @@ export class CallEmitter extends ExpressionEmitter {
|
|
|
655
661
|
// on the curve (Rust's Err(PubkeyError::InvalidSeeds)) or are too many / too long.
|
|
656
662
|
if (args.length !== 2)
|
|
657
663
|
this.fail(node, "createProgramAddress(seeds, program) takes the seeds (bump last) and the program's address");
|
|
658
|
-
const seeds = this.seeds(args[0], 0);
|
|
664
|
+
const seeds = this.seeds(args[0], 0, false, true);
|
|
659
665
|
const program = this.expect(this.expr(args[1], PUBKEY), PUBKEY, args[1]);
|
|
660
666
|
const t = this.tmp("cpa");
|
|
661
667
|
this.model.options.set("pubkey", PUBKEY);
|
|
@@ -1086,12 +1092,19 @@ export class CallEmitter extends ExpressionEmitter {
|
|
|
1086
1092
|
"Math.min": "min(a, b)",
|
|
1087
1093
|
"Math.max": "max(a, b)",
|
|
1088
1094
|
"Date.now": "now() (the Clock's unix timestamp)",
|
|
1095
|
+
"Math.floor": "integer division (a / b with bigints rounds toward zero)",
|
|
1096
|
+
"Math.trunc": "integer division (a / b with bigints rounds toward zero)",
|
|
1097
|
+
"Math.ceil": "integer division rounded up: (a + b - 1n) / b",
|
|
1098
|
+
"Math.round": "integer division rounded: (a + b / 2n) / b",
|
|
1089
1099
|
"JSON.stringify": "log(…) with the values",
|
|
1090
1100
|
Number: "u64(x) or another integer cast",
|
|
1091
1101
|
BigInt: "u64(x) or another integer cast",
|
|
1092
1102
|
String: "a FixedString<N> field, or log(…)",
|
|
1093
1103
|
};
|
|
1094
|
-
|
|
1104
|
+
// Math.floor(Date.now() / 1000): the time in seconds, which is what now() gives.
|
|
1105
|
+
if (/^Math\.(floor|trunc|round|ceil)$/.test(text) && ts.isCallExpression(callee.parent) && /\bDate\.now\(/.test(callee.parent.getText()))
|
|
1106
|
+
return `unsupported call: ${text}(Date.now() …); use now() from "typesol", the cluster's clock in Unix seconds (an i64)`;
|
|
1107
|
+
const hint = Object.hasOwn(hints, text) ? hints[text] : undefined;
|
|
1095
1108
|
if (hint)
|
|
1096
1109
|
return `unsupported call: ${text}; use ${hint}`;
|
|
1097
1110
|
if (ts.isPropertyAccessExpression(callee) && this.accountAt(callee.expression))
|
|
@@ -3043,7 +3056,7 @@ export class CallEmitter extends ExpressionEmitter {
|
|
|
3043
3056
|
if (n.arguments.length !== 1)
|
|
3044
3057
|
this.fail(n, "signer([...seeds]) takes the seeds (with the bump) as an array; signer([]) signs for nothing");
|
|
3045
3058
|
// Bound to a name or given to a helper: the list outlives this expression, so its seeds are copied now.
|
|
3046
|
-
return this.seeds(n.arguments[0], 0, true);
|
|
3059
|
+
return this.seeds(n.arguments[0], 0, true, true);
|
|
3047
3060
|
}
|
|
3048
3061
|
this.fail(n, "pass signer([...seeds with the bump]) or a SignerSeeds parameter");
|
|
3049
3062
|
}
|
|
@@ -7,8 +7,10 @@ export declare abstract class CpiCoreEmitter extends ViewEmitter {
|
|
|
7
7
|
/**
|
|
8
8
|
* Lower a seeds array literal into a `SolSignerSeed[]` with `extra` spare slots. Also describes
|
|
9
9
|
* the seeds in Anchor IDL terms when every element is a constant, an argument or an account path.
|
|
10
|
+
* `withBump`: the list ends with its bump (signer seeds, createProgramAddress), so it may hold MAX_SEEDS entries;
|
|
11
|
+
* otherwise the derivation adds the bump, and the list holds one fewer.
|
|
10
12
|
*/
|
|
11
|
-
protected seeds(node: ts.Expression, extra: number, bound?: boolean): {
|
|
13
|
+
protected seeds(node: ts.Expression, extra: number, bound?: boolean, withBump?: boolean): {
|
|
12
14
|
name: string;
|
|
13
15
|
count: number;
|
|
14
16
|
idl: IdlSeed[] | undefined;
|
|
@@ -9,12 +9,16 @@ export class CpiCoreEmitter extends ViewEmitter {
|
|
|
9
9
|
/**
|
|
10
10
|
* Lower a seeds array literal into a `SolSignerSeed[]` with `extra` spare slots. Also describes
|
|
11
11
|
* the seeds in Anchor IDL terms when every element is a constant, an argument or an account path.
|
|
12
|
+
* `withBump`: the list ends with its bump (signer seeds, createProgramAddress), so it may hold MAX_SEEDS entries;
|
|
13
|
+
* otherwise the derivation adds the bump, and the list holds one fewer.
|
|
12
14
|
*/
|
|
13
|
-
seeds(node, extra, bound = false) {
|
|
15
|
+
seeds(node, extra, bound = false, withBump = false) {
|
|
14
16
|
if (!ts.isArrayLiteralExpression(node))
|
|
15
17
|
this.fail(node, "seeds must be an array literal");
|
|
16
|
-
if (node.elements.length > MAX_SEEDS)
|
|
17
|
-
this.fail(node, `at most ${MAX_SEEDS} seeds`);
|
|
18
|
+
if (withBump && node.elements.length > MAX_SEEDS)
|
|
19
|
+
this.fail(node, `at most ${MAX_SEEDS} seeds, the bump included (Solana's limit)`);
|
|
20
|
+
if (!withBump && node.elements.length > MAX_SEEDS - 1)
|
|
21
|
+
this.fail(node, `at most ${MAX_SEEDS - 1} seeds: the bump the derivation adds makes ${MAX_SEEDS}, Solana's limit`);
|
|
18
22
|
const parts = [];
|
|
19
23
|
let idl = [];
|
|
20
24
|
// The array points at its seeds, which are read when the CPI runs. Seeds that are not constants are copied when
|
|
@@ -240,7 +244,7 @@ export class CpiCoreEmitter extends ViewEmitter {
|
|
|
240
244
|
if (l?.type.kind === "signerSeeds")
|
|
241
245
|
return { name: l.c, count: `${l.c}__n` };
|
|
242
246
|
}
|
|
243
|
-
return this.seeds(node, 0);
|
|
247
|
+
return this.seeds(node, 0, false, true);
|
|
244
248
|
}
|
|
245
249
|
/**
|
|
246
250
|
* Several PDAs signing one CPI: `[["vault", owner.key, vaultBump], ["fees", feeBump]]` (or SignerSeeds parameters
|
|
@@ -6,7 +6,21 @@ import { StorageEmitter } from "./storage.js";
|
|
|
6
6
|
* Expressions: literals, operators with checked arithmetic, property reads, conditionals, text and casts.
|
|
7
7
|
*/
|
|
8
8
|
export declare abstract class ExpressionEmitter extends StorageEmitter {
|
|
9
|
+
/**
|
|
10
|
+
* An f64 is held as its IEEE 754 bits in a u64 and TypeSOL has no float arithmetic: an integer operator on one
|
|
11
|
+
* would work on the bits (1.0 + 1.0 would add 0x3FF0… twice), so every operator with an f64 operand is refused.
|
|
12
|
+
* Equality too: the bits are not the number (NaN would equal itself, 0 would not equal -0), so comparing them is
|
|
13
|
+
* written out as f64Bits(a) === f64Bits(b).
|
|
14
|
+
*/
|
|
15
|
+
protected refuseF64(node: ts.Node, what: string, ...values: Value[]): void;
|
|
9
16
|
protected expect(v: Value, want: ValueType | undefined, node: ts.Node): Value;
|
|
17
|
+
/**
|
|
18
|
+
* An f64 is not an integer: its u64 of bits never becomes an integer's value, nor an integer an f64's, without
|
|
19
|
+
* saying so with f64Bits(x) / f64FromBits(bits). (Options are compared by their inner types.)
|
|
20
|
+
*/
|
|
21
|
+
/** An f64 as text (a template, log()): the digits would be its bits' integer, not the number. */
|
|
22
|
+
protected refuseF64Text(node: ts.Node, t: ValueType): void;
|
|
23
|
+
protected refuseF64Conversion(node: ts.Node, from: ValueType, to: ValueType): void;
|
|
10
24
|
/**
|
|
11
25
|
* Use an Option's value. TypeScript only allows this after narrowing (`x !== null`), so the runtime
|
|
12
26
|
* check is a backstop: an empty option aborts with 5108.
|
|
@@ -2,13 +2,27 @@ import ts from "typescript";
|
|
|
2
2
|
import { base58 } from "../../keys.js";
|
|
3
3
|
import { calleeName, resolveValueType } from "../analyze.js";
|
|
4
4
|
import { INT_INFO, unsignedC, optionKey, sameType, typeName } from "../model.js";
|
|
5
|
-
import { U64, U8, I64, BOOL, PUBKEY, STRING, HASH, cString, intLiteral, scalarStorageC, optC, valueC, vecCount } from "./shared.js";
|
|
5
|
+
import { U64, U8, I64, BOOL, PUBKEY, STRING, HASH, F64_TEXT, F64_TO_INT, INT_TO_F64, cString, intLiteral, scalarStorageC, optC, valueC, vecCount, } from "./shared.js";
|
|
6
6
|
import { StorageEmitter } from "./storage.js";
|
|
7
7
|
import { unsupportedSyntax } from "../hints.js";
|
|
8
8
|
/**
|
|
9
9
|
* Expressions: literals, operators with checked arithmetic, property reads, conditionals, text and casts.
|
|
10
10
|
*/
|
|
11
11
|
export class ExpressionEmitter extends StorageEmitter {
|
|
12
|
+
/**
|
|
13
|
+
* An f64 is held as its IEEE 754 bits in a u64 and TypeSOL has no float arithmetic: an integer operator on one
|
|
14
|
+
* would work on the bits (1.0 + 1.0 would add 0x3FF0… twice), so every operator with an f64 operand is refused.
|
|
15
|
+
* Equality too: the bits are not the number (NaN would equal itself, 0 would not equal -0), so comparing them is
|
|
16
|
+
* written out as f64Bits(a) === f64Bits(b).
|
|
17
|
+
*/
|
|
18
|
+
refuseF64(node, what, ...values) {
|
|
19
|
+
const isF64 = (t) => (t.kind === "int" && t.f64 === true) || (t.kind === "option" && isF64(t.inner));
|
|
20
|
+
if (!values.some((v) => isF64(v.type)))
|
|
21
|
+
return;
|
|
22
|
+
if (what === "===" || what === "!==" || what === "==" || what === "!=" || what === "switch")
|
|
23
|
+
this.fail(node, `${what} 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)`);
|
|
24
|
+
this.fail(node, `${what} 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)`);
|
|
25
|
+
}
|
|
12
26
|
expect(v, want, node) {
|
|
13
27
|
if (v.isNull) {
|
|
14
28
|
if (want?.kind !== "option")
|
|
@@ -28,12 +42,33 @@ export class ExpressionEmitter extends StorageEmitter {
|
|
|
28
42
|
}
|
|
29
43
|
if (v.type.kind === "option" && want.kind !== "option")
|
|
30
44
|
return this.expect(this.unwrap(v, node), want, node);
|
|
45
|
+
this.refuseF64Conversion(node, v.type, want);
|
|
31
46
|
if (v.literal !== undefined && want.kind === "int")
|
|
32
47
|
return this.literal(v.literal, want, node);
|
|
33
48
|
if (!sameType(v.type, want))
|
|
34
49
|
this.fail(node, `expected ${typeName(want)}, got ${typeName(v.type)}`);
|
|
35
50
|
return v;
|
|
36
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* An f64 is not an integer: its u64 of bits never becomes an integer's value, nor an integer an f64's, without
|
|
54
|
+
* saying so with f64Bits(x) / f64FromBits(bits). (Options are compared by their inner types.)
|
|
55
|
+
*/
|
|
56
|
+
/** An f64 as text (a template, log()): the digits would be its bits' integer, not the number. */
|
|
57
|
+
refuseF64Text(node, t) {
|
|
58
|
+
const inner = t.kind === "option" ? t.inner : t;
|
|
59
|
+
if (inner.kind === "int" && inner.f64)
|
|
60
|
+
this.fail(node, F64_TEXT);
|
|
61
|
+
}
|
|
62
|
+
refuseF64Conversion(node, from, to) {
|
|
63
|
+
const inner = (t) => (t.kind === "option" ? t.inner : t);
|
|
64
|
+
const a = inner(from);
|
|
65
|
+
const b = inner(to);
|
|
66
|
+
if (a.kind !== "int" || b.kind !== "int" || !a.f64 === !b.f64)
|
|
67
|
+
return;
|
|
68
|
+
if (a.f64)
|
|
69
|
+
this.fail(node, F64_TO_INT(typeName(b)));
|
|
70
|
+
this.fail(node, INT_TO_F64(typeName(a)));
|
|
71
|
+
}
|
|
37
72
|
/**
|
|
38
73
|
* Use an Option's value. TypeScript only allows this after narrowing (`x !== null`), so the runtime
|
|
39
74
|
* check is a backstop: an empty option aborts with 5108.
|
|
@@ -200,6 +235,8 @@ export class ExpressionEmitter extends StorageEmitter {
|
|
|
200
235
|
if (!t || t.kind === "vecArg" || t.kind === "dataEnumArg")
|
|
201
236
|
this.fail(node.type, "`as` names an integer, boolean, Pubkey, enum, option or string type the value already has");
|
|
202
237
|
const v = this.expr(node.expression, t);
|
|
238
|
+
if (!v.isNull)
|
|
239
|
+
this.refuseF64Conversion(node, v.type, t);
|
|
203
240
|
// Not a conversion: `x as u64 | null` does not make an Option of a u64.
|
|
204
241
|
if (v.literal === undefined && !v.isNull && !sameType(v.type, t))
|
|
205
242
|
this.fail(node, `this is a ${typeName(v.type)}, not a ${typeName(t)}: \`as\` does not convert (use u64(x) and the like for integers)`);
|
|
@@ -312,6 +349,7 @@ export class ExpressionEmitter extends StorageEmitter {
|
|
|
312
349
|
return this.literal(value, hint?.kind === "int" ? hint : I64, node);
|
|
313
350
|
}
|
|
314
351
|
const v = this.expr(node.operand, hint);
|
|
352
|
+
this.refuseF64(node, "unary -", v);
|
|
315
353
|
if (v.type.kind !== "int" || !INT_INFO[v.type.name].signed)
|
|
316
354
|
this.fail(node, "unary minus needs a signed integer (i8 … i128)");
|
|
317
355
|
return this.arith(ts.SyntaxKind.MinusToken, { c: intLiteral(0n, v.type.name), type: v.type }, v, node);
|
|
@@ -319,6 +357,7 @@ export class ExpressionEmitter extends StorageEmitter {
|
|
|
319
357
|
if (node.operator === ts.SyntaxKind.TildeToken) {
|
|
320
358
|
// Bitwise not within the type's width (for signed types this equals JS's -(x + 1)).
|
|
321
359
|
const v = this.expr(node.operand, hint);
|
|
360
|
+
this.refuseF64(node, "~", v);
|
|
322
361
|
const typed = v.literal !== undefined ? this.literal(v.literal, hint?.kind === "int" ? hint : U64, node) : v;
|
|
323
362
|
if (typed.type.kind !== "int")
|
|
324
363
|
this.fail(node, "~ needs an integer");
|
|
@@ -707,6 +746,7 @@ export class ExpressionEmitter extends StorageEmitter {
|
|
|
707
746
|
const v = this.expr(node);
|
|
708
747
|
if (v.type.kind === "string")
|
|
709
748
|
return v.c;
|
|
749
|
+
this.refuseF64Text(node, v.type);
|
|
710
750
|
if (converted)
|
|
711
751
|
converted.yes = true;
|
|
712
752
|
if (v.type.kind === "option" && v.type.inner.kind === "pubkey") {
|
|
@@ -825,7 +865,9 @@ export class ExpressionEmitter extends StorageEmitter {
|
|
|
825
865
|
if (inner.kind === "struct")
|
|
826
866
|
this.fail(node, "?? works on Options of integers, booleans and Pubkeys");
|
|
827
867
|
const nullable = (t) => !!(t.flags & ts.TypeFlags.Null) || (t.isUnion() && t.types.some(nullable));
|
|
828
|
-
|
|
868
|
+
// Over an Option<f64> the result is an f64 whatever the context expects: an integer hint would make it the bits.
|
|
869
|
+
const f64 = inner.kind === "int" && inner.f64 === true;
|
|
870
|
+
const type = (f64 ? undefined : hint) ?? (nullable(this.checker.getTypeAtLocation(node)) ? left.type : inner);
|
|
829
871
|
const lo = this.tmp("opt");
|
|
830
872
|
this.line(`${optC(inner)} ${lo} = ${left.c};`);
|
|
831
873
|
const t = this.tmp("nn");
|
|
@@ -856,6 +898,7 @@ export class ExpressionEmitter extends StorageEmitter {
|
|
|
856
898
|
const exp = this.expr(node.right);
|
|
857
899
|
// JS reads the base first: if the exponent ran statements (a helper that changes it), use the value read before.
|
|
858
900
|
base = this.settle(base, mark);
|
|
901
|
+
this.refuseF64(node, "**", base, exp);
|
|
859
902
|
if (base.literal !== undefined)
|
|
860
903
|
base = this.literal(base.literal, hint?.kind === "int" ? hint : U64, node.left);
|
|
861
904
|
if (base.type.kind !== "int")
|
|
@@ -985,6 +1028,7 @@ export class ExpressionEmitter extends StorageEmitter {
|
|
|
985
1028
|
if (right.literal !== undefined)
|
|
986
1029
|
right = this.literal(right.literal, U64, node.right);
|
|
987
1030
|
left = this.settle(left, mark);
|
|
1031
|
+
this.refuseF64(node, ts.tokenToString(op) ?? "this operator", left, right);
|
|
988
1032
|
if (left.type.kind !== "int" || right.type.kind !== "int")
|
|
989
1033
|
this.fail(node, "shifts need integer operands");
|
|
990
1034
|
return this.arith(op, left, right, node);
|
|
@@ -1008,6 +1052,7 @@ export class ExpressionEmitter extends StorageEmitter {
|
|
|
1008
1052
|
right = this.expr(node.right, left.type.kind === "int" ? left.type : left.type.kind === "option" && left.type.inner.kind === "int" ? left.type.inner : undefined);
|
|
1009
1053
|
left = this.settle(left, mark);
|
|
1010
1054
|
}
|
|
1055
|
+
this.refuseF64(node, ts.tokenToString(op) ?? "this operator", left, right);
|
|
1011
1056
|
// Option equality follows JS: null equals null, and an empty option equals no value.
|
|
1012
1057
|
if (isEq && (left.type.kind === "option" || right.type.kind === "option")) {
|
|
1013
1058
|
const neg = op === K.ExclamationEqualsEqualsToken || op === K.ExclamationEqualsToken;
|
|
@@ -1074,6 +1119,7 @@ export class ExpressionEmitter extends StorageEmitter {
|
|
|
1074
1119
|
}
|
|
1075
1120
|
/** Checked integer arithmetic into a fresh temporary. */
|
|
1076
1121
|
arith(op, left, right, node) {
|
|
1122
|
+
this.refuseF64(node, ts.tokenToString(op) ?? "arithmetic", left, right);
|
|
1077
1123
|
if (left.type.kind !== "int")
|
|
1078
1124
|
this.fail(node, "arithmetic needs integer operands");
|
|
1079
1125
|
const type = left.type;
|
|
@@ -1135,7 +1181,7 @@ export class ExpressionEmitter extends StorageEmitter {
|
|
|
1135
1181
|
if (right.type.kind !== "int")
|
|
1136
1182
|
this.fail(node, "shift amounts must be integers");
|
|
1137
1183
|
if (right.type.name === "u256")
|
|
1138
|
-
this.fail(node, "shift
|
|
1184
|
+
this.fail(node, "a shift amount is a u128 or smaller integer: convert it (u64(n))");
|
|
1139
1185
|
const bits = INT_INFO[type.name].bytes * 8;
|
|
1140
1186
|
const sh = this.tmp("sh");
|
|
1141
1187
|
const rinfo = INT_INFO[right.type.name];
|
|
@@ -1183,7 +1229,7 @@ export class ExpressionEmitter extends StorageEmitter {
|
|
|
1183
1229
|
if (right.type.kind !== "int")
|
|
1184
1230
|
this.fail(node, "shift amounts must be integers");
|
|
1185
1231
|
if (right.type.name === "u256")
|
|
1186
|
-
this.fail(node, "shift
|
|
1232
|
+
this.fail(node, "a shift amount is a u128 or smaller integer: convert it (u64(n))");
|
|
1187
1233
|
const sh = this.tmp("sh");
|
|
1188
1234
|
this.line(`if (${INT_INFO[right.type.name].signed ? `${right.c} < 0 || ` : ""}${right.c} >= 256) TS_FAIL(TS_ERR_OVERFLOW, "TypeSOL: shift amount out of range");`);
|
|
1189
1235
|
this.line(`uint64_t ${sh} = (uint64_t)(${right.c});`);
|
|
@@ -1206,6 +1252,8 @@ export class ExpressionEmitter extends StorageEmitter {
|
|
|
1206
1252
|
return this.literal(v.literal, to, arg);
|
|
1207
1253
|
if (v.type.kind !== "int")
|
|
1208
1254
|
this.fail(arg, `${to.name}() needs an integer`);
|
|
1255
|
+
if (v.type.f64)
|
|
1256
|
+
this.fail(arg, F64_TO_INT(to.name));
|
|
1209
1257
|
if (v.type.name === to.name)
|
|
1210
1258
|
return v;
|
|
1211
1259
|
const from = INT_INFO[v.type.name];
|
|
@@ -4,7 +4,8 @@ import { type OptionInner, type AccountParam, type FieldType, type ProgramModel,
|
|
|
4
4
|
export declare const MAX_ACCOUNTS = 32;
|
|
5
5
|
/** The most accounts one instruction takes: 32, or what the program declares with `const MAX_ACCOUNTS = …n` (33–255). */
|
|
6
6
|
export declare function accountLimit(model: Pick<ProgramModel, "constants">): number;
|
|
7
|
-
|
|
7
|
+
/** Solana's limit on the seeds of one address, the bump included (MAX_SEEDS of solana-pubkey; each seed at most 32 bytes). */
|
|
8
|
+
export declare const MAX_SEEDS = 16;
|
|
8
9
|
/** Max instruction data an invoke() may build, and max event payload. */
|
|
9
10
|
export declare const MAX_CPI_DATA = 1024;
|
|
10
11
|
export declare const MAX_EVENT = 1024;
|
|
@@ -230,3 +231,9 @@ export declare function fixedVecC(t: FieldType & {
|
|
|
230
231
|
* again after it: writeData, storeBorsh, another view). A BigVec slice's length is in range already.
|
|
231
232
|
*/
|
|
232
233
|
export declare function vecCount(v: NonNullable<Ref["vec"]>): string;
|
|
234
|
+
/** An f64 used where an integer is expected (an assignment, an argument, a return, an index, a cast). */
|
|
235
|
+
export declare const F64_TO_INT: (to: string) => string;
|
|
236
|
+
/** An f64 in a template string or log(). */
|
|
237
|
+
export declare const F64_TEXT = "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)";
|
|
238
|
+
/** An integer used where an f64 is expected. */
|
|
239
|
+
export declare const INT_TO_F64: (from: string) => string;
|
|
@@ -7,7 +7,8 @@ export function accountLimit(model) {
|
|
|
7
7
|
const c = model.constants.get("MAX_ACCOUNTS");
|
|
8
8
|
return c && typeof c.value === "bigint" && c.value >= 32n && c.value <= 255n ? Number(c.value) : MAX_ACCOUNTS;
|
|
9
9
|
}
|
|
10
|
-
|
|
10
|
+
/** Solana's limit on the seeds of one address, the bump included (MAX_SEEDS of solana-pubkey; each seed at most 32 bytes). */
|
|
11
|
+
export const MAX_SEEDS = 16;
|
|
11
12
|
/** Max instruction data an invoke() may build, and max event payload. */
|
|
12
13
|
export const MAX_CPI_DATA = 1024;
|
|
13
14
|
export const MAX_EVENT = 1024;
|
|
@@ -323,3 +324,9 @@ export function vecCount(v) {
|
|
|
323
324
|
return `((uint64_t)${v.len} < ${v.big} ? (uint64_t)${v.len} : ${v.big})`;
|
|
324
325
|
return v.cap === undefined || v.heap ? `((uint64_t)${v.len})` : `((uint64_t)(${v.len} < ${v.cap} ? ${v.len} : ${v.cap}))`;
|
|
325
326
|
}
|
|
327
|
+
/** An f64 used where an integer is expected (an assignment, an argument, a return, an index, a cast). */
|
|
328
|
+
export const F64_TO_INT = (to) => `an f64 is not an integer, so it is not converted to ${to}: it is held as its IEEE 754 bits, and the integer would be those bits; to use the bits, write f64Bits(x)`;
|
|
329
|
+
/** An f64 in a template string or log(). */
|
|
330
|
+
export const F64_TEXT = "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)";
|
|
331
|
+
/** An integer used where an f64 is expected. */
|
|
332
|
+
export const INT_TO_F64 = (from) => `a ${from} is not an f64, so it is not converted: an f64 is made from its IEEE 754 bits with f64FromBits(bits)`;
|
|
@@ -3,9 +3,6 @@ import { type TypeScope } from "../analyze.js";
|
|
|
3
3
|
import { type FieldType } from "../model.js";
|
|
4
4
|
import { type Value, type Local, type Closure, type FunctionChoice, type Ref } from "./shared.js";
|
|
5
5
|
import { CpiEmitter } from "./cpi.js";
|
|
6
|
-
/**
|
|
7
|
-
* Statements: variables, assignment, control flow and return.
|
|
8
|
-
*/
|
|
9
6
|
export declare abstract class StatementEmitter extends CpiEmitter {
|
|
10
7
|
protected statement(stmt: ts.Statement, topLevel?: boolean): void;
|
|
11
8
|
protected statementInner(stmt: ts.Statement, topLevel: boolean): void;
|
|
@@ -172,6 +169,14 @@ export declare abstract class StatementEmitter extends CpiEmitter {
|
|
|
172
169
|
* a field path (no calls), so reading it once per name reads the same thing JS reads once.
|
|
173
170
|
*/
|
|
174
171
|
protected destructure(list: ts.VariableDeclarationList, d: ts.VariableDeclaration, pattern: ts.ObjectBindingPattern, init: ts.Expression, topLevel: boolean): void;
|
|
172
|
+
/** merkleVerify, verifyEd25519 or verifySecp256k1 when `e` (in parentheses, or under `!` if `negated`) calls one. */
|
|
173
|
+
protected verifierCall(e: ts.Expression, negated?: boolean): string | undefined;
|
|
174
|
+
/**
|
|
175
|
+
* `const ok = merkleVerify(…)` (or `ok = …`, the call in parentheses or under `as`) where the function that
|
|
176
|
+
* declares `ok` never reads it: nothing is checked. A read anywhere in that function counts, in a closure inside it
|
|
177
|
+
* and before the assignment too: this catches a result left unused, not every path.
|
|
178
|
+
*/
|
|
179
|
+
protected refuseUnreadVerifier(name: ts.Identifier, value: ts.Expression): void;
|
|
175
180
|
protected expressionStatement(e: ts.Expression, topLevel: boolean): void;
|
|
176
181
|
protected assign(target: ts.Expression, valueNode: ts.Expression, op?: ts.BinaryOperator, topLevel?: boolean): void;
|
|
177
182
|
}
|
|
@@ -7,6 +7,8 @@ import { cTokens, declaredNames, isGlobalName, parameterDecls, usedNames } from
|
|
|
7
7
|
/**
|
|
8
8
|
* Statements: variables, assignment, control flow and return.
|
|
9
9
|
*/
|
|
10
|
+
/** A proof or signature check whose result decides nothing: `how` says why (discarded, never read, an empty if). */
|
|
11
|
+
const VERIFIER_UNUSED = (checker, how) => `the result of ${checker} ${how}, so nothing is checked: it returns whether the ${checker === "merkleVerify" ? "proof" : "signature"} holds; write require(${checker}(…), error)`;
|
|
10
12
|
export class StatementEmitter extends CpiEmitter {
|
|
11
13
|
statement(stmt, topLevel = false) {
|
|
12
14
|
const outer = this.where;
|
|
@@ -29,6 +31,11 @@ export class StatementEmitter extends CpiEmitter {
|
|
|
29
31
|
if (ts.isExpressionStatement(stmt))
|
|
30
32
|
return this.expressionStatement(stmt.expression, topLevel);
|
|
31
33
|
if (ts.isIfStatement(stmt)) {
|
|
34
|
+
// `if (merkleVerify(…)) {}` (or `{} else {}`): the result decides nothing.
|
|
35
|
+
const checker = this.verifierCall(stmt.expression, true);
|
|
36
|
+
const empty = (s) => ts.isEmptyStatement(s) || (ts.isBlock(s) && s.statements.length === 0);
|
|
37
|
+
if (checker && (!stmt.elseStatement || empty(stmt.elseStatement)) && empty(stmt.thenStatement))
|
|
38
|
+
this.fail(stmt, `${VERIFIER_UNUSED(checker, "is not acted on: the if around it has an empty body")}`);
|
|
32
39
|
const cond = this.expect(this.expr(stmt.expression, BOOL), BOOL, stmt.expression);
|
|
33
40
|
this.line(`if (${cond.c}) {`);
|
|
34
41
|
this.block(() => this.body(stmt.thenStatement));
|
|
@@ -181,6 +188,7 @@ export class StatementEmitter extends CpiEmitter {
|
|
|
181
188
|
}
|
|
182
189
|
const subject = this.expr(stmt.expression);
|
|
183
190
|
const typed = subject.literal !== undefined ? this.literal(subject.literal, U64, stmt.expression) : subject;
|
|
191
|
+
this.refuseF64(stmt.expression, "switch", typed);
|
|
184
192
|
if (typed.type.kind !== "int" && typed.type.kind !== "enum")
|
|
185
193
|
this.fail(stmt.expression, "switch works on integers and enums");
|
|
186
194
|
// C cannot switch on 128-bit values or u256 limbs: pick the case's index with comparisons, then switch on that.
|
|
@@ -1468,6 +1476,8 @@ export class StatementEmitter extends CpiEmitter {
|
|
|
1468
1476
|
if (!(list.flags & (ts.NodeFlags.Const | ts.NodeFlags.Let)))
|
|
1469
1477
|
this.fail(list, "use const or let, not var");
|
|
1470
1478
|
for (const d of list.declarations) {
|
|
1479
|
+
if (ts.isIdentifier(d.name) && d.initializer)
|
|
1480
|
+
this.refuseUnreadVerifier(d.name, d.initializer);
|
|
1471
1481
|
if (ts.isObjectBindingPattern(d.name) && d.initializer) {
|
|
1472
1482
|
this.destructure(list, d, d.name, d.initializer, topLevel);
|
|
1473
1483
|
continue;
|
|
@@ -1892,11 +1902,56 @@ export class StatementEmitter extends CpiEmitter {
|
|
|
1892
1902
|
this.variable({ ...list, declarations: [decl] }, topLevel);
|
|
1893
1903
|
}
|
|
1894
1904
|
}
|
|
1905
|
+
/** merkleVerify, verifyEd25519 or verifySecp256k1 when `e` (in parentheses, or under `!` if `negated`) calls one. */
|
|
1906
|
+
verifierCall(e, negated = false) {
|
|
1907
|
+
while (ts.isParenthesizedExpression(e) || (negated && ts.isPrefixUnaryExpression(e) && e.operator === ts.SyntaxKind.ExclamationToken))
|
|
1908
|
+
e = ts.isParenthesizedExpression(e) ? e.expression : e.operand;
|
|
1909
|
+
if (!ts.isCallExpression(e))
|
|
1910
|
+
return undefined;
|
|
1911
|
+
const name = this.exportName(e.expression);
|
|
1912
|
+
return name === "merkleVerify" || name === "verifyEd25519" || name === "verifySecp256k1" ? name : undefined;
|
|
1913
|
+
}
|
|
1914
|
+
/**
|
|
1915
|
+
* `const ok = merkleVerify(…)` (or `ok = …`, the call in parentheses or under `as`) where the function that
|
|
1916
|
+
* declares `ok` never reads it: nothing is checked. A read anywhere in that function counts, in a closure inside it
|
|
1917
|
+
* and before the assignment too: this catches a result left unused, not every path.
|
|
1918
|
+
*/
|
|
1919
|
+
refuseUnreadVerifier(name, value) {
|
|
1920
|
+
while (ts.isParenthesizedExpression(value) || ts.isAsExpression(value))
|
|
1921
|
+
value = value.expression;
|
|
1922
|
+
const checker = this.verifierCall(value);
|
|
1923
|
+
if (!checker)
|
|
1924
|
+
return;
|
|
1925
|
+
const symbol = this.checker.getSymbolAtLocation(name);
|
|
1926
|
+
let fn = symbol?.valueDeclaration;
|
|
1927
|
+
while (fn && !ts.isFunctionLike(fn))
|
|
1928
|
+
fn = fn.parent;
|
|
1929
|
+
if (!symbol || !fn)
|
|
1930
|
+
return;
|
|
1931
|
+
let read = false;
|
|
1932
|
+
const visit = (n) => {
|
|
1933
|
+
if (read)
|
|
1934
|
+
return;
|
|
1935
|
+
if (ts.isIdentifier(n) && n !== name && this.checker.getSymbolAtLocation(n) === symbol) {
|
|
1936
|
+
const p = n.parent;
|
|
1937
|
+
const written = (ts.isVariableDeclaration(p) && p.name === n) || (ts.isBinaryExpression(p) && p.left === n && p.operatorToken.kind === ts.SyntaxKind.EqualsToken);
|
|
1938
|
+
if (!written)
|
|
1939
|
+
read = true;
|
|
1940
|
+
}
|
|
1941
|
+
ts.forEachChild(n, visit);
|
|
1942
|
+
};
|
|
1943
|
+
visit(fn);
|
|
1944
|
+
if (!read)
|
|
1945
|
+
this.fail(value, VERIFIER_UNUSED(checker, `is never read (${name.text} is not used)`));
|
|
1946
|
+
}
|
|
1895
1947
|
expressionStatement(e, topLevel) {
|
|
1896
1948
|
if (ts.isBinaryExpression(e)) {
|
|
1897
1949
|
const op = e.operatorToken.kind;
|
|
1898
|
-
if (op === ts.SyntaxKind.EqualsToken)
|
|
1950
|
+
if (op === ts.SyntaxKind.EqualsToken) {
|
|
1951
|
+
if (ts.isIdentifier(e.left))
|
|
1952
|
+
this.refuseUnreadVerifier(e.left, e.right);
|
|
1899
1953
|
return this.assign(e.left, e.right, undefined, topLevel);
|
|
1954
|
+
}
|
|
1900
1955
|
const compound = {
|
|
1901
1956
|
[ts.SyntaxKind.PlusEqualsToken]: ts.SyntaxKind.PlusToken,
|
|
1902
1957
|
[ts.SyntaxKind.MinusEqualsToken]: ts.SyntaxKind.MinusToken,
|
|
@@ -1933,6 +1988,7 @@ export class StatementEmitter extends CpiEmitter {
|
|
|
1933
1988
|
if ((ts.isPostfixUnaryExpression(e) || ts.isPrefixUnaryExpression(e)) &&
|
|
1934
1989
|
(e.operator === ts.SyntaxKind.PlusPlusToken || e.operator === ts.SyntaxKind.MinusMinusToken)) {
|
|
1935
1990
|
const place = this.place(e.operand);
|
|
1991
|
+
this.refuseF64(e, e.operator === ts.SyntaxKind.PlusPlusToken ? "++" : "--", { c: place.read, type: place.type });
|
|
1936
1992
|
if (place.type.kind !== "int")
|
|
1937
1993
|
this.fail(e, "++ and -- need an integer");
|
|
1938
1994
|
const one = this.literal(1n, place.type, e);
|
|
@@ -1940,6 +1996,10 @@ export class StatementEmitter extends CpiEmitter {
|
|
|
1940
1996
|
this.line(place.write(this.arith(op, { c: place.read, type: place.type }, one, e).c) + ";");
|
|
1941
1997
|
return;
|
|
1942
1998
|
}
|
|
1999
|
+
// These return whether a proof or signature holds and fail nothing by themselves: a bare call checks nothing.
|
|
2000
|
+
const checker = this.verifierCall(e);
|
|
2001
|
+
if (checker)
|
|
2002
|
+
this.fail(e, VERIFIER_UNUSED(checker, "is discarded"));
|
|
1943
2003
|
if (ts.isCallExpression(e)) {
|
|
1944
2004
|
this.call(e, topLevel);
|
|
1945
2005
|
return;
|
|
@@ -2002,9 +2062,14 @@ export class StatementEmitter extends CpiEmitter {
|
|
|
2002
2062
|
if (rhs.type.kind !== "int")
|
|
2003
2063
|
this.fail(valueNode, "shift amounts must be integers");
|
|
2004
2064
|
}
|
|
2005
|
-
else
|
|
2006
|
-
rhs = this.
|
|
2065
|
+
else {
|
|
2066
|
+
rhs = this.expr(valueNode, place.type);
|
|
2067
|
+
// `n += x` with an f64 on either side is the operator on an f64, refused as such (not as a conversion).
|
|
2068
|
+
this.refuseF64(target, `${ts.tokenToString(op)}=`, current, rhs);
|
|
2069
|
+
rhs = this.expect(rhs, place.type, valueNode);
|
|
2070
|
+
}
|
|
2007
2071
|
current = this.settle(current, mark);
|
|
2072
|
+
this.refuseF64(target, `${ts.tokenToString(op)}=`, current, rhs);
|
|
2008
2073
|
this.line(place.write(this.arith(op, current, rhs, target).c) + ";");
|
|
2009
2074
|
return;
|
|
2010
2075
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
2
|
import { heapVecOf, resolveScalarType, structNameOf, typesolName } from "../analyze.js";
|
|
3
3
|
import { INT_INFO, isScalar, isWritable, sameType, scalarSize, typeName } from "../model.js";
|
|
4
|
-
import { U64, BOOL, PUBKEY, STRING, optC, fieldC, fixedVecC, storageDecl, vecCount, vecPlace, emptySizeC } from "./shared.js";
|
|
4
|
+
import { U64, BOOL, PUBKEY, STRING, F64_TO_INT, optC, fieldC, fixedVecC, storageDecl, vecCount, vecPlace, emptySizeC } from "./shared.js";
|
|
5
5
|
import { AliasEmitter } from "./alias.js";
|
|
6
6
|
/**
|
|
7
7
|
* Places in account data and locals: references, reads and writes, Vec/array indexing, aliasing rules and
|
|
@@ -470,6 +470,8 @@ export class StorageEmitter extends AliasEmitter {
|
|
|
470
470
|
const idx = v.literal !== undefined ? this.literal(v.literal, U64, n) : v;
|
|
471
471
|
if (idx.type.kind !== "int")
|
|
472
472
|
this.fail(n, "array index must be an integer");
|
|
473
|
+
if (idx.type.f64)
|
|
474
|
+
this.fail(n, F64_TO_INT("an index"));
|
|
473
475
|
const t = this.indexTemp(idx);
|
|
474
476
|
return t;
|
|
475
477
|
}
|
|
@@ -500,6 +502,8 @@ export class StorageEmitter extends AliasEmitter {
|
|
|
500
502
|
const idx = v.literal !== undefined ? this.literal(v.literal, U64, n) : v;
|
|
501
503
|
if (idx.type.kind !== "int")
|
|
502
504
|
this.fail(n, "array index must be an integer");
|
|
505
|
+
if (idx.type.f64)
|
|
506
|
+
this.fail(n, F64_TO_INT("an index"));
|
|
503
507
|
const t = this.indexTemp(idx);
|
|
504
508
|
this.line(`if (${t} >= ${typeof len === "number" ? `${len}ULL` : len}) TS_FAIL(TS_ERR_INDEX, "TypeSOL: array index out of bounds");`);
|
|
505
509
|
return t;
|
package/dist/compiler/hints.d.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
|
+
/** Date.now() and new Date() have no meaning on chain; the cluster's clock does. */
|
|
3
|
+
export declare const CLOCK_HINT = "the time on chain is the cluster's clock: now() from \"typesol\" gives its Unix timestamp in seconds (an i64)";
|
|
4
|
+
export declare const ASYNC_HINT = "async and generators are not supported: an instruction runs start to finish within one call; remove async (or the *) and return void or a value, not a Promise";
|
|
5
|
+
/** The message to show for a TypeScript diagnostic: TypeScript's own, except where its advice points the wrong way. */
|
|
6
|
+
export declare function messageForTypeScript(d: ts.Diagnostic, message: string): string;
|
|
7
|
+
/** A hint for a struct field whose type cannot be account data. */
|
|
8
|
+
export declare function fieldTypeHint(node: ts.Node): string | undefined;
|
|
2
9
|
/** A sentence to add to a TypeScript diagnostic, or undefined when TypeScript's own message says enough. */
|
|
3
10
|
export declare function hintForTypeScript(d: ts.Diagnostic, message: string, libPath: string): string | undefined;
|
|
4
11
|
/** "ClassDeclaration is not supported …" in words a TypeScript developer recognizes. */
|