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