artichoke_backend/extn/core/integer/
mod.rs

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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
use std::fmt::{self, Write as _};
use std::mem;

use crate::extn::core::numeric::{self, Coercion, Outcome};
use crate::extn::prelude::*;
use crate::fmt::WriteError;

pub(in crate::extn) mod mruby;
pub(super) mod trampoline;

#[repr(transparent)]
#[derive(Default, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Integer(i64);

impl fmt::Debug for Integer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl fmt::Display for Integer {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl Convert<Integer, Value> for Artichoke {
    #[inline]
    fn convert(&self, from: Integer) -> Value {
        self.convert(from.0)
    }
}

impl TryConvert<Value, Integer> for Artichoke {
    type Error = Error;

    #[inline]
    fn try_convert(&self, value: Value) -> Result<Integer, Self::Error> {
        let num = self.try_convert(value)?;
        Ok(Integer(num))
    }
}

impl From<i64> for Integer {
    #[inline]
    fn from(int: i64) -> Self {
        Self(int)
    }
}

impl From<Integer> for i64 {
    #[inline]
    fn from(int: Integer) -> Self {
        int.as_i64()
    }
}

impl From<Integer> for f64 {
    #[inline]
    fn from(int: Integer) -> Self {
        int.as_f64()
    }
}

impl From<Integer> for Outcome {
    #[inline]
    fn from(int: Integer) -> Self {
        Self::Integer(int.into())
    }
}

impl From<i64> for Outcome {
    #[inline]
    fn from(int: i64) -> Self {
        Self::Integer(int)
    }
}

impl Integer {
    /// Constructs a new, default, zero `Integer`.
    #[inline]
    #[must_use]
    pub const fn new() -> Self {
        Self(0)
    }

    #[inline]
    #[must_use]
    pub const fn as_i64(self) -> i64 {
        self.0
    }

    #[inline]
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub const fn as_f64(self) -> f64 {
        self.0 as f64
    }

    pub fn chr(self, interp: &mut Artichoke, encoding: Option<Value>) -> Result<spinoso_string::String, Error> {
        if let Some(encoding) = encoding {
            let mut message = b"encoding parameter of Integer#chr (given ".to_vec();
            message.extend(encoding.inspect(interp));
            message.extend_from_slice(b") not supported");
            Err(NotImplementedError::from(message).into())
        } else {
            // When no encoding is supplied, MRI assumes the encoding is
            // either ASCII or ASCII-8BIT.
            //
            // - `Integer`s from 0..127 result in a `String` with ASCII
            //   encoding.
            // - `Integer`s from 128..256 result in a `String` with binary
            //   (ASCII-8BIT) encoding.
            // - All other integers raise a `RangeError`.
            //
            // ```txt
            // [2.6.3] > [0.chr, 0.chr.encoding]
            // => ["\x00", #<Encoding:US-ASCII>]
            // [2.6.3] > [127.chr, 127.chr.encoding]
            // => ["\x7F", #<Encoding:US-ASCII>]
            // [2.6.3] > [128.chr, 128.chr.encoding]
            // => ["\x80", #<Encoding:ASCII-8BIT>]
            // [2.6.3] > [255.chr, 255.chr.encoding]
            // => ["\xFF", #<Encoding:ASCII-8BIT>]
            // [2.6.3] > [256.chr, 256.chr.encoding]
            // Traceback (most recent call last):
            //         5: from /usr/local/var/rbenv/versions/2.6.3/bin/irb:23:in `<main>'
            //         4: from /usr/local/var/rbenv/versions/2.6.3/bin/irb:23:in `load'
            //         3: from /usr/local/var/rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
            //         2: from (irb):9
            //         1: from (irb):9:in `chr'
            // RangeError (256 out of char range)
            // ```
            match u8::try_from(self.as_i64()) {
                // ASCII encoding - `chr[0 - 127]`
                Ok(chr @ 0..=127) => Ok(spinoso_string::String::ascii(vec![chr])),
                // Binary/ASCII-8BIT encoding - `chr[128 - 255]`
                Ok(chr @ 128..=255) => Ok(spinoso_string::String::binary(vec![chr])),
                Err(_) => {
                    let mut message = String::new();
                    write!(&mut message, "{} out of char range", self.as_i64()).map_err(WriteError::from)?;
                    Err(RangeError::from(message).into())
                }
            }
        }
    }

    #[inline]
    pub fn bit(self, bit: i64) -> Self {
        if let Ok(bit) = u32::try_from(bit) {
            self.as_i64().checked_shr(bit).map_or(0, |v| v & 1).into()
        } else {
            Self(0)
        }
    }

    pub fn div(self, interp: &mut Artichoke, denominator: Value) -> Result<Outcome, Error> {
        match denominator.ruby_type() {
            Ruby::Fixnum => {
                let denom = denominator.try_convert_into::<i64>(interp)?;
                let value = self.as_i64();
                if denom == 0 {
                    Err(ZeroDivisionError::with_message("divided by 0").into())
                } else if value < 0 && (value % denom) != 0 {
                    Ok(((value / denom) - 1).into())
                } else {
                    Ok((value / denom).into())
                }
            }
            Ruby::Float => {
                let denom = denominator.try_convert_into::<f64>(interp)?;
                Ok((self.as_f64() / denom).into())
            }
            _ => {
                let x = interp.convert(self);
                let coerced = numeric::coerce(interp, x, denominator)?;
                match coerced {
                    Coercion::Float(_, denom) if denom == 0.0 => {
                        Err(ZeroDivisionError::with_message("divided by 0").into())
                    }
                    Coercion::Integer(_, 0) => Err(ZeroDivisionError::with_message("divided by 0").into()),
                    Coercion::Float(numer, denom) => Ok((numer / denom).into()),
                    Coercion::Integer(numer, denom) if numer < 0 && (numer % denom) != 0 => {
                        Ok(((numer / denom) - 1).into())
                    }
                    Coercion::Integer(numer, denom) => Ok((numer / denom).into()),
                }
            }
        }
    }

