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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use crate::convert::{implicitly_convert_to_int, implicitly_convert_to_string};
use crate::extn::core::array::Array;
use crate::extn::prelude::*;

pub fn plus(interp: &mut Artichoke, mut ary: Value, mut other: Value) -> Result<Value, Error> {
    let array = unsafe { Array::unbox_from_value(&mut ary, interp)? };
    let result = if let Ok(other) = unsafe { Array::unbox_from_value(&mut other, interp) } {
        let mut result = Array::with_capacity(array.len() + other.len());
        result.concat(array.as_slice());
        result.concat(other.as_slice());
        result
    } else if other.respond_to(interp, "to_ary")? {
        let mut other_converted = other.funcall(interp, "to_ary", &[], None)?;
        if let Ok(other) = unsafe { Array::unbox_from_value(&mut other_converted, interp) } {
            let mut result = Array::with_capacity(array.len() + other.len());
            result.concat(array.as_slice());
            result.concat(other.as_slice());
            result
        } else {
            let mut message = String::from("can't convert ");
            let name = interp.inspect_type_name_for_value(other);
            message.push_str(name);
            message.push_str(" to Array (");
            message.push_str(name);
            message.push_str("#to_ary gives ");
            message.push_str(interp.inspect_type_name_for_value(other_converted));
            return Err(TypeError::from(message).into());
        }
    } else {
        let mut message = String::from("no implicit conversion of ");
        message.push_str(interp.inspect_type_name_for_value(other));
        message.push_str(" into Array");
        return Err(TypeError::from(message).into());
    };
    Array::alloc_value(result, interp)
}

pub fn mul(interp: &mut Artichoke, mut ary: Value, mut joiner: Value) -> Result<Value, Error> {
    let array = unsafe { Array::unbox_from_value(&mut ary, interp)? };
    // SAFETY: Convert separator to an owned byte vec to ensure the `RString*`
    // backing `joiner` is not garbage collected when invoking `to_s` during
    // `join`.
    if let Ok(separator) = unsafe { implicitly_convert_to_string(interp, &mut joiner) } {
        let separator = separator.to_vec();
        let s = super::join(interp, &array, &separator)?;
        interp.try_convert_mut(s)
    } else {
        let n = implicitly_convert_to_int(interp, joiner)?;
        if let Ok(n) = usize::try_from(n) {
            let value = super::repeat(&array, n)?;
            let result = Array::alloc_value(value, interp)?;
            let result_value = result.inner();
            let ary_value = ary.inner();
            unsafe {
                let ary_rbasic = ary_value.value.p.cast::<sys::RBasic>();
                let result_rbasic = result_value.value.p.cast::<sys::RBasic>();
                (*result_rbasic).c = (*ary_rbasic).c;
            }
            Ok(result)
        } else {
            Err(ArgumentError::with_message("negative argument").into())
        }
    }
}

pub fn push_single(interp: &mut Artichoke, mut ary: Value, value: Value) -> Result<Value, Error> {
    if ary.is_frozen(interp) {
        return Err(FrozenError::with_message("can't modify frozen Array").into());
    }
    let mut array = unsafe { Array::unbox_from_value(&mut ary, interp)? };

    // SAFETY: The array is repacked without any intervening interpreter heap
    // allocations.
    let array_mut = unsafe { array.as_inner_mut() };
    array_mut.push(value);

    unsafe {
        let inner = array.take();
        Array::box_into_value(inner, ary, interp)?;
    }

    Ok(ary)
}

pub fn element_reference(
    interp: &mut Artichoke,
    mut ary: Value,
    first: Value,
    second: Option<Value>,
) -> Result<Value, Error> {
    let array = unsafe { Array::unbox_from_value(&mut ary, interp)? };
    let elem = super::aref(interp, &array, first, second)?;
    Ok(interp.convert(elem))
}

