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

Add wrapper for zend_exec_stringl with exception handling #67

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

vodik
Copy link
Collaborator

@vodik vodik commented Sep 14, 2021

Add a wrapper around eval. This function exposes zend's behaviour (expects an expression which is automatically prepended with "return"), which is different PHP's builtin's behaviour.

I'm not sure if it would make sense to make a PHP equivalent eval as well (should be really easy to make), so I don't know if we want to be conservative with the exported name.

The eval portion works nicely, but I have no idea how to go from ZendObject to PHPException. Currently working around this by shoving the ZendObject into Error, but this feels icky.

Comment on lines +23 to +24
unsafe { ext_php_rs_executor_globals().as_mut() }
.expect("Static executor globals were invalid")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept this private because I don't know in general if this is thread safe. As I understand it, the take_exception function should be safe.

std::mem::swap(&mut exception_ptr, &mut globals.exception);

if !exception_ptr.is_null() {
Some(unsafe { Box::from_raw(exception_ptr) })
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand the php source code, we're responsible for freeing it if we take it.

};

/// The main result type which is passed by the library.
pub type Result<T> = std::result::Result<T, Error>;

/// The main error type which is passed by the library inside the custom
/// [`Result`] type.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Clone)]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, what's the benefit of having ordering on an error enum?

@vodik
Copy link
Collaborator Author

vodik commented Sep 14, 2021

Example in use:

#[php_function]
pub fn example_eval() -> Result<Vec<String>> {
    ext_php_rs::php::eval::eval("['a', 'b', 'c']").unwrap().extract()
}

@vodik
Copy link
Collaborator Author

vodik commented Sep 14, 2021

Also I suspect this might be a bug, but it seems like wrapped functions are unable to directly return zvals? pub fn example_eval() -> Result<Zval> won't compile

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

Successfully merging this pull request may close these issues.

1 participant