As mentioned in #219, I believe we're making incorrect use of IS_CALLABLE on Zval types right now. Zval type bits are never actually stored as IS_CALLABLE, which overlaps with the _actual_ type value of IS_INDIRECT. IS_INDIRECT is almost the same as IS_REFERENCE, but is a direct reference to another Zval rather than reference object. As `Zval::is_callable()` and `Zval::callable()` don't actually make use of `IS_CALLABLE` then I think it's safe to switch this out.
* feat(zend): add helper for try catch and bailout in PHP
* feat(try): add bindings for bailout
* fix(try): add missing feature flag for test
* feat(try): add a test that expose memory leak problem
* feat(try): make bailout unsafe and explain why
* feat(bailout): flag bailout as a panic function
* feat(embed): add try catch on script / eval
* feat(embed): add embed features, add test example which run php inside it
* feat(embed): use a guard to prevent running in parallel
* chore(ci): update actions to not build and test with embed, add a specific build for embed testing
* feat(embed): correcly start / shutdown embed api
* chore(ci): use stable for rust in embed test
* feat(embed): add documentation, manage potential errors
* Add ability to show PHP warnings (etc)
I don't believe there's a way for extensions to trigger PHP notices or warnings currently. This is done using the `php_error_docref` function. I've placed a function in `ext_php_rs::php_error()` however, there might be a better place?
* Remove uneeded empty value
* Fix url
PHP extensions that want to provide ini settings can do so using `IniEntryDef::register()`; values can then be fetched via `GlobalExecutor::ini_values()`.
Currently we only support throwing exception class entries, i.e. stateless exceptions. Ideally we also want to support throwing a ZVal which has a ClassEntry that extends the Exception PHP class. This can be used to throw stateful exceptions which is not uncommon.
This PR is missing one piece: `throw_object` will currently `drop` the Zval when the function is completed, causing reference / null pointer errors. It seems `ZVal::Drop` doesn't actually free the zval currently, it just sets the type to NULL (which also breaks things.) Discussed briefly in https://discord.com/channels/115233111977099271/1025314959179120714 on how best to solve this, but I'm not totally clear still! Ideally I think we want `throw_object` to own the `zval` but not free it once the function returns.
Closes https://github.com/davidcole1340/ext-php-rs/issues/200
## Rationale
In PHP zend_strings are binary strings with no encoding information. They can contain any byte at any position.
The current implementation use `CString` to transfer zend_strings between Rust and PHP, which prevents zend_strings containing null-bytes to roundtrip through the ffi layer. Moreover, `ZendStr::new()` accepts only a `&str`, which is incorrect since a zend_string is not required to be valid UTF8.
When reading the PHP source code, it is apparent that most functions marked with `ZEND_API` that accept a `const *char` are convenience wrappers that convert the `const *char` to a zend_string and delegate to another function. For example [zend_throw_exception()](eb83e0206c/Zend/zend_exceptions.c (L823)) takes a `const *char message`, and just converts it to a zend_string before delegating to [zend_throw_exception_zstr()](eb83e0206c/Zend/zend_exceptions.c (L795)).
I kept this PR focused around `ZendStr` and it's usages in the library, but it should be seen as the first step of a more global effort to remove usages of `CString` everywhere possible.
Also, I didn't change the return type of the string related methods of `Zval` (e.g. I could have made `Zval::set_string()`
accept an `impl AsRef<[u8]>` instead of `&str` and return `()` instead of `Result<()>`). If I get feedback that it should be done in this PR, I'll do it.
## Summary of the changes:
### ZendStr
* [BC break]: `ZendStr::new()` and `ZendStr::new_interned()` now accept an `impl AsRef<[u8]>` instead of just `&str`, and are therefore infaillible (outside of the cases where we panic, e.g. when allocation fails). This is a BC break, but it's impact shouldn't be huge (users will most likely just have to remove a bunch of `?` or add a few `Ok()`).
* [BC break]: Conversely, `ZendStr::as_c_str()` now returns a `Result<&CStr>` since it can fail on strings containing null bytes.
* [BC break]: `ZensStr::as_str()` now returns a `Result<&str>` instead of an `Option<&str>` since we have to return an error in case of invalid UTF8.
* adds method `ZendStr::as_bytes()` to return the underlying byte slice.
* adds convenience methods `ZendStr::as_ptr()` and `ZendStr::as_mut_ptr()` to return raw pointers to the zend_string.
### ZendStr conversion traits
* adds `impl AsRef<[u8]> for ZendStr`
* [BC break]: replaces `impl TryFrom<String> for ZBox<ZendStr>` by `impl From<String> for ZBox<ZendStr>`.
* [BC break]: replaces `impl TryFrom<&str> for ZBox<ZendStr>` by `impl From<&str> for ZBox<ZendStr>`.
* [BC break]: replaces `impl From<&ZendStr> for &CStr` by `impl TryFrom<&ZendStr> for &CStr`.
### Error
* adds new enum member `Error::InvalidUtf8` used when converting a `ZendStr` to `String` or `&str`
* Add some standard zend interfaces
The zend API also has some standard interfaces it exposes:
c8c09b4aae/Zend/zend_interfaces.h (L27-L33)
```
extern ZEND_API zend_class_entry *zend_ce_traversable;
extern ZEND_API zend_class_entry *zend_ce_aggregate;
extern ZEND_API zend_class_entry *zend_ce_iterator;
extern ZEND_API zend_class_entry *zend_ce_arrayaccess;
extern ZEND_API zend_class_entry *zend_ce_serializable;
extern ZEND_API zend_class_entry *zend_ce_countable;
extern ZEND_API zend_class_entry *zend_ce_stringable;
```
This surfaced in #163 and should make it possible to implement these interfaces.
* Add some links to the php documentation
* update docs.rs bindings
Co-authored-by: David Cole <david.cole1340@gmail.com>
* Preliminary Windows support
* Start work on cross-platform build script
* Fix compilation on macOS
* Updated README, tidied up build script
* Check linker version before starting compilation
It doesn't seem like it's possible to change the linker from within the
build script, however, we can retrieve the linker in use and give the
user a suggestion if the linker will not work.
* Switch to using Github repository for bindgen
* Split Windows and Unix implementations into two files
* Fix building on Windows
* Remove `reqwest` and `zip` as dependencies on Unix
* Fix guide tests on Windows
* Started work on Windows CI
* runs -> run
* Use preinstalled LLVM on Windows
* Debugging for Windows CI
* Switch to upstream `rust-bindgen` master branch
* Switch to `rust-lld` for Windows linking
* Don't compile `cargo-php` on Windows
* Switch to using skeptic for tests
* cargo-php: Disable stub generation, fix ext install/remove
The plan is to replace the stub generation by generating them with PHP
code. This is cross-platform and means we don't need to worry about ABI.
We also don't need to embed information into the library.
* cargo-php: Fix on unix OS
* Fix clippy lint
* Updated README
* Re-add CI for Unix + PHP 8.0
* Fix building on thread-safe PHP
* Tidy up build scripts
* Use dynamic lookup on Linux, test with TS Windows
* Define `ZTS` when compiling PHP ZTS
* Combine Windows and Unix CI, fix linking for Win32TS
* Fix exclusions in build CI
* rust-toolchain -> rust
* Set LLVM version
* Only build docs.rs on Ubuntu PHP 8.1
* Fix build on Linux thread-safe
* Update guide example