Skip to content

Commit 5c93cd5

Browse files
committed
Remove generic type of Objects.
1 parent c121d23 commit 5c93cd5

File tree

18 files changed

+260
-255
lines changed

18 files changed

+260
-255
lines changed

examples/hello/src/lib.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@ use phper::{
1414
functions::Argument,
1515
ini::{Ini, Policy},
1616
modules::{Module, ModuleContext},
17-
objects::Object,
17+
objects::ZObj,
1818
php_get_module,
1919
values::ZVal,
2020
};
2121

22-
fn module_init(_args: ModuleContext) -> bool {
23-
true
24-
}
25-
2622
fn say_hello(arguments: &mut [ZVal]) -> phper::Result<String> {
2723
let name = &mut arguments[0];
2824
name.convert_to_string();
@@ -49,7 +45,7 @@ pub fn get_module() -> Module {
4945
Ini::add("hello.description", "hello world.".to_owned(), Policy::All);
5046

5147
// register hook functions
52-
module.on_module_init(module_init);
48+
module.on_module_init(|_: ModuleContext| true);
5349
module.on_module_shutdown(|_| true);
5450
module.on_request_init(|_| true);
5551
module.on_request_shutdown(|_| true);
@@ -79,7 +75,7 @@ pub fn get_module() -> Module {
7975
foo_class.add_method(
8076
"getFoo",
8177
Visibility::Public,
82-
|this: &mut Object<()>, _: &mut [ZVal]| {
78+
|this: &mut ZObj<()>, _: &mut [ZVal]| {
8379
let prop = this.get_property("foo");
8480
Ok::<_, phper::Error>(prop.clone())
8581
},
@@ -88,7 +84,7 @@ pub fn get_module() -> Module {
8884
foo_class.add_method(
8985
"setFoo",
9086
Visibility::Public,
91-
|this: &mut Object<()>, arguments: &mut [ZVal]| -> phper::Result<()> {
87+
|this: &mut ZObj<()>, arguments: &mut [ZVal]| -> phper::Result<()> {
9288
this.set_property("foo", arguments[0].clone());
9389
Ok(())
9490
},

examples/http-client/src/client.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
use phper::{
1717
classes::{ClassEntry, DynamicClass, Visibility},
1818
functions::Argument,
19-
objects::Object,
19+
objects::ZObj,
2020
};
2121
use reqwest::blocking::{Client, ClientBuilder, RequestBuilder};
2222
use std::time::Duration;
@@ -32,7 +32,7 @@ pub fn make_client_builder_class() -> DynamicClass<ClientBuilder> {
3232
Visibility::Public,
3333
|this, arguments| {
3434
let ms = arguments[0].as_long().unwrap();
35-
let state: &mut ClientBuilder = this.as_mut_state();
35+
let state: &mut ClientBuilder = unsafe { this.as_mut_state() };
3636
replace_and_set(state, |builder| {
3737
builder.timeout(Duration::from_millis(ms as u64))
3838
});
@@ -46,7 +46,7 @@ pub fn make_client_builder_class() -> DynamicClass<ClientBuilder> {
4646
Visibility::Public,
4747
|this, arguments| {
4848
let enable = arguments[0].as_bool().unwrap();
49-
let state = this.as_mut_state();
49+
let state = unsafe { this.as_mut_state() };
5050
replace_and_set(state, |builder| builder.cookie_store(enable));
5151
Ok::<_, HttpClientError>(this.duplicate())
5252
},
@@ -57,11 +57,13 @@ pub fn make_client_builder_class() -> DynamicClass<ClientBuilder> {
5757
"build",
5858
Visibility::Public,
5959
|this, _arguments| {
60-
let state = this.as_mut_state();
60+
let state = unsafe { this.as_mut_state() };
6161
let client = replace_and_get(state, ClientBuilder::build)?;
6262
let mut object = ClassEntry::<Option<Client>>::from_globals(HTTP_CLIENT_CLASS_NAME)?
6363
.init_object()?;
64-
*object.as_mut_state() = Some(client);
64+
unsafe {
65+
*object.as_mut_state() = Some(client);
66+
}
6567
Ok::<_, HttpClientError>(object)
6668
},
6769
vec![],
@@ -76,7 +78,7 @@ pub fn make_client_class() -> DynamicClass<Option<Client>> {
7678
class.add_method(
7779
"__construct",
7880
Visibility::Private,
79-
|_: &mut Object<Option<Client>>, _| {},
81+
|_: &mut ZObj<Option<Client>>, _| {},
8082
vec![],
8183
);
8284

@@ -85,12 +87,14 @@ pub fn make_client_class() -> DynamicClass<Option<Client>> {
8587
Visibility::Public,
8688
|this, arguments| {
8789
let url = arguments[0].as_z_str().unwrap().to_str().unwrap();
88-
let client = this.as_state().as_ref().unwrap();
90+
let client = unsafe { this.as_state().as_ref().unwrap() };
8991
let request_builder = client.get(url);
9092
let mut object =
9193
ClassEntry::<Option<RequestBuilder>>::from_globals(REQUEST_BUILDER_CLASS_NAME)?
9294
.init_object()?;
93-
*object.as_mut_state() = Some(request_builder);
95+
unsafe {
96+
*object.as_mut_state() = Some(request_builder);
97+
}
9498
Ok::<_, HttpClientError>(object)
9599
},
96100
vec![Argument::by_val("url")],
@@ -101,12 +105,14 @@ pub fn make_client_class() -> DynamicClass<Option<Client>> {
101105
Visibility::Public,
102106
|this, arguments| {
103107
let url = arguments[0].as_z_str().unwrap().to_str().unwrap();
104-
let client = this.as_state().as_ref().unwrap();
108+
let client = unsafe { this.as_state().as_ref().unwrap() };
105109
let request_builder = client.post(url);
106110
let mut object =
107111
ClassEntry::<Option<RequestBuilder>>::from_globals(REQUEST_BUILDER_CLASS_NAME)?
108112
.init_object()?;
109-
*object.as_mut_state() = Some(request_builder);
113+
unsafe {
114+
*object.as_mut_state() = Some(request_builder);
115+
}
110116
Ok::<_, HttpClientError>(object)
111117
},
112118
vec![Argument::by_val("url")],

examples/http-client/src/request.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use crate::{errors::HttpClientError, response::RESPONSE_CLASS_NAME, utils::replace_and_get};
1212
use phper::{
1313
classes::{ClassEntry, DynamicClass, Visibility},
14-
objects::Object,
14+
objects::ZObj,
1515
};
1616
use reqwest::blocking::{RequestBuilder, Response};
1717

@@ -23,19 +23,21 @@ pub fn make_request_builder_class() -> DynamicClass<Option<RequestBuilder>> {
2323
class.add_method(
2424
"__construct",
2525
Visibility::Private,
26-
|_: &mut Object<Option<RequestBuilder>>, _| {},
26+
|_: &mut ZObj<Option<RequestBuilder>>, _| {},
2727
vec![],
2828
);
2929

3030
class.add_method(
3131
"send",
3232
Visibility::Public,
3333
|this, _arguments| {
34-
let state = this.as_mut_state();
34+
let state = unsafe { this.as_mut_state() };
3535
let response = replace_and_get(state, |builder| builder.unwrap().send())?;
3636
let mut object =
3737
ClassEntry::<Option<Response>>::from_globals(RESPONSE_CLASS_NAME)?.init_object()?;
38-
*object.as_mut_state() = Some(response);
38+
unsafe {
39+
*object.as_mut_state() = Some(response);
40+
}
3941
Ok::<_, HttpClientError>(object)
4042
},
4143
vec![],

examples/http-client/src/response.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::{errors::HttpClientError, utils::replace_and_get};
1212
use phper::{
1313
arrays::{InsertKey, ZArray},
1414
classes::{DynamicClass, Visibility},
15-
objects::Object,
15+
objects::ZObj,
1616
values::ZVal,
1717
};
1818
use reqwest::blocking::Response;
@@ -25,8 +25,8 @@ pub fn make_response_class() -> DynamicClass<Option<Response>> {
2525
class.add_method(
2626
"body",
2727
Visibility::Public,
28-
|this: &mut Object<Option<Response>>, _arguments| {
29-
let response = this.as_mut_state();
28+
|this: &mut ZObj<Option<Response>>, _arguments| {
29+
let response = unsafe { this.as_mut_state() };
3030
let body = replace_and_get(response, |response| {
3131
response
3232
.ok_or(HttpClientError::ResponseHadRead)
@@ -41,12 +41,12 @@ pub fn make_response_class() -> DynamicClass<Option<Response>> {
4141
"status",
4242
Visibility::Public,
4343
|this, _arguments| {
44-
let response =
45-
this.as_state()
46-
.as_ref()
47-
.ok_or_else(|| HttpClientError::ResponseAfterRead {
48-
method_name: "status".to_owned(),
49-
})?;
44+
let response = unsafe { this.as_state() }.as_ref().ok_or_else(|| {
45+
HttpClientError::ResponseAfterRead {
46+
method_name: "status".to_owned(),
47+
}
48+
})?;
49+
5050
Ok::<_, HttpClientError>(response.status().as_u16() as i64)
5151
},
5252
vec![],
@@ -56,12 +56,13 @@ pub fn make_response_class() -> DynamicClass<Option<Response>> {
5656
"headers",
5757
Visibility::Public,
5858
|this, _arguments| {
59-
let response =
59+
let response = unsafe {
6060
this.as_state()
6161
.as_ref()
6262
.ok_or_else(|| HttpClientError::ResponseAfterRead {
6363
method_name: "headers".to_owned(),
64-
})?;
64+
})?
65+
};
6566
let headers_map =
6667
response
6768
.headers()

phper-alloc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# phper-alloc
22

3-
Alloc related items for [phper](https://crates.io/crates/phper).
3+
Allocator related items for [phper](https://crates.io/crates/phper).
44

55
## License
66

phper-macros/src/derives.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn parse_throwable_input(
9393

9494
Ok((quote! {
9595
impl #crate_ident::errors::Throwable for #input_ident {
96-
fn class_entry(&self) -> &#crate_ident::classes::StatelessClassEntry {
96+
fn class_entry(&self) -> &#crate_ident::classes::ClassEntry {
9797
match self {
9898
#(#class_entry_arms)*
9999
}
@@ -116,7 +116,7 @@ fn parse_throwable_input(
116116
}
117117
Data::Struct(_) => Ok((quote! {
118118
impl #crate_ident::errors::Throwable for #input_ident {
119-
fn class_entry(&self) -> &#crate_ident::classes::StatelessClassEntry {
119+
fn class_entry(&self) -> &#crate_ident::classes::ClassEntry {
120120
ClassEntry::from_globals(#exception).unwrap()
121121
}
122122
}

phper-sys/php_wrapper.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,15 @@ void phper_convert_to_long(zval *op) {
305305
void phper_convert_to_string(zval *op) {
306306
convert_to_string(op);
307307
}
308+
309+
void phper_zend_object_release(zend_object *obj) {
310+
zend_object_release(obj);
311+
}
312+
313+
const zend_object_handlers *phper_z_obj_ht_p(const zval *zv) {
314+
return Z_OBJ_HT_P(zv);
315+
}
316+
317+
zend_object *phper_z_obj_p(const zval *zv) {
318+
return Z_OBJ_P(zv);
319+
}

phper/src/arrays.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,14 @@
1010

1111
//! Apis relate to [crate::sys::zend_array].
1212
13-
use crate::{
14-
alloc::EBox,
15-
strings::{ZStr, ZString},
16-
sys::*,
17-
values::ZVal,
18-
};
13+
use crate::{strings::ZStr, sys::*, values::ZVal};
1914
use derive_more::From;
2015
use phper_alloc::ToRefOwned;
2116
use std::{
2217
borrow::Borrow,
2318
convert::TryInto,
2419
marker::PhantomData,
25-
mem::{forget, zeroed},
20+
mem::forget,
2621
ops::{Deref, DerefMut},
2722
};
2823

0 commit comments

Comments
 (0)