Skip to content

Commit cc66842

Browse files
committed
Remove a lot of unused codes.
1 parent 1ea7015 commit cc66842

23 files changed

+376
-1364
lines changed
File renamed without changes.

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ A library that allows us to write PHP extensions using pure Rust and using safe
66

77
## Requirement
88

9-
- rust nightly channel (now)
109
- libclang >= 9
1110

1211
## Usage

examples/hello/src/lib.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
1-
use once_cell::sync::Lazy;
1+
use std::{fs::OpenOptions, io::Write};
2+
23
use phper::{
3-
c_str_ptr, php_function, php_get_module, php_minfo_function, php_minit_function,
4-
php_mshutdown_function, php_rinit_function, php_rshutdown_function,
4+
c_str_ptr,
5+
classes::{Class, MethodEntity, StdClass, This},
6+
functions::create_zend_arg_info,
7+
ini::Policy,
8+
modules::{read_global_module, write_global_module, Module, ModuleArgs},
9+
php_function, php_get_module, php_minfo_function, php_minit_function, php_mshutdown_function,
10+
php_rinit_function, php_rshutdown_function,
511
sys::{
612
php_info_print_table_end, php_info_print_table_row, php_info_print_table_start,
713
zend_function_entry, OnUpdateBool, PHP_INI_SYSTEM,
814
},
9-
zend::{
10-
api::{FunctionEntries, ModuleGlobals},
11-
classes::{Class, MethodEntity, StdClass, This},
12-
compile::{create_zend_arg_info, MultiInternalArgInfo},
13-
ini::{IniEntries, Policy},
14-
modules::{
15-
read_global_module, write_global_module, Module, ModuleArgs, ModuleEntry,
16-
ModuleEntryBuilder,
17-
},
18-
types::{ExecuteData, SetVal, Val},
19-
},
15+
values::{ExecuteData, Val},
2016
};
21-
use std::{fs::OpenOptions, io::Write};
2217

2318
// static HELLO_ENABLE: ModuleGlobals<bool> = ModuleGlobals::new(false);
2419
//
@@ -108,22 +103,22 @@ use std::{fs::OpenOptions, io::Write};
108103
// module
109104
// }
110105

