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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//! The object contract: the runtime object surface and argument forms.
//!
//! The kernel defines the [`Object`] header, the class/shape/constructor
//! reference types, and the checked [`Args`] forms passed to callables; the
//! libraries supply the concrete object implementations.
use std::any::Any;
use std::sync::OnceLock;
use crate::{
callable::Callable,
capability::TrustLevel,
claim::{Claim, ClaimPattern},
class::Class,
datum::Datum,
encode::{ObjectEncode, ReadConstructor},
env::Cx,
error::Result,
expr::Expr,
id::Symbol,
number_domain::{NumberDomain, NumberValue},
ref_id::Ref,
term::OpKey,
value::Value,
};
/// Runtime value carrying a class object.
pub type ClassRef = Value;
/// Runtime value carrying a read-constructor object.
pub type ReadConstructorRef = Value;
/// Runtime value carrying a shape object.
pub type ShapeRef = Value;
/// Runtime value carrying a table object.
pub type TableRef = Value;
/// Identity and trust header attached to every runtime object.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ObjectHeader {
/// Stable reference identifying the object.
pub id: Ref,
/// Symbol naming the object's kind.
pub kind: Symbol,
/// Trust level governing the object's capabilities.
pub trust: TrustLevel,
}
/// Sink that collects claims emitted while reflecting over an object.
pub trait ClaimSink {
/// Accept one emitted claim.
fn claim(&mut self, claim: Claim) -> Result<()>;
}
/// Checked, already-evaluated positional arguments passed to a callable.
#[derive(Clone, Default)]
pub struct Args {
values: Vec<Value>,
}
impl Args {
/// Build an argument list from evaluated values.
pub fn new(values: Vec<Value>) -> Self {
Self { values }
}
/// Borrow the argument values in order.
pub fn values(&self) -> &[Value] {
&self.values
}
/// Consume the argument list, returning the owned values.
pub fn into_vec(self) -> Vec<Value> {
self.values
}
}
/// Unevaluated argument expressions passed to a raw callable.
#[derive(Clone, Default)]
pub struct RawArgs {
exprs: Vec<Expr>,
}
impl RawArgs {
/// Build a raw argument list from unevaluated expressions.
pub fn new(exprs: Vec<Expr>) -> Self {
Self { exprs }
}
/// Borrow the argument expressions in order.
pub fn exprs(&self) -> &[Expr] {
&self.exprs
}
/// Consume the raw argument list, returning the owned expressions.
pub fn into_exprs(self) -> Vec<Expr> {
self.exprs
}
}
/// Base protocol implemented by every runtime value.
///
/// SIM uses object-style protocol views instead of a closed value enum for
/// extensible runtime behavior. The root trait stays small: behavior is
/// exposed through headers, operations, claims, snapshots, display, and Rust
/// downcasting.
pub trait Object: Send + Sync {
/// Identity and trust header for the object; defaults to the shared
/// anonymous header.
fn header(&self) -> &ObjectHeader {
default_object_header()
}
/// Resolve the operation registered under `key`, if any.
fn op(&self, _key: &OpKey) -> Option<&dyn crate::op::Op> {
None
}
/// Emit the object's claims matching `pattern` into `sink`.
fn claims(
&self,
_cx: &mut Cx,
_pattern: &ClaimPattern,
_sink: &mut dyn ClaimSink,
) -> Result<()> {
Ok(())
}
/// Optional content-addressable snapshot of the object's state.
fn snapshot(&self, _cx: &mut Cx) -> Result<Option<Datum>> {
Ok(None)
}
/// Render the object as a human-readable display string.
fn display(&self, _cx: &mut Cx) -> Result<String>;
/// Expose the object for Rust downcasting.
fn as_any(&self) -> &dyn Any;
}
/// Bridge that exposes typed protocol views (class, callable, shape, encoder,
/// number, fabric, stream, and related accessors) for objects that do not
/// resolve them through operations.
///
/// Callers should resolve operations, claims, refs, or snapshots instead of
/// calling this trait. It is kept outside [`Object`] so the root object trait
/// stays minimal while library objects expose this behavior through
/// compatibility adapters.
pub trait ObjectCompat: Object {
/// Class object this value belongs to; defaults to nil.
fn class(&self, cx: &mut Cx) -> Result<ClassRef> {
cx.factory().nil()
}
/// Callable view, if the object can be invoked.
fn as_callable(&self) -> Option<&dyn Callable> {
None
}
/// Class view, if the object is a class.
fn as_class(&self) -> Option<&dyn Class> {
None
}
/// Shape view, if the object is a shape.
fn as_shape(&self) -> Option<&dyn crate::shape::Shape> {
None
}
/// Object-encoder view, if the object encodes other objects.
fn as_object_encoder(&self) -> Option<&dyn ObjectEncode> {
None
}
/// Read-constructor view, if the object decodes data forms.
fn as_read_constructor(&self) -> Option<&dyn ReadConstructor> {
None
}
/// Number-domain view, if the object is a number domain.
fn as_number_domain(&self) -> Option<&dyn NumberDomain> {
None
}
/// Number-value view, if the object is a domain number.
fn as_number_value(&self) -> Option<&dyn NumberValue> {
None
}
/// Eval-fabric view, if the object is a distributed eval surface.
fn as_eval_fabric(&self) -> Option<&dyn crate::eval::EvalFabric> {
None
}
/// Stream view, if the object is a stream.
fn as_stream(&self) -> Option<&dyn crate::stream::Stream> {
None
}
/// Sequence view, if the object is a sequence.
fn as_sequence(&self) -> Option<&dyn crate::seq::Sequence> {
None
}
/// Thunk view, if the object is a deferred computation.
fn as_thunk(&self) -> Option<&dyn crate::eval::Thunk> {
None
}
/// List view, if the object is a list value.
fn as_list(&self) -> Option<&dyn crate::list::ListValue> {
None
}
/// Table-implementation view, if the object is a table.
fn as_table_impl(&self) -> Option<&dyn crate::table::Table> {
None
}
/// Directory view, if the object is a directory.
fn as_dir(&self) -> Option<&dyn crate::table::Dir> {
None
}
/// Expression form of the object; defaults to an opaque extension node.
fn as_expr(&self, cx: &mut Cx) -> Result<Expr> {
Ok(Expr::Extension {
tag: Symbol::qualified("core", "opaque-object"),
payload: Box::new(Expr::String(Object::display(self, cx)?)),
})
}
/// Truthiness of the object; defaults to true.
fn truth(&self, _cx: &mut Cx) -> Result<bool> {
Ok(true)
}
/// Publish claims asserting that the object satisfies `shape`; returns
/// whether any were published.
fn publish_shape_satisfaction_claims(&self, _cx: &mut Cx, _shape: &Ref) -> Result<bool> {
Ok(false)
}
/// Project the object into a table value; the default exposes its display.
fn as_table(&self, cx: &mut Cx) -> Result<Value> {
let display = Object::display(self, cx)?;
cx.factory().table(vec![(
Symbol::new("display"),
cx.factory().string(display)?,
)])
}
}
impl dyn Object {
/// Downcast the object to a concrete type `T`, if it is that type.
pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
self.as_any().downcast_ref::<T>()
}
}
impl ClaimSink for Vec<Claim> {
/// Collect the claim by pushing it onto the vector.
fn claim(&mut self, claim: Claim) -> Result<()> {
self.push(claim);
Ok(())
}
}
/// The shared anonymous [`ObjectHeader`] used by objects without their own.
pub fn default_object_header() -> &'static ObjectHeader {
static HEADER: OnceLock<ObjectHeader> = OnceLock::new();
HEADER.get_or_init(|| ObjectHeader {
id: default_object_ref(),
kind: Symbol::qualified("core", "Object"),
trust: TrustLevel::HostInternal,
})
}
/// Whether `header` is the shared anonymous [`default_object_header`].
pub fn is_default_object_header(header: &ObjectHeader) -> bool {
header == default_object_header()
}
fn default_object_ref() -> Ref {
Ref::Symbol(Symbol::qualified("core", "anonymous-object"))
}