pub fn element_assignment(
    interp: &mut Artichoke,
    mut ary: Value,
    first: Value,
    second: Value,
    third: Option<Value>,
) -> Result<Value, Error> {
    if ary.is_frozen(interp) {
        return Err(FrozenError::with_message("can't modify frozen Array").into());
    }
    // TODO: properly handle self-referential sets.
    if ary == first || ary == second || Some(ary) == third {
        return Ok(Value::nil());
    }
    let mut array = unsafe { Array::unbox_from_value(&mut ary, interp)? };

    // XXX: Ensure that `array_mut` does not allocate in between mruby
    // allocations.
    let array_mut = unsafe { array.as_inner_mut() };
    let result = super::aset(interp, array_mut, first, second, third);

    unsafe {
        let inner = array.take();
        Array::box_into_value(inner, ary, interp)?;
    }

    result
}

pub fn clear(interp: &mut Artichoke, mut ary: Value) -> Result<Value, Error> {
    if ary.is_frozen(interp) {
        return Err(FrozenError::with_message("can't modify frozen Array").into());
    }
    let mut array = unsafe { Array::unbox_from_value(&mut ary, interp)? };

    // SAFETY: Clearing a `Vec` does not reallocate it, but it does change its
    // length. The array is repacked before any intervening interpreter heap
    // allocations occur.
    unsafe {
        let array_mut = array.as_inner_mut();
        array_mut.clear();

        let inner = array.take();
        Array::box_into_value(inner, ary, interp)?;
    }

    Ok(ary)
}

pub fn push<I>(interp: &mut Artichoke, mut ary: Value, others: I) -> Result<Value, Error>
where
    I: IntoIterator<Item = Value>,
    I::IntoIter: Clone,
{
    if ary.is_frozen(interp) {
        return Err(FrozenError::with_message("can't modify frozen Array").into());
    }

    let mut array = unsafe { Array::unbox_from_value(&mut ary, interp)? };

    // SAFETY: The array is repacked without any intervening interpreter heap
    // allocations.
    let array_mut = unsafe { array.as_inner_mut() };

    array_mut.extend(others);

    unsafe {
        let inner = array.take();
        Array::box_into_value(inner, ary, interp)?;
    }

    Ok(ary)
}

pub fn concat<I>(interp: &mut Artichoke, mut ary: Value, others: I) -> Result<Value, Error>
where
    I: IntoIterator<Item = Value>,
    I::IntoIter: Clone,
{
    // Assumption for the average length of an `Array` that will be concatenated
    // into `ary`.
    const OTHER_ARRAYS_AVG_LENGTH: usize = 5;

    if ary.is_frozen(interp) {
        return Err(FrozenError::with_message("can't modify frozen Array").into());
    }
    let array = unsafe { Array::unbox_from_value(&mut ary, interp)? };
    let others = others.into_iter();

    // Allocate a new buffer and concatenate into it to allow preserving the
    // original `Array`'s items if `ary` is concatenated with itself.
    //
    // This allocation assumes that each `Array` yielded by the iterator is
    // "small", where small means 5 elements or fewer.
    let mut replacement = Array::with_capacity(array.len() + others.clone().count() * OTHER_ARRAYS_AVG_LENGTH);
    replacement.concat(array.as_slice());

    for mut other in others {
        if let Ok(other) = unsafe { Array::unbox_from_value(&mut other, interp) } {
            replacement.reserve(other.len());
            replacement.concat(other.as_slice());
        } else if other.respond_to(interp, "to_ary")? {
            let mut other_converted = other.funcall(interp, "to_ary", &[], None)?;
            if let Ok(other) = unsafe { Array::unbox_from_value(&mut other_converted, interp) } {
                replacement.reserve(other.len());
                replacement.concat(other.as_slice());
            } else {
                let mut message = String::from("can't convert ");
                let name = interp.inspect_type_name_for_value(other);
                message.push_str(name);
                message.push_str(" to Array (");
                message.push_str(name);
                message.push_str("#to_ary gives ");
                message.push_str(interp.inspect_type_name_for_value(other_converted));
                return Err(TypeError::from(message).into());
            }
        } else {
            let mut message = String::from("no implicit conversion of ");
            message.push_str(interp.inspect_type_name_for_value(other));
            message.push_str(" into Array");
            return Err(TypeError::from(message).into());
        }
    }

    unsafe {
        let _old = array.take();
        Array::box_into_value(replacement, ary, interp)?;
    }

    Ok(ary)
}

