1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
use crate::{
alloc::{EAllocatable, EBox},
arrays::Array,
classes::ClassEntry,
errors::{CallFunctionError, NotRefCountedTypeError, Throwable, TypeError},
functions::ZendFunction,
objects::Object,
strings::ZendString,
sys::*,
types::Type,
utils::ensure_end_with_zero,
};
use indexmap::map::IndexMap;
use std::{
collections::{BTreeMap, HashMap},
mem::{transmute, zeroed},
ptr::null_mut,
str,
str::Utf8Error,
};
#[repr(transparent)]
pub struct ExecuteData {
inner: zend_execute_data,
}
impl ExecuteData {
#[inline]
pub unsafe fn from_mut_ptr<'a>(ptr: *mut zend_execute_data) -> &'a mut Self {
&mut *(ptr as *mut Self)
}
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut zend_execute_data {
&mut self.inner
}
#[inline]
pub unsafe fn common_num_args(&self) -> u32 {
(*self.inner.func).common.num_args
}
#[inline]
pub unsafe fn common_required_num_args(&self) -> u16 {
(*self.inner.func).common.required_num_args as u16
}
#[inline]
pub unsafe fn common_arg_info(&self) -> *mut zend_arg_info {
(*self.inner.func).common.arg_info
}
#[inline]
pub unsafe fn num_args(&self) -> u16 {
self.inner.This.u2.num_args as u16
}
pub unsafe fn func(&self) -> &ZendFunction {
ZendFunction::from_mut_ptr(self.inner.func)
}
pub unsafe fn get_this<T>(&mut self) -> Option<&mut Object<T>> {
let ptr = phper_get_this(&mut self.inner) as *mut Val;
ptr.as_ref().map(|val| val.as_mut_object_unchecked())
}
pub(crate) unsafe fn get_parameters_array(&mut self) -> Vec<Val> {
let num_args = self.num_args();
let mut arguments = vec![zeroed::<zval>(); num_args as usize];
if num_args > 0 {
_zend_get_parameters_array_ex(num_args.into(), arguments.as_mut_ptr());
}
transmute(arguments)
}
}
#[repr(transparent)]
pub struct Val {
inner: zval,
}
impl Val {
pub fn new(t: impl SetVal) -> Self {
let mut val = unsafe { zeroed::<Val>() };
unsafe {
SetVal::set_val(t, &mut val);
}
val
}
pub fn undef() -> Self {
let mut val = unsafe { zeroed::<Val>() };
val.set_type(Type::undef());
val
}
pub fn null() -> Self {
Self::new(())
}
pub fn set(&mut self, t: impl SetVal) {
unsafe {
self.drop_value();
SetVal::set_val(t, self);
}
}
pub unsafe fn from_mut_ptr<'a>(ptr: *mut zval) -> &'a mut Self {
assert!(!ptr.is_null(), "ptr should not be null");
&mut *(ptr as *mut Self)
}
#[inline]
pub fn as_ptr(&self) -> *const zval {
&self.inner
}
#[inline]
pub fn as_mut_ptr(&mut self) -> *mut zval {
&mut self.inner
}
pub fn get_type(&self) -> Type {
let t = unsafe { self.inner.u1.type_info };
t.into()
}
pub fn into_inner(self) -> zval {
self.inner
}
fn get_type_name(&self) -> crate::Result<String> {
self.get_type().get_base_type_name()
}
fn set_type(&mut self, t: Type) {
self.inner.u1.type_info = t.into_raw();
}
pub fn as_null(&self) -> crate::Result<()> {
if self.get_type().is_null() {
Ok(())
} else {
Err(self.must_be_type_error("null").into())
}
}
pub fn as_bool(&self) -> crate::Result<bool> {
let t = self.get_type();
if t.is_true() {
Ok(true)
} else if t.is_false() {
Ok(false)
} else {
Err(self.must_be_type_error("bool").into())
}
}
pub fn as_long(&self) -> crate::Result<i64> {
if self.get_type().is_long() {
unsafe { Ok(self.inner.value.lval) }
} else {
Err(self.must_be_type_error("int").into())
}
}
pub fn as_long_value(&self) -> i64 {
unsafe { phper_zval_get_long(&self.inner as *const _ as *mut _) }
}
pub fn as_double(&self) -> crate::Result<f64> {
if self.get_type().is_double() {
unsafe { Ok(self.inner.value.dval) }
} else {
Err(self.must_be_type_error("float").into())
}
}
pub fn as_string(&self) -> crate::Result<String> {
if self.get_type().is_string() {
unsafe {
let zs = ZendString::from_ptr(self.inner.value.str).unwrap();
Ok(zs.as_str()?.to_owned())
}
} else {
Err(self.must_be_type_error("string").into())
}
}
pub fn as_string_value(&self) -> Result<String, Utf8Error> {
unsafe {
let s = phper_zval_get_string(&self.inner as *const _ as *mut _);
ZendString::from_raw(s).as_str().map(ToOwned::to_owned)
}
}
pub fn as_array(&self) -> crate::Result<&Array> {
if self.get_type().is_array() {
unsafe {
let ptr = self.inner.value.arr;
Ok(Array::from_mut_ptr(ptr).unwrap())
}
} else {
Err(self.must_be_type_error("array").into())
}
}
pub fn as_object(&self) -> crate::Result<&Object<()>> {
if self.get_type().is_object() {
unsafe {
let ptr = self.inner.value.obj;
Ok(Object::from_mut_ptr(ptr))
}
} else {
Err(self.must_be_type_error("object").into())
}
}
pub(crate) unsafe fn as_mut_object_unchecked<T>(&self) -> &mut Object<T> {
let object = Object::from_mut_ptr(self.inner.value.obj);
let class = object.get_class();
ClassEntry::check_type_id(class).unwrap();
object
}
fn must_be_type_error(&self, expect_type: &str) -> crate::Error {
match self.get_type_name() {
Ok(type_name) => {
let message = format!("must be of type {}, {} given", expect_type, type_name);
TypeError::new(message).into()
}
Err(e) => e.into(),
}
}
pub fn duplicate(&mut self) -> Result<EBox<Self>, NotRefCountedTypeError> {
unsafe {
if !phper_z_refcounted_p(self.as_mut_ptr()) {
Err(NotRefCountedTypeError)
} else {
(*self.inner.value.counted).gc.refcount += 1;
let val = EBox::from_raw(self.as_mut_ptr().cast());
Ok(val)
}
}
}
pub fn call(&self, mut arguments: impl AsMut<[Val]>) -> Result<EBox<Val>, CallFunctionError> {
let arguments = arguments.as_mut();
let mut ret = EBox::new(Val::null());
unsafe {
if phper_call_user_function(
null_mut(),
null_mut(),
self.as_ptr() as *mut _,
ret.as_mut_ptr(),
arguments.len() as u32,
arguments.as_mut_ptr().cast(),
) && !ret.get_type().is_undef()
{
Ok(ret)
} else {
Err(CallFunctionError::new("{closure}".to_owned()))
}
}
}
unsafe fn drop_value(&mut self) {
phper_zval_ptr_dtor_nogc(self.as_mut_ptr());
}
}
impl EAllocatable for Val {
fn free(ptr: *mut Self) {
unsafe {
ptr.as_mut().unwrap().drop_value();
_efree(ptr.cast());
}
}
}
impl Drop for Val {
fn drop(&mut self) {
unsafe {
self.drop_value();
}
}
}
pub trait SetVal {
unsafe fn set_val(self, val: &mut Val);
}
impl SetVal for () {
unsafe fn set_val(self, val: &mut Val) {
val.set_type(Type::null());
}
}
impl SetVal for bool {
unsafe fn set_val(self, val: &mut Val) {
val.set_type(Type::bool(self));
}
}
impl SetVal for i32 {
unsafe fn set_val(self, val: &mut Val) {
SetVal::set_val(self as i64, val)
}
}
impl SetVal for u32 {
unsafe fn set_val(self, val: &mut Val) {
SetVal::set_val(self as i64, val)
}
}
impl SetVal for i64 {
unsafe fn set_val(self, val: &mut Val) {
val.set_type(Type::long());
(*val.as_mut_ptr()).value.lval = self;
}
}
impl SetVal for f64 {
unsafe fn set_val(self, val: &mut Val) {
val.set_type(Type::double());
(*val.as_mut_ptr()).value.dval = self;
}
}
impl SetVal for &str {
unsafe fn set_val(self, val: &mut Val) {
phper_zval_stringl(val.as_mut_ptr(), self.as_ptr().cast(), self.len());
}
}
impl SetVal for String {
unsafe fn set_val(self, val: &mut Val) {
let s: &str = &self;
SetVal::set_val(s, val)
}
}
impl SetVal for &[u8] {
unsafe fn set_val(self, val: &mut Val) {
phper_zval_stringl(val.as_mut_ptr(), self.as_ptr().cast(), self.len());
}
}
impl<const N: usize> SetVal for &[u8; N] {
unsafe fn set_val(self, val: &mut Val) {
phper_zval_stringl(val.as_mut_ptr(), self.as_ptr().cast(), self.len());
}
}
impl SetVal for Vec<u8> {
unsafe fn set_val(self, val: &mut Val) {
let v: &[u8] = &self;
SetVal::set_val(v, val)
}
}
impl<T: SetVal> SetVal for Vec<T> {
unsafe fn set_val(self, val: &mut Val) {
phper_array_init(val.as_mut_ptr());
for (k, v) in self.into_iter().enumerate() {
let v = EBox::new(Val::new(v));
phper_zend_hash_index_update(
(*val.as_mut_ptr()).value.arr,
k as u64,
EBox::into_raw(v).cast(),
);
}
}
}
impl<K: AsRef<str>, V: SetVal> SetVal for HashMap<K, V> {
unsafe fn set_val(self, val: &mut Val) {
map_set_val(self, val);
}
}
impl<K: AsRef<str>, V: SetVal> SetVal for IndexMap<K, V> {
unsafe fn set_val(self, val: &mut Val) {
map_set_val(self, val);
}
}
impl<K: AsRef<str>, V: SetVal> SetVal for BTreeMap<K, V> {
unsafe fn set_val(self, val: &mut Val) {
map_set_val(self, val);
}
}
unsafe fn map_set_val<K, V, I>(iterator: I, val: &mut Val)
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<str>,
V: SetVal,
{
phper_array_init(val.as_mut_ptr());
for (k, v) in iterator.into_iter() {
let k = k.as_ref();
let v = EBox::new(Val::new(v));
phper_zend_hash_str_update(
(*val.as_mut_ptr()).value.arr,
k.as_ptr().cast(),
k.len(),
EBox::into_raw(v).cast(),
);
}
}
impl SetVal for EBox<Array> {
unsafe fn set_val(self, val: &mut Val) {
let arr = EBox::into_raw(self);
phper_zval_arr(val.as_mut_ptr(), arr.cast());
}
}
impl<T> SetVal for EBox<Object<T>> {
unsafe fn set_val(self, val: &mut Val) {
let object = EBox::into_raw(self);
val.inner.value.obj = object.cast();
val.set_type(Type::object_ex());
}
}
impl<T: SetVal> SetVal for Option<T> {
unsafe fn set_val(self, val: &mut Val) {
match self {
Some(t) => SetVal::set_val(t, val),
None => SetVal::set_val((), val),
}
}
}
impl<T: SetVal, E: Throwable> SetVal for Result<T, E> {
unsafe fn set_val(self, val: &mut Val) {
match self {
Ok(t) => t.set_val(val),
Err(e) => {
let class_entry = e.class_entry();
let message = ensure_end_with_zero(e.message());
zend_throw_exception(
class_entry.as_ptr() as *mut _,
message.as_ptr().cast(),
e.code() as i64,
);
SetVal::set_val((), val);
}
}
}
}
impl SetVal for Val {
unsafe fn set_val(mut self, val: &mut Val) {
phper_zval_copy(val.as_mut_ptr(), self.as_mut_ptr());
}
}
impl SetVal for EBox<Val> {
unsafe fn set_val(self, val: &mut Val) {
phper_zval_zval(val.as_mut_ptr(), EBox::into_raw(self).cast(), 0, 1);
}
}