Skip to main content

vibe/extension_registry/
prepare.rs

1//! Cold call preparation (`prepare_extension_call`) resolving canonical IRIs,
2//! short aliases, `SemVerReq` constraints, and target profiles into `PreparedCallHandle`s.
3
4use super::registry::ExtensionRegistry;
5use vibescript_abi::{
6    PreparedCallHandle, SemVerReq, TargetProfileMask, VibeOpCode, VibeTrap,
7};
8
9/// Cold-stage resolver (`Stage 2: Prepare`) that binds `(namespace_or_alias, sub_namespace, func_name)`
10/// against `version_req` and `target_profile` into a generational `PreparedCallHandle`.
11pub fn prepare_extension_call(
12    registry: &ExtensionRegistry<'_>,
13    namespace_or_alias: &str,
14    sub_namespace: &str,
15    func_name: &str,
16    version_req: SemVerReq,
17    target_profile: TargetProfileMask,
18) -> Result<PreparedCallHandle, VibeTrap> {
19    let (slot, _provider, desc) = registry
20        .find_by_namespace_or_alias(namespace_or_alias)
21        .ok_or(VibeTrap::NamespaceNotFound)?;
22
23    if !version_req.matches(desc.versions.provider_version) {
24        return Err(VibeTrap::VersionRequirementUnsatisfied);
25    }
26
27    if !desc.supported_targets.supports(target_profile) {
28        return Err(VibeTrap::TargetProfileUnsupported);
29    }
30
31    let sub = ExtensionRegistry::find_sub_namespace(desc, sub_namespace)
32        .ok_or(VibeTrap::FunctionNotFound)?;
33
34    let func = sub
35        .functions
36        .iter()
37        .find(|f| f.name == func_name)
38        .ok_or(VibeTrap::FunctionNotFound)?;
39
40    let opcode = VibeOpCode::pack(slot, func.stable_id);
41    Ok(PreparedCallHandle {
42        opcode,
43        registry_generation: registry.generation(),
44        reserved: 0,
45        provider_iri_hash: desc.canonical_iri.fnv1a_hash,
46        func_iri_hash: func.canonical_iri.fnv1a_hash,
47        contract_digest: func.contract.contract_digest,
48    })
49}