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)
}
}
}