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
use raw::*;
use alloc::oom;
use function::Func;
use util::{from_ptr, from_ptr_opt};
use std::marker::PhantomData;
use std::{mem, ptr};
use std::ops::{Index, IndexMut};
use std::iter::IntoIterator;
pub struct Context<T = ()> {
_context: jit_context_t,
marker: PhantomData<T>
}
native_ref!(Context<T>, _context: jit_context_t, marker = PhantomData);
impl<T = ()> Index<i32> for Context<T> {
type Output = T;
fn index(&self, index: i32) -> &T {
unsafe {
let meta = jit_context_get_meta(self.into(), index);
if meta.is_null() {
panic!("No such index {} on Context", index)
}
mem::transmute(meta)
}
}
}
impl<T = ()> IndexMut<i32> for Context<T> {
fn index_mut(&mut self, index: i32) -> &mut T {
unsafe {
let meta = jit_context_get_meta(self.into(), index);
if meta.is_null() {
let boxed = Box::new(mem::uninitialized::<T>());
if jit_context_set_meta(self.into(), index, mem::transmute(boxed), Some(::free_data::<T>)) == 0 {
oom()
} else {
mem::transmute(jit_context_get_meta(self.into(), index))
}
} else {
mem::transmute(meta)
}
}
}
}
impl<T = ()> Context<T> {
#[inline(always)]
pub fn new() -> Context<T> {
unsafe {
from_ptr(jit_context_create())
}
}
pub fn functions(&self) -> Functions {
Functions {
context: self.into(),
last: ptr::null_mut(),
lifetime: PhantomData,
}
}
}
impl !Send for Context {
}
impl<'a, T> IntoIterator for &'a Context<T> {
type IntoIter = Functions<'a>;
type Item = &'a Func;
fn into_iter(self) -> Functions<'a> {
self.functions()
}
}
impl<T> Drop for Context<T> {
#[inline(always)]
fn drop(&mut self) {
unsafe {
jit_context_destroy(self.into());
}
}
}
pub struct Functions<'a> {
context: jit_context_t,
last: jit_function_t,
lifetime: PhantomData<&'a ()>
}
impl<'a> Iterator for Functions<'a> {
type Item = &'a Func;
fn next(&mut self) -> Option<&'a Func> {
unsafe {
self.last = jit_function_next(self.context, self.last);
from_ptr_opt(self.last)
}
}
}