initial return type handling

This commit is contained in:
Robin Appelman 2019-04-05 19:56:34 +02:00
commit 42f3a76e07
3 changed files with 42 additions and 1 deletions

View file

@ -11,6 +11,16 @@ fn hello_world() {
printf("Hello world, Rust!"); printf("Hello world, Rust!");
} }
#[ivory_export]
fn add1(input: i64) -> i64 {
input + 1
}
#[ivory_export]
fn format_hello(other: String) -> String {
format!("Hello {}", other)
}
ivory_module!({ ivory_module!({
name: "demo", name: "demo",
version: "0.0.1", version: "0.0.1",

View file

@ -89,6 +89,12 @@ fn export_fn(item: ItemFn) -> TokenStream {
let mut args = data.args(); let mut args = data.args();
#(#arg_cast);* #(#arg_cast);*
let result = #body; let result = #body;
let php_val = ::ivory::zend::PhpVal::from(result);
let zval = ::ivory::zend::ZVal::from(php_val);
unsafe {
*retval = zval
};
} }
} }
} }

View file

@ -7,7 +7,7 @@ use std::mem::size_of;
use std::os::raw::c_char; use std::os::raw::c_char;
use std::str; use std::str;
use ivory_sys::{zend_execute_data, zend_string, zval}; use ivory_sys::*;
use crate::CastError; use crate::CastError;
@ -274,6 +274,12 @@ impl_from_phpval!(f64, Double);
impl_from_phpval!(bool, Bool); impl_from_phpval!(bool, Bool);
impl_from_phpval!(String, String); impl_from_phpval!(String, String);
impl From<()> for PhpVal {
fn from(_input: ()) -> Self {
PhpVal::Null
}
}
impl<T: Into<PhpVal>> From<Option<T>> for PhpVal { impl<T: Into<PhpVal>> From<Option<T>> for PhpVal {
fn from(input: Option<T>) -> Self { fn from(input: Option<T>) -> Self {
match input { match input {
@ -314,3 +320,22 @@ impl<K: Into<ArrayKey> + Hash + Eq + Ord, T: Into<PhpVal>> From<HashMap<K, T>> f
vec.into() vec.into()
} }
} }
impl From<PhpVal> for ZVal {
fn from(input: PhpVal) -> Self {
match input {
PhpVal::Long(val) => ZVal(zval {
value: zend_value { lval: val },
u1: _zval_struct__bindgen_ty_1 {
v: _zval_struct__bindgen_ty_1__bindgen_ty_1 {
type_: ZValType::Long as zend_uchar,
type_flags: 0,
u: _zval_struct__bindgen_ty_1__bindgen_ty_1__bindgen_ty_1 { extra: 0 },
},
},
u2: _zval_struct__bindgen_ty_2 { extra: 0 },
}),
_ => unimplemented!(),
}
}
}