pub fn first(interp: &mut Artichoke, mut ary: Value, num: Option<Value>) -> Result<Value, Error> {
    let array = unsafe { Array::unbox_from_value(&mut ary, interp)? };
    if let Some(num) = num {
        // Hack to detect `BigNum`
        if let Ruby::Float = num.ruby_type() {
            return Err(RangeError::with_message("bignum too big to convert into `long'").into());
        }
        let n = implicitly_convert_to_int(interp, num)?;
        if let Ok(n) = usize::try_from(n) {
            let slice = array.first_n(n);
            let result = Array::from(slice);
            Array::alloc_value(result, interp)
        } else {
            Err(ArgumentError::with_message("negative array size").into())
        }
    } else {
        let last = array.first();
        Ok(interp.convert(last))
    }
}

pub fn initialize(
    interp: &mut Artichoke,
    into: Value,
    first: Option<Value>,
    second: Option<Value>,
    block: Option<Block>,
) -> Result<Value, Error> {
    // Pack an empty `Array` into the given uninitialized `RArray *` so it can
    // be safely marked if an mruby allocation occurs and a GC is triggered in
    // `Array::initialize`.
    //
    // Allocations are likely in the case where a block is passed to
    // `Array#initialize` or when the first and second args must be coerced with
    // the `#to_*` family of methods.
    Array::box_into_value(Array::new(), into, interp)?;
    let array = super::initialize(interp, first, second, block)?;
    Array::box_into_value(array, into, interp)
}

pub fn initialize_copy(interp: &mut Artichoke, ary: Value, mut from: Value) -> Result<Value, Error> {
    // Pack an empty `Array` into the given uninitialized `RArray *` so it can
    // be safely marked if an mruby allocation occurs and a GC is triggered in
    // `Array::initialize`.
    //
    // This ensures the given `RArry *` is initialized even if a non-`Array`
    // object is called with `Array#initialize_copy` and the
    // `Array::unbox_from_value` call below short circuits with an error.
    Array::box_into_value(Array::new(), ary, interp)?;
    let from = unsafe { Array::unbox_from_value(&mut from, interp)? };
    let result = from.clone();
    Array::box_into_value(result, ary, interp)
}

pub fn last(interp: &mut Artichoke, mut ary: Value, num: Option<Value>) -> Result<Value, Error> {
    let array = unsafe { Array::unbox_from_value(&mut ary, interp)? };
    if let Some(num) = num {
        // Hack to detect `BigNum`
        if let Ruby::Float = num.ruby_type() {
            return Err(RangeError::with_message("bignum too big to convert into `long'").into());
        }
        let n = implicitly_convert_to_int(interp, num)?;
        if let Ok(n) = usize::try_from(n) {
            let slice = array.last_n(n);
            let result = Array::from(slice);
            Array::alloc_value(result, interp)
        } else {
            Err(ArgumentError::with_message("negative array size").into())
        }
    } else {
        let last = array.last();
        Ok(interp.convert(last))
    }
}

pub fn len(interp: &mut Artichoke, mut ary: Value) -> Result<usize, Error> {
    let array = unsafe { Array::unbox_from_value(&mut ary, interp)? };
    Ok(array.len())
}

pub fn pop(interp: &mut Artichoke, mut ary: Value) -> Result<Value, Error> {
    if ary.is_frozen(interp) {
        return Err(FrozenError::with_message("can't modify frozen Array").into());
    }
    let mut array = unsafe { Array::unbox_from_value(&mut ary, interp)? };

    // SAFETY: The array is repacked without any intervening interpreter heap
    // allocations.
    let result = unsafe {
        let array_mut = array.as_inner_mut();
        let result = array_mut.pop();

        let inner = array.take();
        Array::box_into_value(inner, ary, interp)?;

        result
    };

    Ok(interp.convert(result))
}

pub fn reverse(interp: &mut Artichoke, mut ary: Value) -> Result<Value, Error> {
    let array = unsafe { Array::unbox_from_value(&mut ary, interp)? };
    let mut reversed = array.clone();
    reversed.reverse();
    Array::alloc_value(reversed, interp)
}

