Skip to content

Commit 60658ff

Browse files
committed
Modify trait SetVal api.
1 parent e9b2b81 commit 60658ff

File tree

9 files changed

+121
-59
lines changed

9 files changed

+121
-59
lines changed

examples/hello/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,16 @@ pub fn get_module(module: &mut Module) {
6767
"getFoo",
6868
|this: &mut Object, _: &mut [Val]| {
6969
let prop = this.get_property("foo");
70-
Val::from_val(prop)
70+
Val::new(prop.as_string())
7171
},
7272
vec![],
7373
);
7474
foo_class.add_method(
7575
"setFoo",
7676
|this: &mut Object, arguments: &mut [Val]| {
7777
let prop = this.get_property("foo");
78-
prop.set(&mut arguments[0]);
78+
// TODO add set_property method
79+
// prop.set(&mut arguments[0]);
7980
},
8081
vec![Argument::by_val("foo")],
8182
);

phper-alloc/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#![warn(rust_2018_idioms, clippy::dbg_macro, clippy::print_stdout)]
22

33
/// The Box which use php `emalloc` and `efree` to manage memory.
4+
///
45
/// TODO now feature `allocator_api` is still unstable, using global allocator instead.
56
pub type EBox<T> = Box<T>;
67

78
/// The Vec which use php `emalloc` and `efree` to manage memory.
9+
///
810
/// TODO now feature `allocator_api` is still unstable, using global allocator instead.
911
pub type EVec<T> = Vec<T>;

phper-build/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
//! cfg:
2-
//!
3-
//!
4-
51
use phper_sys::{PHP_DEBUG, PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION, USING_ZTS};
62

73
/// Register useful rust cfg for project using phper.

phper-macros/src/lib.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,51 @@ mod utils;
55

66
use proc_macro::TokenStream;
77

8+
/// C style string end with '\0'.
9+
///
10+
/// # Examples
11+
///
12+
/// ```no_test
13+
/// use std::ffi::CStr;
14+
///
15+
/// assert_eq!(c_str!("foo"), unsafe {
16+
/// CStr::from_ptr("foo\0".as_ptr().cast())
17+
/// });
18+
/// ```
819
#[proc_macro]
920
pub fn c_str(input: TokenStream) -> TokenStream {
1021
utils::c_str(input)
1122
}
1223

24+
/// C style string end with '\0'.
25+
///
26+
/// # Examples
27+
///
28+
/// ```no_test
29+
/// assert_eq!(c_str_ptr!("foo"), "foo\0".as_ptr().cast());
30+
/// ```
1331
#[proc_macro]
1432
pub fn c_str_ptr(input: TokenStream) -> TokenStream {
1533
utils::c_str_ptr(input)
1634
}
1735

