Skip to content

Commit 74f1316

Browse files
committed
Refactor propertys.
1 parent 6e5d260 commit 74f1316

File tree

6 files changed

+113
-93
lines changed

6 files changed

+113
-93
lines changed

phper/src/classes.rs

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use once_cell::sync::OnceCell;
1414
use std::{
1515
any::{Any, TypeId},
1616
marker::PhantomData,
17-
mem::{replace, size_of, zeroed, ManuallyDrop},
17+
mem::{size_of, zeroed, ManuallyDrop},
1818
os::raw::c_int,
1919
ptr::null_mut,
2020
sync::{
@@ -126,11 +126,11 @@ impl<T: Send + 'static> DynamicClass<T> {
126126
pub fn add_property<'a>(
127127
&mut self,
128128
name: impl ToString,
129-
vis: Visibility,
130-
value: impl Into<Scalar<'a>>,
129+
visibility: Visibility,
130+
value: impl Into<Scalar>,
131131
) {
132132
self.property_entities
133-
.push(PropertyEntity::new(name, vis, value));
133+
.push(PropertyEntity::new(name, visibility, value));
134134
}
135135

136136
pub fn extends(&mut self, name: impl ToString) {
@@ -285,14 +285,7 @@ impl ClassEntity {
285285
pub(crate) unsafe fn declare_properties(&mut self) {
286286
let properties = self.classifiable.properties();
287287
for property in properties {
288-
let val = replace(&mut property.value, None).unwrap();
289-
zend_declare_property(
290-
self.entry.load(Ordering::SeqCst).cast(),
291-
property.name.as_ptr().cast(),
292-
property.name.len(),
293-
EBox::into_raw(val).cast(),
294-
property.visibility as c_int,
295-
);
288+
property.declare(self.entry.load(Ordering::SeqCst).cast());
296289
}
297290
}
298291

@@ -328,19 +321,60 @@ impl ClassEntity {
328321
pub struct PropertyEntity {
329322
name: String,
330323
visibility: Visibility,
331-
value: Option<EBox<Val>>,
324+
value: Scalar,
332325
}
333326

334327
impl PropertyEntity {
335-
pub fn new<'a>(
336-
name: impl ToString,
337-
visibility: Visibility,
338-
value: impl Into<Scalar<'a>>,
339-
) -> Self {
328+
pub fn new(name: impl ToString, visibility: Visibility, value: impl Into<Scalar>) -> Self {
340329
Self {
341330
name: name.to_string(),
342331
visibility,
343-
value: Some(EBox::new(Val::new(value.into()))),
332+
value: value.into(),
333+
}
334+
}
335+
336+
pub(crate) fn declare(&self, ce: *mut zend_class_entry) {
337+
let name = self.name.as_ptr().cast();
338+
let name_length = self.name.len();
339+
let access_type = self.visibility as u32 as i32;
340+
341+
unsafe {
342+
match &self.value {
343+
Scalar::Null => {
344+
zend_declare_property_null(ce, name, name_length, access_type);
345+
}
346+
Scalar::Bool(b) => {
347+
zend_declare_property_bool(ce, name, name_length, *b as zend_long, access_type);
348+
}
349+
Scalar::I64(i) => {
350+
zend_declare_property_bool(ce, name, name_length, *i, access_type);
351+
}
352+
Scalar::F64(f) => {
353+
zend_declare_property_double(ce, name, name_length, *f, access_type);
354+
}
355+
Scalar::String(s) => {
356+
// If the `ce` is `ZEND_INTERNAL_CLASS`, then the `zend_string` is allocated
357+
// as persistent.
358+
zend_declare_property_stringl(
359+
ce,
360+
name,
361+
name_length,
362+
s.as_ptr().cast(),
363+
s.len(),
364+
access_type,
365+
);
366+
}
367+
Scalar::Bytes(b) => {
368+
zend_declare_property_stringl(
369+
ce,
370+
name,
371+
name_length,
372+
b.as_ptr().cast(),
373+
b.len(),
374+
access_type,
375+
);
376+
}
377+
}
344378
}
345379
}
346380
}

phper/src/ini.rs

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
//! Apis relate to [crate::sys::zend_ini_entry_def].
22
3-
use crate::{
4-
errors::Error::Type,
5-
sys::{
6-
phper_zend_ini_mh, zend_ini_entry_def, OnUpdateBool, OnUpdateLong, OnUpdateReal,
7-
OnUpdateString, PHP_INI_ALL, PHP_INI_PERDIR, PHP_INI_SYSTEM, PHP_INI_USER,
8-
},
3+
use crate::sys::{
4+
phper_zend_ini_mh, zend_ini_entry_def, OnUpdateBool, OnUpdateLong, OnUpdateReal,
5+
OnUpdateString, PHP_INI_ALL, PHP_INI_PERDIR, PHP_INI_SYSTEM, PHP_INI_USER,
96
};
107
use dashmap::DashMap;
11-
use derive_more::{From, TryInto};
128
use std::{
13-
any::{Any, TypeId},
14-
collections::HashMap,
15-
convert::TryFrom,
9+
any::TypeId,
1610
ffi::CStr,
17-
mem::{forget, size_of, transmute, transmute_copy, zeroed},
11+
mem::{size_of, zeroed},
1812
os::raw::{c_char, c_void},
1913
ptr::null_mut,
2014
str,
15+
sync::atomic::{AtomicBool, Ordering},
2116
};
2217

18+
static REGISTERED: AtomicBool = AtomicBool::new(false);
19+
20+
thread_local! {
21+
static INI_ENTITIES: DashMap<String, IniEntity> = DashMap::new();
22+
}
23+
2324
pub struct Ini;
2425

2526
impl Ini {
26-
// TODO Remove thread_local.
27-
thread_local! {
28-
static REGISTERED: bool = false;
29-
static INI_ENTITIES: DashMap<String, IniEntity> = DashMap::new();
30-
}
31-
3227
pub fn add(name: impl ToString, default_value: impl TransformIniValue, policy: Policy) {
33-
Self::INI_ENTITIES.with(|ini_entities| {
28+
assert!(
29+
!REGISTERED.load(Ordering::SeqCst),
30+
"shouldn't add ini after registered"
31+
);
32+
33+
INI_ENTITIES.with(|ini_entities| {
3434
ini_entities.insert(
3535
name.to_string(),
3636
IniEntity::new(name, default_value, policy),
@@ -39,17 +39,24 @@ impl Ini {
3939
}
4040

4141
pub fn get<T: TransformIniValue>(name: &str) -> Option<T> {
42-
Self::INI_ENTITIES.with(|ini_entities| {
42+
assert!(
43+
REGISTERED.load(Ordering::SeqCst),
44+
"shouldn't get ini before registered"
45+
);
46+
47+
INI_ENTITIES.with(|ini_entities| {
4348
ini_entities
4449
.get(name)
4550
.and_then(|entity| entity.value().value())
4651
})
4752
}
4853

4954
pub(crate) unsafe fn entries() -> *const zend_ini_entry_def {
55+
REGISTERED.store(true, Ordering::SeqCst);
56+
5057
let mut entries = Vec::new();
5158

52-
Self::INI_ENTITIES.with(|ini_entities| {
59+
INI_ENTITIES.with(|ini_entities| {
5360
for mut entity in ini_entities.iter_mut() {
5461
entries.push(entity.value_mut().entry());
5562
}
@@ -204,15 +211,13 @@ impl IniEntity {
204211
}
205212

206213
pub(crate) fn entry(&mut self) -> zend_ini_entry_def {
207-
unsafe {
208-
create_ini_entry_ex(
209-
&self.name,
210-
&self.default_value,
211-
self.transform.on_modify(),
212-
self.policy as u32,
213-
&mut self.value as *mut _ as *mut c_void,
214-
)
215-
}
214+
create_ini_entry_ex(
215+
&self.name,
216+
&self.default_value,
217+
self.transform.on_modify(),
218+
self.policy as u32,
219+
&mut self.value as *mut _ as *mut c_void,
220+
)
216221
}
217222
}
218223

@@ -236,14 +241,8 @@ pub(crate) fn create_ini_entry_ex(
236241
))]
237242
let (modifiable, name_length) = (modifiable as std::os::raw::c_int, name.len() as u32);
238243

239-
let name = name.to_string();
240-
let boxed_name = name.into_boxed_str();
241-
let name = boxed_name.as_ptr().cast();
242-
forget(boxed_name);
243-
244244
zend_ini_entry_def {
245-
// name: name.as_ptr().cast(),
246-
name,
245+
name: name.as_ptr().cast(),
247246
on_modify,
248247
mh_arg1: null_mut(),
249248
mh_arg2: arg2,

phper/src/modules.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@ use crate::{
44
c_str_ptr,
55
classes::{ClassEntity, Classifiable},
66
functions::{Argument, Function, FunctionEntity},
7-
ini::{Ini, IniEntity, Policy, TransformIniValue},
7+
ini::Ini,
88
sys::*,
99
utils::ensure_end_with_zero,
1010
values::{SetVal, Val},
1111
};
1212
use std::{
13-
borrow::BorrowMut,
14-
cell::RefCell,
15-
collections::HashMap,
16-
convert::TryFrom,
1713
mem::{size_of, zeroed},
1814
os::raw::{c_int, c_uchar, c_uint, c_ushort},
1915
ptr::{null, null_mut},
@@ -221,13 +217,13 @@ impl ModuleArgs {
221217

222218
pub(crate) fn register_ini_entries(&self, ini_entries: *const zend_ini_entry_def) {
223219
unsafe {
224-
// zend_register_ini_entries(ini_entries, self.module_number);
220+
zend_register_ini_entries(ini_entries, self.module_number);
225221
}
226222
}
227223

228224
pub(crate) fn unregister_ini_entries(&self) {
229225
unsafe {
230-
// zend_unregister_ini_entries(self.module_number);
226+
zend_unregister_ini_entries(self.module_number);
231227
}
232228
}
233229
}

phper/src/types.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,29 @@ pub(crate) fn get_type_by_const(t: u32) -> crate::Result<String> {
100100
}
101101

102102
#[derive(From)]
103-
pub enum Scalar<'a> {
104-
Null(()),
103+
pub enum Scalar {
104+
Null,
105105
Bool(bool),
106-
I32(i32),
107-
U32(u32),
108106
I64(i64),
109107
F64(f64),
110-
Str(&'a str),
111108
String(String),
112-
Byte(&'a [u8]),
113109
Bytes(Vec<u8>),
114110
}
111+
112+
impl From<i32> for Scalar {
113+
fn from(i: i32) -> Self {
114+
Self::I64(i.into())
115+
}
116+
}
117+
118+
impl From<&str> for Scalar {
119+
fn from(s: &str) -> Self {
120+
Self::String(s.to_owned())
121+
}
122+
}
123+
124+
impl From<&[u8]> for Scalar {
125+
fn from(b: &[u8]) -> Self {
126+
Self::Bytes(b.to_owned())
127+
}
128+
}

phper/src/values.rs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
objects::{Object, StatelessObject},
99
strings::ZendString,
1010
sys::*,
11-
types::{get_type_by_const, Scalar, Type},
11+
types::{get_type_by_const, Type},
1212
utils::ensure_end_with_zero,
1313
};
1414
use indexmap::map::IndexMap;
@@ -426,20 +426,3 @@ impl SetVal for Val {
426426
phper_zval_copy(val.as_mut_ptr(), self.as_mut_ptr());
427427
}
428428
}
429-
430-
impl SetVal for Scalar<'_> {
431-
unsafe fn set_val(self, val: &mut Val) {
432-
match self {
433-
Scalar::Null(_) => SetVal::set_val((), val),
434-
Scalar::Bool(b) => SetVal::set_val(b, val),
435-
Scalar::I32(i) => SetVal::set_val(i, val),
436-
Scalar::U32(u) => SetVal::set_val(u, val),
437-
Scalar::I64(i) => SetVal::set_val(i, val),
438-
Scalar::F64(f) => SetVal::set_val(f, val),
439-
Scalar::Str(s) => SetVal::set_val(s, val),
440-
Scalar::String(s) => SetVal::set_val(s, val),
441-
Scalar::Byte(b) => SetVal::set_val(b, val),
442-
Scalar::Bytes(b) => SetVal::set_val(b, val),
443-
}
444-
}
445-
}

tests/integration/src/arguments.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
use phper::{
2-
alloc::EBox,
3-
arrays::Array,
4-
functions::Argument,
5-
ini::{Ini, Policy},
6-
modules::Module,
7-
objects::Object,
8-
values::Val,
2+
alloc::EBox, arrays::Array, functions::Argument, modules::Module, objects::Object, values::Val,
93
};
104

115
pub fn integrate(module: &mut Module) {

0 commit comments

Comments
 (0)