    #[must_use]
    pub const fn is_allbits(self, mask: i64) -> bool {
        self.as_i64() & mask == mask
    }

    #[must_use]
    pub const fn is_anybits(self, mask: i64) -> bool {
        self.as_i64() & mask != 0
    }

    #[must_use]
    pub const fn is_nobits(self, mask: i64) -> bool {
        self.as_i64() & mask == 0
    }

    #[must_use]
    pub const fn size() -> usize {
        const SIZE_OF_INT: usize = mem::size_of::<i64>();

        SIZE_OF_INT
    }
}

#[cfg(test)]
mod tests {
    use quickcheck::quickcheck;

    use crate::test::prelude::*;

    quickcheck! {
        fn positive_integer_division_vm_opcode(x: u8, y: u8) -> bool {
            let mut interp = interpreter();
            match (x, y) {
                (0, 0) => interp.eval(b"0 / 0").is_err(),
                (x, 0) | (0, x) => {
                    let expr = format!("{x} / 0").into_bytes();
                    if interp.eval(expr.as_slice()).is_ok() {
                        return false;
                    }
                    let expr = format!("0 / {x}").into_bytes();
                    let quotient = interp.eval(expr.as_slice()).unwrap().try_convert_into::<i64>(&interp).unwrap();
                    quotient == 0
                }
                (x, y) => {
                    let expr = format!("{x} / {y}").into_bytes();
                    let quotient = interp.eval(expr.as_slice()).unwrap().try_convert_into::<i64>(&interp).unwrap();
                    let expected = i64::from(x) / i64::from(y);
                    quotient == expected
                }
            }
        }

        fn positive_integer_division_send(x: u8, y: u8) -> bool {
            let mut interp = interpreter();
            match (x, y) {
                (0, 0) => interp.eval(b"0.send('/', 0)").is_err(),
                (x, 0) | (0, x) => {
                    let expr = format!("{x}.send('/', 0)").into_bytes();
                    if interp.eval(expr.as_slice()).is_ok() {
                        return false;
                    }
                    let expr = format!("0.send('/', {x})").into_bytes();
                    let quotient = interp.eval(expr.as_slice()).unwrap().try_convert_into::<i64>(&interp).unwrap();
                    quotient == 0
                }
                (x, y) => {
                    let expr = format!("{x}.send('/', {y})").into_bytes();
                    let quotient = interp.eval(expr.as_slice()).unwrap().try_convert_into::<i64>(&interp).unwrap();
                    let expected = i64::from(x) / i64::from(y);
                    quotient == expected
                }
            }
        }

        fn negative_integer_division_vm_opcode(x: u8, y: u8) -> bool {
            let mut interp = interpreter();
            match (x, y) {
                (0, 0) => interp.eval(b"-0 / 0").is_err(),
                (x, 0) | (0, x) => {
                    let expr = format!("-{x} / 0").into_bytes();
                    if interp.eval(expr.as_slice()).is_ok() {
                        return false;
                    }
                    let expr = format!("0 / -{x}").into_bytes();
                    let quotient = interp.eval(expr.as_slice()).unwrap().try_convert_into::<i64>(&interp).unwrap();
                    quotient == 0
                }
                (x, y) => {
                    let expr = format!("-{x} / {y}").into_bytes();
                    let quotient = interp.eval(expr.as_slice()).unwrap().try_convert_into::<i64>(&interp).unwrap();
                    if x % y == 0 {
                        let expected = -i64::from(x) / i64::from(y);
                        quotient == expected
                    } else {
                        // Round negative integer division toward negative infinity.
                        let expected = (-i64::from(x) / i64::from(y)) - 1;
                        quotient == expected
                    }
                }
            }
        }

        fn negative_integer_division_send(x: u8, y: u8) -> bool {
            let mut interp = interpreter();
            match (x, y) {
                (0, 0) => interp.eval(b"-0.send('/', 0)").is_err(),
                (x, 0) | (0, x) => {
                    let expr = format!("-{x}.send('/', 0)").into_bytes();
                    if interp.eval(expr.as_slice()).is_ok() {
                        return false;
                    }
                    let expr = format!("0.send('/', -{x})").into_bytes();
                    let quotient = interp.eval(expr.as_slice()).unwrap().try_convert_into::<i64>(&interp).unwrap();
                    quotient == 0
                }
                (x, y) => {
                    let expr = format!("-{x}.send('/', {y})").into_bytes();
                    let quotient = interp.eval(expr.as_slice()).unwrap().try_convert_into::<i64>(&interp).unwrap();
                    if x % y == 0 {
                        let expected = -i64::from(x) / i64::from(y);
                        quotient == expected
                    } else {
                        // Round negative integer division toward negative infinity.
                        let expected = (-i64::from(x) / i64::from(y)) - 1;
                        quotient == expected
                    }
                }
            }
        }
    }
}