Skip to content

Commit c6d9a68

Browse files
committed
Working on binding.
1 parent 02c4305 commit c6d9a68

File tree

10 files changed

+263
-19
lines changed

10 files changed

+263
-19
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
members = [
33
"phper",
44
"phper-sys",
5+
"phper-macros",
56
]

phper-macros/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "phper-macros"
3+
version = "0.1.0"
4+
authors = ["__JM_Joy__ <[email protected]>"]
5+
edition = "2018"
6+
description = "PHP binding."
7+
license-file = "../LICENSE"
8+
keywords = ["php", "binding"]
9+
10+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
11+
12+
[lib]
13+
proc-macro = true
14+
15+
[dependencies]
16+
quote = "1.0.2"
17+
syn = { version = "1.0.11", features = ["full"] }
18+
proc-macro2 = "1.0.6"

phper-macros/src/lib.rs

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
extern crate proc_macro;
2+
extern crate syn;
3+
4+
use proc_macro::TokenStream;
5+
use syn::{parse_macro_input, ItemFn, FnArg, parse_str, ExprLet};
6+
use quote::quote;
7+
use proc_macro2::{Ident, Span};
8+
use syn::punctuated::Punctuated;
9+
use syn::token::Comma;
10+
11+
#[proc_macro_attribute]
12+
pub fn php_function(_attr: TokenStream, input: TokenStream) -> TokenStream {
13+
let input = parse_macro_input!(input as ItemFn);
14+
15+
let ret = &input.sig.output;
16+
let inputs = &input.sig.inputs;
17+
let name = &input.sig.ident;
18+
let body = &input.block;
19+
let attrs = &input.attrs;
20+
21+
let mut inputs = &mut inputs.clone();
22+
internal_function_parameters(&mut inputs);
23+
24+
let name = Ident::new(&format!("zif_{}", name), Span::call_site());
25+
26+
let result = quote! {
27+
#[no_mangle]
28+
#(#attrs)*
29+
fn #name(#inputs) #ret {
30+
#body
31+
}
32+
};
33+
34+
result.into()
35+
}
36+
37+
#[proc_macro_attribute]
38+
pub fn php_minit_function(_attr: TokenStream, input: TokenStream) -> TokenStream {
39+
let input = parse_macro_input!(input as ItemFn);
40+
41+
let inputs = &input.sig.inputs;
42+
let name = &input.sig.ident;
43+
let body = &input.block;
44+
let attrs = &input.attrs;
45+
46+
let mut inputs = &mut inputs.clone();
47+
init_func_args(&mut inputs);
48+
49+
let name = Ident::new(&format!("zm_startup_{}", name), Span::call_site());
50+
51+
let result = quote! {
52+
#[no_mangle]
53+
#(#attrs)*
54+
fn #name(#inputs) -> ::std::os::raw::c_int {
55+
#body
56+
}
57+
};
58+
59+
result.into()
60+
}
61+
62+
#[proc_macro_attribute]
63+
pub fn php_mshutdown_function(_attr: TokenStream, input: TokenStream) -> TokenStream {
64+
let input = parse_macro_input!(input as ItemFn);
65+
66+
let inputs = &input.sig.inputs;
67+
let name = &input.sig.ident;
68+
let body = &input.block;
69+
let attrs = &input.attrs;
70+
71+
let mut inputs = &mut inputs.clone();
72+
shutdown_func_args(&mut inputs);
73+
74+
let name = Ident::new(&format!("zm_shutdown_{}", name), Span::call_site());
75+
76+
let result = quote! {
77+
#[no_mangle]
78+
#(#attrs)*
79+
fn #name(#inputs) -> ::std::os::raw::c_int {
80+
#body
81+
}
82+
};
83+
84+
result.into()
85+
}
86+
87+
#[proc_macro_attribute]
88+
pub fn php_rinit_function(_attr: TokenStream, input: TokenStream) -> TokenStream {
89+
let input = parse_macro_input!(input as ItemFn);
90+
91+
let inputs = &input.sig.inputs;
92+
let name = &input.sig.ident;
93+
let body = &input.block;
94+
let attrs = &input.attrs;
95+
96+
let mut inputs = &mut inputs.clone();
97+
init_func_args(&mut inputs);
98+
99+
let name = Ident::new(&format!("zm_activate_{}", name), Span::call_site());
100+
101+
let result = quote! {
102+
#[no_mangle]
103+
#(#attrs)*
104+
fn #name(#inputs) -> ::std::os::raw::c_int {
105+
#body
106+
}
107+
};
108+
109+
result.into()
110+
}
111+
112+
#[proc_macro_attribute]
113+
pub fn php_rshutdown_function(_attr: TokenStream, input: TokenStream) -> TokenStream {
114+
let input = parse_macro_input!(input as ItemFn);
115+
116+
let inputs = &input.sig.inputs;
117+
let name = &input.sig.ident;
118+
let body = &input.block;
119+
let attrs = &input.attrs;
120+
121+
let mut inputs = &mut inputs.clone();
122+
shutdown_func_args(&mut inputs);
123+
124+
let name = Ident::new(&format!("zm_deactivate_{}", name), Span::call_site());
125+
126+
let result = quote! {
127+
#[no_mangle]
128+
#(#attrs)*
129+
fn #name(#inputs) -> ::std::os::raw::c_int {
130+
#body
131+
}
132+
};
133+
134+
result.into()
135+
}
136+
137+
//#[proc_macro_attribute]
138+
//pub fn zend_parse_parameters(_attr: TokenStream, input: TokenStream) -> TokenStream {
139+
// dbg!(&input);
140+
//
141+
//// let input = parse_macro_input!(input as ExprLet);
142+
//
143+
// input
144+
//}
145+
146+
#[proc_macro_attribute]
147+
pub fn hehe(_attr: TokenStream, input: TokenStream) -> TokenStream {
148+
input
149+
}
150+
151+
fn internal_function_parameters(inputs: &mut Punctuated<FnArg, Comma>) {
152+
inputs.push(parse_str("execute_data: *mut ::phper_sys::zend_execute_data").unwrap());
153+
inputs.push(parse_str("return_value: *mut ::phper_sys::zval").unwrap());
154+
}
155+
156+
fn init_func_args(inputs: &mut Punctuated<FnArg, Comma>) {
157+
inputs.push(parse_str("r#type: ::std::os::raw::c_int").unwrap());
158+
inputs.push(parse_str("module_number: ::std::os::raw::c_int").unwrap());
159+
}
160+
161+
fn shutdown_func_args(inputs: &mut Punctuated<FnArg, Comma>) {
162+
inputs.push(parse_str("r#type: ::std::os::raw::c_int").unwrap());
163+
inputs.push(parse_str("module_number: ::std::os::raw::c_int").unwrap());
164+
}
165+
166+
167+

