Skip to content

Commit 3103553

Browse files
committed
Make GenericValue a CBox wrapped type
1 parent 019b875 commit 3103553

File tree

1 file changed

+26
-44
lines changed

1 file changed

+26
-44
lines changed

src/engine.rs

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub trait ExecutionEngine<'a>:'a + Sized + DisposeRef where LLVMExecutionEngineR
5959
///
6060
/// To convert the arguments to `GenericValue`s, you should use the `GenericValueCast::to_generic` method.
6161
/// To convert the return value from a `GenericValue`, you should use the `GenericValueCast::from_generic` method.
62-
fn run_function(&'a self, function: &'a Function, args: &[GenericValue<'a>]) -> GenericValue<'a> {
62+
fn run_function(&'a self, function: &'a Function, args: &[&'a GenericValue]) -> &'a GenericValue {
6363
let ptr = args.as_ptr() as *mut LLVMGenericValueRef;
6464
unsafe { engine::LLVMRunFunction(self.into(), function.into(), args.len() as c_uint, ptr).into() }
6565
}
@@ -164,16 +164,6 @@ impl<'a> ExecutionEngine<'a> for JitEngine {
164164
pub struct Interpreter(PhantomData<[u8]>);
165165
native_ref!{&Interpreter = LLVMExecutionEngineRef}
166166
dispose!{Interpreter, LLVMOpaqueExecutionEngine, LLVMDisposeExecutionEngine}
167-
impl<'a> Interpreter {
168-
/// Run `function` with the arguments given as ``GenericValue`s, then return the result as one.
169-
///
170-
/// To convert the arguments to `GenericValue`s, you should use the `GenericValueCast::to_generic` method.
171-
/// To convert the return value from a `GenericValue`, you should use the `GenericValueCast::from_generic` method.
172-
pub fn run_function(&self, function: &'a Function, args: &[GenericValue<'a>]) -> GenericValue<'a> {
173-
let ptr = args.as_ptr() as *mut LLVMGenericValueRef;
174-
unsafe { engine::LLVMRunFunction(self.into(), function.into(), args.len() as c_uint, ptr).into() }
175-
}
176-
}
177167
impl<'a> ExecutionEngine<'a> for Interpreter {
178168
type Options = ();
179169
fn new(module: &'a Module, _: ()) -> Result<CSemiBox<'a, Interpreter>, CBox<str>> {
@@ -191,52 +181,43 @@ impl<'a> ExecutionEngine<'a> for Interpreter {
191181
}
192182
}
193183
/// A wrapped value that can be passed to an interpreted function or returned from one
194-
pub struct GenericValue<'a> {
195-
value: LLVMGenericValueRef,
196-
marker: PhantomData<&'a ()>
197-
}
198-
native_ref!(contra GenericValue, value: LLVMGenericValueRef);
199-
impl<'a> Drop for GenericValue<'a> {
200-
fn drop(&mut self) {
201-
unsafe {
202-
engine::LLVMDisposeGenericValue(self.value)
203-
}
204-
}
205-
}
184+
pub struct GenericValue(PhantomData<[u8]>);
185+
native_ref!{&GenericValue = LLVMGenericValueRef}
186+
dispose!{GenericValue, LLVMOpaqueGenericValue, LLVMDisposeGenericValue}
206187

207188
/// A value that can be cast into a `GenericValue` and that a `GenericValue` can be cast into.
208189
///
209190
/// Both these methods require contexts because some `Type` constructors are needed for the
210191
/// conversion and these constructors need a context.
211-
pub trait GenericValueCast<'a> {
192+
pub trait GenericValueCast {
212193
/// Create a `GenericValue` from this value.
213-
fn to_generic(self, context: &'a Context) -> GenericValue<'a>;
194+
fn to_generic(self, context: &Context) -> CSemiBox<GenericValue>;
214195
/// Convert the `GenericValue` into a value of this type again.
215-
fn from_generic(value: GenericValue<'a>, context: &'a Context) -> Self;
196+
fn from_generic(value: &GenericValue, context: &Context) -> Self;
216197
}
217198

218-
impl<'a> GenericValueCast<'a> for f64 {
219-
fn to_generic(self, ctx: &'a Context) -> GenericValue<'a> {
199+
impl GenericValueCast for f64 {
200+
fn to_generic(self, ctx: &Context) -> CSemiBox<GenericValue> {
220201
unsafe {
221202
let ty = core::LLVMDoubleTypeInContext(ctx.into());
222-
engine::LLVMCreateGenericValueOfFloat(ty, self).into()
203+
CSemiBox::new(engine::LLVMCreateGenericValueOfFloat(ty, self))
223204
}
224205
}
225-
fn from_generic(value: GenericValue<'a>, ctx: &'a Context) -> f64 {
206+
fn from_generic(value: &GenericValue, ctx: &Context) -> f64 {
226207
unsafe {
227208
let ty = core::LLVMDoubleTypeInContext(ctx.into());
228209
engine::LLVMGenericValueToFloat(ty, value.into())
229210
}
230211
}
231212
}
232-
impl<'a> GenericValueCast<'a> for f32 {
233-
fn to_generic(self, ctx: &'a Context) -> GenericValue<'a> {
213+
impl GenericValueCast for f32 {
214+
fn to_generic(self, ctx: &Context) -> CSemiBox<GenericValue> {
234215
unsafe {
235216
let ty = core::LLVMFloatTypeInContext(ctx.into());
236-
engine::LLVMCreateGenericValueOfFloat(ty, self as f64).into()
217+
CSemiBox::new(engine::LLVMCreateGenericValueOfFloat(ty, self as f64))
237218
}
238219
}
239-
fn from_generic(value: GenericValue<'a>, ctx: &'a Context) -> f32 {
220+
fn from_generic(value: &GenericValue, ctx: &Context) -> f32 {
240221
unsafe {
241222
let ty = core::LLVMFloatTypeInContext(ctx.into());
242223
engine::LLVMGenericValueToFloat(ty, value.into()) as f32
@@ -245,14 +226,14 @@ impl<'a> GenericValueCast<'a> for f32 {
245226
}
246227
macro_rules! generic_int(
247228
($ty:ty, $signed:expr) => (
248-
impl<'a> GenericValueCast<'a> for $ty {
249-
fn to_generic(self, ctx: &'a Context) -> GenericValue<'a> {
229+
impl GenericValueCast for $ty {
230+
fn to_generic(self, ctx: &Context) -> CSemiBox<GenericValue> {
250231
unsafe {
251-
let ty = <Self as Compile<'a>>::get_type(ctx);
252-
engine::LLVMCreateGenericValueOfInt(ty.into(), self as c_ulonglong, $signed as c_int).into()
232+
let ty = <Self as Compile>::get_type(ctx);
233+
CSemiBox::new(engine::LLVMCreateGenericValueOfInt(ty.into(), self as c_ulonglong, $signed as c_int))
253234
}
254235
}
255-
fn from_generic(value: GenericValue<'a>, _: &'a Context) -> $ty {
236+
fn from_generic(value: &GenericValue, _: &Context) -> $ty {
256237
unsafe {
257238
engine::LLVMGenericValueToInt(value.into(), $signed as c_int) as $ty
258239
}
@@ -265,14 +246,14 @@ macro_rules! generic_int(
265246
);
266247
);
267248

268-
impl<'a> GenericValueCast<'a> for bool {
269-
fn to_generic(self, ctx: &'a Context) -> GenericValue<'a> {
249+
impl GenericValueCast for bool {
250+
fn to_generic(self, ctx: &Context) -> CSemiBox<GenericValue> {
270251
unsafe {
271-
let ty = <Self as Compile<'a>>::get_type(ctx);
272-
engine::LLVMCreateGenericValueOfInt(ty.into(), self as c_ulonglong, 0).into()
252+
let ty = <Self as Compile>::get_type(ctx);
253+
CSemiBox::new(engine::LLVMCreateGenericValueOfInt(ty.into(), self as c_ulonglong, 0))
273254
}
274255
}
275-
fn from_generic(value: GenericValue<'a>, _: &'a Context) -> bool {
256+
fn from_generic(value: &GenericValue, _: &Context) -> bool {
276257
unsafe {
277258
engine::LLVMGenericValueToInt(value.into(), 0) != 0
278259
}
@@ -282,3 +263,4 @@ generic_int!{some i8, u8}
282263
generic_int!{some i16, u16}
283264
generic_int!{some i32, u32}
284265
generic_int!{some i64, u64}
266+
generic_int!{some isize, usize}

0 commit comments

Comments
 (0)