Skip to main content

xur_runtime/registry/
builder.rs

1use std::marker::PhantomData;
2
3use facet::{Facet, Shape};
4use xur_resolve::{NativeFnId, NativeTypeId};
5
6use super::hooks::TypeHooks;
7use super::into_native::{
8    IntoBuiltinMethod, IntoBuiltinMethodMut, IntoNativeFn, IntoNativeMethod, IntoNativeMethodMut,
9};
10use super::{CallFn, Context, NativeType, Registry, Signature};
11use crate::{BuiltinType, RunResult, Value};
12use xur_resolve::{NativeVariantDecl, NativeVariantKind};
13
14/// A type whose static [`Shape`] can serve as a return-type hint for the LSP.
15///
16/// Blanket-implemented for every [`Facet`] type. Implement manually for
17/// xur-only runtime types that have no Rust equivalent (see [`XurPtr`]).
18pub trait Returns {
19    const RET: &'static Shape;
20}
21
22impl<T: Facet<'static>> Returns for T {
23    const RET: &'static Shape = T::SHAPE;
24}
25
26/// Runtime-only xur types that have no Facet equivalent. Use with the
27/// An element type for compound return-type hints.
28#[derive(Clone, Copy)]
29pub enum ElemHint {
30    /// A Facet shape, introspectable for generics.
31    Shape(&'static Shape),
32    /// A type name for primitives that have no Facet representation (e.g. `type`).
33    Name(&'static str),
34    /// The nth type parameter of the receiver (e.g. element type of a list).
35    Param(u8),
36}
37
38/// `_xur` builder methods to annotate return types for the LSP.
39#[derive(Clone, Copy)]
40pub enum XurType {
41    /// Pointer with optional pointee type, displayed as `*T` or `ptr`.
42    Ptr(Option<&'static Shape>),
43    /// List with optional element type, displayed as `list` or `list[T]`.
44    List(Option<ElemHint>),
45    /// Map with optional key/value types, displayed as `map` or `map<K, V>`.
46    Map(Option<&'static Shape>, Option<&'static Shape>),
47    /// A named type with generic type parameters, e.g. `Result<module, str>`.
48    Named(&'static str, &'static [ElemHint]),
49    Record,
50    Module,
51    Unit,
52}
53
54impl XurType {
55    pub const PTR: Self = Self::Ptr(None);
56    pub const LIST: Self = Self::List(None);
57    pub const MAP: Self = Self::Map(None, None);
58    pub const UNIT: Self = Self::Unit;
59
60    pub fn ptr<T: Facet<'static>>() -> Self {
61        Self::Ptr(Some(T::SHAPE))
62    }
63    pub fn list<T: Facet<'static>>() -> Self {
64        Self::List(Some(ElemHint::Shape(T::SHAPE)))
65    }
66    pub fn list_named(name: &'static str) -> Self {
67        Self::List(Some(ElemHint::Name(name)))
68    }
69    pub fn map<K: Facet<'static>, V: Facet<'static>>() -> Self {
70        Self::Map(Some(K::SHAPE), Some(V::SHAPE))
71    }
72    pub fn map_keys<K: Facet<'static>>() -> Self {
73        Self::Map(Some(K::SHAPE), None)
74    }
75    pub fn map_values<V: Facet<'static>>() -> Self {
76        Self::Map(None, Some(V::SHAPE))
77    }
78}
79
80/// A return-type hint stored on a raw function: either a Facet [`Shape`]
81/// (introspectable for generics) or an [`XurType`] name.
82#[derive(Clone, Copy)]
83pub enum ReturnHint {
84    Shape(&'static Shape),
85    Xur(XurType),
86}
87
88/// Adds items to one module of a [`Registry`].
89pub struct ModuleBuilder<'r> {
90    registry: &'r Registry,
91    pub(super) module: usize,
92}
93
94impl<'r> ModuleBuilder<'r> {
95    pub(super) fn new(registry: &'r Registry, module: usize) -> Self {
96        Self { registry, module }
97    }
98
99    /// The registry being filled in.
100    pub fn registry(&self) -> &'r Registry {
101        self.registry
102    }
103
104    /// The submodule `name` of this module, created if needed. A lazy
105    /// module can fill in submodules as well as itself.
106    pub fn submodule(&self, name: &str) -> ModuleBuilder<'r> {
107        let parent = self.module;
108        let module = self
109            .registry
110            .with_inner(|inner| inner.child(Some(parent), name));
111        ModuleBuilder::new(self.registry, module)
112    }
113}
114
115impl ModuleBuilder<'_> {
116    /// Registers a compile-time macro expander in this module. Items with
117    /// `#[name(...)]` attributes are expanded before name resolution.
118    pub fn register_macro(
119        &mut self,
120        name: &str,
121        expander: impl xur_resolve::macros::MacroExpander + 'static,
122    ) -> &mut Self {
123        self.registry.with_inner(|inner| {
124            inner.modules[self.module]
125                .macros
126                .insert(name.to_owned(), std::sync::Arc::new(expander));
127        });
128        self
129    }
130
131    /// Registers a Rust function or closure whose argument and return
132    /// types implement [`Facet`].
133    pub fn function<Marker>(&mut self, name: &str, f: impl IntoNativeFn<Marker>) -> &mut Self {
134        let (signature, call) = f.into_native();
135        self.add_module_fn(name, Some(signature), Vec::new(), call, None)
136    }
137
138    /// Like [`function`](Self::function), with explicit parameter names for
139    /// LSP hover and inlay hints.
140    pub fn named_function<Marker>(
141        &mut self,
142        name: &str,
143        params: &[&str],
144        f: impl IntoNativeFn<Marker>,
145    ) -> &mut Self {
146        let (signature, call) = f.into_native();
147        let names = params.iter().map(|s| s.to_string()).collect();
148        self.add_module_fn(name, Some(signature), names, call, None)
149    }
150
151    /// Registers a function that works on Xur values directly and can use
152    /// the running program through its [`Context`].
153    pub fn raw_function(
154        &mut self,
155        name: &str,
156        f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
157    ) -> &mut Self {
158        self.add_module_fn(name, None, Vec::new(), Box::new(f), None)
159    }
160
161    /// Like [`raw_function`](Self::raw_function), declaring the return type
162    /// for LSP inlay hints.
163    pub fn raw_function_returns<R: Returns>(
164        &mut self,
165        name: &str,
166        f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
167    ) -> &mut Self {
168        self.add_module_fn(
169            name,
170            None,
171            Vec::new(),
172            Box::new(f),
173            Some(ReturnHint::Shape(R::RET)),
174        )
175    }
176
177    /// Like [`raw_function_returns`](Self::raw_function_returns) for
178    /// xur-internal types that have no Facet equivalent.
179    pub fn raw_function_returns_xur(
180        &mut self,
181        name: &str,
182        ret: XurType,
183        f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
184    ) -> &mut Self {
185        self.add_module_fn(
186            name,
187            None,
188            Vec::new(),
189            Box::new(f),
190            Some(ReturnHint::Xur(ret)),
191        )
192    }
193
194    /// Like [`raw_function`](Self::raw_function), with explicit parameter
195    /// names for LSP hover and inlay hints.
196    pub fn named_raw_function(
197        &mut self,
198        name: &str,
199        params: &[&str],
200        f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
201    ) -> &mut Self {
202        let names = params.iter().map(|s| s.to_string()).collect();
203        self.add_module_fn(name, None, names, Box::new(f), None)
204    }
205
206    /// Like [`named_raw_function`](Self::named_raw_function), declaring the
207    /// return type for LSP inlay hints.
208    pub fn named_raw_function_returns<R: Returns>(
209        &mut self,
210        name: &str,
211        params: &[&str],
212        f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
213    ) -> &mut Self {
214        let names = params.iter().map(|s| s.to_string()).collect();
215        self.add_module_fn(
216            name,
217            None,
218            names,
219            Box::new(f),
220            Some(ReturnHint::Shape(R::RET)),
221        )
222    }
223
224    /// Like [`named_raw_function_returns`](Self::named_raw_function_returns)
225    /// for xur-internal types that have no Facet equivalent.
226    pub fn named_raw_function_returns_xur(
227        &mut self,
228        name: &str,
229        params: &[&str],
230        ret: XurType,
231        f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
232    ) -> &mut Self {
233        let names = params.iter().map(|s| s.to_string()).collect();
234        self.add_module_fn(name, None, names, Box::new(f), Some(ReturnHint::Xur(ret)))
235    }
236
237    pub(super) fn add_raw(
238        &mut self,
239        name: &str,
240        names: Vec<String>,
241        call: Box<CallFn>,
242    ) -> NativeFnId {
243        let decl = self.registry.add_fn(name, None, names, call);
244        let id = decl.id;
245        let module = self.module;
246        self.registry
247            .with_inner(|inner| inner.modules[module].functions.push(decl));
248        id
249    }
250
251    fn add_module_fn(
252        &mut self,
253        name: &str,
254        signature: Option<Signature>,
255        names: Vec<String>,
256        call: Box<CallFn>,
257        return_hint: Option<ReturnHint>,
258    ) -> &mut Self {
259        let decl = self.registry.add_fn(name, signature, names, call);
260        if let Some(hint) = return_hint {
261            self.registry.set_return_hint(decl.id, hint);
262        }
263        let module = self.module;
264        self.registry
265            .with_inner(|inner| inner.modules[module].functions.push(decl));
266        self
267    }
268
269    /// Registers the type with facet shape `shape` as `name`, without its
270    /// Rust type: values of it passed to Xur become records of this type,
271    /// which written types can check and which display with its name. For
272    /// registering every type reachable from some shape, such as a
273    /// library's.
274    pub fn shape_type(&mut self, name: &str, shape: &'static Shape) -> NativeTypeId {
275        let module = self.module;
276        self.registry.with_inner(|inner| {
277            let id = NativeTypeId(inner.types.len() as u32);
278            inner.types.push(NativeType {
279                name: name.to_owned(),
280                shape,
281                functions: Vec::new(),
282                hooks: None,
283                explicit_variants: None,
284            });
285            inner.modules[module].types.push(id);
286            id
287        })
288    }
289
290    /// Registers a `#[repr(inttype)]` enum whose variants are integer
291    /// constants. Each `(name, value)` pair becomes a unit variant that
292    /// lowers to `Const(Int(value))`.
293    pub fn repr_enum(&mut self, name: &str, variants: &[(&str, i64)]) -> &mut Self {
294        let module = self.module;
295        let variant_decls: Vec<NativeVariantDecl> = variants
296            .iter()
297            .map(|(vname, value)| NativeVariantDecl {
298                name: vname.to_string(),
299                kind: NativeVariantKind::Unit,
300                repr_value: Some(*value),
301            })
302            .collect();
303        self.registry.with_inner(|inner| {
304            let id = NativeTypeId(inner.types.len() as u32);
305            inner.types.push(NativeType {
306                name: name.to_owned(),
307                shape: <()>::SHAPE,
308                functions: Vec::new(),
309                hooks: None,
310                explicit_variants: Some(variant_decls),
311            });
312            inner.modules[module].types.push(id);
313        });
314        self
315    }
316
317    /// Returns a [`TypeBuilder`] for a type `T` that was already registered
318    /// (by this or another module's [`ty`](Self::ty)). Use it to add methods
319    /// to a shared type from a different module. Panics if `T` has not been
320    /// registered yet.
321    pub fn ty_existing<T: Facet<'static> + Clone + Send + 'static>(
322        &mut self,
323    ) -> TypeBuilder<'_, T> {
324        let id = self
325            .registry
326            .type_of_shape(T::SHAPE)
327            .expect("ty_existing: type not registered");
328        TypeBuilder {
329            registry: self.registry,
330            ty: id,
331            _ty: PhantomData,
332        }
333    }
334
335    /// Registers the Rust type `T` as `name`. Values of `T` passed to Xur
336    /// become native objects holding the Rust value.
337    pub fn ty<T: Facet<'static> + Clone + Send + 'static>(
338        &mut self,
339        name: &str,
340    ) -> TypeBuilder<'_, T> {
341        let module = self.module;
342        let id = self.registry.with_inner(|inner| {
343            let id = NativeTypeId(inner.types.len() as u32);
344            inner.types.push(NativeType {
345                name: name.to_owned(),
346                shape: T::SHAPE,
347                functions: Vec::new(),
348                hooks: Some(TypeHooks::new::<T>()),
349                explicit_variants: None,
350            });
351            inner.modules[module].types.push(id);
352            id
353        });
354        TypeBuilder {
355            registry: self.registry,
356            ty: id,
357            _ty: PhantomData,
358        }
359    }
360
361    /// Registers the Rust type `T` as `name` without requiring `Clone`, for
362    /// a type that borrows memory kept alive beside it (see
363    /// [`Registry::borrowing_object`]). Its fields and methods work, but it
364    /// cannot be cloned into another value or rebuilt from Xur.
365    pub fn ty_borrowing<T: Facet<'static> + Send + 'static>(
366        &mut self,
367        name: &str,
368    ) -> TypeBuilder<'_, T> {
369        let module = self.module;
370        let id = self.registry.with_inner(|inner| {
371            let id = NativeTypeId(inner.types.len() as u32);
372            inner.types.push(NativeType {
373                name: name.to_owned(),
374                shape: T::SHAPE,
375                functions: Vec::new(),
376                hooks: Some(TypeHooks::borrowing::<T>()),
377                explicit_variants: None,
378            });
379            inner.modules[module].types.push(id);
380            id
381        });
382        TypeBuilder {
383            registry: self.registry,
384            ty: id,
385            _ty: PhantomData,
386        }
387    }
388}
389
390/// Adds functions to the registered native type `T`.
391pub struct TypeBuilder<'r, T> {
392    registry: &'r Registry,
393    ty: NativeTypeId,
394    _ty: PhantomData<T>,
395}
396
397impl<T: Facet<'static> + 'static> TypeBuilder<'_, T> {
398    /// Registers the associated function `Type::name`. If its first
399    /// parameter is `T` it can also be called as a method, on a copy.
400    pub fn function<Marker>(&mut self, name: &str, f: impl IntoNativeFn<Marker>) -> &mut Self {
401        let (signature, call) = f.into_native();
402        self.add(name, Some(signature), Vec::new(), call, None)
403    }
404
405    /// Registers a function working on Xur values directly, with the
406    /// object first when called as a method. For methods taking or
407    /// returning arbitrary Xur values, like a channel's `send`.
408    pub fn raw_function(
409        &mut self,
410        name: &str,
411        f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
412    ) -> &mut Self {
413        self.add(name, None, Vec::new(), Box::new(f), None)
414    }
415
416    /// Like [`raw_function`](Self::raw_function), declaring the return type
417    /// for LSP inlay hints.
418    pub fn raw_function_returns<R: Returns>(
419        &mut self,
420        name: &str,
421        f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
422    ) -> &mut Self {
423        self.add(
424            name,
425            None,
426            Vec::new(),
427            Box::new(f),
428            Some(ReturnHint::Shape(R::RET)),
429        )
430    }
431
432    /// Like [`raw_function_returns`](Self::raw_function_returns) for
433    /// xur-internal types that have no Facet equivalent.
434    pub fn raw_function_returns_xur(
435        &mut self,
436        name: &str,
437        ret: XurType,
438        f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
439    ) -> &mut Self {
440        self.add(
441            name,
442            None,
443            Vec::new(),
444            Box::new(f),
445            Some(ReturnHint::Xur(ret)),
446        )
447    }
448
449    /// Registers a method taking `&self`: `value.name(args)` in Xur.
450    pub fn method<Marker>(&mut self, name: &str, f: impl IntoNativeMethod<T, Marker>) -> &mut Self {
451        let (signature, call) = f.into_native();
452        self.add(name, Some(signature), Vec::new(), call, None)
453    }
454
455    /// Registers a method taking `&mut self`, which changes the object in
456    /// place.
457    pub fn method_mut<Marker>(
458        &mut self,
459        name: &str,
460        f: impl IntoNativeMethodMut<T, Marker>,
461    ) -> &mut Self {
462        let (signature, call) = f.into_native();
463        self.add(name, Some(signature), Vec::new(), call, None)
464    }
465
466    /// Like [`function`](Self::function), naming the parameters for hovers
467    /// and docs.
468    pub fn named_function<Marker>(
469        &mut self,
470        name: &str,
471        params: &[&str],
472        f: impl IntoNativeFn<Marker>,
473    ) -> &mut Self {
474        let (signature, call) = f.into_native();
475        self.add(name, Some(signature), owned(params), call, None)
476    }
477
478    /// Like [`method`](Self::method), naming the parameters after the
479    /// receiver for hovers and docs.
480    pub fn named_method<Marker>(
481        &mut self,
482        name: &str,
483        params: &[&str],
484        f: impl IntoNativeMethod<T, Marker>,
485    ) -> &mut Self {
486        let (signature, call) = f.into_native();
487        self.add(name, Some(signature), with_self(params), call, None)
488    }
489
490    /// Like [`method_mut`](Self::method_mut), naming the parameters after
491    /// the receiver for hovers and docs.
492    pub fn named_method_mut<Marker>(
493        &mut self,
494        name: &str,
495        params: &[&str],
496        f: impl IntoNativeMethodMut<T, Marker>,
497    ) -> &mut Self {
498        let (signature, call) = f.into_native();
499        self.add(name, Some(signature), with_self(params), call, None)
500    }
501
502    /// The registry being filled in.
503    pub fn registry(&self) -> &Registry {
504        self.registry
505    }
506
507    pub(super) fn add_raw(
508        &mut self,
509        name: &str,
510        names: Vec<String>,
511        call: Box<CallFn>,
512    ) -> NativeFnId {
513        let decl = self.registry.add_fn(name, None, names, call);
514        let id = decl.id;
515        let ty = self.ty;
516        self.registry
517            .with_inner(|inner| inner.types[ty.0 as usize].functions.push(decl));
518        id
519    }
520
521    fn add(
522        &mut self,
523        name: &str,
524        signature: Option<Signature>,
525        param_names: Vec<String>,
526        call: Box<CallFn>,
527        return_hint: Option<ReturnHint>,
528    ) -> &mut Self {
529        let decl = self.registry.add_fn(name, signature, param_names, call);
530        if let Some(hint) = return_hint {
531            self.registry.set_return_hint(decl.id, hint);
532        }
533        let ty = self.ty;
534        self.registry
535            .with_inner(|inner| inner.types[ty.0 as usize].functions.push(decl));
536        self
537    }
538}
539
540/// Adds methods to a built-in type, like `list` or `str`.
541pub struct BuiltinBuilder<'r> {
542    registry: &'r Registry,
543    ty: BuiltinType,
544}
545
546impl<'r> BuiltinBuilder<'r> {
547    pub(super) fn new(registry: &'r Registry, ty: BuiltinType) -> Self {
548        Self { registry, ty }
549    }
550
551    /// Registers a method as a Rust function taking the receiver, converted,
552    /// first: `|s: String| s.to_uppercase()` for `"a".to_upper()`.
553    ///
554    /// **Note:** this clones the receiver out of its `Value` wrapper. For
555    /// builtin types backed by `Arc<Mutex<..>>` (bytes, list), prefer
556    /// [`method`](Self::method) which borrows in place.
557    pub fn function<Marker>(&mut self, name: &str, f: impl IntoNativeFn<Marker>) -> &mut Self {
558        let (signature, call) = f.into_native();
559        self.add(name, Some(signature), Vec::new(), call, None)
560    }
561
562    /// Registers a method that borrows the receiver without cloning:
563    /// `|bytes: &[u8], i: i64| -> Option<i64>`.
564    ///
565    /// `Recv` is `[u8]` for bytes or `str` for strings — any type that
566    /// implements [`BuiltinRecv`].
567    pub fn method<Recv: ?Sized + 'static, Marker>(
568        &mut self,
569        name: &str,
570        f: impl IntoBuiltinMethod<Recv, Marker>,
571    ) -> &mut Self {
572        let (signature, call) = f.into_native();
573        self.add(name, Some(signature), Vec::new(), call, None)
574    }
575
576    /// Like [`method`](Self::method) but borrows mutably:
577    /// `|bytes: &mut Vec<u8>, val: i64| { bytes.push(val as u8); }`.
578    pub fn method_mut<Recv: 'static, Marker>(
579        &mut self,
580        name: &str,
581        f: impl IntoBuiltinMethodMut<Recv, Marker>,
582    ) -> &mut Self {
583        let (signature, call) = f.into_native();
584        self.add(name, Some(signature), Vec::new(), call, None)
585    }
586
587    /// Registers a method working on Xur values directly, with the receiver
588    /// first; needed to change a list in place.
589    pub fn raw_function(
590        &mut self,
591        name: &str,
592        f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
593    ) -> &mut Self {
594        self.add(name, None, Vec::new(), Box::new(f), None)
595    }
596
597    /// Like [`raw_function`](Self::raw_function), declaring the return type
598    /// for LSP inlay hints.
599    pub fn raw_function_returns<R: Returns>(
600        &mut self,
601        name: &str,
602        f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
603    ) -> &mut Self {
604        self.add(
605            name,
606            None,
607            Vec::new(),
608            Box::new(f),
609            Some(ReturnHint::Shape(R::RET)),
610        )
611    }
612
613    /// Like [`raw_function_returns`](Self::raw_function_returns) for
614    /// xur-internal types that have no Facet equivalent.
615    pub fn raw_function_returns_xur(
616        &mut self,
617        name: &str,
618        ret: XurType,
619        f: impl Fn(&mut dyn Context, &[Value]) -> RunResult<Value> + Send + Sync + 'static,
620    ) -> &mut Self {
621        self.add(
622            name,
623            None,
624            Vec::new(),
625            Box::new(f),
626            Some(ReturnHint::Xur(ret)),
627        )
628    }
629
630    /// Like [`function`](Self::function), naming the parameters (the
631    /// receiver first) for hovers and docs.
632    pub fn named_function<Marker>(
633        &mut self,
634        name: &str,
635        params: &[&str],
636        f: impl IntoNativeFn<Marker>,
637    ) -> &mut Self {
638        let (signature, call) = f.into_native();
639        self.add(name, Some(signature), owned(params), call, None)
640    }
641
642    /// Like [`method`](Self::method), naming the parameters after the
643    /// receiver for hovers and docs.
644    pub fn named_method<Recv: ?Sized + 'static, Marker>(
645        &mut self,
646        name: &str,
647        params: &[&str],
648        f: impl IntoBuiltinMethod<Recv, Marker>,
649    ) -> &mut Self {
650        let (signature, call) = f.into_native();
651        self.add(name, Some(signature), owned(params), call, None)
652    }
653
654    /// Like [`method_mut`](Self::method_mut), naming the parameters after
655    /// the receiver for hovers and docs.
656    pub fn named_method_mut<Recv: 'static, Marker>(
657        &mut self,
658        name: &str,
659        params: &[&str],
660        f: impl IntoBuiltinMethodMut<Recv, Marker>,
661    ) -> &mut Self {
662        let (signature, call) = f.into_native();
663        self.add(name, Some(signature), owned(params), call, None)
664    }
665
666    /// The registry being filled in.
667    pub fn registry(&self) -> &'r Registry {
668        self.registry
669    }
670
671    pub(super) fn add_raw(
672        &mut self,
673        name: &str,
674        names: Vec<String>,
675        call: Box<CallFn>,
676    ) -> NativeFnId {
677        let decl = self.registry.add_fn(name, None, names, call);
678        let key = (self.ty, name.to_owned());
679        self.registry
680            .with_inner(|inner| inner.builtin_methods.insert(key, decl.id));
681        decl.id
682    }
683
684    fn add(
685        &mut self,
686        name: &str,
687        signature: Option<Signature>,
688        param_names: Vec<String>,
689        call: Box<CallFn>,
690        return_hint: Option<ReturnHint>,
691    ) -> &mut Self {
692        let decl = self.registry.add_fn(name, signature, param_names, call);
693        if let Some(hint) = return_hint {
694            self.registry.set_return_hint(decl.id, hint);
695        }
696        let key = (self.ty, name.to_owned());
697        self.registry
698            .with_inner(|inner| inner.builtin_methods.insert(key, decl.id));
699        self
700    }
701}
702
703fn owned(names: &[&str]) -> Vec<String> {
704    names.iter().map(|s| s.to_string()).collect()
705}
706
707/// Parameter names for a native type's method, whose signature includes the
708/// receiver: `self` and then `params`.
709fn with_self(params: &[&str]) -> Vec<String> {
710    std::iter::once("self")
711        .chain(params.iter().copied())
712        .map(String::from)
713        .collect()
714}