pub struct Cx { /* private fields */ }Expand description
The evaluation context threaded through every checked call.
Cx bundles the registry handle, factory, capability set, eval and control
policies, the data substrate (datum/fact/handle stores and ledgers), and the
diagnostic sink. The kernel defines this context; libraries supply the
behavior reached through it (registered classes, functions, number domains,
list/table backends, and so on). See the README sections “Library system”
and “Capabilities and trust”.
Implementations§
Source§impl Cx
impl Cx
Sourcepub fn new(eval_policy: EvalPolicyRef, factory: Arc<dyn Factory>) -> Self
pub fn new(eval_policy: EvalPolicyRef, factory: Arc<dyn Factory>) -> Self
Builds a fresh context with the given eval policy and factory.
The registry, capability set, and stores start empty (boot claims aside); libraries register behavior into the returned context.
§Examples
let cx = Cx::new(Arc::new(NoopEvalPolicy), Arc::new(DefaultFactory));
assert!(cx.capabilities().iter().next().is_none());Sourcepub fn with_env<T>(
&mut self,
env: Env,
f: impl FnOnce(&mut Self) -> Result<T>,
) -> Result<T>
pub fn with_env<T>( &mut self, env: Env, f: impl FnOnce(&mut Self) -> Result<T>, ) -> Result<T>
Runs f with env installed as the active environment, then restores it.
Sourcepub fn factory_ref(&self) -> Arc<dyn Factory> ⓘ
pub fn factory_ref(&self) -> Arc<dyn Factory> ⓘ
Returns a shared handle to the active object factory.
Sourcepub fn with_factory<T>(
&mut self,
factory: Arc<dyn Factory>,
f: impl FnOnce(&mut Self) -> Result<T>,
) -> Result<T>
pub fn with_factory<T>( &mut self, factory: Arc<dyn Factory>, f: impl FnOnce(&mut Self) -> Result<T>, ) -> Result<T>
Runs f with factory installed as the active factory, then restores it.
Sourcepub fn registry_mut(&mut self) -> &mut Registry
pub fn registry_mut(&mut self) -> &mut Registry
Returns the behavior registry mutably, for registering exports.
Sourcepub fn list_registry(&self) -> &ListRegistry
pub fn list_registry(&self) -> &ListRegistry
Returns the registered list backend.
Sourcepub fn list_registry_mut(&mut self) -> &mut ListRegistry
pub fn list_registry_mut(&mut self) -> &mut ListRegistry
Returns the list backend mutably.
Sourcepub fn table_registry(&self) -> &TableRegistry
pub fn table_registry(&self) -> &TableRegistry
Returns the registered table backend.
Sourcepub fn table_registry_mut(&mut self) -> &mut TableRegistry
pub fn table_registry_mut(&mut self) -> &mut TableRegistry
Returns the table backend mutably.
Sourcepub fn with_registry<T>(
&mut self,
registry: Registry,
f: impl FnOnce(&mut Self) -> Result<T>,
) -> Result<T>
pub fn with_registry<T>( &mut self, registry: Registry, f: impl FnOnce(&mut Self) -> Result<T>, ) -> Result<T>
Runs f with registry installed as the active registry, then restores it.
Sourcepub fn new_list(&mut self, items: Vec<Value>) -> Result<Value>
pub fn new_list(&mut self, items: Vec<Value>) -> Result<Value>
Builds a list value through the registered list backend.
Sourcepub fn new_cons(&mut self, car: Value, cdr: Value) -> Result<Value>
pub fn new_cons(&mut self, car: Value, cdr: Value) -> Result<Value>
Builds a cons cell through the registered list backend.
Sourcepub fn new_table(&mut self, entries: Vec<(Symbol, Value)>) -> Result<Value>
pub fn new_table(&mut self, entries: Vec<(Symbol, Value)>) -> Result<Value>
Builds a table value through the registered table backend.
Sourcepub fn sources(&self) -> &SourceRegistry
pub fn sources(&self) -> &SourceRegistry
Returns the source registry.
Sourcepub fn sources_mut(&mut self) -> &mut SourceRegistry
pub fn sources_mut(&mut self) -> &mut SourceRegistry
Returns the source registry mutably.
Sourcepub fn datum_store(&self) -> &BTreeDatumStore
pub fn datum_store(&self) -> &BTreeDatumStore
Returns the datum store.
Sourcepub fn datum_store_mut(&mut self) -> &mut BTreeDatumStore
pub fn datum_store_mut(&mut self) -> &mut BTreeDatumStore
Returns the datum store mutably.
Sourcepub fn handles(&self) -> &BTreeHandleStore
pub fn handles(&self) -> &BTreeHandleStore
Returns the handle store.
Sourcepub fn handles_mut(&mut self) -> &mut BTreeHandleStore
pub fn handles_mut(&mut self) -> &mut BTreeHandleStore
Returns the handle store mutably.
Sourcepub fn facts(&self) -> &BTreeFactStore
pub fn facts(&self) -> &BTreeFactStore
Returns the fact store.
Sourcepub fn facts_mut(&mut self) -> &mut BTreeFactStore
pub fn facts_mut(&mut self) -> &mut BTreeFactStore
Returns the fact store mutably.
Sourcepub fn effect_ledger(&self) -> &EffectLedger
pub fn effect_ledger(&self) -> &EffectLedger
Returns the effect ledger.
Sourcepub fn effect_ledger_mut(&mut self) -> &mut EffectLedger
pub fn effect_ledger_mut(&mut self) -> &mut EffectLedger
Returns the effect ledger mutably.
Sourcepub fn control_policy(&self) -> &dyn ControlPolicy
pub fn control_policy(&self) -> &dyn ControlPolicy
Returns the active control policy.
Sourcepub fn control_policy_ref(&self) -> ControlPolicyRef
pub fn control_policy_ref(&self) -> ControlPolicyRef
Returns a shared handle to the active control policy.
Sourcepub fn control_policy_name(&self) -> &'static str
pub fn control_policy_name(&self) -> &'static str
Returns the name of the active control policy.
Sourcepub fn set_control_policy(&mut self, control_policy: ControlPolicyRef)
pub fn set_control_policy(&mut self, control_policy: ControlPolicyRef)
Replaces the active control policy.
Sourcepub fn insert_fact(&mut self, claim: Claim) -> Result<Ref>
pub fn insert_fact(&mut self, claim: Claim) -> Result<Ref>
Inserts a claim into the fact store, subject to capability authorization.
Sourcepub fn insert_fact_for_lib(
&mut self,
lib_id: LibId,
claim: Claim,
) -> Result<Ref>
pub fn insert_fact_for_lib( &mut self, lib_id: LibId, claim: Claim, ) -> Result<Ref>
Inserts a claim and records it as part of a loaded library’s receipt.
When the claim did not already exist, unload_lib retracts it with the
rest of lib_id’s recorded load effects. Pre-existing identical claims
are left owned by their original publisher.
Sourcepub fn query_facts(&self, pattern: ClaimPattern) -> Result<Vec<Claim>>
pub fn query_facts(&self, pattern: ClaimPattern) -> Result<Vec<Claim>>
Queries the fact store for claims matching pattern, applying read policy.
Sourcepub fn promotion_search_limits(&self) -> PromotionSearchLimits
pub fn promotion_search_limits(&self) -> PromotionSearchLimits
Returns the limits bounding number-domain promotion search.
Sourcepub fn set_promotion_search_limits(&mut self, limits: PromotionSearchLimits)
pub fn set_promotion_search_limits(&mut self, limits: PromotionSearchLimits)
Sets the limits bounding number-domain promotion search.
Sourcepub fn eval_policy(&self) -> &dyn EvalPolicy
pub fn eval_policy(&self) -> &dyn EvalPolicy
Returns the active evaluation policy.
Sourcepub fn eval_policy_ref(&self) -> EvalPolicyRef
pub fn eval_policy_ref(&self) -> EvalPolicyRef
Returns a shared handle to the active evaluation policy.
Sourcepub fn eval_policy_name(&self) -> &'static str
pub fn eval_policy_name(&self) -> &'static str
Returns the name of the active evaluation policy.
Sourcepub fn set_eval_policy(&mut self, eval_policy: EvalPolicyRef)
pub fn set_eval_policy(&mut self, eval_policy: EvalPolicyRef)
Replaces the active evaluation policy.
Sourcepub fn set_macro_expander(&mut self, macro_expander: MacroExpanderRef)
pub fn set_macro_expander(&mut self, macro_expander: MacroExpanderRef)
Installs a macro expander.
Sourcepub fn clear_macro_expander(&mut self)
pub fn clear_macro_expander(&mut self)
Removes any installed macro expander.
Sourcepub fn macro_expander_ref(&self) -> Option<MacroExpanderRef>
pub fn macro_expander_ref(&self) -> Option<MacroExpanderRef>
Returns the installed macro expander, if any.
Sourcepub fn expand_macros(&mut self, phase: Phase, expr: Expr) -> Result<Expr>
pub fn expand_macros(&mut self, phase: Phase, expr: Expr) -> Result<Expr>
Expands macros in expr for the given phase, or returns it unchanged.
Sourcepub fn diagnostics(&self) -> &Diagnostics
pub fn diagnostics(&self) -> &Diagnostics
Returns the accumulated diagnostics.
Sourcepub fn take_diagnostics(&mut self) -> Vec<Diagnostic>
pub fn take_diagnostics(&mut self) -> Vec<Diagnostic>
Drains and returns the accumulated diagnostics.
Sourcepub fn push_diagnostic(&mut self, diagnostic: Diagnostic)
pub fn push_diagnostic(&mut self, diagnostic: Diagnostic)
Records an already-built diagnostic.
Sourcepub fn push_info(&mut self, message: impl Into<String>)
pub fn push_info(&mut self, message: impl Into<String>)
Records an info-level diagnostic from a message.
Sourcepub fn grant(&mut self, capability: CapabilityName)
pub fn grant(&mut self, capability: CapabilityName)
Grants a capability to this context.
Sourcepub fn grant_named(&mut self, capability: &'static str)
pub fn grant_named(&mut self, capability: &'static str)
Grants a capability named by a static string.
Sourcepub fn capabilities(&self) -> &Capabilities
pub fn capabilities(&self) -> &Capabilities
Returns the granted capability set.
Sourcepub fn with_capabilities<T>(
&mut self,
capabilities: Capabilities,
f: impl FnOnce(&mut Self) -> Result<T>,
) -> Result<T>
pub fn with_capabilities<T>( &mut self, capabilities: Capabilities, f: impl FnOnce(&mut Self) -> Result<T>, ) -> Result<T>
Runs f with capabilities installed, then restores the prior set.
Sourcepub fn resolve_class(&self, symbol: &Symbol) -> Result<Value>
pub fn resolve_class(&self, symbol: &Symbol) -> Result<Value>
Resolves a registered class by symbol.
Sourcepub fn resolve_function(&self, symbol: &Symbol) -> Result<Value>
pub fn resolve_function(&self, symbol: &Symbol) -> Result<Value>
Resolves a registered function by symbol.
Sourcepub fn resolve_macro(&self, symbol: &Symbol) -> Result<Value>
pub fn resolve_macro(&self, symbol: &Symbol) -> Result<Value>
Resolves a registered macro by symbol.
Sourcepub fn resolve_shape(&self, symbol: &Symbol) -> Result<Value>
pub fn resolve_shape(&self, symbol: &Symbol) -> Result<Value>
Resolves a registered shape by symbol.
Sourcepub fn resolve_codec(&self, symbol: &Symbol) -> Result<Value>
pub fn resolve_codec(&self, symbol: &Symbol) -> Result<Value>
Resolves a registered codec by symbol.
Sourcepub fn resolve_number_domain(&self, symbol: &Symbol) -> Result<Value>
pub fn resolve_number_domain(&self, symbol: &Symbol) -> Result<Value>
Resolves a registered number domain by symbol.
Sourcepub fn resolve_value(&self, symbol: &Symbol) -> Result<Value>
pub fn resolve_value(&self, symbol: &Symbol) -> Result<Value>
Resolves a registered value binding by symbol.
Sourcepub fn call_value(&mut self, value: Value, args: Args) -> Result<Value>
pub fn call_value(&mut self, value: Value, args: Args) -> Result<Value>
Calls a callable value with already-evaluated arguments.
Sourcepub fn call_exprs(&mut self, value: Value, args: Vec<Expr>) -> Result<Value>
pub fn call_exprs(&mut self, value: Value, args: Vec<Expr>) -> Result<Value>
Calls a callable value with raw, unevaluated argument expressions.
Sourcepub fn call_function(&mut self, symbol: &Symbol, args: Args) -> Result<Value>
pub fn call_function(&mut self, symbol: &Symbol, args: Args) -> Result<Value>
Resolves a function by symbol and calls it.
Sourcepub fn call_class(&mut self, symbol: &Symbol, args: Args) -> Result<Value>
pub fn call_class(&mut self, symbol: &Symbol, args: Args) -> Result<Value>
Resolves a class by symbol and calls its constructor.
Sourcepub fn read_construct(
&mut self,
class: &Symbol,
args: Vec<Value>,
) -> Result<Value>
pub fn read_construct( &mut self, class: &Symbol, args: Vec<Value>, ) -> Result<Value>
Constructs an instance of class from read-time arguments.
Requires the read_construct_capability.
Sourcepub fn force(&mut self, value: Value, demand: Demand) -> Result<Value>
pub fn force(&mut self, value: Value, demand: Demand) -> Result<Value>
Forces a value to the requested demand through the active eval policy.
Sourcepub fn eval_expr(&mut self, expr: Expr) -> Result<Value>
pub fn eval_expr(&mut self, expr: Expr) -> Result<Value>
Evaluates an expression through the active eval policy.
Sourcepub fn symbol_is_bound(&mut self, name: &Symbol) -> bool
pub fn symbol_is_bound(&mut self, name: &Symbol) -> bool
Returns true when name resolves across environment or registry layers.
Sourcepub fn resolve_unbound_symbol(&mut self, symbol: Symbol) -> Result<Value>
pub fn resolve_unbound_symbol(&mut self, symbol: Symbol) -> Result<Value>
Resolves an unbound value-position symbol through the active eval policy.
Sourcepub fn resolve_unbound_call(
&mut self,
operator: Symbol,
args: Vec<Expr>,
) -> Result<Value>
pub fn resolve_unbound_call( &mut self, operator: Symbol, args: Vec<Expr>, ) -> Result<Value>
Resolves an unbound call through the active eval policy.
Sourcepub fn require(&self, capability: &CapabilityName) -> Result<()>
pub fn require(&self, capability: &CapabilityName) -> Result<()>
Demands a capability, returning Error::CapabilityDenied when absent.
Sourcepub fn require_all(&self, capabilities: &[CapabilityName]) -> Result<()>
pub fn require_all(&self, capabilities: &[CapabilityName]) -> Result<()>
Demands every capability in turn, failing on the first absent one.
Source§impl Cx
impl Cx
Sourcepub fn parse_number_literal(
&mut self,
text: &str,
) -> Result<Option<NumberLiteral>>
pub fn parse_number_literal( &mut self, text: &str, ) -> Result<Option<NumberLiteral>>
Parses a number literal by trying registered number domains in
sorted_number_domains order.
That ordering is only a parse-disambiguation rule. Codecs that already
have an explicit source NumberLiteral must preserve its domain instead
of relying on this order to recover the same domain from canonical text.
Sourcepub fn encode_number_value(
&mut self,
value: Value,
) -> Result<Option<NumberLiteral>>
pub fn encode_number_value( &mut self, value: Value, ) -> Result<Option<NumberLiteral>>
Encodes a value to a number literal via the first domain that accepts it.
Sourcepub fn number_value_ref(
&mut self,
value: Value,
) -> Result<Option<NumberValueRef>>
pub fn number_value_ref( &mut self, value: Value, ) -> Result<Option<NumberValueRef>>
Resolves a value to a NumberValueRef carrying its domain and literal.
Sourcepub fn apply_value_number_binary_op(
&mut self,
operator: &Symbol,
left: Value,
right: Value,
) -> Result<Value>
pub fn apply_value_number_binary_op( &mut self, operator: &Symbol, left: Value, right: Value, ) -> Result<Value>
Applies a binary numeric operator to two values, selecting the best registered rule after promoting operands.
Errors with Error::AmbiguousNumberDispatch when no single rule wins.
Sourcepub fn apply_number_binary_op(
&mut self,
operator: &Symbol,
left: NumberLiteral,
right: NumberLiteral,
) -> Result<Value>
pub fn apply_number_binary_op( &mut self, operator: &Symbol, left: NumberLiteral, right: NumberLiteral, ) -> Result<Value>
Applies a binary numeric operator to two literals.
Convenience over apply_value_number_binary_op.
Sourcepub fn apply_value_number_unary_op(
&mut self,
operator: &Symbol,
operand: Value,
) -> Result<Value>
pub fn apply_value_number_unary_op( &mut self, operator: &Symbol, operand: Value, ) -> Result<Value>
Applies a unary numeric operator to a value, selecting the best registered rule after promoting the operand.
Sourcepub fn apply_number_unary_op(
&mut self,
operator: &Symbol,
operand: NumberLiteral,
) -> Result<Value>
pub fn apply_number_unary_op( &mut self, operator: &Symbol, operand: NumberLiteral, ) -> Result<Value>
Applies a unary numeric operator to a literal.
Convenience over apply_value_number_unary_op.
Sourcepub fn apply_value_number_reduction_op(
&mut self,
operator: &Symbol,
operands: Vec<Value>,
) -> Result<Value>
pub fn apply_value_number_reduction_op( &mut self, operator: &Symbol, operands: Vec<Value>, ) -> Result<Value>
Applies a reduction numeric operator across many values, selecting the best registered rule after promoting all operands to a common domain.
Sourcepub fn apply_number_reduction_op(
&mut self,
operator: &Symbol,
operands: Vec<NumberLiteral>,
) -> Result<Value>
pub fn apply_number_reduction_op( &mut self, operator: &Symbol, operands: Vec<NumberLiteral>, ) -> Result<Value>
Applies a reduction numeric operator across many literals.
Convenience over apply_value_number_reduction_op.
Source§impl Cx
impl Cx
Sourcepub fn load_lib(&mut self, lib: &dyn Lib) -> Result<LibId>
pub fn load_lib(&mut self, lib: &dyn Lib) -> Result<LibId>
Loads a single library: checks its requested capabilities and dependency
versions, runs its Lib::load against a
load transaction, commits the result, and publishes its load claims.
Sourcepub fn load_libs(&mut self, libs: &[&dyn Lib]) -> Result<Vec<LibId>>
pub fn load_libs(&mut self, libs: &[&dyn Lib]) -> Result<Vec<LibId>>
Loads several libraries in dependency order, returning their ids in load order.