-
Notifications
You must be signed in to change notification settings - Fork 146
/
lib.rs
358 lines (312 loc) · 9.22 KB
/
lib.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
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
//! A (WIP) C API allowing use of gluon in other langauges than Rust.
#![doc(html_root_url = "https://docs.rs/gluon_c-api/0.18.2")] // # GLUON
use std::{slice, str};
use futures::{executor::block_on, future};
use gluon::{
vm::{
api::{CPrimitive, Getable, Hole, OpaqueValue, Pushable},
stack,
thread::{RootedThread, Status, Thread, ThreadInternal},
types::{VmIndex, VmInt},
},
ThreadExt,
};
pub type Function = extern "C" fn(&Thread) -> Status;
// TODO What should the c api return as errors
// TODO How should error messages be returned
#[repr(C)]
#[derive(Debug, PartialEq)]
pub enum Error {
Ok,
Unknown,
}
#[no_mangle]
pub extern "C" fn glu_new_vm() -> *const Thread {
let vm = RootedThread::new();
vm.into_raw()
}
#[no_mangle]
pub unsafe extern "C" fn glu_free_vm(vm: &Thread) {
RootedThread::from_raw(vm);
}
#[no_mangle]
pub unsafe extern "C" fn glu_run_expr(
vm: &Thread,
module: &u8,
module_len: usize,
expr: &u8,
expr_len: usize,
) -> Error {
let module = match str::from_utf8(slice::from_raw_parts(module, module_len)) {
Ok(s) => s,
Err(_) => return Error::Unknown,
};
let expr = match str::from_utf8(slice::from_raw_parts(expr, expr_len)) {
Ok(s) => s,
Err(_) => return Error::Unknown,
};
let result = vm.run_expr::<OpaqueValue<&Thread, Hole>>(module, expr);
match result {
Ok(_) => Error::Ok,
Err(_) => Error::Unknown,
}
}
#[no_mangle]
pub unsafe extern "C" fn glu_load_script(
vm: &Thread,
module: &u8,
module_len: usize,
expr: &u8,
expr_len: usize,
) -> Error {
let module = match str::from_utf8(slice::from_raw_parts(module, module_len)) {
Ok(s) => s,
Err(_) => return Error::Unknown,
};
let expr = match str::from_utf8(slice::from_raw_parts(expr, expr_len)) {
Ok(s) => s,
Err(_) => return Error::Unknown,
};
let result = vm.load_script(module, expr);
match result {
Ok(_) => Error::Ok,
Err(_) => Error::Unknown,
}
}
#[no_mangle]
pub extern "C" fn glu_call_function(thread: &Thread, args: VmIndex) -> Error {
match block_on(future::poll_fn(|cx| {
let context = thread.context();
thread.call_function(cx, context, args)
})) {
Ok(_) => Error::Ok,
Err(_) => Error::Unknown,
}
}
#[no_mangle]
pub extern "C" fn glu_len(vm: &Thread) -> usize {
let mut context = vm.context();
let stack = context.stack_frame::<stack::State>();
stack.len() as usize
}
#[no_mangle]
pub extern "C" fn glu_pop(vm: &Thread, n: usize) {
let mut context = vm.context();
let mut stack = context.stack_frame::<stack::State>();
for _ in 0..n {
stack.pop();
}
}
#[no_mangle]
pub extern "C" fn glu_push_int(vm: &Thread, int: VmInt) {
Thread::push(vm, int).unwrap();
}
#[no_mangle]
pub extern "C" fn glu_push_byte(vm: &Thread, b: u8) {
Thread::push(vm, b).unwrap();
}
#[no_mangle]
pub extern "C" fn glu_push_float(vm: &Thread, float: f64) {
Thread::push(vm, float).unwrap();
}
#[no_mangle]
pub extern "C" fn glu_push_bool(vm: &Thread, b: i8) {
Thread::push(vm, b != 0).unwrap();
}
#[no_mangle]
pub unsafe extern "C" fn glu_push_function(
vm: &Thread,
name: &u8,
len: usize,
function: Function,
args: VmIndex,
) -> Error {
let s = match str::from_utf8(slice::from_raw_parts(name, len)) {
Ok(s) => s,
Err(_) => return Error::Unknown,
};
match Thread::push(vm, CPrimitive::new(function, args, s)) {
Ok(()) => Error::Ok,
Err(_) => Error::Unknown,
}
}
/// Push a string to the stack. The string must be valid utf-8 or an error will be returned
#[no_mangle]
pub unsafe extern "C" fn glu_push_string(vm: &Thread, s: &u8, len: usize) -> Error {
let s = match str::from_utf8(slice::from_raw_parts(s, len)) {
Ok(s) => s,
Err(_) => return Error::Unknown,
};
match s.vm_push(&mut vm.current_context()) {
Ok(()) => Error::Ok,
Err(_) => Error::Unknown,
}
}
/// Push a string to the stack. If the string is not utf-8 this function will trigger undefined
/// behaviour.
#[no_mangle]
pub unsafe extern "C" fn glu_push_string_unchecked(vm: &Thread, s: &u8, len: usize) -> Error {
let s = str::from_utf8_unchecked(slice::from_raw_parts(s, len));
match s.vm_push(&mut vm.current_context()) {
Ok(()) => Error::Ok,
Err(_) => Error::Unknown,
}
}
#[cfg(not(target_arch = "wasm32"))]
#[no_mangle]
pub extern "C" fn glu_push_light_userdata(vm: &Thread, data: *mut libc::c_void) {
Thread::push(vm, data as usize).unwrap()
}
#[no_mangle]
pub extern "C" fn glu_get_byte(vm: &Thread, index: VmIndex, out: &mut u8) -> Error {
get_value(vm, index, out)
}
#[no_mangle]
pub extern "C" fn glu_get_int(vm: &Thread, index: VmIndex, out: &mut VmInt) -> Error {
get_value(vm, index, out)
}
#[no_mangle]
pub extern "C" fn glu_get_float(vm: &Thread, index: VmIndex, out: &mut f64) -> Error {
get_value(vm, index, out)
}
#[no_mangle]
pub extern "C" fn glu_get_bool(vm: &Thread, index: VmIndex, out: &mut i8) -> Error {
let mut b = false;
let err = get_value(vm, index, &mut b);
if err == Error::Ok {
*out = b as i8;
}
err
}
/// The returned string is garbage collected and may not be valid after the string is removed from
/// its slot in the stack
#[no_mangle]
pub unsafe extern "C" fn glu_get_string(
vm: &Thread,
index: VmIndex,
out: &mut *const u8,
out_len: &mut usize,
) -> Error {
let mut context = vm.context();
let stack = context.stack_frame::<stack::State>();
match stack
.get_variant(index)
.map(|value| <&str>::from_value(vm, value))
{
Some(value) => {
*out = &*value.as_ptr();
*out_len = value.len();
Error::Ok
}
None => Error::Unknown,
}
}
#[cfg(not(target_arch = "wasm32"))]
#[no_mangle]
pub extern "C" fn glu_get_light_userdata(
vm: &Thread,
index: VmIndex,
out: &mut *mut libc::c_void,
) -> Error {
let mut userdata = 0usize;
let err = get_value(vm, index, &mut userdata);
if err == Error::Ok {
*out = userdata as *mut libc::c_void;
}
err
}
fn get_value<T>(vm: &Thread, index: VmIndex, out: &mut T) -> Error
where
T: for<'vm, 'value> Getable<'vm, 'value>,
{
let mut context = vm.context();
let stack = context.stack_frame::<stack::State>();
match stack
.get_variant(index)
.map(|value| T::from_value(vm, value))
{
Some(value) => {
*out = value;
Error::Ok
}
None => Error::Unknown,
}
}
#[cfg(test)]
mod tests {
use super::*;
use gluon::vm::thread::{Status, Thread};
use std::{ptr, slice, str};
#[test]
fn push_pop() {
unsafe {
let vm = &*glu_new_vm();
glu_push_int(vm, 123);
glu_push_float(vm, 3.14);
let s = "test";
glu_push_string(vm, &s.as_bytes()[0], s.len());
glu_push_bool(vm, 1);
glu_push_byte(vm, 128);
let mut int = 0;
assert_eq!(glu_get_int(vm, 0, &mut int), Error::Ok);
assert_eq!(int, 123);
let mut float = 0.0;
assert_eq!(glu_get_float(vm, 1, &mut float), Error::Ok);
assert_eq!(float, 3.14);
let mut string_ptr = ptr::null();
let mut string_len = 0;
assert_eq!(
glu_get_string(vm, 2, &mut string_ptr, &mut string_len),
Error::Ok
);
assert_eq!(
str::from_utf8(slice::from_raw_parts(string_ptr, string_len)),
Ok("test")
);
let mut b = 0;
assert_eq!(glu_get_bool(vm, 3, &mut b), Error::Ok);
assert_eq!(b, 1);
let mut b = 0;
assert_eq!(glu_get_byte(vm, 4, &mut b), Error::Ok);
assert_eq!(b, 128);
assert_eq!(glu_len(vm), 5);
glu_pop(vm, 4);
glu_free_vm(vm);
}
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn push_userdata() {
unsafe {
let vm = &*glu_new_vm();
let x = 123i32;
glu_push_light_userdata(vm, &x as *const i32 as *mut ::libc::c_void);
let mut p = ptr::null_mut();
assert_eq!(glu_get_light_userdata(vm, 0, &mut p), Error::Ok);
assert_eq!(*(p as *const i32), 123);
}
}
#[test]
fn call_function() {
extern "C" fn mult(vm: &Thread) -> Status {
let mut l = 0.0;
assert_eq!(glu_get_float(vm, 0, &mut l), Error::Ok);
let mut r = 0.0;
assert_eq!(glu_get_float(vm, 1, &mut r), Error::Ok);
glu_push_float(vm, l * r);
Status::Ok
}
unsafe {
let vm = &*glu_new_vm();
let name = "mult";
glu_push_function(vm, &name.as_bytes()[0], name.len(), mult, 2);
glu_push_float(vm, 12.0);
glu_push_float(vm, 3.0);
assert_eq!(glu_call_function(vm, 2), Error::Ok);
let mut result = 0.0;
assert_eq!(glu_get_float(vm, 0, &mut result), Error::Ok);
assert_eq!(result, 36.0);
glu_free_vm(vm);
}
}
}