Skip to content

Commit e7f4f6c

Browse files
committed
Pass the example hello tests temporarily.
1 parent 0fa3430 commit e7f4f6c

File tree

6 files changed

+55
-19
lines changed

6 files changed

+55
-19
lines changed

examples/hello/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub fn get_module() -> Module {
6464

6565
// register classes
6666
let mut foo_class = StdClass::new();
67-
foo_class.add_property("foo", 100.to_string());
67+
foo_class.add_property("foo", "100".to_string());
6868
foo_class.add_method(
6969
"getFoo",
7070
|this: &mut Object, _: &mut [Val]| -> phper::Result<Val> {

phper-sys/php_wrapper.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ void phper_array_init(zval *arg) {
8282
array_init(arg);
8383
}
8484

85+
void *phper_zend_hash_str_find_ptr(const HashTable *ht, const char *str, size_t len) {
86+
return zend_hash_str_find_ptr(ht, str, len);
87+
}
88+
89+
zval* phper_zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData) {
90+
return zend_hash_index_update(ht, h, pData);
91+
}
92+
8593
void phper_zend_hash_merge_with_key(HashTable *target, HashTable *source) {
8694
uint32_t idx;
8795
Bucket *p;
@@ -103,3 +111,7 @@ void phper_zend_hash_merge_with_key(HashTable *target, HashTable *source) {
103111
}
104112
}
105113
}
114+
115+
void phper_zval_obj(zval *z, zend_object *o) {
116+
ZVAL_OBJ(z, o);
117+
}

phper-sys/php_wrapper.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ void phper_zend_string_release(zend_string *s);
3535
void phper_zend_hash_str_update(HashTable *ht, const char *key, size_t len, zval *pData);
3636

3737
void phper_array_init(zval *arg);
38+
void *phper_zend_hash_str_find_ptr(const HashTable *ht, const char *str, size_t len);
39+
zval* phper_zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData);
3840
void phper_zend_hash_merge_with_key(HashTable *target, HashTable *source);
3941

42+
void phper_zval_obj(zval *z, zend_object *o);
43+
4044
#endif //PHPER_PHP_WRAPPER_H

phper/src/classes.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
functions::{Argument, Callable, FunctionEntity, FunctionEntry, Method},
33
sys::*,
4-
values::Val,
4+
utils::ensure_end_with_zero,
55
};
66
use once_cell::sync::OnceCell;
77
use std::{
@@ -138,12 +138,12 @@ impl ClassEntity {
138138
pub(crate) unsafe fn declare_properties(&mut self) {
139139
let properties = self.class.properties();
140140
for property in properties {
141-
let mut val = Val::new(&*property.value);
142-
zend_declare_property(
141+
let value = ensure_end_with_zero(property.value.clone());
142+
zend_declare_property_string(
143143
self.entry.load(Ordering::SeqCst).cast(),
144144
property.name.as_ptr().cast(),
145145
property.name.len(),
146-
val.as_mut_ptr(),
146+
value.as_ptr().cast(),
147147
Visibility::Public as c_int,
148148
);
149149
}
@@ -190,7 +190,7 @@ pub enum Visibility {
190190
pub(crate) fn get_global_class_entry_ptr(name: impl AsRef<str>) -> *mut zend_class_entry {
191191
let name = name.as_ref();
192192
unsafe {
193-
zend_hash_str_find_ptr_lc(
193+
phper_zend_hash_str_find_ptr(
194194
compiler_globals.class_table,
195195
name.as_ptr().cast(),
196196
name.len(),

phper/src/objects.rs

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ use crate::{
44
values::{SetVal, Val},
55
ClassNotFoundError,
66
};
7-
use std::{mem::zeroed, ptr::null_mut};
7+
use std::{
8+
mem::{forget, zeroed},
9+
ptr::null_mut,
10+
};
811

912
/// Wrapper of [crate::sys::zend_object].
1013
#[repr(transparent)]
@@ -19,6 +22,7 @@ impl Object {
1922
let class_name = class_name.as_ref();
2023
let ce = get_global_class_entry_ptr(class_name);
2124
if ce.is_null() {
25+
forget(object);
2226
Err(ClassNotFoundError::new(class_name.to_string()))
2327
} else {
2428
zend_object_std_init(object.as_mut_ptr(), ce);
@@ -29,7 +33,7 @@ impl Object {
2933
}
3034

3135
pub fn new_std() -> Self {
32-
Self::new("stdClass").expect("stdClass not found")
36+
Self::new("stdclass").expect("stdClass not found")
3337
}
3438

3539
pub unsafe fn from_mut_ptr<'a>(ptr: *mut zend_object) -> &'a mut Object {
@@ -61,12 +65,13 @@ impl Object {
6165
null_mut(),
6266
)
6367
}
64-
6568
#[cfg(phper_major_version = "7")]
6669
{
70+
let mut zv = zeroed::<zval>();
71+
phper_zval_obj(&mut zv, self.as_ptr() as *mut _);
6772
zend_read_property(
6873
self.inner.ce,
69-
&self.inner as *const _ as *mut _,
74+
&mut zv,
7075
name.as_ptr().cast(),
7176
name.len(),
7277
false.into(),
@@ -82,13 +87,28 @@ impl Object {
8287
let name = name.as_ref();
8388
let mut val = Val::new(value);
8489
unsafe {
85-
zend_update_property(
86-
self.inner.ce,
87-
&mut self.inner as *mut _,
88-
name.as_ptr().cast(),
89-
name.len(),
90-
val.as_mut_ptr(),
91-
)
90+
#[cfg(phper_major_version = "8")]
91+
{
92+
zend_update_property(
93+
self.inner.ce,
94+
&mut self.inner,
95+
name.as_ptr().cast(),
96+
name.len(),
97+
val.as_mut_ptr(),
98+
)
99+
}
100+
#[cfg(phper_major_version = "7")]
101+
{
102+
let mut zv = zeroed::<zval>();
103+
phper_zval_obj(&mut zv, self.as_mut_ptr());
104+
zend_update_property(
105+
self.inner.ce,
106+
&mut zv,
107+
name.as_ptr().cast(),
108+
name.len(),
109+
val.as_mut_ptr(),
110+
)
111+
}
92112
}
93113
}
94114
}

phper/src/values.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl<T: SetVal> SetVal for Vec<T> {
312312
unsafe {
313313
phper_array_init(val.as_mut_ptr());
314314
for (k, v) in self.into_iter().enumerate() {
315-
zend_hash_index_update(
315+
phper_zend_hash_index_update(
316316
(*val.as_mut_ptr()).value.arr,
317317
k as u64,
318318
Val::new(v).as_mut_ptr(),
@@ -347,7 +347,7 @@ where
347347
phper_array_init(val.as_mut_ptr());
348348
for (k, v) in iterator.into_iter() {
349349
let k = k.as_ref();
350-
zend_hash_str_update(
350+
phper_zend_hash_str_update(
351351
(*val.as_mut_ptr()).value.arr,
352352
k.as_ptr().cast(),
353353
k.len(),

0 commit comments

Comments
 (0)