Skip to content

Commit 167605c

Browse files
committed
Remove generic type of Objects.
1 parent 5c93cd5 commit 167605c

File tree

4 files changed

+17
-18
lines changed

4 files changed

+17
-18
lines changed

phper/src/classes.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ use crate::{
1515
arrays::ZArr,
1616
errors::{ClassNotFoundError, InitializeObjectError, StateTypeError},
1717
functions::{Argument, Function, FunctionEntity, FunctionEntry, Method},
18-
objects::{ExtendObject, ZObj},
18+
objects::{ExtendObject, ZObj, ZObject},
1919
strings::ZStr,
2020
sys::*,
2121
types::Scalar,
2222
values::ZVal,
2323
};
2424
use dashmap::DashMap;
2525
use once_cell::sync::OnceCell;
26+
use phper_alloc::ToRefOwned;
2627
use std::{
2728
any::{Any, TypeId},
2829
convert::TryInto,
@@ -214,17 +215,15 @@ impl ClassEntry {
214215

215216
/// Create the object from class, without calling `__construct`, be careful
216217
/// when `__construct` is necessary.
217-
pub fn init_object(&self) -> crate::Result<ZObject> {
218+
pub(crate) fn init_object(&self) -> crate::Result<ZObject> {
218219
unsafe {
219220
let ptr = self.as_ptr() as *mut _;
220-
let mut val = ZVal::from(());
221+
let mut val = ZVal::default();
221222
if !phper_object_init_ex(val.as_mut_ptr(), ptr) {
222223
Err(InitializeObjectError::new(self.get_name().to_str()?.to_owned()).into())
223224
} else {
224-
let object = (*val.as_mut_ptr()).value.obj;
225-
forget(val);
226-
let object: EBox<ZObj> = EBox::from_raw(object.cast());
227-
Ok(object)
225+
let ptr = phper_z_obj_p(val.as_mut_ptr());
226+
Ok(ZObj::from_mut_ptr(ptr).to_ref_owned())
228227
}
229228
}
230229
}
@@ -283,7 +282,7 @@ impl ClassEntity {
283282
.parent()
284283
.map(|s| ClassEntry::from_globals(s).unwrap());
285284

286-
let class: *mut ClassEntry<()> = match parent {
285+
let class: *mut ClassEntry = match parent {
287286
Some(parent) => {
288287
zend_register_internal_class_ex(&mut class_ce, parent.as_ptr() as *mut _).cast()
289288
}

phper/src/functions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ where
9393
&self, execute_data: &mut ExecuteData, arguments: &mut [ZVal], return_value: &mut ZVal,
9494
) {
9595
unsafe {
96-
let this = execute_data.get_this::<T>().unwrap();
96+
let this = execute_data.get_this().unwrap();
9797
let r = (self.f)(this, arguments);
9898
*return_value = r.into();
9999
}
@@ -242,7 +242,7 @@ impl ZendFunction {
242242
}
243243
}
244244

245-
pub(crate) fn call<T: 'static>(
245+
pub(crate) fn call(
246246
&mut self, mut object: Option<&mut ZObj>, mut arguments: impl AsMut<[ZVal]>,
247247
) -> crate::Result<EBox<ZVal>> {
248248
let arguments = arguments.as_mut();
@@ -256,7 +256,7 @@ impl ZendFunction {
256256
let called_scope = unsafe {
257257
let mut called_scope = object
258258
.as_mut()
259-
.map(|o| o.get_class::<T>().as_ptr() as *mut zend_class_entry)
259+
.map(|o| o.get_class().as_ptr() as *mut zend_class_entry)
260260
.unwrap_or(null_mut());
261261
if called_scope.is_null() {
262262
called_scope = self.inner.common.scope;

phper/src/objects.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,13 @@ pub struct ZObject {
207207

208208
impl ZObject {
209209
/// Another way to new object like [crate::classes::ClassEntry::new_object].
210-
pub fn new(class_entry: &ClassEntry, arguments: &mut [ZVal]) -> crate::Result<EBox<Self>> {
210+
pub fn new(class_entry: &ClassEntry, arguments: &mut [ZVal]) -> crate::Result<Self> {
211211
class_entry.new_object(arguments)
212212
}
213213

214214
pub fn new_by_class_name(
215215
class_name: impl AsRef<str>, arguments: &mut [ZVal],
216-
) -> crate::Result<EBox<Self>> {
216+
) -> crate::Result<Self> {
217217
let class_entry = ClassEntry::from_globals(class_name)?;
218218
Self::new(class_entry, arguments)
219219
}

phper/src/values.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
classes::ClassEntry,
1717
errors::{ExpectTypeError, NotRefCountedTypeError, Throwable, TypeError},
1818
functions::{call_internal, ZendFunction},
19-
objects::ZObj,
19+
objects::{ZObj, ZObject},
2020
resources::ZRes,
2121
strings::{ZStr, ZString},
2222
sys::*,
@@ -98,8 +98,8 @@ impl ExecuteData {
9898
///
9999
/// The type of `T` should be careful.
100100
pub unsafe fn get_this(&mut self) -> Option<&mut ZObj> {
101-
let ptr = phper_get_this(&mut self.inner) as *mut ZVal;
102-
ptr.as_mut().map(|val| val.as_mut_z_obj())
101+
let val = ZVal::from_mut_ptr(phper_get_this(&mut self.inner));
102+
val.as_mut_z_obj()
103103
}
104104

105105
/// TODO Do not return owned object, because usually Val should not be drop.
@@ -499,8 +499,8 @@ impl From<ZArray> for ZVal {
499499
}
500500
}
501501

502-
impl From<ZObj> for ZVal {
503-
fn from(mut obj: ZObj) -> Self {
502+
impl From<ZObject> for ZVal {
503+
fn from(mut obj: ZObject) -> Self {
504504
unsafe {
505505
let mut val = MaybeUninit::<ZVal>::uninit();
506506
phper_zval_obj(obj.as_mut_ptr().cast(), obj.into_raw());

0 commit comments

Comments
 (0)