vile / scheme / port_custom_raw_point.v
128 lines · 115 sloc · 4.55 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3// Only the initial inherited raw buffer can precede an authoritative getter.
4// Readiness may decode it without running Scheme. Its points share this one
5// lazily owned, single-assignment origin so escaped snapshots resolve together.
6@[heap]
7struct CustomRawAnchor {
8mut:
9 base Value
10 known bool
11 symbolic bool
12}
13
14fn (mut anchor CustomRawAnchor) resolve(base Value) ! {
15 if anchor.symbolic { return failure('symbolic raw origin has no absolute position') }
16 check_custom_binary_position(base)!
17 if anchor.known {
18 if !numeric_equal(anchor.base, base) {
19 return failure('initial raw origin is already resolved')
20 }
21 return
22 }
23 anchor.base = base
24 anchor.known = true
25}
26
27// The offset belongs to one bounded raw publication, never the port lifetime.
28// Old CR/carry/probe endpoints keep their own exact anchor across arbitrarily
29// long ignored input or noncontiguous continuation replay. No per-point box.
30struct CustomRawPoint {
31 valid bool
32 origin Value
33 initial &CustomRawAnchor = unsafe { nil }
34 offset int
35}
36
37fn custom_raw_point(origin Value, offset int) !CustomRawPoint {
38 check_custom_binary_position(origin)!
39 if offset < 0 { return failure('negative raw boundary offset') }
40 return CustomRawPoint{ valid: true, origin: origin, offset: offset }
41}
42
43fn custom_initial_raw_point(anchor &CustomRawAnchor, offset int) !CustomRawPoint {
44 if isnil(anchor) || anchor.symbolic || offset < 0 {
45 return failure('invalid initial raw boundary')
46 }
47 return CustomRawPoint{ valid: true, initial: anchor, offset: offset }
48}
49
50fn (point CustomRawPoint) absolute() !Value {
51 if !point.valid || point.offset < 0 { return failure('invalid raw boundary') }
52 mut base := point.origin
53 if !isnil(point.initial) {
54 if point.initial.symbolic { return failure('symbolic raw origin has no absolute position') }
55 if !point.initial.known { return failure('initial raw origin is unknown') }
56 base = point.initial.base
57 }
58 check_custom_binary_position(base)!
59 if point.offset == 0 { return base }
60 return numeric_add(base, integer(point.offset))
61}
62
63// Compare anchor identity without big-integer arithmetic on the scalar path.
64// Equal numeric values with distinct boxes conservatively start a new span.
65@[inline]
66fn (point CustomRawPoint) same_origin(other CustomRawPoint) bool {
67 if !point.valid || !other.valid { return false }
68 if !isnil(point.initial) || !isnil(other.initial) {
69 if voidptr(point.initial) != voidptr(other.initial) { return false }
70 if point.initial.symbolic {
71 return point.origin.tag == .integer && other.origin.tag == .integer
72 && point.origin.whole() == other.origin.whole()
73 }
74 return true
75 }
76 if point.origin.tag != other.origin.tag { return false }
77 return if point.origin.tag == .integer {
78 point.origin.whole() == other.origin.whole()
79 } else {
80 point.origin.tag == .big_integer && point.origin.pointer() == other.origin.pointer()
81 }
82}
83
84@[inline]
85fn (point CustomRawPoint) same(other CustomRawPoint) bool {
86 return point.offset == other.offset && point.same_origin(other)
87}
88
89@[inline]
90fn (point CustomRawPoint) delta(other CustomRawPoint) i64 {
91 if !point.same_origin(other) { return -1 }
92 return i64(point.offset) - other.offset
93}
94
95fn (point CustomRawPoint) retreat(distance i64) !CustomRawPoint {
96 if !point.valid || distance < 0 || distance > point.offset {
97 return failure('raw boundary is outside its publication')
98 }
99 return CustomRawPoint{ ...point, offset: point.offset - int(distance) }
100}
101
102// Missing absolute callbacks do not imply position zero. These nonmaterializable
103// coordinates only prove identity/relative offsets within one acknowledged raw
104// publication. The otherwise-unused origin Value carries an inline cohort ID;
105// the symbolic anchor prevents it from ever becoming an absolute coordinate.
106fn custom_symbolic_raw_point(anchor &CustomRawAnchor, cohort u64, offset int) !CustomRawPoint {
107 if isnil(anchor) || !anchor.symbolic || cohort > u64(max_i64) || offset < 0 {
108 return failure('invalid symbolic raw boundary')
109 }
110 return CustomRawPoint{ valid: true, initial: anchor, origin: integer(i64(cohort)), offset: offset }
111}
112
113struct CustomRawCohorts {
114mut:
115 anchor &CustomRawAnchor = unsafe { nil }
116 serial u64
117}
118
119// One cold anchor serves all ordinary publications. Rotating it on counter
120// exhaustion avoids aliasing even if an old continuation still holds ID 1.
121fn (mut cohorts CustomRawCohorts) next() CustomRawPoint {
122 if isnil(cohorts.anchor) || cohorts.serial == u64(max_i64) {
123 cohorts.anchor = &CustomRawAnchor{ symbolic: true }
124 cohorts.serial = 0
125 }
126 cohorts.serial++
127 return CustomRawPoint{ valid: true, initial: cohorts.anchor, origin: integer(i64(cohorts.serial)) }
128}
129