Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass Zval as argument to PHP functions #198

Closed
Christian-Rades opened this issue Nov 25, 2022 · 4 comments
Closed

Pass Zval as argument to PHP functions #198

Christian-Rades opened this issue Nov 25, 2022 · 4 comments

Comments

@Christian-Rades
Copy link
Contributor

Hi 👋

First of all thank you for building this crate!

I think there's currently no built-in way to pass a Zval to a function call.
The issue is that Zval does not implement IntoZvalDyn because it is not Clone.
I'm currently working around this like so:

struct ObjAsParamHack{inner: Zval}

impl Clone for ObjAsParamHack {
    fn clone(&self) -> Self {
        Self { inner: self.inner.shallow_clone() }
    }
}

impl IntoZval for ObjAsParamHack {
    const TYPE: DataType = DataType::Reference;

    fn into_zval(self, _persistend: bool) -> Result<_zval_struct, ext_php_rs::error::Error> {
        Ok(self.inner)
    }

    fn set_zval(self, zv: &mut Zval, _persistent: bool) -> Result<(), ext_php_rs::error::Error> {
        let Self { inner } = self;
        *zv = inner;
        Ok(())
    }
}

For context, I'm using it here:
https://github.com/Christian-Rades/Tape/blob/060e3195bf5b4d4651b694d8cc4ebff82db45bf5/src/evaluation/config.rs#L68

So I've got two questions:

  • Is a wrapper struct that implements IntoZvalDyn an ok solution here?
  • Can I contribute this workaround or a different solution to this crate?
@ptondereau
Copy link
Collaborator

Hello! Thank you for your issue and I'll try to answer:
I don't know what twig_env could be, but you can use more concrete type of Zval.
Did you try ZendObject?

@Christian-Rades
Copy link
Contributor Author

Thanks for the Idea, I tried to use a ZendObject type, but I have issues creating an owned version of it. And I fear it also does not implement Clone. Maybe I am missing a conversion function?
I replicated it on a smaller scale (with some ideas in comments):
https://gist.github.com/Christian-Rades/66983ca9de0b817fa52f415c882c04a8

@joehoyle
Copy link
Collaborator

This should have been resolved by #256

@Christian-Rades
Copy link
Contributor Author

Thank you for responding, this seems to be the fix. Nice 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants