Skip to content

Commit 97172d1

Browse files
committed
Modify some values apis.
1 parent 2b878ab commit 97172d1

File tree

12 files changed

+147
-81
lines changed

12 files changed

+147
-81
lines changed

examples/hello/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
use phper::{
2+
alloc::EBox,
23
arrays::Array,
34
classes::StdClass,
45
functions::Argument,
56
ini::Policy,
67
modules::{Module, ModuleArgs},
78
objects::Object,
89
php_get_module,
9-
values::{SetVal, Val},
10+
values::Val,
1011
};
1112

1213
fn module_init(_args: ModuleArgs) -> bool {
@@ -56,7 +57,7 @@ pub fn get_module() -> Module {
5657
let mut hello_description = Val::new(Module::get_str_ini("hello.description"));
5758
arr.insert("hello.description", hello_description);
5859

59-
arr
60+
EBox::new(arr)
6061
},
6162
vec![],
6263
);

examples/http-client/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub fn get_module() -> Module {
1111
env!("CARGO_PKG_AUTHORS"),
1212
);
1313

14-
let client = HttpClient::new();
14+
// let client = HttpClient::new();
1515
let client_class = StdClass::new();
1616
module.add_class("HttpClient", client_class);
1717

examples/log/tests/integration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use phper_test::test_php_scripts_with_condition;
2-
use std::{env, path::Path, process::Output, str};
2+
use std::{env, path::Path, str};
33

44
#[test]
55
fn test_php() {

phper-alloc/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ impl<T> EBox<T> {
2929
}
3030
}
3131

