1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! The limits a decoded value is held to (`execution.md`, *Limits*).
/// How large a decoded value may be.
///
/// Every host decodes within [`Limits::LANGUAGE`]; other values exist only
/// so that tests can reach a limit with a small value.
///
/// A value past any of these does not decode, like any other value that does
/// not match its type: in a Tool reply that is no answer, so the call stays
/// open and the run stops, never a fault (`abi.md`, *Decoding*).
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct Limits {
/// How deeply the decoded value may nest: a scalar is 0 deep, and a
/// list, set, dictionary, tuple, record or enum value one deeper than its
/// deepest part, as the evaluator counts.
pub depth: usize,
/// How many bits an `Int` may take, sign excluded, and so the integer
/// any number's significant digits make, which bounds a `Decimal`'s
/// coefficient.
pub int_bits: u64,
/// How many bytes one `Text` or `Bytes` may hold, how many digits a
/// number may take written in plain decimal notation, and how many
/// elements written in no bytes, such as `Unit`s, one binary value may
/// hold in all its collections together.
pub text_bytes: usize,
}
impl Limits {
/// The language's limits, which every conforming implementation holds a
/// program to exactly: no host raises or lowers them.
pub const LANGUAGE: Limits = Limits {
depth: 256,
int_bits: 4096,
text_bytes: 16 * 1024 * 1024,
};
/// No limit at all: for bytes this implementation wrote itself, such as
/// a value already held, or type metadata, whose nesting no program
/// value's limit bounds.
pub const UNBOUNDED: Limits = Limits {
depth: usize::MAX,
int_bits: u64::MAX,
text_bytes: usize::MAX,
};
/// The reply limit: the most bytes one Tool reply may take.
pub const REPLY_BYTES: usize = 16 * 1024 * 1024;
/// How deeply JSON arrays and objects may nest before decoding refuses
/// the text unread.
///
/// A JSON level makes at least half a level of value — an enum with a
/// named payload is two objects around one value — so text nested deeper
/// than this could not decode within [`Limits::depth`] in any case.
pub(crate) fn json_depth(&self) -> usize {
self.depth.saturating_mul(2).saturating_add(1)
}
}
impl Default for Limits {
fn default() -> Self {
Limits::LANGUAGE
}
}