artichoke_backend/extn/core/symbol/
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
use std::ffi::{c_char, CStr};
use std::ptr;
use std::slice;

use crate::extn::core::symbol::Symbol;
use crate::extn::prelude::*;

// ```c
// MRB_API mrb_sym mrb_intern(mrb_state*,const char*,size_t);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_intern(mrb: *mut sys::mrb_state, name: *const c_char, len: usize) -> sys::mrb_sym {
    let bytes = slice::from_raw_parts(name.cast::<u8>(), len);
    let bytes = bytes.to_vec();
    unwrap_interpreter!(mrb, to => guard, or_else = 0);
    let sym = guard.intern_bytes(bytes);
    let sym = sym.map(u32::from);
    sym.unwrap_or_default()
}

// ```c
// MRB_API mrb_sym mrb_intern_static(mrb_state*,const char*,size_t);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_intern_static(mrb: *mut sys::mrb_state, name: *const c_char, len: usize) -> sys::mrb_sym {
    let bytes = slice::from_raw_parts::<'static, _>(name.cast::<u8>(), len);
    unwrap_interpreter!(mrb, to => guard, or_else = 0);
    let sym = guard.intern_bytes(bytes);
    let sym = sym.map(u32::from);
    sym.unwrap_or_default()
}

// ```c
// MRB_API mrb_sym mrb_intern_cstr(mrb_state *mrb, const char* str);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_intern_cstr(mrb: *mut sys::mrb_state, name: *const c_char) -> sys::mrb_sym {
    let string = CStr::from_ptr(name);
    let bytes = string.to_bytes_with_nul().to_vec();
    unwrap_interpreter!(mrb, to => guard, or_else = 0);
    let sym = guard.intern_bytes_with_trailing_nul(bytes);
    let sym = sym.map(u32::from);
    sym.unwrap_or_default()
}

// ```c
// MRB_API mrb_sym mrb_intern_str(mrb_state*,mrb_value);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_intern_str(mrb: *mut sys::mrb_state, name: sys::mrb_value) -> sys::mrb_sym {
    unwrap_interpreter!(mrb, to => guard, or_else = 0);
    let name = Value::from(name);
    if let Ok(bytes) = name.try_convert_into_mut::<Vec<u8>>(&mut guard) {
        let sym = guard.intern_bytes(bytes);
        let sym = sym.map(u32::from);
        sym.unwrap_or_default()
    } else {
        0
    }
}

/* `mrb_intern_check` series functions returns 0 if the symbol is not defined */

// ```c
// MRB_API mrb_sym mrb_intern_check(mrb_state*,const char*,size_t);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_intern_check(mrb: *mut sys::mrb_state, name: *const c_char, len: usize) -> sys::mrb_sym {
    let bytes = slice::from_raw_parts(name.cast::<u8>(), len);
    unwrap_interpreter!(mrb, to => guard, or_else = 0);
    if let Ok(Some(sym)) = guard.check_interned_bytes(bytes) {
        sym
    } else {
        0
    }
}

// ```c
// MRB_API mrb_sym mrb_intern_check_cstr(mrb_state*,const char*);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_intern_check_cstr(mrb: *mut sys::mrb_state, name: *const c_char) -> sys::mrb_sym {
    let string = CStr::from_ptr(name);
    let bytes = string.to_bytes_with_nul();
    unwrap_interpreter!(mrb, to => guard, or_else = 0);
    if let Ok(Some(sym)) = guard.check_interned_bytes_with_trailing_nul(bytes) {
        sym
    } else {
        0
    }
}

// ```c
// MRB_API mrb_sym mrb_intern_check_str(mrb_state*,mrb_value);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_intern_check_str(mrb: *mut sys::mrb_state, name: sys::mrb_value) -> sys::mrb_sym {
    unwrap_interpreter!(mrb, to => guard, or_else = 0);
    let name = Value::from(name);
    if let Ok(bytes) = name.try_convert_into_mut::<&[u8]>(&mut guard) {
        if let Ok(Some(sym)) = guard.check_interned_bytes(bytes) {
            return sym;
        }
    }
    0
}

// `mrb_check_intern` series functions returns `nil` if the symbol is not
// defined; otherwise returns `mrb_value`.

// ```c
// MRB_API mrb_value mrb_check_intern(mrb_state*,const char*,size_t);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_check_intern(mrb: *mut sys::mrb_state, name: *const c_char, len: usize) -> sys::mrb_value {
    let bytes = slice::from_raw_parts(name.cast::<u8>(), len);
    unwrap_interpreter!(mrb, to => guard);
    let symbol = if let Ok(Some(sym)) = guard.check_interned_bytes(bytes) {
        Symbol::alloc_value(sym.into(), &mut guard).unwrap_or_default()
    } else {
        Value::nil()
    };
    symbol.inner()
}

