artichoke_backend/extn/core/array/
ffi.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
use std::ptr::NonNull;
use std::slice;

use crate::extn::core::array::{Array, RawParts};
use crate::extn::prelude::*;

// ```c
// MRB_API mrb_value mrb_ary_new(mrb_state *mrb);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_ary_new(mrb: *mut sys::mrb_state) -> sys::mrb_value {
    unwrap_interpreter!(mrb, to => guard);
    let result = Array::new();
    let result = Array::alloc_value(result, &mut guard);
    match result {
        Ok(value) => value.inner(),
        Err(exception) => error::raise(guard, exception),
    }
}

// ```c
// MRB_API mrb_value mrb_ary_new_capa(mrb_state*, mrb_int);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_ary_new_capa(mrb: *mut sys::mrb_state, capa: sys::mrb_int) -> sys::mrb_value {
    unwrap_interpreter!(mrb, to => guard);
    let capacity = usize::try_from(capa).unwrap_or_default();
    let result = Array::with_capacity(capacity);
    let result = Array::alloc_value(result, &mut guard);
    match result {
        Ok(value) => value.inner(),
        Err(exception) => error::raise(guard, exception),
    }
}

// ```c
// MRB_API mrb_value mrb_ary_new_from_values(mrb_state *mrb, mrb_int size, const mrb_value *vals);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_ary_new_from_values(
    mrb: *mut sys::mrb_state,
    size: sys::mrb_int,
    vals: *const sys::mrb_value,
) -> sys::mrb_value {
    unwrap_interpreter!(mrb, to => guard);
    let size = usize::try_from(size).unwrap_or_default();
    let values = slice::from_raw_parts(vals, size);
    let result = Array::from(values);
    let result = Array::alloc_value(result, &mut guard);
    match result {
        Ok(value) => {
            let basic = sys::mrb_sys_basic_ptr(value.inner());
            sys::mrb_write_barrier(mrb, basic);
            value.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}

// ```c
// MRB_API mrb_value mrb_assoc_new(mrb_state *mrb, mrb_value car, mrb_value cdr)
// ```
#[no_mangle]
unsafe extern "C" fn mrb_assoc_new(
    mrb: *mut sys::mrb_state,
    one: sys::mrb_value,
    two: sys::mrb_value,
) -> sys::mrb_value {
    unwrap_interpreter!(mrb, to => guard);
    let result = Array::assoc(one.into(), two.into());
    let result = Array::alloc_value(result, &mut guard);
    match result {
        Ok(value) => {
            let basic = sys::mrb_sys_basic_ptr(value.inner());
            sys::mrb_write_barrier(mrb, basic);
            value.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}

// ```c
// MRB_API mrb_value mrb_ary_splat(mrb_state *mrb, mrb_value value);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_ary_splat(mrb: *mut sys::mrb_state, value: sys::mrb_value) -> sys::mrb_value {
    unwrap_interpreter!(mrb, to => guard);
    let mut value = Value::from(value);
    let result = if Array::unbox_from_value(&mut value, &mut guard).is_ok() {
        Ok(value)
    } else {
        let mut result = Array::with_capacity(1);
        result.push(value);
        Array::alloc_value(result, &mut guard)
    };
    match result {
        Ok(value) => {
            let basic = sys::mrb_sys_basic_ptr(value.inner());
            sys::mrb_write_barrier(mrb, basic);
            value.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}

// ```c
// MRB_API void mrb_ary_concat(mrb_state *mrb, mrb_value self, mrb_value other);
// ```
//
// This function corresponds to the `OP_ARYCAT` VM opcode.
#[no_mangle]
unsafe extern "C" fn mrb_ary_concat(mrb: *mut sys::mrb_state, ary: sys::mrb_value, other: sys::mrb_value) {
    unwrap_interpreter!(mrb, to => guard, or_else = ());
    let mut array = Value::from(ary);
    let mut other = Value::from(other);
    if let Ok(mut array) = Array::unbox_from_value(&mut array, &mut guard) {
        if let Ok(other) = Array::unbox_from_value(&mut other, &mut guard) {
            // SAFETY: The array is repacked before any intervening uses of
            // `interp` which means no mruby heap allocations can occur.
            let array_mut = array.as_inner_mut();
            array_mut.extend(other.iter());

            let inner = array.take();
            Array::box_into_value(inner, ary.into(), &mut guard).expect("Array reboxing should not fail");
        } else {
            emit_fatal_warning!(
                "ffi: mrb_ary_concat: Expected {:?} argument but got {:?} argument",
                Ruby::Array,
                other.ruby_type()
            );
        }

        let basic = sys::mrb_sys_basic_ptr(ary);
        sys::mrb_write_barrier(mrb, basic);
    }
}

// ```c
// MRB_API mrb_value mrb_ary_pop(mrb_state *mrb, mrb_value ary);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_ary_pop(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    unwrap_interpreter!(mrb, to => guard);
    let mut array = Value::from(ary);
    let result = if let Ok(mut array) = Array::unbox_from_value(&mut array, &mut guard) {
        // SAFETY: The array is repacked before any intervening uses of `interp`
        // which means no mruby heap allocations can occur.
        let array_mut = array.as_inner_mut();
        let popped = array_mut.pop();

        let inner = array.take();
        Array::box_into_value(inner, ary.into(), &mut guard).expect("Array reboxing should not fail");

        guard.convert(popped)
    } else {
        Value::nil()
    };
    let basic = sys::mrb_sys_basic_ptr(ary);
    sys::mrb_write_barrier(mrb, basic);
    result.inner()
}

// ```c
// MRB_API void mrb_ary_push(mrb_state *mrb, mrb_value array, mrb_value value);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_ary_push(mrb: *mut sys::mrb_state, ary: sys::mrb_value, value: sys::mrb_value) {
    unwrap_interpreter!(mrb, to => guard, or_else = ());
    let mut array = Value::from(ary);
    let value = Value::from(value);
    if let Ok(mut array) = Array::unbox_from_value(&mut array, &mut guard) {
        // SAFETY: The array is repacked before any intervening uses of `interp`
        // which means no mruby heap allocations can occur.
        let array_mut = array.as_inner_mut();
        array_mut.push(value);

        let inner = array.take();
        Array::box_into_value(inner, ary.into(), &mut guard).expect("Array reboxing should not fail");
    }
    let basic = sys::mrb_sys_basic_ptr(ary);
    sys::mrb_write_barrier(mrb, basic);
}

// ```c
// MRB_API mrb_value mrb_ary_ref(mrb_state *mrb, mrb_value ary, mrb_int n);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_ary_ref(
    mrb: *mut sys::mrb_state,
    ary: sys::mrb_value,
    offset: sys::mrb_int,
) -> sys::mrb_value {
    unwrap_interpreter!(mrb, to => guard);
    let mut ary = Value::from(ary);
    let offset = usize::try_from(offset).unwrap_or_default();
    let result = if let Ok(array) = Array::unbox_from_value(&mut ary, &mut guard) {
        guard.convert(array.get(offset))
    } else {
        Value::nil()
    };
    result.inner()
}

// ```c
// MRB_API void mrb_ary_set(mrb_state *mrb, mrb_value ary, mrb_int n, mrb_value val);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_ary_set(
    mrb: *mut sys::mrb_state,
    ary: sys::mrb_value,
    offset: sys::mrb_int,
    value: sys::mrb_value,
) {
    unwrap_interpreter!(mrb, to => guard, or_else = ());
    let mut array = Value::from(ary);
    let value = Value::from(value);
    if let Ok(mut array) = Array::unbox_from_value(&mut array, &mut guard) {
        let offset = aref::offset_to_index(offset, array.len()).unwrap_or(0);
        // TODO: properly handle self-referential sets.
        if Value::from(ary) != value {
            // SAFETY: The array is repacked before any intervening uses of
            // `interp` which means no mruby heap allocations can occur.
            let array_mut = array.as_inner_mut();
            array_mut.set(offset, value);

            let inner = array.take();
            Array::box_into_value(inner, ary.into(), &mut guard).expect("Array reboxing should not fail");
        }
    }
    let basic = sys::mrb_sys_basic_ptr(ary);
    sys::mrb_write_barrier(mrb, basic);
}

// ```c
// MRB_API mrb_value mrb_ary_shift(mrb_state *mrb, mrb_value self)
// ```
#[no_mangle]
unsafe extern "C" fn mrb_ary_shift(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    unwrap_interpreter!(mrb, to => guard);
    let mut array = Value::from(ary);
    let result = if let Ok(mut array) = Array::unbox_from_value(&mut array, &mut guard) {
        // SAFETY: The array is repacked before any intervening uses of `interp`
        // which means no mruby heap allocations can occur.
        let array_mut = array.as_inner_mut();
        let result = array_mut.shift();

        let inner = array.take();
        Array::box_into_value(inner, ary.into(), &mut guard).expect("Array reboxing should not fail");

        guard.convert(result)
    } else {
        Value::nil()
    };
    let basic = sys::mrb_sys_basic_ptr(ary);
    sys::mrb_write_barrier(mrb, basic);
    result.inner()
}

// ```c
// MRB_API mrb_value mrb_ary_unshift(mrb_state *mrb, mrb_value self, mrb_value item)
// ```
#[no_mangle]
unsafe extern "C" fn mrb_ary_unshift(
    mrb: *mut sys::mrb_state,
    ary: sys::mrb_value,
    value: sys::mrb_value,
) -> sys::mrb_value {
    unwrap_interpreter!(mrb, to => guard);
    let mut array = Value::from(ary);
    if let Ok(mut array) = Array::unbox_from_value(&mut array, &mut guard) {
        // SAFETY: The array is repacked before any intervening uses of `interp`
        // which means no mruby heap allocations can occur.
        let array_mut = array.as_inner_mut();
        array_mut.unshift(value.into());

        let inner = array.take();
        Array::box_into_value(inner, ary.into(), &mut guard).expect("Array reboxing should not fail");
    }
    let basic = sys::mrb_sys_basic_ptr(ary);
    sys::mrb_write_barrier(mrb, basic);
    value
}

#[no_mangle]
#[allow(clippy::cast_possible_truncation)]
#[allow(clippy::cast_sign_loss)]
unsafe extern "C" fn mrb_ary_artichoke_free(mrb: *mut sys::mrb_state, ary: *mut sys::RArray) {
    let _ = mrb;

    let Some(mut ptr) = NonNull::new((*ary).as_.heap.ptr) else {
        // An allocated but uninitialized array has a null pointer, so there is
        // nothing to free.
        return;
    };
    let length = (*ary).as_.heap.len as usize;
    let capacity = (*ary).as_.heap.aux.capa as usize;

    drop(Array::from_raw_parts(RawParts {
        ptr: ptr.as_mut(),
        length,
        capacity,
    }));
}