111-
fn module_init(args: ModuleArgs) -> bool {
106+
fn module_init(_args: ModuleArgs) -> bool {
112107
// append_file("module_init");
113108
true
114109
}
115110

116-
fn module_shutdown(args: ModuleArgs) -> bool {
111+
fn module_shutdown(_args: ModuleArgs) -> bool {
117112
// append_file("module_shutdown");
118113
true
119114
}
120115

121-
fn request_init(args: ModuleArgs) -> bool {
116+
fn request_init(_args: ModuleArgs) -> bool {
122117
// append_file("request_init");
123118
true
124119
}
125120

126-
fn request_shutdown(args: ModuleArgs) -> bool {
121+
fn request_shutdown(_args: ModuleArgs) -> bool {
127122
// append_file("request_shutdown");
128123
true
129124
}
@@ -154,7 +149,7 @@ pub extern "C" fn get_module() -> *const ::phper::sys::zend_module_entry {
154149
module.set_name(env!("CARGO_PKG_NAME"));
155150
module.set_version(env!("CARGO_PKG_VERSION"));
156151

157-
module.add_ini("hello.enable", "on", Policy::All);
152+
module.add_ini("hello.enable", "off", Policy::All);
158153

159154
module.on_module_init(module_init);
160155
module.on_module_shutdown(module_shutdown);

examples/hello/src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use phper::cmd::make;
2+
3+
fn main() {
4+
make();
5+
}

phper/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ keywords = ["php", "binding", "extension"]
1212
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1313

1414
[dependencies]
15+
anyhow = "1.0.40"
16+
clap = "3.0.0-beta.2"
1517
once_cell = "1.7.2"
1618
phper-alloc = { version = "0.2", path = "../phper-alloc" }
1719
phper-macros = { version = "0.2", path = "../phper-macros" }

phper/src/zend/classes.rs renamed to phper/src/classes.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,3 @@
1-
use crate::{
2-
sys::{
3-
phper_init_class_entry_ex, zend_class_entry, zend_declare_property_long,
4-
zend_function_entry, zend_internal_arg_info, zend_register_internal_class, zval,
5-
},
6-
zend::{
7-
api::{invoke, method_invoke, FunctionEntry},
8-
compile::Visibility,
9-
types::Val,
10-
},
11-
};
12-
use once_cell::sync::OnceCell;
131
use std::{
142
mem::zeroed,
153
os::raw::c_int,
@@ -20,6 +8,18 @@ use std::{
208
},
219
};
2210

11+
use once_cell::sync::OnceCell;
12+
13+
use crate::{
14+
functions::{method_invoke, FunctionEntry},
15+
sys::{
16+
phper_init_class_entry_ex, zend_class_entry, zend_declare_property_long,
17+
zend_function_entry, zend_internal_arg_info, zend_register_internal_class, zval,
18+
ZEND_ACC_PRIVATE, ZEND_ACC_PROTECTED, ZEND_ACC_PUBLIC,
19+
},
20+
values::Val,
21+
};
22+
2323
pub trait Method: Send + Sync {
2424
fn call(&self, this: &mut This, arguments: &mut [Val], return_value: &mut Val);
2525
}
@@ -28,7 +28,7 @@ impl<F> Method for F
2828
where
2929
F: Fn(&mut This) + Send + Sync,
3030
{
31-
fn call(&self, this: &mut This, arguments: &mut [Val], return_value: &mut Val) {
31+
fn call(&self, this: &mut This, _arguments: &mut [Val], _return_value: &mut Val) {
3232
self(this)
3333
}
3434
}
@@ -199,3 +199,11 @@ impl PropertyEntity {
199199
}
200200
}
201201
}
202+
203+
#[repr(u32)]
204+
#[derive(Debug, Copy, Clone, PartialEq)]
205+
pub enum Visibility {
206+
Public = ZEND_ACC_PUBLIC,
207+
Protected = ZEND_ACC_PROTECTED,
208+
Private = ZEND_ACC_PRIVATE,
209+
}

phper/src/cmd.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use crate::sys::PHP_EXTENSION_DIR;
2+
use anyhow::Context;
3+
use clap::Clap;
4+
use std::{
5+
env,
6+
ffi::{CStr, OsString},
7+
fs,
8+
path::{Path, PathBuf},
9+
};
10+
11+
/// Make utility.
12+
#[derive(Clap)]
13+
struct Make {
14+
#[clap(subcommand)]
15+
sub: SubCommand,
16+
}
17+
18+
#[derive(Clap)]
19+
enum SubCommand {
20+
Install(InstallCommand),
21+
}
22+
23+
#[derive(Clap)]
24+
struct InstallCommand {}
25+
26+
pub fn make() {
27+
try_make().expect("make failed");
28+
}
29+
30+
pub fn try_make() -> anyhow::Result<()> {
31+
let make: Make = Make::parse();
32+
match make.sub {
33+
SubCommand::Install(_) => {
34+
let (lib_path, ext_name) = get_lib_path_and_ext_name()?;
35+
let extension_dir = CStr::from_bytes_with_nul(PHP_EXTENSION_DIR)?.to_str()?;
36+
println!("Installing shared extensions: {}", extension_dir);
37+
let ext_path = Path::new(extension_dir).join(ext_name);
38+
fs::copy(lib_path, ext_path)?;
39+
}
40+
}
41+
Ok(())
42+
}
43+
44+
fn get_lib_path_and_ext_name() -> anyhow::Result<(PathBuf, OsString)> {
45+
let exe_path = env::current_exe()?;
46+
let exe_stem = exe_path
47+
.file_stem()
48+
.context("failed to get current exe stem")?;
49+
let target_dir = exe_path
50+
.parent()
51+
.context("failed to get current exe directory")?;
52+
53+
let mut exe_name = OsString::new();
54+
exe_name.push("lib");
55+
exe_name.push(exe_stem);
56+
exe_name.push(".so");
57+
58+
let mut ext_name = OsString::new();
59+
ext_name.push(exe_stem);
60+
ext_name.push(".so");
61+
62+
Ok((target_dir.join(exe_name), ext_name))
63+
}
File renamed without changes.

phper/src/functions.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
use std::{
2+
cell::Cell,
3+
ffi::CStr,
4+
mem::{size_of, transmute, zeroed},
5+
os::raw::{c_char, c_int},
6+
ptr::null,
7+
};
8+
9+
use crate::{
10+
classes::Method,
11+
ini::create_ini_entry_ex,
12+
sys::{
13+
_zend_get_parameters_array_ex, phper_z_strval_p, zend_arg_info, zend_execute_data,
14+
zend_function_entry, zend_ini_entry_def, zend_internal_arg_info, zend_uchar, zif_handler,
15+
zval,
16+
},
17+
values::{ExecuteData, Val},
18+
};
19+
20+
pub struct Parameters;
21+
22+
pub trait Function: Send + Sync {
23+
fn call(&self, arguments: &mut [Val], return_value: &mut Val);
24+
}
25+
26+
impl<F> Function for F
27+
where
28+
F: Fn() + Send + Sync,
29+
{
30+
fn call(&self, _arguments: &mut [Val], _return_value: &mut Val) {
31+
self()
32+
}
33+
}
34+
35+
#[repr(transparent)]
36+
pub struct FunctionEntry {
37+
inner: zend_function_entry,
38+
}
39+
40+
pub(crate) struct FunctionEntity {
41+
pub(crate) name: String,
42+
pub(crate) handler: Box<dyn Function>,
43+
}
44+
45+
pub(crate) unsafe extern "C" fn invoke(
46+
execute_data: *mut zend_execute_data,
47+
return_value: *mut zval,
48+
) {
49+
let execute_data = ExecuteData::from_mut(execute_data);
50+
let return_value = Val::from_mut(return_value);
51+
52+
let num_args = execute_data.common_num_args();
53+
let arg_info = execute_data.common_arg_info();
54+
55+
let last_arg_info = arg_info.offset(num_args as isize);
56+
let handler = (*last_arg_info).name as *const Box<dyn Function>;
57+
let handler = handler.as_ref().expect("handler is null");
58+
59+
// TODO Do num args check
60+
61+
let mut arguments = execute_data.get_parameters_array();
62+
63+
handler.call(&mut arguments, return_value);
64+
}
65+
66+
pub(crate) unsafe extern "C" fn method_invoke(
67+
execute_data: *mut zend_execute_data,
68+
return_value: *mut zval,
69+
) {
70+
let execute_data = ExecuteData::from_mut(execute_data);
71+
let return_value = Val::from_mut(return_value);
72+
73+
let num_args = execute_data.common_num_args();
74+
let arg_info = execute_data.common_arg_info();
75+
76+
let last_arg_info = arg_info.offset(num_args as isize);
77+
let handler = (*last_arg_info).name as *const Box<dyn Method>;
78+
let handler = handler.as_ref().expect("handler is null");
79+
80+
// TODO Do num args check
81+
82+
let mut arguments = execute_data.get_parameters_array();
83+
84+
handler.call(execute_data.get_this(), &mut arguments, return_value);
85+
}
86+
87+
pub const fn create_zend_arg_info(
88+
name: *const c_char,
89+
pass_by_ref: bool,
90+
) -> zend_internal_arg_info {
91+
#[cfg(any(
92+
phper_php_version = "8.0",
93+
phper_php_version = "7.4",
94+
phper_php_version = "7.3",
95+
phper_php_version = "7.2"
96+
))]
97+
{
98+
zend_internal_arg_info {
99+
name,
100+
type_: 0 as crate::sys::zend_type,
101+
pass_by_reference: pass_by_ref as zend_uchar,
102+
is_variadic: 0,
103+
}
104+
}
105+
106+
#[cfg(any(phper_php_version = "7.1", phper_php_version = "7.0",))]
107+
{
108+
zend_internal_arg_info {
109+
name,
110+
class_name: std::ptr::null(),
111+
type_hint: 0,
112+
allow_null: 0,
113+
pass_by_reference: pass_by_ref as zend_uchar,
114+
is_variadic: 0,
115+
}
116+
}
117+
}

0 commit comments

Comments
 (0)