36+
/// PHP module entry, wrap the `phper::modules::Module` write operation.
37+
///
38+
/// # Examples
39+
///
40+
/// ```no_test
41+
/// use phper::{php_get_module, modules::Module};
42+
///
43+
/// #[php_get_module]
44+
/// pub fn get_module(module: &mut Module) {
45+
/// // set module metadata
46+
/// module.set_name(env!("CARGO_PKG_NAME"));
47+
/// module.set_version(env!("CARGO_PKG_VERSION"));
48+
/// module.set_author(env!("CARGO_PKG_AUTHORS"));
49+
///
50+
/// // ...
51+
/// }
52+
/// ```
1853
#[proc_macro_attribute]
1954
pub fn php_get_module(attr: TokenStream, input: TokenStream) -> TokenStream {
2055
inner::php_get_module(attr, input)

phper/src/arrays.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ impl Array {
2323
self.inner
2424
}
2525

26+
pub fn into_raw(mut self) -> *mut zend_array {
27+
self.leak = true;
28+
self.inner
29+
}
30+
2631
pub fn insert(&mut self, key: impl AsRef<str>, value: &mut Val) {
2732
let key = key.as_ref();
2833
unsafe {
29-
phper_zend_hash_str_update(self.inner, key.as_ptr().cast(), key.len(), value.as_mut());
34+
phper_zend_hash_str_update(self.inner, key.as_ptr().cast(), key.len(), value.as_mut_ptr());
3035
}
3136
}
3237

@@ -41,10 +46,6 @@ impl Array {
4146
pub fn len(&mut self) -> usize {
4247
unsafe { zend_array_count(self.inner) as usize }
4348
}
44-
45-
pub(crate) fn leak(&mut self) -> &mut bool {
46-
&mut self.leak
47-
}
4849
}
4950

5051
impl Drop for Array {

phper/src/classes.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,14 @@ impl ClassEntity {
136136
pub(crate) unsafe fn declare_properties(&mut self) {
137137
let properties = self.class.properties();
138138
for property in properties {
139-
let mut val = Val::null();
140-
val.set(&mut property.value);
139+
// TODO Fix
140+
// let mut val = Val::null();
141+
// val.set(property.value);
141142
zend_declare_property(
142143
self.entry.load(Ordering::SeqCst).cast(),
143144
property.name.as_ptr().cast(),
144145
property.name.len(),
145-
val.as_mut(),
146+
null_mut(),
146147
Visibility::Public as c_int,
147148
);
148149
}

phper/src/errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use crate::{classes::ClassEntity, modules::read_global_module, Error::Other};
22
use anyhow::anyhow;
33
use std::{error, ffi::FromBytesWithNulError, io, str::Utf8Error};
44

5+
/// Type of [std::result::Result]<T, [crate::Error]>.
56
pub type Result<T> = std::result::Result<T, self::Error>;
67

8+
/// Crate level Error, which also can become an exception in php.
79
#[derive(thiserror::Error, Debug)]
810
pub enum Error {
911
#[error(transparent)]
@@ -20,12 +22,14 @@ pub enum Error {
2022
}
2123

2224
impl Error {
25+
/// An essy way to cause an anyhow::Error.
2326
pub fn other(message: impl ToString) -> Self {
2427
let message = message.to_string();
2528
Other(anyhow!(message))
2629
}
2730
}
2831

32+
/// PHP Throwable, can cause throwing an exception when setting to [crate::values::Val].
2933
pub trait Throwable: error::Error {
3034
fn class_entity(&self) -> *const ClassEntity;
3135
fn code(&self) -> u64;

phper/src/macros.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,66 @@
1+
/// PHP echo.
2+
///
3+
/// # Examples
4+
///
5+
/// ```no_run
6+
/// phper::echo!("Hello, {}!", message)
7+
/// ```
18
#[macro_export]
29
macro_rules! echo {
310
($($arg:tt)*) => ({
411
$crate::output::echo(std::format!($($arg)*))
512
})
613
}
714

15+
/// PHP error log, will exit the request.
16+
///
17+
/// # Examples
18+
///
19+
/// ```no_run
20+
/// phper::errro!("Hello, {}!", message)
21+
/// ```
822
#[macro_export]
923
macro_rules! error {
1024
($($arg:tt)*) => ({
1125
$crate::output::log($crate::output::LogLevel::Error, std::format!($($arg)*))
1226
})
1327
}
1428

29+
/// PHP warning log.
30+
///
31+
/// # Examples
32+
///
33+
/// ```no_run
34+
/// phper::warning!("Hello, {}!", message)
35+
/// ```
1536
#[macro_export]
1637
macro_rules! warning {
1738
($($arg:tt)*) => ({
1839
$crate::output::log($crate::output::LogLevel::Warning, std::format!($($arg)*))
1940
})
2041
}
2142

43+
/// PHP notice log.
44+
///
45+
/// # Examples
46+
///
47+
/// ```no_run
48+
/// phper::notice!("Hello, {}!", message)
49+
/// ```
2250
#[macro_export]
2351
macro_rules! notice {
2452
($($arg:tt)*) => ({
2553
$crate::output::log($crate::output::LogLevel::Notice, std::format!($($arg)*))
2654
})
2755
}
2856

57+
/// PHP deprecated log.
58+
///
59+
/// # Examples
60+
///
61+
/// ```no_run
62+
/// phper::deprecated!("Hello, {}!", message)
63+
/// ```
2964
#[macro_export]
3065
macro_rules! deprecated {
3166
($($arg:tt)*) => ({

phper/src/values.rs

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl Val {
7676
}
7777

7878
#[inline]
79-
fn empty() -> Self {
79+
pub(crate) fn empty() -> Self {
8080
Self {
8181
inner: unsafe { zeroed::<zval>() },
8282
}
@@ -94,13 +94,13 @@ impl Val {
9494
val
9595
}
9696

97-
pub fn from_val(other: &mut Val) -> Self {
97+
pub fn from_val(other: Val) -> Self {
9898
let mut val = Self::empty();
9999
val.set(other);
100100
val
101101
}
102102

103-
pub fn as_mut(&mut self) -> *mut zval {
103+
pub fn as_mut_ptr(&mut self) -> *mut zval {
104104
&mut self.inner
105105
}
106106

@@ -127,93 +127,92 @@ impl Val {
127127
}
128128

129129
pub trait SetVal {
130-
fn set_val(&mut self, val: &mut Val);
130+
fn set_val(self, val: &mut Val);
131131
}
132132

133133
impl SetVal for () {
134-
fn set_val(&mut self, val: &mut Val) {
134+
fn set_val(self, val: &mut Val) {
135135
unsafe {
136136
*val.type_info() = IS_NULL;
137137
}
138138
}
139139
}
140140

141141
impl SetVal for bool {
142-
fn set_val(&mut self, val: &mut Val) {
142+
fn set_val(self, val: &mut Val) {
143143
unsafe {
144-
*val.type_info() = if *self { IS_TRUE } else { IS_FALSE };
144+
*val.type_info() = if self { IS_TRUE } else { IS_FALSE };
145145
}
146146
}
147147
}
148148

149149
impl SetVal for i32 {
150-
fn set_val(&mut self, val: &mut Val) {
151-
(*self as i64).set_val(val)
150+
fn set_val(self, val: &mut Val) {
151+
SetVal::set_val(self as i64, val)
152152
}
153153
}
154154

155155
impl SetVal for u32 {
156-
fn set_val(&mut self, val: &mut Val) {
157-
(*self as i64).set_val(val)
156+
fn set_val(self, val: &mut Val) {
157+
SetVal::set_val(self as i64, val)
158158
}
159159
}
160160

161161
impl SetVal for i64 {
162-
fn set_val(&mut self, val: &mut Val) {
162+
fn set_val(self, val: &mut Val) {
163163
unsafe {
164-
(*val.as_mut()).value.lval = *self;
165-
(*val.as_mut()).u1.type_info = IS_LONG;
164+
(*val.as_mut_ptr()).value.lval = self;
165+
(*val.as_mut_ptr()).u1.type_info = IS_LONG;
166166
}
167167
}
168168
}
169169

170170
impl SetVal for f64 {
171-
fn set_val(&mut self, val: &mut Val) {
171+
fn set_val(self, val: &mut Val) {
172172
unsafe {
173-
(*val.as_mut()).value.dval = *self;
174-
(*val.as_mut()).u1.type_info = IS_DOUBLE;
173+
(*val.as_mut_ptr()).value.dval = self;
174+
(*val.as_mut_ptr()).u1.type_info = IS_DOUBLE;
175175
}
176176
}
177177
}
178178

179-
impl SetVal for str {
180-
fn set_val(&mut self, val: &mut Val) {
179+
impl SetVal for &str {
180+
fn set_val(self, val: &mut Val) {
181181
unsafe {
182-
phper_zval_stringl(val.as_mut(), self.as_ptr().cast(), self.len());
182+
phper_zval_stringl(val.as_mut_ptr(), self.as_ptr().cast(), self.len());
183183
}
184184
}
185185
}
186186

187187
impl SetVal for String {
188-
fn set_val(&mut self, val: &mut Val) {
188+
fn set_val(self, val: &mut Val) {
189189
unsafe {
190-
phper_zval_stringl(val.as_mut(), self.as_ptr().cast(), self.len());
190+
phper_zval_stringl(val.as_mut_ptr(), self.as_ptr().cast(), self.len());
191191
}
192192
}
193193
}
194194

195195
impl SetVal for Array {
196-
fn set_val(&mut self, val: &mut Val) {
196+
fn set_val(self, val: &mut Val) {
197197
unsafe {
198-
let mut new_val = zeroed::<zval>();
199-
phper_zval_arr(&mut new_val, self.as_mut_ptr());
200-
*self.leak() = true;
201-
phper_zval_zval(val.as_mut(), &mut new_val, true.into(), false.into());
198+
let mut new_val = Val::empty();
199+
phper_zval_arr(new_val.as_mut_ptr(), self.into_raw());
200+
phper_zval_zval(val.as_mut_ptr(), new_val.as_mut_ptr(), true.into(), false.into());
202201
}
203202
}
204203
}
205204

206205
impl<T: SetVal> SetVal for Option<T> {
207-
fn set_val(&mut self, val: &mut Val) {
206+
fn set_val(self, val: &mut Val) {
208207
match self {
209-
Some(t) => t.set_val(val),
210-
None => ().set_val(val),
208+
Some(t) => SetVal::set_val(t, val),
209+
None => SetVal::set_val((), val),
211210
}
212211
}
213212
}
214213

215214
impl<T: SetVal, E: Throwable> SetVal for Result<T, E> {
216-
fn set_val(&mut self, val: &mut Val) {
215+
fn set_val(self, val: &mut Val) {
217216
match self {
218217
Ok(t) => t.set_val(val),
219218
Err(e) => unsafe {
@@ -233,21 +232,9 @@ impl<T: SetVal, E: Throwable> SetVal for Result<T, E> {
233232
}
234233

235234
impl SetVal for Val {
236-
fn set_val(&mut self, val: &mut Val) {
235+
fn set_val(mut self, val: &mut Val) {
237236
unsafe {
238-
phper_zval_copy_value(val.as_mut(), &mut self.inner);
237+
phper_zval_copy_value(val.as_mut_ptr(), self.as_mut_ptr());
239238
}
240239
}
241240
}
242-
243-
impl<T: SetVal + ?Sized> SetVal for Box<T> {
244-
fn set_val(&mut self, val: &mut Val) {
245-
T::set_val(self, val)
246-
}
247-
}
248-
249-
impl<T: SetVal + ?Sized> SetVal for &mut T {
250-
fn set_val(&mut self, val: &mut Val) {
251-
T::set_val(self, val)
252-
}
253-
}

0 commit comments

Comments
 (0)