Skip to content

Commit b8e0a23

Browse files
authored
Optimize code. (#37)
1 parent 3069548 commit b8e0a23

File tree

23 files changed

+249
-86
lines changed

23 files changed

+249
-86
lines changed

.github/workflows/ci.yml

+7-6
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ jobs:
9494
command: build
9595
args: --release
9696

97-
# - name: Cargo clippy
98-
# uses: actions-rs/cargo@v1
99-
# with:
100-
# toolchain: stable
101-
# command: clippy
102-
# args: --release
97+
- name: Cargo clippy
98+
uses: actions-rs/cargo@v1
99+
with:
100+
toolchain: stable
101+
command: clippy
102+
args: --release
103103

104104
- name: Cargo test
105105
uses: actions-rs/cargo@v1
@@ -117,6 +117,7 @@ jobs:
117117
command: doc
118118
args: --workspace --no-deps --all-features
119119

120+
# Test every features for single crate.
120121
check:
121122
name: Check
122123
strategy:

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
## Rust ❤️ PHP
1010

11-
A library that allows us to write PHP extensions using pure Rust and using safe Rust whenever possible.
11+
The framework that allows us to write PHP extensions using pure and safe Rust whenever possible.
1212

1313
## Requirement
1414

examples/hello/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ path = "src/_reexport.rs"
2929
crate-type = ["cdylib"]
3030

3131
[dependencies]
32-
phper = { version = "0.4.0-alpha.4", path = "../../phper" }
32+
phper = { version = "0.4.0", path = "../../phper" }
3333

3434
[dev-dependencies]
35-
phper-test = { version = "0.4.0-alpha.4", path = "../../phper-test" }
35+
phper-test = { version = "0.4.0", path = "../../phper-test" }
3636

3737
[build-dependencies]
38-
phper-build = { version = "0.4.0-alpha.4", path = "../../phper-build" }
38+
phper-build = { version = "0.4.0", path = "../../phper-build" }

examples/http-client/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ crate-type = ["cdylib"]
3232
anyhow = "1.0.58"
3333
bytes = "1.1.0"
3434
indexmap = "1.9.1"
35-
phper = { version = "0.4.0-alpha.4", path = "../../phper" }
35+
phper = { version = "0.4.0", path = "../../phper" }
3636
reqwest = { version = "0.11.11", features = ["blocking", "cookies"] }
3737
thiserror = "1.0.31"
3838

3939
[dev-dependencies]
40-
phper-test = { version = "0.4.0-alpha.4", path = "../../phper-test" }
40+
phper-test = { version = "0.4.0", path = "../../phper-test" }

examples/http-server/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ crate-type = ["cdylib"]
3030

3131
[dependencies]
3232
hyper = { version = "0.14.20", features = ["http1", "runtime", "server"] }
33-
phper = { version = "0.4.0-alpha.4", path = "../../phper" }
33+
phper = { version = "0.4.0", path = "../../phper" }
3434
thiserror = "1.0.31"
3535
tokio = { version = "1.19.2", features = ["full"] }
3636

3737
[dev-dependencies]
38-
phper-test = { version = "0.4.0-alpha.4", path = "../../phper-test" }
38+
phper-test = { version = "0.4.0", path = "../../phper-test" }
3939
reqwest = "0.11.11"

examples/logging/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ crate-type = ["cdylib"]
3030

3131
[dependencies]
3232
anyhow = "1.0.58"
33-
phper = { version = "0.4.0-alpha.4", path = "../../phper" }
33+
phper = { version = "0.4.0", path = "../../phper" }
3434

3535
[dev-dependencies]
36-
phper-test = { version = "0.4.0-alpha.4", path = "../../phper-test" }
36+
phper-test = { version = "0.4.0", path = "../../phper-test" }
3737

3838
[build-dependencies]
39-
phper-build = { version = "0.4.0-alpha.4", path = "../../phper-build" }
39+
phper-build = { version = "0.4.0", path = "../../phper-build" }

phper-alloc/Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
[package]
1212
name = "phper-alloc"
13-
version = "0.4.0-alpha.4"
13+
version = "0.4.0"
1414
authors = ["jmjoy <[email protected]>"]
1515
edition = "2021"
1616
rust-version = "1.56"
@@ -22,7 +22,7 @@ keywords = ["php", "alloc"]
2222
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
2323

2424
[dependencies]
25-
phper-sys = { version = "0.4.0-alpha.4", path = "../phper-sys" }
25+
phper-sys = { version = "0.4.0", path = "../phper-sys" }
2626

2727
[build-dependencies]
28-
phper-build = { version = "0.4.0-alpha.4", path = "../phper-build" }
28+
phper-build = { version = "0.4.0", path = "../phper-build" }

phper-build/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
[package]
1212
name = "phper-build"
13-
version = "0.4.0-alpha.4"
13+
version = "0.4.0"
1414
authors = ["jmjoy <[email protected]>"]
1515
edition = "2021"
1616
rust-version = "1.56"
@@ -22,4 +22,4 @@ keywords = ["php", "binding"]
2222
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
2323

2424
[dependencies]
25-
phper-sys = { version = "0.4.0-alpha.4", path = "../phper-sys" }
25+
phper-sys = { version = "0.4.0", path = "../phper-sys" }

phper-macros/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
[package]
1212
name = "phper-macros"
13-
version = "0.4.0-alpha.4"
13+
version = "0.4.0"
1414
authors = ["jmjoy <[email protected]>"]
1515
edition = "2021"
1616
rust-version = "1.56"

phper-sys/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
[package]
1212
name = "phper-sys"
13-
version = "0.4.0-alpha.4"
13+
version = "0.4.0"
1414
authors = ["jmjoy <[email protected]>"]
1515
edition = "2021"
1616
rust-version = "1.56"

phper-test/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
[package]
1212
name = "phper-test"
13-
version = "0.4.0-alpha.4"
13+
version = "0.4.0"
1414
authors = ["jmjoy <[email protected]>"]
1515
edition = "2021"
1616
rust-version = "1.56"
@@ -28,7 +28,7 @@ fpm = ["fastcgi-client", "tokio/full"]
2828
fastcgi-client = { version = "0.7.0", optional = true }
2929
libc = "0.2.126"
3030
once_cell = "1.13.0"
31-
phper-macros = { version = "0.4.0-alpha.4", path = "../phper-macros" }
31+
phper-macros = { version = "0.4.0", path = "../phper-macros" }
3232
tempfile = "3.3.0"
3333
tokio = { version = "1.19.2", optional = true }
3434

phper-test/README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ The `php-config` is needed. You can set environment `PHP_CONFIG` to specify the
66

77
## Notice
88

9-
Because the `phper-test` depends on the `cdylib` to do integration test, but now `cargo test` don't build `cdylib` in `[lib]`, so you must call `cargo build` before `cargo test`.
9+
1. Because the `phper-test` depends on the `cdylib` to do integration test, but now `cargo test` don't build `cdylib` in `[lib]`, so you must call `cargo build` before `cargo test`.
1010

11-
Maybe, when the feature [artifact-dependencies](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#artifact-dependencies) becomes stable, or the [issue](https://github.com/rust-lang/cargo/issues/8628) be solved, you don't have to call `cargo build` in advance, but I think it will be a long long stage.
11+
Maybe, when the feature [artifact-dependencies](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#artifact-dependencies) becomes stable, or the [issue](https://github.com/rust-lang/cargo/issues/8628) be solved, you don't have to call `cargo build` in advance, but I think it will be a long long stage.
12+
13+
1. Or, define an `[[example]]` section, re-export all the symbols of your crate, and set `lto = true`. It's strange, but this is the only method to just run `cargo test` without running `cargo build` in advance. Please refer to [tests/integration/Cargo.toml](https://github.com/jmjoy/phper/blob/master/tests/integration/Cargo.toml).
1214

1315
## License
1416

phper/Cargo.toml

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
[package]
1212
name = "phper"
13-
version = "0.4.0-alpha.4"
13+
version = "0.4.0"
1414
authors = ["jmjoy <[email protected]>"]
1515
edition = "2021"
1616
rust-version = "1.56"
17-
description = "A library that allows us to write PHP extensions using pure Rust and using safe Rust whenever possible."
17+
description = "The framework that allows us to write PHP extensions using pure and safe Rust whenever possible."
1818
repository = "https://github.com/jmjoy/phper.git"
1919
documentation = "https://docs.rs/phper"
2020
license = "MulanPSL-2.0"
@@ -30,11 +30,11 @@ dashmap = "5.3.4"
3030
derive_more = "0.99.17"
3131
indexmap = "1.9.1"
3232
once_cell = "1.13.0"
33-
phper-alloc = { version = "0.4.0-alpha.4", path = "../phper-alloc" }
34-
phper-macros = { version = "0.4.0-alpha.4", path = "../phper-macros" }
35-
phper-sys = { version = "0.4.0-alpha.4", path = "../phper-sys" }
33+
phper-alloc = { version = "0.4.0", path = "../phper-alloc" }
34+
phper-macros = { version = "0.4.0", path = "../phper-macros" }
35+
phper-sys = { version = "0.4.0", path = "../phper-sys" }
3636
thiserror = "1.0.31"
3737

3838
[build-dependencies]
39-
phper-build = { version = "0.4.0-alpha.4", path = "../phper-build" }
40-
phper-sys = { version = "0.4.0-alpha.4", path = "../phper-sys" }
39+
phper-build = { version = "0.4.0", path = "../phper-build" }
40+
phper-sys = { version = "0.4.0", path = "../phper-sys" }

phper/src/arrays.rs

+32
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,38 @@ pub struct ZArr {
5757
}
5858

5959
impl ZArr {
60+
/// # Safety
61+
///
62+
/// Create from raw pointer.
63+
#[inline]
6064
pub unsafe fn from_ptr<'a>(ptr: *const zend_array) -> &'a Self {
6165
(ptr as *const Self).as_ref().expect("ptr should't be null")
6266
}
6367

68+
/// # Safety
69+
///
70+
/// Create from raw pointer.
71+
#[inline]
72+
pub unsafe fn try_from_ptr<'a>(ptr: *const zend_array) -> Option<&'a Self> {
73+
(ptr as *const Self).as_ref()
74+
}
75+
76+
/// # Safety
77+
///
78+
/// Create from raw pointer.
79+
#[inline]
6480
pub unsafe fn from_mut_ptr<'a>(ptr: *mut zend_array) -> &'a mut Self {
6581
(ptr as *mut Self).as_mut().expect("ptr should't be null")
6682
}
6783

84+
/// # Safety
85+
///
86+
/// Create from raw pointer.
87+
#[inline]
88+
pub unsafe fn try_from_mut_ptr<'a>(ptr: *mut zend_array) -> Option<&'a mut Self> {
89+
(ptr as *mut Self).as_mut()
90+
}
91+
6892
pub const fn as_ptr(&self) -> *const zend_array {
6993
&self.inner
7094
}
@@ -277,6 +301,14 @@ impl ZArray {
277301
}
278302
}
279303

304+
/// Create owned object From raw pointer, usually used in pairs with
305+
/// `into_raw`.
306+
///
307+
/// # Safety
308+
///
309+
/// This function is unsafe because improper use may lead to memory
310+
/// problems. For example, a double-free may occur if the function is called
311+
/// twice on the same raw pointer.
280312
#[inline]
281313
pub unsafe fn from_raw(ptr: *mut zend_array) -> Self {
282314
Self {

phper/src/classes.rs

+33-9
Original file line numberDiff line numberDiff line change
@@ -168,22 +168,36 @@ pub struct ClassEntry {
168168
}
169169

170170
impl ClassEntry {
171+
/// # Safety
172+
///
173+
/// Create from raw pointer.
174+
#[inline]
171175
pub unsafe fn from_ptr<'a>(ptr: *const zend_class_entry) -> &'a Self {
172176
(ptr as *const Self).as_ref().expect("ptr should't be null")
173177
}
174178

179+
/// # Safety
180+
///
181+
/// Create from raw pointer.
182+
#[inline]
183+
pub unsafe fn try_from_ptr<'a>(ptr: *const zend_class_entry) -> Option<&'a Self> {
184+
(ptr as *const Self).as_ref()
185+
}
186+
187+
/// # Safety
188+
///
189+
/// Create from raw pointer.
190+
#[inline]
175191
pub unsafe fn from_mut_ptr<'a>(ptr: *mut zend_class_entry) -> &'a mut Self {
176192
(ptr as *mut Self).as_mut().expect("ptr should't be null")
177193
}
178194

179-
pub fn from_globals<'a>(class_name: impl AsRef<str>) -> crate::Result<&'a Self> {
180-
let name = class_name.as_ref();
181-
let ptr: *mut Self = find_global_class_entry_ptr(name).cast();
182-
unsafe {
183-
ptr.as_ref().ok_or_else(|| {
184-
crate::Error::ClassNotFound(ClassNotFoundError::new(name.to_string()))
185-
})
186-
}
195+
/// # Safety
196+
///
197+
/// Create from raw pointer.
198+
#[inline]
199+
pub unsafe fn try_from_mut_ptr<'a>(ptr: *mut zend_class_entry) -> Option<&'a mut Self> {
200+
(ptr as *mut Self).as_mut()
187201
}
188202

189203
pub const fn as_ptr(&self) -> *const zend_class_entry {
@@ -195,6 +209,16 @@ impl ClassEntry {
195209
&mut self.inner
196210
}
197211

212+
pub fn from_globals<'a>(class_name: impl AsRef<str>) -> crate::Result<&'a Self> {
213+
let name = class_name.as_ref();
214+
let ptr: *mut Self = find_global_class_entry_ptr(name).cast();
215+
unsafe {
216+
ptr.as_ref().ok_or_else(|| {
217+
crate::Error::ClassNotFound(ClassNotFoundError::new(name.to_string()))
218+
})
219+
}
220+
}
221+
198222
/// Create the object from class and call `__construct` with arguments.
199223
pub fn new_object(&self, arguments: impl AsMut<[ZVal]>) -> crate::Result<ZObject> {
200224
let mut object = self.init_object()?;
@@ -382,7 +406,7 @@ impl PropertyEntity {
382406
}
383407

384408
#[repr(u32)]
385-
#[derive(Debug, Copy, Clone, PartialEq)]
409+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
386410
pub enum Visibility {
387411
Public = ZEND_ACC_PUBLIC,
388412
Protected = ZEND_ACC_PROTECTED,

phper/src/functions.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
use std::{
2727
convert::TryInto,
2828
marker::PhantomData,
29-
mem::{forget, size_of, zeroed},
29+
mem::{size_of, transmute, zeroed},
3030
os::raw::c_char,
3131
ptr::null_mut,
3232
};
@@ -93,7 +93,7 @@ where
9393
) {
9494
unsafe {
9595
let this = execute_data.get_this_mut().unwrap();
96-
let this = StatefulObj::from_z_obj(this);
96+
let this = this.as_mut_stateful_obj();
9797
let r = (self.f)(this, arguments);
9898
*return_value = r.into();
9999
}
@@ -225,8 +225,7 @@ impl ZendFunction {
225225
ptr.as_mut().expect("ptr shouldn't be null")
226226
}
227227

228-
#[inline]
229-
pub fn as_ptr(&self) -> *const zend_function {
228+
pub const fn as_ptr(&self) -> *const zend_function {
230229
&self.inner
231230
}
232231

@@ -368,16 +367,10 @@ unsafe extern "C" fn invoke(execute_data: *mut zend_execute_data, return_value:
368367
}
369368

370369
let mut arguments = execute_data.get_parameters_array();
370+
let arguments = arguments.as_mut_slice();
371371

372372
// TODO catch_unwind for call, translate some panic to throwing Error.
373-
handler.call(execute_data, &mut arguments, return_value);
374-
375-
// Do not call the drop method, because there is the `zend_vm_stack_free_args`
376-
// call after executing function.
377-
// TODO remove after arguments become reference.
378-
for argument in arguments {
379-
forget(argument);
380-
}
373+
handler.call(execute_data, transmute(arguments), return_value);
381374
}
382375

383376
pub(crate) const fn create_zend_arg_info(

0 commit comments

Comments
 (0)