logo
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
use std::ffi::CStr;

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

const ARRAY_CSTR: &CStr = qed::const_cstr_from_str!("Array\0");
static ARRAY_RUBY_SOURCE: &[u8] = include_bytes!("array.rb");

pub fn init(interp: &mut Artichoke) -> InitializeResult<()> {
    if interp.is_class_defined::<Array>() {
        return Ok(());
    }

    let spec = class::Spec::new("Array", ARRAY_CSTR, None, Some(def::box_unbox_free::<Array>))?;
    class::Builder::for_spec(interp, &spec)
        .add_self_method("[]", ary_cls_constructor, sys::mrb_args_rest())?
        .add_method("+", ary_plus, sys::mrb_args_req(1))?
        .add_method("*", ary_mul, sys::mrb_args_req(1))?
        .add_method("[]", ary_element_reference, sys::mrb_args_req_and_opt(1, 1))?
        .add_method("[]=", ary_element_assignment, sys::mrb_args_req_and_opt(2, 1))?
        .add_method("clear", ary_clear, sys::mrb_args_none())?
        .add_method("concat", ary_concat, sys::mrb_args_rest())?
        .add_method("first", ary_first, sys::mrb_args_opt(1))?
        .add_method(
            "initialize",
            ary_initialize,
            sys::mrb_args_opt(2) | sys::mrb_args_block(),
        )?
        .add_method("initialize_copy", ary_initialize_copy, sys::mrb_args_req(1))?
        .add_method("last", ary_last, sys::mrb_args_opt(1))?
        .add_method("length", ary_len, sys::mrb_args_none())?
        .add_method("pop", ary_pop, sys::mrb_args_none())?
        .add_method("reverse", ary_reverse, sys::mrb_args_none())?
        .add_method("reverse!", ary_reverse_bang, sys::mrb_args_none())?
        .add_method("shift", ary_shift, sys::mrb_args_opt(1))?
        .add_method("size", ary_len, sys::mrb_args_none())?
        .define()?;
    interp.def_class::<Array>(spec)?;
    interp.eval(ARRAY_RUBY_SOURCE)?;

    Ok(())
}

unsafe extern "C" fn ary_cls_constructor(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    let rest = mrb_get_args!(mrb, *args);
    unwrap_interpreter!(mrb, to => guard);
    let result = Array::from(rest);
    let result = Array::alloc_value(result, &mut guard);
    match result {
        Ok(value) => {
            let rclass = ary.value.p.cast::<sys::RClass>();
            let value = value.inner();
            let target_rbasic = value.value.p.cast::<sys::RBasic>();

            // Copy `RClass` from source class to newly allocated `Array`.
            (*target_rbasic).c = rclass;

            value
        }
        Err(exception) => error::raise(guard, exception),
    }
}

