artichoke_backend/extn/core/symbol/
trampoline.rs

1use spinoso_symbol::{CaseFold, InternerAllSymbols};
2
3use crate::extn::core::array::Array;
4use crate::extn::core::symbol::Symbol;
5use crate::extn::prelude::*;
6
7pub fn all_symbols(interp: &mut Artichoke) -> Result<Value, Error> {
8    let all_symbols = interp
9        .all_symbols()
10        .map(|sym| Symbol::alloc_value(sym, interp))
11        .collect::<Result<Array, Error>>()?;
12    Array::alloc_value(all_symbols, interp)
13}
14
15pub fn equal_equal(interp: &mut Artichoke, mut value: Value, mut other: Value) -> Result<Value, Error> {
16    let symbol = unsafe { Symbol::unbox_from_value(&mut value, interp)? };
17    if let Ok(other) = unsafe { Symbol::unbox_from_value(&mut other, interp) } {
18        let eql = symbol.id() == other.id();
19        Ok(interp.convert(eql))
20    } else {
21        Ok(interp.convert(false))
22    }
23}
24
25pub fn ascii_casecmp(interp: &mut Artichoke, mut value: Value, mut other: Value) -> Result<Value, Error> {
26    let symbol = unsafe { Symbol::unbox_from_value(&mut value, interp)? };
27    if let Ok(other) = unsafe { Symbol::unbox_from_value(&mut other, interp) } {
28        let cmp = spinoso_symbol::ascii_casecmp(interp, symbol.id(), other.id())?;
29        Ok(interp.convert(cmp as i64))
30    } else {
31        Ok(Value::nil())
32    }
33}
34
35pub fn unicode_casecmp(interp: &mut Artichoke, mut value: Value, mut other: Value) -> Result<Value, Error> {
36    let symbol = unsafe { Symbol::unbox_from_value(&mut value, interp)? };
37    if let Ok(other) = unsafe { Symbol::unbox_from_value(&mut other, interp) } {
38        let cmp = spinoso_symbol::unicode_case_eq(interp, symbol.id(), other.id(), CaseFold::new())?;
39        Ok(interp.convert(cmp))
40    } else {
41        Ok(Value::nil())
42    }
43}
44
45pub fn is_empty(interp: &mut Artichoke, mut value: Value) -> Result<Value, Error> {
46    let symbol = unsafe { Symbol::unbox_from_value(&mut value, interp)? };
47    let is_empty = symbol.is_empty(interp);
48    Ok(interp.convert(is_empty))
49}
50
51pub fn length(interp: &mut Artichoke, mut value: Value) -> Result<Value, Error> {
52    let symbol = unsafe { Symbol::unbox_from_value(&mut value, interp)? };
53    let len = symbol.len(interp);
54    interp.try_convert(len)
55}
56
57pub fn bytes(interp: &mut Artichoke, mut value: Value) -> Result<Value, Error> {
58    let symbol = unsafe { Symbol::unbox_from_value(&mut value, interp)? };
59    // These bytes must be cloned because they are owned by the interpreter.
60    let bytes = symbol.bytes(interp).to_vec();
61    interp.try_convert_mut(bytes)
62}
63
64pub fn inspect(interp: &mut Artichoke, mut value: Value) -> Result<Value, Error> {
65    let symbol = unsafe { Symbol::unbox_from_value(&mut value, interp)? };
66    let inspect = symbol.inspect(interp);
67    let debug = inspect.collect::<String>();
68    interp.try_convert_mut(debug)
69}