Skip to content

Commit 994ed89

Browse files
authored
Add try_* methods and modify zstr and zstring debug methods. (#33)
* Add try_* methods. * Modify zstr and zstring debug methods. * Add publish ci.
1 parent 6a42591 commit 994ed89

File tree

5 files changed

+164
-10
lines changed

5 files changed

+164
-10
lines changed

.github/workflows/publish.yml

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Copyright (c) 2019 jmjoy
2+
# PHPER is licensed under Mulan PSL v2.
3+
# You can use this software according to the terms and conditions of the Mulan
4+
# PSL v2. You may obtain a copy of Mulan PSL v2 at:
5+
# http://license.coscl.org.cn/MulanPSL2
6+
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
7+
# KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
8+
# NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
9+
# See the Mulan PSL v2 for more details.
10+
11+
name: Publish
12+
13+
on:
14+
push:
15+
tags: [ "**" ]
16+
17+
env:
18+
RUST_LOG: debug
19+
CARGO_TERM_COLOR: always
20+
RUST_BACKTRACE: "1"
21+
RUSTFLAGS: "-D warnings"
22+
LLVM_CONFIG_PATH: llvm-config-10
23+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
24+
25+
jobs:
26+
publish:
27+
name: Publish
28+
29+
runs-on: ubuntu-latest
30+
steps:
31+
- name: Checkout
32+
uses: actions/checkout@v2
33+
34+
- name: Git config
35+
shell: bash
36+
run: |
37+
git config --global user.email "918734043@qq.com"
38+
git config --global user.name "jmjoy"
39+
40+
- name: Install libclang
41+
run: sudo apt-get install -y llvm-10-dev libclang-10-dev
42+
43+
- name: Setup PHP
44+
uses: shivammathur/setup-php@v2
45+
with:
46+
php-version: 7.4
47+
tools: php-config
48+
49+
- name: Install Rust Stable
50+
uses: actions-rs/toolchain@v1
51+
with:
52+
toolchain: stable
53+
override: true
54+
55+
- name: Cargo publish phper-sys
56+
uses: actions-rs/cargo@v1
57+
with:
58+
command: publish
59+
args: --manifest-path phper-sys/Cargo.toml
60+
61+
- name: Delay
62+
run: sleep 20
63+
64+
- name: Cargo publish phper-build
65+
uses: actions-rs/cargo@v1
66+
with:
67+
command: publish
68+
args: --manifest-path phper-build/Cargo.toml
69+
70+
- name: Delay
71+
run: sleep 20
72+
73+
- name: Cargo publish phper-macros
74+
uses: actions-rs/cargo@v1
75+
with:
76+
command: publish
77+
args: --manifest-path phper-macros/Cargo.toml
78+
79+
- name: Delay
80+
run: sleep 20
81+
82+
- name: Cargo publish phper-alloc
83+
uses: actions-rs/cargo@v1
84+
with:
85+
command: publish
86+
args: --manifest-path phper-alloc/Cargo.toml
87+
88+
- name: Delay
89+
run: sleep 20
90+
91+
- name: Cargo publish phper-test
92+
uses: actions-rs/cargo@v1
93+
with:
94+
command: publish
95+
args: --manifest-path phper-test/Cargo.toml
96+
97+
- name: Delay
98+
run: sleep 20
99+
100+
- name: Cargo publish phper
101+
uses: actions-rs/cargo@v1
102+
with:
103+
command: publish
104+
args: --manifest-path phper/Cargo.toml

phper-sys/php_wrapper.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <php_ini.h>
1414
#include <ext/standard/info.h>
1515
#include <zend_exceptions.h>
16+
#include <zend_interfaces.h>
1617
#include <main/SAPI.h>
1718

1819
typedef ZEND_INI_MH(phper_zend_ini_mh);

phper/src/functions.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
1515
use crate::{
1616
cg,
17-
classes::Visibility,
17+
classes::{ClassEntry, Visibility},
1818
errors::{ArgumentCountError, CallFunctionError, CallMethodError},
1919
exceptions::Exception,
2020
objects::{StatefulObj, ZObj},
@@ -235,10 +235,10 @@ impl ZendFunction {
235235
&mut self.inner
236236
}
237237

238-
pub fn get_function_name(&self) -> &ZStr {
238+
pub fn get_function_name(&self) -> Option<&ZStr> {
239239
unsafe {
240240
let s = phper_get_function_name(self.as_ptr());
241-
ZStr::from_ptr(s)
241+
ZStr::try_from_ptr(s)
242242
}
243243
}
244244

@@ -249,6 +249,17 @@ impl ZendFunction {
249249
}
250250
}
251251

252+
pub fn get_class(&self) -> Option<&ClassEntry> {
253+
unsafe {
254+
let ptr = self.inner.common.scope;
255+
if ptr.is_null() {
256+
None
257+
} else {
258+
Some(ClassEntry::from_ptr(self.inner.common.scope))
259+
}
260+
}
261+
}
262+
252263
pub(crate) fn call(
253264
&mut self, mut object: Option<&mut ZObj>, mut arguments: impl AsMut<[ZVal]>,
254265
) -> crate::Result<ZVal> {

phper/src/strings.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use phper_alloc::ToRefOwned;
1515
use std::{
1616
borrow::Borrow,
1717
convert::TryInto,
18-
ffi::CStr,
18+
ffi::{CStr, FromBytesWithNulError},
1919
fmt::Debug,
2020
marker::PhantomData,
2121
mem::forget,
@@ -34,14 +34,26 @@ pub struct ZStr {
3434
}
3535

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

42+
#[inline]
43+
pub unsafe fn try_from_ptr<'a>(ptr: *const zend_string) -> Option<&'a Self> {
44+
(ptr as *const Self).as_ref()
45+
}
46+
47+
#[inline]
4148
pub unsafe fn from_mut_ptr<'a>(ptr: *mut zend_string) -> &'a mut Self {
4249
(ptr as *mut Self).as_mut().expect("ptr should't be null")
4350
}
4451

52+
#[inline]
53+
pub unsafe fn try_from_mut_ptr<'a>(ptr: *mut zend_string) -> Option<&'a mut Self> {
54+
(ptr as *mut Self).as_mut()
55+
}
56+
4557
pub const fn as_ptr(&self) -> *const zend_string {
4658
&self.inner
4759
}
@@ -71,8 +83,8 @@ impl ZStr {
7183
unsafe { from_raw_parts(phper_zstr_val(&self.inner).cast(), self.len()) }
7284
}
7385

74-
pub fn to_c_str(&self) -> &CStr {
75-
CStr::from_bytes_with_nul(self.to_bytes()).unwrap()
86+
pub fn to_c_str(&self) -> Result<&CStr, FromBytesWithNulError> {
87+
CStr::from_bytes_with_nul(self.to_bytes())
7688
}
7789

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

8395
impl Debug for ZStr {
8496
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85-
Debug::fmt(self.to_c_str(), f)
97+
f.debug_tuple("ZStr").field(&self.to_c_str()).finish()
8698
}
8799
}
88100

@@ -152,7 +164,7 @@ impl ZString {
152164

153165
impl Debug for ZString {
154166
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
155-
Debug::fmt(self.deref(), f)
167+
f.debug_tuple("ZString").field(&self.to_c_str()).finish()
156168
}
157169
}
158170

phper/src/values.rs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ impl ExecuteData {
4545
(ptr as *const Self).as_ref().expect("ptr should't be null")
4646
}
4747

48+
/// # Safety
49+
///
50+
/// Create from raw pointer.
51+
#[inline]
52+
pub unsafe fn try_from_ptr<'a>(ptr: *const zend_execute_data) -> Option<&'a Self> {
53+
(ptr as *const Self).as_ref()
54+
}
55+
4856
#[inline]
4957
pub fn as_ptr(&self) -> *const zend_execute_data {
5058
&self.inner
@@ -58,6 +66,14 @@ impl ExecuteData {
5866
(ptr as *mut Self).as_mut().expect("ptr should't be null")
5967
}
6068

69+
/// # Safety
70+
///
71+
/// Create from raw pointer.
72+
#[inline]
73+
pub unsafe fn try_from_mut_ptr<'a>(ptr: *mut zend_execute_data) -> Option<&'a mut Self> {
74+
(ptr as *mut Self).as_mut()
75+
}
76+
6177
#[inline]
6278
pub fn as_mut_ptr(&mut self) -> *mut zend_execute_data {
6379
&mut self.inner
@@ -95,8 +111,8 @@ impl ExecuteData {
95111
/// # Safety
96112
///
97113
/// From inner raw pointer.
98-
pub unsafe fn func(&self) -> &ZendFunction {
99-
ZendFunction::from_mut_ptr(self.inner.func)
114+
pub fn func(&self) -> &ZendFunction {
115+
unsafe { ZendFunction::from_mut_ptr(self.inner.func) }
100116
}
101117

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

164+
pub unsafe fn try_from_ptr<'a>(ptr: *const zval) -> Option<&'a Self> {
165+
(ptr as *const Self).as_ref()
166+
}
167+
148168
pub unsafe fn from_mut_ptr<'a>(ptr: *mut zval) -> &'a mut Self {
149169
(ptr as *mut Self).as_mut().expect("ptr should't be null")
150170
}
151171

172+
pub unsafe fn try_from_mut_ptr<'a>(ptr: *mut zval) -> Option<&'a mut Self> {
173+
(ptr as *mut Self).as_mut()
174+
}
175+
152176
pub const fn as_ptr(&self) -> *const zval {
153177
&self.inner
154178
}
@@ -318,12 +342,14 @@ impl ZVal {
318342
}
319343
}
320344

345+
/// TODO To fix assertion failed.
321346
pub fn convert_to_long(&mut self) {
322347
unsafe {
323348
phper_convert_to_long(self.as_mut_ptr());
324349
}
325350
}
326351

352+
/// TODO To fix assertion failed.
327353
pub fn convert_to_string(&mut self) {
328354
unsafe {
329355
phper_convert_to_string(self.as_mut_ptr());

0 commit comments

Comments
 (0)