pub fn reverse_bang(interp: &mut Artichoke, mut ary: Value) -> Result<Value, Error> {
    if ary.is_frozen(interp) {
        return Err(FrozenError::with_message("can't modify frozen Array").into());
    }
    let mut array = unsafe { Array::unbox_from_value(&mut ary, interp)? };

    // SAFETY: Reversing an `Array` in place does not reallocate it. The array
    // is repacked without any intervening interpreter heap allocations.
    unsafe {
        let array_mut = array.as_inner_mut();
        array_mut.reverse();

        let inner = array.take();
        Array::box_into_value(inner, ary, interp)?;
    }

    Ok(ary)
}

pub fn shift(interp: &mut Artichoke, mut ary: Value, count: Option<Value>) -> Result<Value, Error> {
    if ary.is_frozen(interp) {
        return Err(FrozenError::with_message("can't modify frozen Array").into());
    }
    let mut array = unsafe { Array::unbox_from_value(&mut ary, interp)? };
    if let Some(count) = count {
        let count = implicitly_convert_to_int(interp, count)?;
        let count = usize::try_from(count).map_err(|_| ArgumentError::with_message("negative array size"))?;

        // SAFETY: The call to `Array::shift_n` will potentially invalidate the
        // raw parts stored in `ary`'s `RArray*`.
        //
        // The below call to `Array::alloc_value` will trigger an mruby heap
        // allocation which may trigger a garbage collection.
        //
        // The raw parts in `ary`'s `RArray*` must be repacked before a potential
        // garbage collection, otherwise marking the children in `ary` will have
        // undefined behavior.
        //
        // The call to `Array::alloc_value` happens outside this block after
        // the `Array` has been repacked.
        let shifted = unsafe {
            let array_mut = array.as_inner_mut();
            let shifted = array_mut.shift_n(count);

            let inner = array.take();
            Array::box_into_value(inner, ary, interp)?;

            shifted
        };

        Array::alloc_value(shifted, interp)
    } else {
        // SAFETY: The call to `Array::shift` will potentially invalidate the
        // raw parts stored in `ary`'s `RArray*`.
        //
        // The raw parts in `ary`'s `RArray *` must be repacked before a
        // potential garbage collection, otherwise marking the children in `ary`
        // will have undefined behavior.
        //
        // The call to `interp.convert` happens outside this block after the
        // `Array` has been repacked.
        let shifted = unsafe {
            let array_mut = array.as_inner_mut();
            let shifted = array_mut.shift();

            let inner = array.take();
            Array::box_into_value(inner, ary, interp)?;

            shifted
        };

        Ok(interp.convert(shifted))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test::prelude::*;

    #[test]
    fn mutating_methods_may_raise_frozen_error() {
        let mut interp = interpreter();
        let mut slf = interp.eval(b"[1,2,3]").unwrap();
        slf.freeze(&mut interp).unwrap();

        let value = interp.convert(0);
        assert_eq!(
            push_single(&mut interp, slf, value).unwrap_err().to_string(),
            "FrozenError (can't modify frozen Array)",
        );

        let first = interp.convert(0);
        let second = interp.try_convert_mut(Vec::<u8>::new()).unwrap();
        assert_eq!(
            element_assignment(&mut interp, slf, first, second, None)
                .unwrap_err()
                .to_string(),
            "FrozenError (can't modify frozen Array)",
        );

        assert_eq!(
            clear(&mut interp, slf).unwrap_err().to_string(),
            "FrozenError (can't modify frozen Array)",
        );

        assert_eq!(
            push(&mut interp, slf, None).unwrap_err().to_string(),
            "FrozenError (can't modify frozen Array)",
        );

        assert_eq!(
            concat(&mut interp, slf, []).unwrap_err().to_string(),
            "FrozenError (can't modify frozen Array)",
        );

        assert_eq!(
            reverse_bang(&mut interp, slf).unwrap_err().to_string(),
            "FrozenError (can't modify frozen Array)",
        );

        assert_eq!(
            shift(&mut interp, slf, None).unwrap_err().to_string(),
            "FrozenError (can't modify frozen Array)",
        );
    }
}