Skip to content

Add try_* methods and modify zstr and zstring debug methods. #33

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 5 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
104 changes: 104 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Copyright (c) 2019 jmjoy
# 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.

name: Publish

on:
push:
tags: [ "**" ]

env:
RUST_LOG: debug
CARGO_TERM_COLOR: always
RUST_BACKTRACE: "1"
RUSTFLAGS: "-D warnings"
LLVM_CONFIG_PATH: llvm-config-10
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

jobs:
publish:
name: Publish

runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Git config
shell: bash
run: |
git config --global user.email "918734043@qq.com"
git config --global user.name "jmjoy"

- name: Install libclang
run: sudo apt-get install -y llvm-10-dev libclang-10-dev

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
tools: php-config

- name: Install Rust Stable
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true

- name: Cargo publish phper-sys
uses: actions-rs/cargo@v1
with:
command: publish
args: --manifest-path phper-sys/Cargo.toml

- name: Delay
run: sleep 20

- name: Cargo publish phper-build
uses: actions-rs/cargo@v1
with:
command: publish
args: --manifest-path phper-build/Cargo.toml

- name: Delay
run: sleep 20

- name: Cargo publish phper-macros
uses: actions-rs/cargo@v1
with:
command: publish
args: --manifest-path phper-macros/Cargo.toml

- name: Delay
run: sleep 20

- name: Cargo publish phper-alloc
uses: actions-rs/cargo@v1
with:
command: publish
args: --manifest-path phper-alloc/Cargo.toml

- name: Delay
run: sleep 20

- name: Cargo publish phper-test
uses: actions-rs/cargo@v1
with:
command: publish
args: --manifest-path phper-test/Cargo.toml

- name: Delay
run: sleep 20

- name: Cargo publish phper
uses: actions-rs/cargo@v1
with:
command: publish
args: --manifest-path phper/Cargo.toml
1 change: 1 addition & 0 deletions phper-sys/php_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <php_ini.h>
#include <ext/standard/info.h>
#include <zend_exceptions.h>
#include <zend_interfaces.h>
#include <main/SAPI.h>

typedef ZEND_INI_MH(phper_zend_ini_mh);
Expand Down
17 changes: 14 additions & 3 deletions phper/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use crate::{
cg,
classes::Visibility,
classes::{ClassEntry, Visibility},
errors::{ArgumentCountError, CallFunctionError, CallMethodError},
exceptions::Exception,
objects::{StatefulObj, ZObj},
Expand Down Expand Up @@ -235,10 +235,10 @@ impl ZendFunction {
&mut self.inner
}

pub fn get_function_name(&self) -> &ZStr {
pub fn get_function_name(&self) -> Option<&ZStr> {
unsafe {
let s = phper_get_function_name(self.as_ptr());
ZStr::from_ptr(s)
ZStr::try_from_ptr(s)
}
}

Expand All @@ -249,6 +249,17 @@ impl ZendFunction {
}
}

pub fn get_class(&self) -> Option<&ClassEntry> {
unsafe {
let ptr = self.inner.common.scope;
if ptr.is_null() {
None
} else {
Some(ClassEntry::from_ptr(self.inner.common.scope))
}
}
}

pub(crate) fn call(
&mut self, mut object: Option<&mut ZObj>, mut arguments: impl AsMut<[ZVal]>,
) -> crate::Result<ZVal> {
Expand Down
22 changes: 17 additions & 5 deletions phper/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use phper_alloc::ToRefOwned;
use std::{
borrow::Borrow,
convert::TryInto,
ffi::CStr,
ffi::{CStr, FromBytesWithNulError},
fmt::Debug,
marker::PhantomData,
mem::forget,
Expand All @@ -34,14 +34,26 @@ pub struct ZStr {
}

impl ZStr {
#[inline]
pub unsafe fn from_ptr<'a>(ptr: *const zend_string) -> &'a Self {
(ptr as *const Self).as_ref().expect("ptr should't be null")
}

#[inline]
pub unsafe fn try_from_ptr<'a>(ptr: *const zend_string) -> Option<&'a Self> {
(ptr as *const Self).as_ref()
}

#[inline]
pub unsafe fn from_mut_ptr<'a>(ptr: *mut zend_string) -> &'a mut Self {
(ptr as *mut Self).as_mut().expect("ptr should't be null")
}

#[inline]
pub unsafe fn try_from_mut_ptr<'a>(ptr: *mut zend_string) -> Option<&'a mut Self> {
(ptr as *mut Self).as_mut()
}

pub const fn as_ptr(&self) -> *const zend_string {
&self.inner
}
Expand Down Expand Up @@ -71,8 +83,8 @@ impl ZStr {
unsafe { from_raw_parts(phper_zstr_val(&self.inner).cast(), self.len()) }
}

pub fn to_c_str(&self) -> &CStr {
CStr::from_bytes_with_nul(self.to_bytes()).unwrap()
pub fn to_c_str(&self) -> Result<&CStr, FromBytesWithNulError> {
CStr::from_bytes_with_nul(self.to_bytes())
}

pub fn to_str(&self) -> Result<&str, Utf8Error> {
Expand All @@ -82,7 +94,7 @@ impl ZStr {

impl Debug for ZStr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(self.to_c_str(), f)
f.debug_tuple("ZStr").field(&self.to_c_str()).finish()
}
}

Expand Down Expand Up @@ -152,7 +164,7 @@ impl ZString {

impl Debug for ZString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(self.deref(), f)
f.debug_tuple("ZString").field(&self.to_c_str()).finish()
}
}

Expand Down
30 changes: 28 additions & 2 deletions phper/src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ impl ExecuteData {
(ptr as *const Self).as_ref().expect("ptr should't be null")
}

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

#[inline]
pub fn as_ptr(&self) -> *const zend_execute_data {
&self.inner
Expand All @@ -58,6 +66,14 @@ impl ExecuteData {
(ptr as *mut Self).as_mut().expect("ptr should't be null")
}

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

#[inline]
pub fn as_mut_ptr(&mut self) -> *mut zend_execute_data {
&mut self.inner
Expand Down Expand Up @@ -95,8 +111,8 @@ impl ExecuteData {
/// # Safety
///
/// From inner raw pointer.
pub unsafe fn func(&self) -> &ZendFunction {
ZendFunction::from_mut_ptr(self.inner.func)
pub fn func(&self) -> &ZendFunction {
unsafe { ZendFunction::from_mut_ptr(self.inner.func) }
}

pub fn get_this(&mut self) -> Option<&ZObj> {
Expand Down Expand Up @@ -145,10 +161,18 @@ impl ZVal {
(ptr as *const Self).as_ref().expect("ptr should't be null")
}

pub unsafe fn try_from_ptr<'a>(ptr: *const zval) -> Option<&'a Self> {
(ptr as *const Self).as_ref()
}

pub unsafe fn from_mut_ptr<'a>(ptr: *mut zval) -> &'a mut Self {
(ptr as *mut Self).as_mut().expect("ptr should't be null")
}

pub unsafe fn try_from_mut_ptr<'a>(ptr: *mut zval) -> Option<&'a mut Self> {
(ptr as *mut Self).as_mut()
}

pub const fn as_ptr(&self) -> *const zval {
&self.inner
}
Expand Down Expand Up @@ -318,12 +342,14 @@ impl ZVal {
}
}

/// TODO To fix assertion failed.
pub fn convert_to_long(&mut self) {
unsafe {
phper_convert_to_long(self.as_mut_ptr());
}
}

/// TODO To fix assertion failed.
pub fn convert_to_string(&mut self) {
unsafe {
phper_convert_to_string(self.as_mut_ptr());
Expand Down