Skip to main content

doge_runtime/methods/
mod.rs

1//! Built-in method dispatch for collection values — the counterpart of
2//! [`crate::objects`] for `many` instances. A method call on a non-`Object`
3//! receiver (`xs.append(1)`, `d.keys()`) routes here; the generated `call_method`
4//! dispatcher handles `Object` receivers itself and forwards everything else.
5//!
6//! Behaviour lives here, not in codegen (generated Rust is thin glue): each
7//! method mutates or reads the value's shared cell and returns a `Value`, and
8//! every failure is a catchable [`DogeError`], never a panic.
9
10use std::rc::Rc;
11
12use crate::error::{DogeError, DogeResult};
13use crate::objects::method_arity_error;
14use crate::value::Value;
15
16mod dict;
17mod list;
18#[cfg(test)]
19mod tests;
20
21use dict::dict_method;
22use list::list_method;
23
24/// Dispatch a method call on a List or Dict. `recv` is the receiver, `name` the
25/// method, `args` the already-evaluated arguments. A non-collection receiver, an
26/// unknown method, or a wrong argument count is a catchable error.
27pub fn builtin_method(recv: &Value, name: &str, args: Vec<Value>) -> DogeResult {
28    match recv {
29        Value::List(_) => list_method(recv, name, args),
30        Value::Dict(_) => dict_method(recv, name, args),
31        // The dispatcher routes objects to its own class match, never here; this
32        // defensive branch names the class rather than claiming "no methods".
33        Value::Object(_) => Err(crate::objects::no_such_method(recv, name)),
34        // No other value has methods at all. Listed by variant rather than a
35        // wildcard, so a new Value variant with methods forces a decision here.
36        Value::Int(_)
37        | Value::Float(_)
38        | Value::Str(_)
39        | Value::Bool(_)
40        | Value::None
41        | Value::Function(_)
42        | Value::Class(_)
43        | Value::BoundMethod(_)
44        | Value::Error(_) => Err(crate::objects::no_methods_error(recv)),
45    }
46}
47
48/// Whether a List or Dict has a built-in method named `name` — the gate a bound
49/// method read (`such f = xs.append`) checks before capturing the receiver. The
50/// method-name sets live beside each collection's dispatch (`list::LIST_METHODS`,
51/// `dict::DICT_METHODS`), so binding and dispatch never disagree.
52pub fn has_builtin_method(recv: &Value, name: &str) -> bool {
53    match recv {
54        Value::List(_) => list::LIST_METHODS.contains(&name),
55        Value::Dict(_) => dict::DICT_METHODS.contains(&name),
56        _ => false,
57    }
58}
59
60/// The arity gate every method runs first: reuses the object-method wording so a
61/// List/Dict arity error reads exactly like `Shibe.speak takes 1 argument, got 0`.
62pub(super) fn check_arity(
63    class: &str,
64    method: &str,
65    expected: usize,
66    got: usize,
67) -> DogeResult<()> {
68    if got == expected {
69        Ok(())
70    } else {
71        Err(method_arity_error(
72            class,
73            method,
74            expected,
75            Some(expected),
76            got,
77        ))
78    }
79}
80
81/// Take an argument that must be an Int, or raise the standard type error naming
82/// the method and what it got. `what` is the argument's role, e.g. `List.insert
83/// needs an Int index`.
84pub(super) fn expect_int(value: Value, what: &str) -> DogeResult<i64> {
85    match value {
86        Value::Int(n) => Ok(n),
87        other => Err(DogeError::type_error(format!(
88            "{what}, got {}",
89            other.describe()
90        ))),
91    }
92}
93
94/// Take an argument that must be a Str, or raise the standard type error naming
95/// the method and what it got.
96pub(super) fn expect_str(value: Value, what: &str) -> DogeResult<Rc<str>> {
97    match value {
98        Value::Str(s) => Ok(s),
99        other => Err(DogeError::type_error(format!(
100            "{what}, got {}",
101            other.describe()
102        ))),
103    }
104}