32+
pub unsafe fn from_raw(raw: *mut T) -> Self {
33+
Self { ptr: raw }
34+
}
35+
3236
pub fn into_raw(b: EBox<T>) -> *mut T {
3337
let ptr = b.ptr;
3438
forget(b);

phper/src/arrays.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ impl Array {
1010
pub fn new() -> Self {
1111
unsafe {
1212
let mut array = zeroed::<Array>();
13-
_zend_hash_init(&mut array.inner, 0, None, true.into());
13+
_zend_hash_init(array.as_mut_ptr(), 0, None, false.into());
1414
array
1515
}
1616
}
1717

18-
pub(crate) unsafe fn from_raw<'a>(ptr: *mut zend_array) -> &'a mut Array {
18+
pub(crate) unsafe fn from_mut_ptr<'a>(ptr: *mut zend_array) -> &'a mut Array {
1919
let ptr = ptr as *mut Array;
2020
ptr.as_mut().expect("ptr shouldn't be null")
2121
}

phper/src/classes.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
use crate::{
2-
arrays::Array,
32
functions::{Argument, Callable, FunctionEntity, FunctionEntry, Method},
43
sys::*,
5-
utils::ensure_end_with_zero,
6-
values::SetVal,
4+
values::{SetVal, Val},
75
};
86
use once_cell::sync::OnceCell;
97
use std::{
108
mem::zeroed,
119
os::raw::c_int,
12-
ptr::{null, null_mut},
10+
ptr::null_mut,
11+
rc::Rc,
1312
sync::atomic::{AtomicPtr, Ordering},
1413
};
1514

@@ -175,13 +174,15 @@ impl ClassEntity {
175174
pub struct PropertyEntity {
176175
name: String,
177176
value: Box<dyn SetVal + Send + Sync>,
177+
_x: Val,
178178
}
179179

180180
impl PropertyEntity {
181181
pub fn new(name: impl ToString, value: impl SetVal + Send + Sync + 'static) -> Self {
182182
Self {
183183
name: name.to_string(),
184184
value: Box::new(value),
185+
_x: Val::null(),
185186
}
186187
}
187188
}

phper/src/functions.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
use std::{
2-
mem::zeroed,
3-
os::raw::c_char,
4-
sync::atomic::{AtomicPtr, Ordering},
5-
};
1+
use std::{mem::zeroed, os::raw::c_char, sync::atomic::AtomicPtr};
62

73
use crate::{
84
classes::ClassEntry,
@@ -37,7 +33,7 @@ where
3733
R: SetVal,
3834
{
3935
fn call(&self, this: &mut Object, arguments: &mut [Val], return_value: &mut Val) {
40-
let mut r = self(this, arguments);
36+
let r = self(this, arguments);
4137
r.set_val(return_value);
4238
}
4339
}
@@ -177,10 +173,10 @@ pub(crate) unsafe extern "C" fn invoke(
177173
Callable::Function(f) => {
178174
f.call(&mut arguments, return_value);
179175
}
180-
Callable::Method(m, class) => {
181-
let mut this = execute_data.get_this();
176+
Callable::Method(m, _class) => {
177+
let this = execute_data.get_this();
182178
let this = this.as_mut().expect("this should not be null");
183-
assert!(this.is_object());
179+
assert!(this.get_type().is_object());
184180
m.call(
185181
Object::from_mut_ptr(this.inner.value.obj),
186182
&mut arguments,

phper/src/objects.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
use crate::{
2-
classes::{get_global_class_entry_ptr, ClassEntry},
2+
classes::get_global_class_entry_ptr,
33
sys::*,
44
values::{SetVal, Val},
55
ClassNotFoundError,
66
};
7-
use std::{
8-
mem::zeroed,
9-
ptr::null_mut,
10-
sync::atomic::{AtomicPtr, Ordering},
11-
};
7+
use std::{mem::zeroed, ptr::null_mut};
128

139
/// Wrapper of [crate::sys::zend_object].
1410
#[repr(transparent)]

phper/src/strings.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
use crate::sys::*;
22

3+
/// Wrapper of [crate::sys::zend_string].
34
#[repr(transparent)]
45
pub struct ZendString {
56
inner: zend_string,
67
}
8+
9+
impl ZendString {
10+
pub(crate) unsafe fn from_mut_ptr<'a>(ptr: *mut zend_array) -> &'a mut Self {
11+
let ptr = ptr as *mut Self;
12+
ptr.as_mut().expect("ptr shouldn't be null")
13+
}
14+
15+
pub fn as_ptr(&self) -> *const zend_string {
16+
&self.inner
17+
}
18+
19+
pub fn as_mut_ptr(&mut self) -> *mut zend_string {
20+
&mut self.inner
21+
}
22+
}

phper/src/types.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::sys::*;
22
use num_traits::cast::FromPrimitive;
33
use std::{ffi::CStr, os::raw::c_int};
44

5-
#[derive(FromPrimitive, PartialEq)]
5+
#[derive(FromPrimitive, PartialEq, Clone, Copy)]
66
#[repr(u32)]
77
#[non_exhaustive]
88
pub enum Type {
@@ -24,6 +24,53 @@ pub enum Type {
2424
IsCallable = IS_CALLABLE,
2525
}
2626

27+
impl Type {
28+
#[inline]
29+
pub fn is_null(self) -> bool {
30+
self == Type::Null
31+
}
32+
33+
#[inline]
34+
pub fn is_bool(self) -> bool {
35+
matches!(self, Type::True | Type::False)
36+
}
37+
38+
#[inline]
39+
pub fn is_true(self) -> bool {
40+
self == Type::True
41+
}
42+
43+
#[inline]
44+
pub fn is_false(self) -> bool {
45+
self == Type::False
46+
}
47+
48+
#[inline]
49+
pub fn is_long(self) -> bool {
50+
self == Type::Long
51+
}
52+
53+
#[inline]
54+
pub fn is_double(self) -> bool {
55+
self == Type::Double
56+
}
57+
58+
#[inline]
59+
pub fn is_string(self) -> bool {
60+
matches!(self, Type::String | Type::StringEx)
61+
}
62+
63+
#[inline]
64+
pub fn is_array(self) -> bool {
65+
matches!(self, Type::Array | Type::ArrayEx)
66+
}
67+
68+
#[inline]
69+
pub fn is_object(self) -> bool {
70+
matches!(self, Type::Object | Type::ObjectEx)
71+
}
72+
}
73+
2774
impl From<u32> for Type {
2875
fn from(n: u32) -> Self {
2976
match FromPrimitive::from_u32(n) {

0 commit comments

Comments
 (0)