Skip to content

Commit 5338356

Browse files
committed
added tests and fix minor bugs
1 parent 01fdd07 commit 5338356

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

src/types/zval.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -723,13 +723,16 @@ impl<'a> FromZvalMut<'a> for &'a [&'a Zval] {
723723
const TYPE: DataType = DataType::Array;
724724

725725
fn from_zval_mut(zval: &'a mut Zval) -> Option<Self> {
726+
let mut slice: Vec<&'a Zval> = Vec::new();
727+
726728
// Check if the input Zval is an array and convert it into a slice of references
727729
if let Some(a) = zval.array() {
728730
// Collect references to each element in the array
729-
let slice: Vec<&'a Zval> = a.values().collect();
730-
Some(Box::leak(slice.into_boxed_slice()))
731+
slice = a.values().collect();
731732
} else {
732-
None
733+
slice.push(zval);
733734
}
735+
736+
Some(Box::leak(slice.into_boxed_slice()))
734737
}
735738
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
require "_utils.php";
4+
5+
$a = 'a';
6+
$b = 'b';
7+
$c = 'c';
8+
9+
// Passing arguments as references
10+
$args = test_variadic_optional_args();
11+
assert(count($args) === 0, 'Expected no arguments to be returned');
12+
13+
$args = test_variadic_optional_args($a);
14+
assert(count($args) === 1, 'Expected to have 1 argument');
15+
16+
$args = test_variadic_optional_args($a, $b, $c);
17+
assert(count($args) === 3, 'Expected to have 3 argument');
18+
assert($args[1] === $b, 'Expected second argument to have a value of \$b aka "b"');
19+
assert($args[2] === $c, 'Expected third argument to have a value of \$c aka "c"');
20+
21+
// Must have arguments.. so catch ArgumentCountError errors!
22+
assert_exception_thrown('test_variadic_args');
23+
// try {
24+
// $args = test_variadic_args();
25+
// } catch (ArgumentCountError $e) {
26+
// var_dump($e->getMessage());
27+
// }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[test]
2+
fn test_variadic_optional_args() {
3+
assert!(crate::integration::run_php("variadic_args.php"));
4+
}

tests/src/lib.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#![cfg_attr(windows, feature(abi_vectorcall))]
2-
use ext_php_rs::{binary::Binary, prelude::*, types::ZendObject, types::Zval};
2+
use ext_php_rs::{
3+
binary::Binary,
4+
prelude::*,
5+
types::{ZendObject, Zval},
6+
};
37
use std::collections::HashMap;
48

59
#[php_function]
@@ -72,6 +76,28 @@ pub fn test_callable(call: ZendCallable, a: String) -> Zval {
7276
call.try_call(vec![&a]).expect("Failed to call function")
7377
}
7478

79+
// Rust type &[&Zval] must be converted because to Vec<Zval> because of
80+
// lifetime hell.
81+
#[php_function(optional = "params")]
82+
pub fn test_variadic_optional_args(params: &[&Zval]) -> Vec<Zval> {
83+
params.iter().map(|x| x.shallow_clone()).collect()
84+
}
85+
86+
#[php_function]
87+
pub fn test_variadic_args(params: &[&Zval]) -> Vec<Zval> {
88+
params.iter().map(|x| x.shallow_clone()).collect()
89+
}
90+
91+
// Rust type &[&Zval] must be converted because to Vec<Zval> because
92+
// of lifetime hell... #[php_function] does not support lifetime.
93+
// error1: error[E0261]: use of undeclared lifetime name `'a`
94+
// error2: lifetime `'a` is missing in item created through this procedural
95+
// macro #[php_function]
96+
// pub fn test_variadic_optional2_args<'a>(params: &'a [&'a Zval]) -> &'a [&'a
97+
// Zval] { println!("Hiero: {:#?}", params);
98+
// params
99+
// }
100+
75101
#[php_class]
76102
pub struct TestClass {
77103
string: String,
@@ -184,4 +210,5 @@ mod integration {
184210
mod object;
185211
mod string;
186212
mod types;
213+
mod variadic_args;
187214
}

0 commit comments

Comments
 (0)