Skip to content

Commit 96555c4

Browse files
authored
fix: fix object access in object write/read/has_property handlers
Partially derived from #313, this commit fixes access to object properties from Rust code. This is needed to correctly initialise exception objects. Refs: #313, #438
1 parent 5aa2145 commit 96555c4

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

src/zend/handlers.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl ZendObjectHandlers {
9292
let prop_name = member
9393
.as_ref()
9494
.ok_or("Invalid property name pointer given")?;
95-
let self_ = &mut **obj;
95+
let self_ = &mut *obj;
9696
let props = T::get_metadata().get_properties();
9797
let prop = props.get(prop_name.as_str()?);
9898

@@ -141,7 +141,7 @@ impl ZendObjectHandlers {
141141
let prop_name = member
142142
.as_ref()
143143
.ok_or("Invalid property name pointer given")?;
144-
let self_ = &mut **obj;
144+
let self_ = &mut *obj;
145145
let props = T::get_metadata().get_properties();
146146
let prop = props.get(prop_name.as_str()?);
147147
let value_mut = value.as_mut().ok_or("Invalid return zval given")?;
@@ -178,7 +178,7 @@ impl ZendObjectHandlers {
178178
.as_mut()
179179
.and_then(|obj| ZendClassObject::<T>::from_zend_obj_mut(obj))
180180
.ok_or("Invalid object pointer given")?;
181-
let self_ = &mut **obj;
181+
let self_ = &mut *obj;
182182
let struct_props = T::get_metadata().get_properties();
183183

184184
for (name, val) in struct_props {
@@ -230,7 +230,7 @@ impl ZendObjectHandlers {
230230
.ok_or("Invalid property name pointer given")?;
231231
let props = T::get_metadata().get_properties();
232232
let prop = props.get(prop_name.as_str()?);
233-
let self_ = &mut **obj;
233+
let self_ = &mut *obj;
234234

235235
match has_set_exists {
236236
//
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
require(__DIR__ . '/../_utils.php');
4+
5+
assert_exception_thrown(fn() => throw_default_exception(), \Exception::class);
6+
7+
try {
8+
throw_custom_exception();
9+
} catch (\Throwable $e) {
10+
// Check if object is initiated
11+
assert($e instanceof \Test\TestException);
12+
assert("Not good custom!" === $e->getMessage());
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use ext_php_rs::{prelude::*, zend::ce};
2+
3+
#[php_class]
4+
#[php(name = "Test\\TestException")]
5+
#[php(extends(ce = ce::exception, stub = "\\Exception"))]
6+
#[derive(Debug)]
7+
pub struct TestException;
8+
9+
#[php_function]
10+
pub fn throw_custom_exception() -> PhpResult<i32> {
11+
Err(PhpException::from_class::<TestException>(
12+
"Not good custom!".into(),
13+
))
14+
}
15+
16+
#[php_function]
17+
pub fn throw_default_exception() -> PhpResult<i32> {
18+
Err(PhpException::default("Not good!".into()))
19+
}
20+
21+
pub fn build_module(builder: ModuleBuilder) -> ModuleBuilder {
22+
builder
23+
.class::<TestException>()
24+
.function(wrap_function!(throw_default_exception))
25+
.function(wrap_function!(throw_custom_exception))
26+
}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
#[test]
31+
fn exception_works() {
32+
assert!(crate::integration::test::run_php("exception/exception.php"));
33+
}
34+
}

tests/src/integration/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod callable;
55
pub mod class;
66
pub mod closure;
77
pub mod defaults;
8+
pub mod exception;
89
pub mod globals;
910
pub mod iterator;
1011
pub mod magic_method;

tests/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub fn build_module(module: ModuleBuilder) -> ModuleBuilder {
1818
module = integration::class::build_module(module);
1919
module = integration::closure::build_module(module);
2020
module = integration::defaults::build_module(module);
21+
module = integration::exception::build_module(module);
2122
module = integration::globals::build_module(module);
2223
module = integration::iterator::build_module(module);
2324
module = integration::magic_method::build_module(module);

0 commit comments

Comments
 (0)