phper-sys/build.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ use std::path::PathBuf;
55
use std::process::Command;
66

77
fn main() -> Result<(), Box<dyn Error + 'static>> {
8+
println!("cargo:rerun-if-changed=php_wrapper.h");
9+
810
let out_path = PathBuf::from(env::var("OUT_DIR")?);
911

10-
let php_config = env::var("PHPER_PHP_CONFIG").unwrap_or("php-config".to_string());
12+
let php_config = env::var("PHP_CONFIG_PATH").unwrap_or("php-config".to_string());
1113
let output = Command::new(php_config).arg("--includes").output()?.stdout;
1214
let includes = String::from_utf8(output)?;
15+
let includes = includes.trim();
1316
let includes = includes.split(' ').collect::<Vec<_>>();
1417

15-
println!("cargo:rerun-if-changed=php_wrapper.h");
18+
includes.iter().for_each(|include| {
19+
let include = &include[2..];
20+
println!("cargo:include={}", include);
21+
});
1622

1723
let bindings = Builder::default()
1824
.header("php_wrapper.h")

phper-sys/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@
44

55
include!(concat!(env!("OUT_DIR"), "/php_bindings.rs"));
66

7-
mod r#macro;

phper-sys/src/macro.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

phper/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "phper"
3-
version = "0.0.0"
3+
version = "0.1.0"
44
authors = ["__JM_Joy__ <[email protected]>"]
55
edition = "2018"
66
description = "PHP binding."
@@ -12,3 +12,4 @@ keywords = ["php", "binding"]
1212

1313
[dependencies]
1414
phper-sys = {path = "../phper-sys"}
15+
phper-macros = {path = "../phper-macros"}

phper/src/alloc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use core::alloc::{GlobalAlloc, Layout};
22
use core::ffi::c_void;
33

4-
use phper_sys::efree;
5-
use phper_sys::emalloc;
4+
use crate::emalloc;
5+
use crate::efree;
66

77
pub struct EAllocator;
88

phper/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1+
extern crate phper_macros;
2+
13
pub mod alloc;
4+
mod macros;
5+
6+
pub use phper_macros::*;
7+
8+

phper/src/macros.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#[macro_export]
2+
macro_rules! emalloc {
3+
( $( $x:expr ),* ) => {
4+
::phper_sys::_emalloc( $( $x, )* )
5+
};
6+
}
7+
8+
#[macro_export]
9+
macro_rules! efree {
10+
( $( $x:expr ),* ) => {
11+
::phper_sys::_efree( $( $x, )* )
12+
};
13+
}
14+
15+
#[macro_export]
16+
macro_rules! zend_call_num_args {
17+
($execute_data: expr) => {
18+
(* $execute_data).This.u2.num_args
19+
};
20+
}
21+
22+
#[macro_export]
23+
macro_rules! zval_str {
24+
($return_value: expr, $s: expr) => {
25+
let __z: *mut ::phper_sys::zval = $return_value;
26+
let __s: *mut ::phper_sys::zend_string = $s;
27+
$crate::z_str_p!(__z) = __s;
28+
};
29+
}
30+
31+
32+
// zval *__z = (z); \
33+
// zend_string *__s = (s); \
34+
// Z_STR_P(__z) = __s; \
35+
// /* interned strings support */ \
36+
// Z_TYPE_INFO_P(__z) = ZSTR_IS_INTERNED(__s) ? \
37+
// IS_INTERNED_STRING_EX : \
38+
// IS_STRING_EX; \
39+
40+
41+
42+
43+
#[macro_export]
44+
macro_rules! z_str_p {
45+
($zval_p: expr) => {
46+
$crate::z_str!(*$zval_p)
47+
};
48+
}
49+
50+
#[macro_export]
51+
macro_rules! z_str {
52+
($zval: expr) => {
53+
($zval).value.str
54+
};
55+
}
56+
57+
58+

0 commit comments

Comments
 (0)