unsafe extern "C" fn ary_plus(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    let other = mrb_get_args!(mrb, required = 1);
    unwrap_interpreter!(mrb, to => guard);
    let array = Value::from(ary);
    let other = Value::from(other);
    let result = trampoline::plus(&mut guard, array, other);
    match result {
        Ok(value) => {
            let basic = sys::mrb_sys_basic_ptr(ary);
            sys::mrb_write_barrier(mrb, basic);
            value.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}

unsafe extern "C" fn ary_mul(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    let joiner = mrb_get_args!(mrb, required = 1);
    unwrap_interpreter!(mrb, to => guard);
    let array = Value::from(ary);
    let joiner = Value::from(joiner);
    let result = trampoline::mul(&mut guard, array, joiner);
    match result {
        Ok(value) => {
            let basic = sys::mrb_sys_basic_ptr(ary);
            sys::mrb_write_barrier(mrb, basic);
            value.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}

unsafe extern "C" fn ary_element_reference(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    let (elem, len) = mrb_get_args!(mrb, required = 1, optional = 1);
    unwrap_interpreter!(mrb, to => guard);
    let elem = Value::from(elem);
    let len = len.map(Value::from);
    let array = Value::from(ary);
    let result = trampoline::element_reference(&mut guard, array, elem, len);
    match result {
        Ok(value) => value.inner(),
        Err(exception) => error::raise(guard, exception),
    }
}

unsafe extern "C" fn ary_element_assignment(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    let (first, second, third) = mrb_get_args!(mrb, required = 2, optional = 1);
    unwrap_interpreter!(mrb, to => guard);
    let first = Value::from(first);
    let second = Value::from(second);
    let third = third.map(Value::from);
    let array = Value::from(ary);
    let result = trampoline::element_assignment(&mut guard, array, first, second, third);
    match result {
        Ok(value) => {
            let basic = sys::mrb_sys_basic_ptr(ary);
            sys::mrb_write_barrier(mrb, basic);
            value.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}

unsafe extern "C" fn ary_clear(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    mrb_get_args!(mrb, none);
    unwrap_interpreter!(mrb, to => guard);
    let array = Value::from(ary);
    let result = trampoline::clear(&mut guard, array);
    match result {
        Ok(value) => {
            let basic = sys::mrb_sys_basic_ptr(ary);
            sys::mrb_write_barrier(mrb, basic);
            value.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}

unsafe extern "C" fn ary_concat(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    let others = mrb_get_args!(mrb, *args);
    unwrap_interpreter!(mrb, to => guard);
    let array = Value::from(ary);
    let others = others.iter().map(|&other| Value::from(other));
    let result = trampoline::concat(&mut guard, array, others);
    match result {
        Ok(value) => {
            let basic = sys::mrb_sys_basic_ptr(ary);
            sys::mrb_write_barrier(mrb, basic);
            value.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}

unsafe extern "C" fn ary_first(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    let num = mrb_get_args!(mrb, optional = 1);
    unwrap_interpreter!(mrb, to => guard);
    let array = Value::from(ary);
    let num = num.map(Value::from);
    let result = trampoline::first(&mut guard, array, num);
    match result {
        Ok(value) => {
            let basic = sys::mrb_sys_basic_ptr(ary);
            sys::mrb_write_barrier(mrb, basic);
            value.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}

unsafe extern "C" fn ary_initialize(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    let (first, second, block) = mrb_get_args!(mrb, optional = 2, &block);
    unwrap_interpreter!(mrb, to => guard);
    let array = Value::from(ary);
    let first = first.map(Value::from);
    let second = second.map(Value::from);
    let result = trampoline::initialize(&mut guard, array, first, second, block);
    match result {
        Ok(value) => {
            let basic = sys::mrb_sys_basic_ptr(ary);
            sys::mrb_write_barrier(mrb, basic);
            value.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}

unsafe extern "C" fn ary_initialize_copy(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    let other = mrb_get_args!(mrb, required = 1);
    unwrap_interpreter!(mrb, to => guard);
    let array = Value::from(ary);
    let other = Value::from(other);
    let result = trampoline::initialize_copy(&mut guard, array, other);
    match result {
        Ok(value) => {
            let basic = sys::mrb_sys_basic_ptr(ary);
            sys::mrb_write_barrier(mrb, basic);
            value.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}

unsafe extern "C" fn ary_last(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    let num = mrb_get_args!(mrb, optional = 1);
    unwrap_interpreter!(mrb, to => guard);
    let array = Value::from(ary);
    let num = num.map(Value::from);
    let result = trampoline::last(&mut guard, array, num);
    match result {
        Ok(value) => {
            let basic = sys::mrb_sys_basic_ptr(ary);
            sys::mrb_write_barrier(mrb, basic);
            value.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}

unsafe extern "C" fn ary_len(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    mrb_get_args!(mrb, none);
    unwrap_interpreter!(mrb, to => guard);
    let ary = Value::from(ary);
    let result = trampoline::len(&mut guard, ary).and_then(|len| {
        if let Ok(len) = sys::mrb_int::try_from(len) {
            Ok(len)
        } else {
            Err(Fatal::from("Array length does not fit in mruby Integer max").into())
        }
    });
    match result {
        Ok(len) => {
            let len = guard.convert(len);
            len.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}

unsafe extern "C" fn ary_pop(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    mrb_get_args!(mrb, none);
    unwrap_interpreter!(mrb, to => guard);
    let array = Value::from(ary);
    let result = trampoline::pop(&mut guard, array);
    match result {
        Ok(value) => {
            let basic = sys::mrb_sys_basic_ptr(ary);
            sys::mrb_write_barrier(mrb, basic);
            value.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}

unsafe extern "C" fn ary_reverse(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    mrb_get_args!(mrb, none);
    unwrap_interpreter!(mrb, to => guard);
    let array = Value::from(ary);
    let result = trampoline::reverse(&mut guard, array);
    match result {
        Ok(value) => {
            let basic = sys::mrb_sys_basic_ptr(ary);
            sys::mrb_write_barrier(mrb, basic);
            value.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}

unsafe extern "C" fn ary_reverse_bang(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    mrb_get_args!(mrb, none);
    unwrap_interpreter!(mrb, to => guard);
    let array = Value::from(ary);
    let result = trampoline::reverse_bang(&mut guard, array);
    match result {
        Ok(value) => {
            let basic = sys::mrb_sys_basic_ptr(ary);
            sys::mrb_write_barrier(mrb, basic);
            value.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}

unsafe extern "C" fn ary_shift(mrb: *mut sys::mrb_state, ary: sys::mrb_value) -> sys::mrb_value {
    let count = mrb_get_args!(mrb, optional = 1);
    unwrap_interpreter!(mrb, to => guard);
    let count = count.map(Value::from);
    let array = Value::from(ary);
    let result = trampoline::shift(&mut guard, array, count);
    match result {
        Ok(value) => {
            let basic = sys::mrb_sys_basic_ptr(ary);
            sys::mrb_write_barrier(mrb, basic);
            value.inner()
        }
        Err(exception) => error::raise(guard, exception),
    }
}