Skip to content

Commit 92d2f2d

Browse files
committed
Revert "simplify with macro"
This reverts commit 7f4d4c4.
1 parent 58af242 commit 92d2f2d

File tree

3 files changed

+34
-36
lines changed

3 files changed

+34
-36
lines changed

phper/src/macros.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,3 @@ macro_rules! sg {
109109
$crate::sys::sapi_globals.$x
110110
};
111111
}
112-
113-
/// Define a class stub
114-
#[macro_export]
115-
macro_rules! define_class_stub {
116-
($name:expr, $cls_var:ident) => {{
117-
use std::{cell::RefCell, rc::Rc};
118-
119-
let $cls_var: Rc<RefCell<Option<_>>> = Rc::new(RefCell::new(None));
120-
let entity = ::phper::classes::ClassEntity::new($name);
121-
(entity, $cls_var)
122-
}};
123-
}

tests/integration/src/classes.rs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use phper::{
1616
types::{ArgumentTypeHint, ReturnTypeHint},
1717
values::ZVal,
1818
};
19-
use std::{collections::HashMap, convert::Infallible, rc::Rc};
19+
use std::{collections::HashMap, convert::Infallible};
20+
use std::rc::Rc;
2021

2122
pub fn integrate(module: &mut Module) {
2223
integrate_a(module);
@@ -234,39 +235,50 @@ fn integrate_bar_extends_foo(module: &mut Module, foo_class: StateClass<Foo>) {
234235
fn integrate_dependent_classes(module: &mut Module) {
235236
const A_CLS: &str = r"IntegrationTest\Dependency\A";
236237
const B_CLS: &str = r"IntegrationTest\Dependency\B";
238+
// Build both class entities
239+
let mut a_cls = ClassEntity::new(A_CLS);
240+
let mut b_cls = ClassEntity::new(B_CLS);
237241

238-
let (mut a_cls, a_ref) = define_class_stub!(A_CLS, a_ref);
239-
let (mut b_cls, b_ref) = define_class_stub!(B_CLS, b_ref);
242+
// Placeholder, will clone StateClass into closures after registration
243+
let a_cls_ref = std::rc::Rc::new(std::cell::RefCell::new(None));
244+
let b_cls_ref = std::rc::Rc::new(std::cell::RefCell::new(None));
240245

241246
{
242-
let b_ref = Rc::clone(&b_ref);
243-
a_cls
244-
.add_static_method("createB", Visibility::Public, move |_| {
245-
let borrow = b_ref.borrow();
246-
let b_state: &StateClass<()> = borrow.as_ref().unwrap();
247-
Ok::<_, phper::Error>(b_state.init_object()?)
248-
})
249-
.return_type(ReturnType::new(ReturnTypeHint::ClassEntry(B_CLS.into())));
247+
let b_cls_ref = Rc::clone(&b_cls_ref);
248+
a_cls.add_static_method("createB", Visibility::Public, move |_| {
249+
let borrow = b_cls_ref.borrow();
250+
let b_state: &StateClass<()> = borrow
251+
.as_ref()
252+
.expect("B class not registered");
253+
let obj = b_state.init_object()?;
254+
Ok::<_, phper::Error>(obj)
255+
})
256+
.return_type(ReturnType::new(ReturnTypeHint::ClassEntry(String::from(B_CLS))));
250257
}
251258

252259
{
253-
let a_ref = Rc::clone(&a_ref);
254-
b_cls
255-
.add_static_method("createA", Visibility::Public, move |_| {
256-
let borrow = a_ref.borrow();
257-
let a_state: &StateClass<()> = borrow.as_ref().unwrap();
258-
Ok::<_, phper::Error>(a_state.init_object()?)
259-
})
260-
.return_type(ReturnType::new(ReturnTypeHint::ClassEntry(A_CLS.into())));
260+
let a_cls_ref = Rc::clone(&a_cls_ref);
261+
b_cls.add_static_method("createA", Visibility::Public, move |_| {
262+
let borrow = a_cls_ref.borrow();
263+
let a_state: &StateClass<()> = borrow
264+
.as_ref()
265+
.expect("A class not registered");
266+
let obj = a_state.init_object()?;
267+
Ok::<_, phper::Error>(obj)
268+
})
269+
.return_type(ReturnType::new(ReturnTypeHint::ClassEntry(String::from(A_CLS))));
261270
}
262271

263-
// Register both classes
272+
273+
// Register both classes and save the StateClass into shared RefCells
264274
let a_state = module.add_class(a_cls);
265275
let b_state = module.add_class(b_cls);
266-
*a_ref.borrow_mut() = Some(a_state.clone());
267-
*b_ref.borrow_mut() = Some(b_state.clone());
276+
277+
*a_cls_ref.borrow_mut() = Some(a_state);
278+
*b_cls_ref.borrow_mut() = Some(b_state);
268279
}
269280

281+
270282
#[cfg(phper_major_version = "8")]
271283
fn integrate_stringable(module: &mut Module) {
272284
use phper::{functions::ReturnType, types::ReturnTypeHint};

tests/integration/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
// See the Mulan PSL v2 for more details.
1010

1111
#![warn(rust_2018_idioms, clippy::dbg_macro, clippy::print_stdout)]
12-
#[macro_use]
13-
extern crate phper;
1412

1513
mod arguments;
1614
mod arrays;

0 commit comments

Comments
 (0)