1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use intaglio::SymbolOverflowError;
use std::borrow::Cow;

use crate::core::{ClassRegistry, Intern, TryConvertMut};
use crate::error::{Error, RubyException};
use crate::extn::core::exception::Fatal;
use crate::ffi::InterpreterExtractError;
use crate::sys;
use crate::Artichoke;

impl Artichoke {
    pub fn lookup_symbol_with_trailing_nul(&self, symbol: u32) -> Result<Option<&[u8]>, Error> {
        let state = self.state.as_deref().ok_or_else(InterpreterExtractError::new)?;
        if let Some(symbol) = symbol.checked_sub(1) {
            if let Some(bytes) = state.symbols.get(symbol.into()) {
                Ok(Some(bytes))
            } else {
                Ok(None)
            }
        } else {
            Ok(None)
        }
    }

    pub fn intern_bytes_with_trailing_nul<T>(&mut self, bytes: T) -> Result<u32, Error>
    where
        T: Into<Cow<'static, [u8]>>,
    {
        let bytes = bytes.into();
        let state = self.state.as_deref_mut().ok_or_else(InterpreterExtractError::new)?;
        let symbol = state.symbols.intern(bytes)?;
        let symbol = u32::from(symbol);
        // mruby expexts symbols to be non-zero.
        let symbol = symbol
            .checked_add(<Self as Intern>::SYMBOL_RANGE_START)
            .ok_or_else(SymbolOverflowError::new)?;
        Ok(symbol)
    }

    pub fn check_interned_bytes_with_trailing_nul(&self, bytes: &[u8]) -> Result<Option<u32>, Error> {
        let state = self.state.as_deref().ok_or_else(InterpreterExtractError::new)?;
        let symbol = state.symbols.check_interned(bytes);
        if let Some(symbol) = symbol {
            let symbol = u32::from(symbol);
            // mruby expexts symbols to be non-zero.
            let symbol = symbol
                .checked_add(<Self as Intern>::SYMBOL_RANGE_START)
                .ok_or_else(SymbolOverflowError::new)?;
            Ok(Some(symbol))
        } else {
            Ok(None)
        }
    }
}

impl Intern for Artichoke {
    type Symbol = u32;

    type Error = Error;

    const SYMBOL_RANGE_START: Self::Symbol = 1;

    fn intern_bytes<T>(&mut self, bytes: T) -> Result<Self::Symbol, Self::Error>
    where
        T: Into<Cow<'static, [u8]>>,
    {
        let bytes = bytes.into();
        let state = self.state.as_deref_mut().ok_or_else(InterpreterExtractError::new)?;
        let mut bytes = bytes.into_owned();
        bytes.push(b'\0');
        let symbol = state.symbols.intern(bytes)?;
        let symbol = u32::from(symbol);
        // mruby expexts symbols to be non-zero.
        let symbol = symbol
            .checked_add(Self::SYMBOL_RANGE_START)
            .ok_or_else(SymbolOverflowError::new)?;
        Ok(symbol)
    }

    fn check_interned_bytes(&self, bytes: &[u8]) -> Result<Option<Self::Symbol>, Self::Error> {
        let state = self.state.as_deref().ok_or_else(InterpreterExtractError::new)?;
        let mut bytes = bytes.to_vec();
        bytes.push(b'\0');
        let symbol = state.symbols.check_interned(&bytes);
        if let Some(symbol) = symbol {
            let symbol = u32::from(symbol);
            // mruby expexts symbols to be non-zero.
            let symbol = symbol
                .checked_add(Self::SYMBOL_RANGE_START)
                .ok_or_else(SymbolOverflowError::new)?;
            Ok(Some(symbol))
        } else {
            Ok(None)
        }
    }

    fn lookup_symbol(&self, symbol: Self::Symbol) -> Result<Option<&[u8]>, Self::Error> {
        let state = self.state.as_deref().ok_or_else(InterpreterExtractError::new)?;
        if let Some(symbol) = symbol.checked_sub(Self::SYMBOL_RANGE_START) {
            if let Some(bytes) = state.symbols.get(symbol.into()) {
                if bytes.is_empty() {
                    Ok(Some(bytes))
                } else {
                    Ok(bytes.get(..bytes.len() - 1))
                }
            } else {
                Ok(None)
            }
        } else {
            Ok(None)
        }
    }

    fn symbol_count(&self) -> usize {
        if let Some(state) = self.state.as_deref() {
            state.symbols.len()
        } else {
            0
        }
    }
}

impl RubyException for SymbolOverflowError {
    #[inline]
    fn message(&self) -> Cow<'_, [u8]> {
        self.to_string().into_bytes().into()
    }

    #[inline]
    fn name(&self) -> Cow<'_, str> {
        "fatal".into()
    }

    fn vm_backtrace(&self, interp: &mut Artichoke) -> Option<Vec<Vec<u8>>> {
        let _ = interp;
        None
    }

    fn as_mrb_value(&self, interp: &mut Artichoke) -> Option<sys::mrb_value> {
        let message = interp.try_convert_mut(self.message()).ok()?;
        let value = interp.new_instance::<Fatal>(&[message]).ok().flatten()?;
        Some(value.inner())
    }
}

impl From<SymbolOverflowError> for Error {
    #[inline]
    fn from(exception: SymbolOverflowError) -> Self {
        Self::from(Box::<dyn RubyException>::from(exception))
    }
}

impl From<Box<SymbolOverflowError>> for Error {
    #[inline]
    fn from(exception: Box<SymbolOverflowError>) -> Self {
        Self::from(Box::<dyn RubyException>::from(exception))
    }
}

impl From<SymbolOverflowError> for Box<dyn RubyException> {
    #[inline]
    fn from(exception: SymbolOverflowError) -> Box<dyn RubyException> {
        Box::new(exception)
    }
}

impl From<Box<SymbolOverflowError>> for Box<dyn RubyException> {
    #[inline]
    fn from(exception: Box<SymbolOverflowError>) -> Box<dyn RubyException> {
        exception
    }
}