Skip to content

Commit 62a43e6

Browse files
Added Debug and Clone implementations (#30)
* Implemented Debug and Clone for most types * Added `throw!` macro to throw and return
1 parent f11451f commit 62a43e6

File tree

10 files changed

+103
-25
lines changed

10 files changed

+103
-25
lines changed

example/skel/src/lib.rs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub extern "C" fn module_init(_type: i32, module_number: i32) -> i32 {
118118
#[no_mangle]
119119
pub extern "C" fn get_module() -> *mut ext_php_rs::php::module::ModuleEntry {
120120
let funct = FunctionBuilder::new("skeleton_version", skeleton_version)
121-
.arg(Arg::new("a", DataType::Long))
121+
.arg(Arg::new("a", DataType::Array))
122122
.arg(Arg::new("b", DataType::Double))
123123
.not_required()
124124
.arg(Arg::new("c", DataType::Double))
@@ -144,32 +144,14 @@ pub extern "C" fn get_module() -> *mut ext_php_rs::php::module::ModuleEntry {
144144
}
145145

146146
#[no_mangle]
147-
pub extern "C" fn skeleton_version(execute_data: &mut ExecutionData, _retval: &mut Zval) {
148-
let mut x = Arg::new("x", DataType::Long);
147+
pub extern "C" fn skeleton_version(execute_data: &mut ExecutionData, retval: &mut Zval) {
148+
let mut x = Arg::new("x", DataType::Array);
149149
let mut y = Arg::new("y", DataType::Double);
150150
let mut z = Arg::new("z", DataType::Double);
151151

152-
let result = ArgParser::new(execute_data)
153-
.arg(&mut x)
154-
.arg(&mut y)
155-
.not_required()
156-
.arg(&mut z)
157-
.parse();
158-
159-
if result.is_err() {
160-
return;
161-
}
162-
163-
throw(ClassEntry::exception(), "Hello!");
164-
165-
let result = format!(
166-
"x: {}, y: {}, z: {}",
167-
x.val::<ZendLong>().unwrap_or_default(),
168-
y.val::<f64>().unwrap_or_default(),
169-
z.val::<f64>().unwrap_or_default()
170-
);
171-
172-
_retval.set_string(result);
152+
parse_args!(execute_data, x, y; z);
153+
dbg!(x);
154+
retval.set_string("Hello");
173155
}
174156

175157
#[no_mangle]

example/skel/test.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<?php
22

3+
var_dump(skeleton_version(['world' => 'hello', 1, 3],2.1123));
4+
die;
5+
36
var_dump(SKEL_TEST_CONST, SKEL_TEST_LONG_CONST);
47
var_dump(test_array());
58
die;

src/macros.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,31 @@ macro_rules! parse_args {
131131
}
132132
}};
133133
}
134+
135+
/// Throws an exception and returns from the current function.
136+
///
137+
/// Wraps the [`throw`] function by inserting a `return` statement after throwing the exception.
138+
///
139+
/// [`throw`]: crate::php::exceptions::throw
140+
///
141+
/// # Examples
142+
///
143+
/// ```
144+
/// use ext_php_rs::{throw, php::{class::ClassEntry, execution_data::ExecutionData, types::zval::Zval}};
145+
///
146+
/// pub extern "C" fn example_fn(execute_data: &mut ExecutionData, _: &mut Zval) {
147+
/// let something_wrong = true;
148+
/// if something_wrong {
149+
/// throw!(ClassEntry::exception(), "Something is wrong!");
150+
/// }
151+
///
152+
/// assert!(false); // This will not run.
153+
/// }
154+
/// ```
155+
#[macro_export]
156+
macro_rules! throw {
157+
($ex: expr, $reason: expr) => {
158+
$crate::php::exceptions::throw($ex, $reason);
159+
return;
160+
};
161+
}

src/php/args.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::{
1616
};
1717

1818
/// Represents an argument to a function.
19+
#[derive(Debug, Clone)]
1920
pub struct Arg<'a> {
2021
pub(crate) name: String,
2122
pub(crate) _type: DataType,

src/php/function.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub type FunctionHandler = extern "C" fn(execute_data: &mut ExecutionData, retva
4040
type FunctionPointerHandler = extern "C" fn(execute_data: *mut ExecutionData, retval: *mut Zval);
4141

4242
/// Builds a function to be exported as a PHP function.
43+
#[derive(Debug, Clone)]
4344
pub struct FunctionBuilder<'a> {
4445
function: FunctionEntry,
4546
args: Vec<Arg<'a>>,

src/php/module.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub type InfoFunc = extern "C" fn(zend_module: *mut ModuleEntry);
4242
/// .into_raw()
4343
/// }
4444
/// ```
45+
#[derive(Debug, Clone)]
4546
pub struct ModuleBuilder {
4647
module: ModuleEntry,
4748
functions: Vec<FunctionEntry>,

src/php/types/array.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use std::{
55
collections::HashMap,
66
convert::{TryFrom, TryInto},
7+
fmt::Debug,
78
u64,
89
};
910

@@ -227,6 +228,17 @@ impl ZendHashTable {
227228
}
228229
}
229230

231+
impl Debug for ZendHashTable {
232+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
233+
f.debug_map()
234+
.entries(
235+
self.into_iter()
236+
.map(|(k, k2, v)| (k2.unwrap_or_else(|| k.to_string()), v)),
237+
)
238+
.finish()
239+
}
240+
}
241+
230242
impl Default for ZendHashTable {
231243
fn default() -> Self {
232244
Self::new()

src/php/types/object.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! allowing users to store Rust data inside a PHP object.
33
44
use std::{
5+
fmt::Debug,
56
mem,
67
ops::{Deref, DerefMut},
78
};
@@ -95,6 +96,12 @@ impl<T: Default> ZendClassObject<T> {
9596
}
9697
}
9798

99+
impl<T: Default + Debug> Debug for ZendClassObject<T> {
100+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
101+
self.obj.fmt(f)
102+
}
103+
}
104+
98105
impl<T: Default> Deref for ZendClassObject<T> {
99106
type Target = T;
100107

src/php/types/string.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//! contains the length of the string, meaning the string can contain the NUL character.
33
44
use core::slice;
5+
use std::fmt::Debug;
56

67
use crate::{
78
bindings::{ext_php_rs_zend_string_init, zend_string, zend_string_init_interned},
@@ -49,6 +50,13 @@ impl ZendString {
4950
}
5051
}
5152

53+
impl Debug for ZendString {
54+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55+
let s: String = self.into();
56+
s.fmt(f)
57+
}
58+
}
59+
5260
impl From<&ZendString> for String {
5361
fn from(zs: &ZendString) -> Self {
5462
let len = zs.len;

src/php/types/zval.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! determined by a property inside the struct. The content of the Zval is stored in a union.
33
44
use core::slice;
5-
use std::{convert::TryFrom, ptr};
5+
use std::{convert::TryFrom, fmt::Debug, ptr};
66

77
use crate::{
88
bindings::{
@@ -365,6 +365,41 @@ impl<'a> Zval {
365365
}
366366
}
367367

368+
impl Debug for Zval {
369+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
370+
let mut dbg = f.debug_struct("Zval");
371+
let ty = self.get_type();
372+
dbg.field("type", &ty);
373+
374+
if let Ok(ty) = ty {
375+
macro_rules! field {
376+
($value: expr) => {
377+
dbg.field("val", &$value)
378+
};
379+
}
380+
381+
match ty {
382+
DataType::Undef => field!("Undefined"),
383+
DataType::Null => field!("Null"),
384+
DataType::False => field!(false),
385+
DataType::True => field!(true),
386+
DataType::Long => field!(self.long()),
387+
DataType::Double => field!(self.double()),
388+
DataType::String => field!(self.string()),
389+
DataType::Array => field!(self.array()),
390+
DataType::Object => field!(self.object()),
391+
DataType::Resource => field!(self.resource()),
392+
DataType::Reference => field!(self.reference()),
393+
DataType::Callable => field!(self.string()),
394+
DataType::ConstantExpression => field!("Constant Expression"),
395+
DataType::Void => field!("Void"),
396+
};
397+
}
398+
399+
dbg.finish()
400+
}
401+
}
402+
368403
#[macro_use]
369404
macro_rules! try_from_zval {
370405
($type: ty, $fn: ident) => {

0 commit comments

Comments
 (0)