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>> + Send>>;
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. Receives an explicit
44/// [`crate::vm::AsyncBuiltinCtx`] handle so handlers thread the context they
45/// were given through async helper calls.
46pub type AsyncHandler = fn(crate::vm::AsyncBuiltinCtx, Vec<VmValue>) -> AsyncBuiltinFuture;
47
48/// Runtime handler attached to a `VmBuiltinDef`. `None` covers parser-only
49/// builtins (method-dispatched at runtime, but the parser still wants a
50/// signature for typo suggestion + return-type inference).
51#[derive(Debug, Clone, Copy)]
52pub enum VmBuiltinHandler {
53 Sync(SyncHandler),
54 Async(AsyncHandler),
55 /// No runtime impl — registered with the parser only (e.g. `len`,
56 /// `split`, method-dispatch builtins).
57 None,
58}
59
60/// `BuiltinDef` specialized to the VM's handler type.
61pub type VmBuiltinDef = BuiltinDef<VmBuiltinHandler>;
62
63/// Eager-registration helper: install every entry (name + aliases) on `vm`
64/// using the macro-emitted handler. Each module's `register_*_builtins(vm)`
65/// calls this with its `MODULE_BUILTINS` slice so the call ordering between
66/// modules stays deterministic (e.g. `clock` overrides `process::timestamp`).
67pub fn register_builtin_defs(vm: &mut crate::vm::Vm, defs: &'static [&'static VmBuiltinDef]) {
68 for def in defs {
69 vm.register_builtin_def(def);
70 }
71}