Skip to content

Add references support, and rename ZendFunction. #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add references support, rename ZendFunction.
  • Loading branch information
jmjoy committed Dec 14, 2022
commit 80a38af7b7efe3d7d05e3e56c1b10424f7caec2d
12 changes: 8 additions & 4 deletions phper-sys/php_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,12 @@ zend_array *phper_z_arr_p(const zval *zv) {
return Z_ARR_P(zv);
}

zend_long phper_z_lval_p(const zval *zv) {
return Z_LVAL_P(zv);
zend_long *phper_z_lval_p(zval *zv) {
return &(Z_LVAL_P(zv));
}

double phper_z_dval_p(const zval *zv) {
return Z_DVAL_P(zv);
double *phper_z_dval_p(zval *zv) {
return &(Z_DVAL_P(zv));
}

zend_string *phper_z_str_p(const zval *zv) {
Expand All @@ -324,6 +324,10 @@ zend_resource *phper_z_res_p(const zval *zv) {
return Z_RES_P(zv);
}

zend_reference *phper_z_ref_p(const zval *zv) {
return Z_REF_P(zv);
}

zend_string *phper_zend_string_copy(zend_string *s) {
return zend_string_copy(s);
}
Expand Down
6 changes: 3 additions & 3 deletions phper/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,12 @@ impl Argument {
}

#[repr(transparent)]
pub struct ZendFunction {
pub struct ZendFunc {
inner: zend_function,
}

impl ZendFunction {
pub(crate) unsafe fn from_mut_ptr<'a>(ptr: *mut zend_function) -> &'a mut ZendFunction {
impl ZendFunc {
pub(crate) unsafe fn from_mut_ptr<'a>(ptr: *mut zend_function) -> &'a mut ZendFunc {
let ptr = ptr as *mut Self;
ptr.as_mut().expect("ptr shouldn't be null")
}
Expand Down
1 change: 1 addition & 0 deletions phper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod ini;
pub mod modules;
pub mod objects;
pub mod output;
pub mod references;
pub mod resources;
pub mod strings;
pub mod types;
Expand Down
4 changes: 2 additions & 2 deletions phper/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use crate::{
alloc::EBox,
classes::ClassEntry,
functions::{call_internal, ZendFunction},
functions::{call_internal, ZendFunc},
sys::*,
values::ZVal,
};
Expand Down Expand Up @@ -231,7 +231,7 @@ impl ZObj {
if f.is_null() {
Ok(false)
} else {
let zend_fn = ZendFunction::from_mut_ptr(f);
let zend_fn = ZendFunc::from_mut_ptr(f);
zend_fn.call(Some(self), arguments)?;
Ok(true)
}
Expand Down
68 changes: 68 additions & 0 deletions phper/src/references.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright (c) 2022 PHPER Framework Team
// PHPER is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan
// PSL v2. You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

//! Apis relate to [crate::sys::zend_resource].

use crate::{sys::*, values::ZVal};

/// Wrapper of [crate::sys::zend_resource].
#[repr(transparent)]
pub struct ZRef {
inner: zend_reference,
}

impl ZRef {
/// # Safety
///
/// Create from raw pointer.
pub unsafe fn from_ptr<'a>(ptr: *const zend_reference) -> &'a Self {
(ptr as *const Self)
.as_ref()
.expect("ptr should not be null")
}

/// # Safety
///
/// Create from raw pointer.
pub unsafe fn try_from_ptr<'a>(ptr: *const zend_reference) -> Option<&'a Self> {
(ptr as *const Self).as_ref()
}

/// # Safety
///
/// Create from raw pointer.
pub unsafe fn from_mut_ptr<'a>(ptr: *mut zend_reference) -> &'a mut Self {
(ptr as *mut Self).as_mut().expect("ptr should not be null")
}

/// # Safety
///
/// Create from raw pointer.
pub unsafe fn try_from_mut_ptr<'a>(ptr: *mut zend_reference) -> Option<&'a mut Self> {
(ptr as *mut Self).as_mut()
}

pub const fn as_ptr(&self) -> *const zend_reference {
&self.inner
}

#[inline]
pub fn as_mut_ptr(&mut self) -> *mut zend_reference {
&mut self.inner
}

pub fn val(&self) -> &ZVal {
unsafe { ZVal::from_ptr(&self.inner.val) }
}

pub fn val_mut(&mut self) -> &mut ZVal {
unsafe { ZVal::from_mut_ptr(&mut self.inner.val) }
}
}
114 changes: 33 additions & 81 deletions phper/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

//! Apis relate to PHP types.

use crate::sys::*;
use crate::{c_str, sys::*};
use derive_more::From;
use std::{ffi::CStr, fmt::Display, os::raw::c_int};

Expand All @@ -27,6 +27,7 @@ impl TypeInfo {
pub const LONG: TypeInfo = TypeInfo::from_raw(IS_LONG);
pub const NULL: TypeInfo = TypeInfo::from_raw(IS_NULL);
pub const OBJECT: TypeInfo = TypeInfo::from_raw(IS_OBJECT);
pub const REFERENCE: TypeInfo = TypeInfo::from_raw(IS_REFERENCE);
pub const RESOURCE: TypeInfo = TypeInfo::from_raw(IS_RESOURCE);
pub const STRING: TypeInfo = TypeInfo::from_raw(IS_STRING);
pub const UNDEF: TypeInfo = TypeInfo::from_raw(IS_UNDEF);
Expand All @@ -41,46 +42,6 @@ impl TypeInfo {
self.t
}

pub const fn undef() -> TypeInfo {
Self::from_raw(IS_UNDEF)
}

pub const fn null() -> TypeInfo {
Self::from_raw(IS_NULL)
}

pub const fn bool(b: bool) -> TypeInfo {
Self::from_raw(if b { IS_TRUE } else { IS_FALSE })
}

pub const fn long() -> TypeInfo {
Self::from_raw(IS_LONG)
}

pub const fn double() -> TypeInfo {
Self::from_raw(IS_DOUBLE)
}

pub const fn string() -> TypeInfo {
Self::from_raw(IS_STRING)
}

pub const fn array() -> TypeInfo {
Self::from_raw(IS_ARRAY)
}

pub const fn array_ex() -> TypeInfo {
Self::from_raw(IS_ARRAY_EX)
}

pub const fn object() -> TypeInfo {
Self::from_raw(IS_OBJECT)
}

pub const fn object_ex() -> TypeInfo {
Self::from_raw(IS_OBJECT_EX)
}

pub const fn is_undef(self) -> bool {
self.t == IS_UNDEF
}
Expand Down Expand Up @@ -125,16 +86,42 @@ impl TypeInfo {
get_base_type_by_raw(self.t) == IS_RESOURCE
}

pub const fn is_indirect(self) -> bool {
self.t == IS_INDIRECT
pub const fn is_reference(self) -> bool {
get_base_type_by_raw(self.t) == IS_REFERENCE
}

pub const fn get_base_type(self) -> TypeInfo {
Self::from_raw(get_base_type_by_raw(self.t))
}

pub fn get_base_type_name(self) -> crate::Result<String> {
get_type_by_const(self.t)
/// Can be sure, `zend_get_type_by_const` returns the const string, So this
/// method returns `&'static CStr`.
#[inline]
pub fn get_base_type_name(self) -> &'static CStr {
unsafe {
let t = get_base_type_by_raw(self.t);

if t == IS_UNDEF {
return c_str!("undef");
}
if t == IS_REFERENCE {
return c_str!("reference");
}

let s = zend_get_type_by_const(t as c_int);
let s = CStr::from_ptr(s);

// Compact with PHP7.
let bs = s.to_bytes();
if bs == b"boolean" {
return c_str!("bool");
}
if bs == b"integer" {
return c_str!("int");
}

s
}
}
}

Expand All @@ -146,46 +133,11 @@ impl From<u32> for TypeInfo {

impl Display for TypeInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let t = if self.is_null() {
"null"
} else if self.is_bool() {
"bool"
} else if self.is_long() {
"int"
} else if self.is_double() {
"float"
} else if self.is_string() {
"string"
} else if self.is_array() {
"array"
} else if self.is_object() {
"object"
} else if self.is_resource() {
"resource"
} else {
"unknown"
};
let t = self.get_base_type_name().to_str().unwrap_or("unknown");
Display::fmt(t, f)
}
}

fn get_type_by_const(mut t: u32) -> crate::Result<String> {
unsafe {
t = get_base_type_by_raw(t);
let s = zend_get_type_by_const(t as c_int);
let mut s = CStr::from_ptr(s).to_str()?.to_string();

// Compact with PHP7.
if s == "boolean" {
s = "bool".to_string();
} else if s == "integer" {
s = "int".to_string();
}

Ok(s)
}
}

const fn get_base_type_by_raw(t: u32) -> u32 {
t & !(!0 << Z_TYPE_FLAGS_SHIFT)
}
Expand Down
Loading