Skip to main content

harn_vm/stdlib/
macros.rs

1//! Support module referenced by `#[harn_builtin]`-emitted code.
2//!
3//! The proc-macro emits paths like `crate::stdlib::macros::VmBuiltinDef`,
4//! `crate::stdlib::macros::BuiltinSignature`, etc. This module re-exports
5//! everything those expansions need so any `harn-vm/src/stdlib/*.rs` file
6//! can apply `#[harn_builtin]` to a fn without extra imports.
7
8use std::future::Future;
9use std::pin::Pin;
10
11pub use crate::value::{VmError, VmValue};
12
13pub use harn_builtin_macros::{harn_builtin, harn_capability_method};
14pub use harn_builtin_meta::{
15    BuiltinContract, BuiltinExposure, BuiltinSignature, CapabilityId, EffectAccess, EffectKind,
16    EffectSpec, Param, ResourceSelector, ShapeFieldDescriptor, Ty, TY_ANY, TY_BOOL, TY_BYTES,
17    TY_BYTES_OR_NIL, TY_CLOSURE, TY_DICT, TY_DICT_OR_NIL, TY_DURATION, TY_FLOAT, TY_INT,
18    TY_INT_OR_NIL, TY_LIST, TY_NEVER, TY_NIL, TY_NUMBER, TY_RESOURCE, TY_STRING, TY_STRING_OR_NIL,
19};
20pub use harn_builtin_registry::BuiltinDef;
21// Re-export the shared shape vocabulary so `#[harn_builtin]` sig strings can
22// reference a named structural shape via the `@NAME` injection form, which
23// the macro expands to `crate::stdlib::macros::shapes::NAME`.
24pub use harn_builtin_meta::shapes;
25// Re-export so the `#[harn_builtin]` proc-macro can name the
26// distributed-slice attribute without each call-site importing linkme.
27pub use linkme::distributed_slice;
28
29/// Workspace-global registry of `#[harn_builtin]`-emitted definitions.
30/// Each annotated fn contributes one entry via
31/// `#[linkme::distributed_slice(ALL_BUILTIN_DEFS)]`, so there is no
32/// per-module `MODULE_BUILTINS` slice to maintain. The CLI / LSP / lint /
33/// serve / dap binaries force-link `harn-vm` to defeat rlib dead-code
34/// stripping (linkme issue #36) so every static lands in this slice at
35/// link time.
36#[linkme::distributed_slice]
37pub static ALL_BUILTIN_DEFS: [&'static VmBuiltinDef];
38
39/// Pinned future returned by async builtin handlers.
40pub type AsyncBuiltinFuture = Pin<Box<dyn Future<Output = Result<VmValue, VmError>> + Send>>;
41
42/// Sync builtin handler signature (matches `crate::vm::dispatch`'s register_builtin shape).
43pub type SyncHandler = fn(&[VmValue], &mut String) -> Result<VmValue, VmError>;
44/// Async builtin handler signature. Receives an explicit
45/// [`crate::vm::AsyncBuiltinCtx`] handle so handlers thread the context they
46/// were given through async helper calls.
47pub type AsyncHandler = fn(crate::vm::AsyncBuiltinCtx, Vec<VmValue>) -> AsyncBuiltinFuture;
48
49/// Runtime handler attached to a `VmBuiltinDef`. `None` covers parser-only
50/// builtins (method-dispatched at runtime, but the parser still wants a
51/// signature for typo suggestion + return-type inference).
52#[derive(Debug, Clone, Copy)]
53pub enum VmBuiltinHandler {
54    Sync(SyncHandler),
55    Async(AsyncHandler),
56    /// No runtime impl — registered with the parser only (e.g. `len`,
57    /// `split`, method-dispatch builtins).
58    None,
59}
60
61/// `BuiltinDef` specialized to the VM's handler type.
62pub type VmBuiltinDef = BuiltinDef<VmBuiltinHandler>;
63
64/// Eager-registration helper: install every entry (name + aliases) on `vm`
65/// using the macro-emitted handler. Each module's `register_*_builtins(vm)`
66/// calls this with its `MODULE_BUILTINS` slice so the call ordering between
67/// modules stays deterministic (e.g. `clock` overrides `process::timestamp`).
68pub fn register_builtin_defs(vm: &mut crate::vm::Vm, defs: &'static [&'static VmBuiltinDef]) {
69    for def in defs {
70        vm.register_builtin_def(def);
71    }
72}