// ```c
// MRB_API mrb_value mrb_check_intern_cstr(mrb_state*,const char*);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_check_intern_cstr(mrb: *mut sys::mrb_state, name: *const c_char) -> sys::mrb_value {
    let string = CStr::from_ptr(name);
    let bytes = string.to_bytes_with_nul();
    unwrap_interpreter!(mrb, to => guard);
    let symbol = if let Ok(Some(sym)) = guard.check_interned_bytes_with_trailing_nul(bytes) {
        Symbol::alloc_value(sym.into(), &mut guard).unwrap_or_default()
    } else {
        Value::nil()
    };
    symbol.inner()
}

// ```c
// MRB_API mrb_value mrb_check_intern_str(mrb_state*,mrb_value);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_check_intern_str(mrb: *mut sys::mrb_state, name: sys::mrb_value) -> sys::mrb_value {
    unwrap_interpreter!(mrb, to => guard);
    let name = Value::from(name);
    let symbol = if let Ok(bytes) = name.try_convert_into_mut::<&[u8]>(&mut guard) {
        if let Ok(Some(sym)) = guard.check_interned_bytes(bytes) {
            Symbol::alloc_value(sym.into(), &mut guard).unwrap_or_default()
        } else {
            Value::nil()
        }
    } else {
        Value::nil()
    };
    symbol.inner()
}

// ```c
// MRB_API const char *mrb_sym_name(mrb_state*,mrb_sym);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_sym_name(mrb: *mut sys::mrb_state, sym: sys::mrb_sym) -> *const c_char {
    unwrap_interpreter!(mrb, to => guard, or_else = ptr::null());
    if let Ok(Some(bytes)) = guard.lookup_symbol_with_trailing_nul(sym) {
        bytes.as_ptr().cast::<c_char>()
    } else {
        ptr::null()
    }
}

// ```c
// MRB_API const char *mrb_sym_name_len(mrb_state*,mrb_sym,mrb_int*);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_sym_name_len(
    mrb: *mut sys::mrb_state,
    sym: sys::mrb_sym,
    lenp: *mut sys::mrb_int,
) -> *const c_char {
    if !lenp.is_null() {
        ptr::write(lenp, 0);
    }
    unwrap_interpreter!(mrb, to => guard, or_else = ptr::null());
    if let Ok(Some(bytes)) = guard.lookup_symbol(sym) {
        if !lenp.is_null() {
            if let Ok(len) = sys::mrb_int::try_from(bytes.len()) {
                ptr::write(lenp, len);
            } else {
                return ptr::null();
            }
        }
        bytes.as_ptr().cast()
    } else {
        ptr::null()
    }
}

// ```c
// MRB_API const char *mrb_sym_dump(mrb_state*,mrb_sym);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_sym_dump(mrb: *mut sys::mrb_state, sym: sys::mrb_sym) -> *const c_char {
    unwrap_interpreter!(mrb, to => guard, or_else = ptr::null());
    if let Ok(Some(bytes)) = guard.lookup_symbol(sym) {
        let bytes = bytes.to_vec();
        // Allocate a buffer with the lifetime of the interpreter and return
        // a pointer to it.
        if let Ok(string) = guard.try_convert_mut(bytes) {
            if let Ok(bytes) = string.try_convert_into_mut::<&[u8]>(&mut guard) {
                return bytes.as_ptr().cast();
            }
        }
    }
    ptr::null()
}

// ```c
// MRB_API mrb_value mrb_sym_str(mrb_state*,mrb_sym);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_sym_str(mrb: *mut sys::mrb_state, sym: sys::mrb_sym) -> sys::mrb_value {
    unwrap_interpreter!(mrb, to => guard);

    let value = if let Ok(Some(bytes)) = guard.lookup_symbol(sym) {
        let bytes = bytes.to_vec();
        guard.try_convert_mut(bytes)
    } else {
        guard.try_convert_mut("")
    };
    value.unwrap_or_default().inner()
}

// ```c
// void mrb_init_symtbl(mrb_state*);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_init_symtbl(mrb: *mut sys::mrb_state) {
    // The symbol table is initialized before the call to `mrb_open_allocf` in
    // `crate::interpreter::interpreter`. This function is intended to be called
    // during the initialization of the `mrb_state`.
    let _ = mrb;
}

// ```c
// void mrb_free_symtbl(mrb_state *mrb);
// ```
#[no_mangle]
unsafe extern "C" fn mrb_free_symtbl(mrb: *mut sys::mrb_state) {
    // The symbol table is freed when the Rust `State` is freed.
    let _ = mrb;
}