Commit Graph

122 Commits

Author SHA1 Message Date
Pierre Tondereau
6b6489fddb
chore: fix clippy warnings. (#235) 2023-03-13 18:00:45 +01:00
Pierre Tondereau
e6afecbddd
chore(clippy): Fix clippy warnings. (#228) 2023-02-07 22:52:56 +01:00
Joe Hoyle
0b27dc349f
Fix Zval IS_PTR type detection (#223) 2023-02-07 22:25:15 +01:00
Joe Hoyle
831b3c8597
Mate GlobalExecutor::get_mut() public (#227) 2023-02-07 22:21:53 +01:00
Christian Rades
87ac43d05e
Add is_identical for zvals (#217) 2023-01-19 12:40:24 +01:00
Pierre Tondereau
11e0998bc4
feat: Add support for PHP 8.2 (#212)
## Small Fixes due to PHP8.2 upgrade:
- Internal PHP `ZendHashmap` changed the way that we access to the bucket ([commit](90b7bde615 (diff-dca4782c62f10b418ea7193e4641e688ffd7a9c97869f2b1a3a96e28da5653aaL371)))
- Internal PHP `ArrayIterator` doesn't support parent `get_iterator` ([commit](15bbf6f337 (diff-bc002b59b592b4d44c13d898f9dc3ca60b1b1c8505a51a2a3557dfde51dfeb61L273)))
2022-12-20 17:11:35 +01:00
Pierre Tondereau
6965f4a198
Prepare v0.9.0 (#211) 2022-12-11 22:10:25 +01:00
ju1ius
d52a878e7b
feat: allows ZendStr to contain null bytes (#202)
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`
2022-12-09 10:54:17 +01:00
ju1ius
a331213670
Add instance_of() and get_class_entry() methods on ZendObject (#197)
* adds `ZendObject::get_class_entry()` to retrieve the class entry of an object without casting pointers
* adds `ZendObject::instance_of()` to allow more idiomatic instanceof checks.
* adds a mention that `ZendObject::is_instance::<T>()` does not check the parent classes or interfaces. This bit me when I tried to check if `my_object.is_instance::<MyInterface>()` and it didn't work.
2022-11-24 11:17:12 +01:00
ju1ius
3d742262c8
Add get_id() and hash() methods on ZendObject (#196) 2022-11-24 11:07:37 +01:00
ju1ius
9a105abb63
fixes CI workflow configuration (#195)
* upgrades LLVM to v14
* migrates from the unmaintained `action-rs/*` actions to [dtolnay/rust-toolchain](https://github.com/dtolnay/rust-toolchain), using [Swatinem/rust-cache](https://github.com/Swatinem/rust-cache/) as a cache layer.
* adds a cache layer for LLVM build
* adds a weekly cron schedule for all workflows
* fixes an issue in the docblocks
2022-11-24 09:05:36 +01:00
David Cole
8d2ad7d418
fix binary slice lifetimes (#181) 2022-11-19 08:58:18 +01:00
ju1ius
4ea01f8d98
fixes inifinte loop in ClassEntry::instance_of (#188) 2022-11-16 07:25:42 +01:00
David Cole
eafd3b4471
fix type links in docs.rs (#179)
[ci skip]
2022-11-11 12:08:23 +13:00
David Cole
4133a0fe78
add ability to define abstract methods (#171)
* add ability to define abstract methods

`new_abstract` can be used for interface and abstract class methods.

* rustfmt
2022-11-10 13:51:20 +13:00
David Cole
5f33598fcf
add before flag to #[php_startup] (#170)
* add `before` flag to `#[php_startup]`

this calls the user-provided startup function _before_ the classes and
constants registered by the macro system are registered with PHP. by
default the behaviour is to run this function after, which means you
cannot define an interface and use it on a struct.

* cargo fmt
2022-11-10 13:51:05 +13:00
Denzyl Dick
ad048d0e05
Update lib.rs (#168)
Correct field name.

[ci skip]
2022-10-22 21:33:48 +13:00
Niklas Mollenhauer
24d703d955
Add some standard zend interfaces (#164)
* 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>
2022-10-16 13:13:09 +13:00
Joe Hoyle
90cbbc0fca
Specify classes as fully-qualified names in stubs (#156)
* Specify classes as fully-qualified names in stubs

When stubs are generated, the type annotations don't use a loading `\`, which means they are interpreted as relative to the current namespace. That's wrong, as all types are relative to the root namespace.

* rustfmt
2022-10-01 11:23:27 +13:00
Dirk Stolle
b192841cfa fix causes of some clippy warnings
Clippy linting had some errors:

    error: boolean to int conversion using if
    Error:   --> src/builders/module.rs:59:29
       |
    59 |                 zend_debug: if PHP_DEBUG { 1 } else { 0 },
       |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `u8::from(PHP_DEBUG)`
       |
       = note: `-D clippy::bool-to-int-with-if` implied by `-D warnings`
       = note: `PHP_DEBUG as u8` or `PHP_DEBUG.into()` can also be valid options
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_to_int_with_if

    error: boolean to int conversion using if
    Error:   --> src/builders/module.rs:60:22
       |
    60 |                 zts: if PHP_ZTS { 1 } else { 0 },
       |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `u8::from(PHP_ZTS)`
       |
       = note: `PHP_ZTS as u8` or `PHP_ZTS.into()` can also be valid options
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#bool_to_int_with_if

    error: could not compile `ext-php-rs` due to 2 previous errors

This commit tries to fix those.
2022-09-13 00:43:11 +02:00
Dirk Stolle
4bb0559ab8 Fix a few typos 2022-09-10 16:40:38 +02:00
Tobias Bengtsson
976bc7d686 Fix clippy errors 2022-08-13 18:29:15 +02:00
Tobias Bengtsson
231b3005c4 Support for BinarySlice to avoid allocation 2022-07-10 11:24:27 +02:00
Tobias Bengtsson
a110c1710c Fix clippy error in nightly - useless transmute 2022-06-07 12:22:23 +02:00
Tobias Bengtsson
ab85298b37 Fix clippy warning, unused lifetime parameter 2022-05-30 16:14:32 +02:00
David Cole
664981f4fb
Windows support (#128)
* 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
2022-03-18 16:36:51 +13:00
David Cole
a9e1dad59f Add bindings for php_printf 2022-02-24 22:45:32 +13:00
David Cole
16c456de7d 'Fixed' lifetimes for array conversions
Not 100% sure why we were using higher-ranked trait bounds for this. It
doesn't really make sense to me all these months later.
2022-02-22 12:08:35 +13:00
David Cole
a09b8474bb Merge branch 'zval_shallow_clone' 2022-02-22 11:14:34 +13:00
glyphpoch
cbc802fbf2 Fix request_(startup|shutdown)_function in ModuleBuilder
Request specific builder methods were assigning to the wrong fields,
overwriting the MINIT/MSHUTDOWN functions, instead of RINIT/RSHUTDOWN.
2021-12-27 22:03:34 +00:00
Joe Hoyle
910400a1b2
Fix is_true() / is_false() in Zval (#116)
Looks like a copy/pasta
2021-12-14 20:46:15 +13:00
David Cole
f9528f07a8 Add shallow_clone to Zval 2021-12-13 22:43:34 +13:00
David Cole
1f546b2c71
Add functions to check for numerical and/or sequential keys (#115) 2021-12-13 19:58:02 +13:00
David Cole
3598d9cb71 Add FromZval and FromZvalMut implementations for Zval 2021-12-12 21:11:02 +13:00
David Cole
54c6c64fa6
Create properties hashmap once instead of on each call (#114)
This adds `once_cell` as a dependency, however, it is used for both
handlers and properties. Better to be 'safe' than sorry ;)
2021-12-11 23:54:21 +13:00
David Cole
051e469a44 Print full error when using anyhow 2021-11-24 23:11:26 +13:00
David Cole
8d37afc510
Make ClassMetadata: Send + Sync (#111)
This wasn't the case because of `PhantomData<T>` inside the metadata.
Replacing this with `PhantomData<AtomicPtr<T>>` ensures that the
metadata will always be `Send + Sync`.
2021-11-23 18:59:09 +13:00
David Cole
c12dde1866
Add feature to convert anyhow errors to PHP exceptions (#110) 2021-11-23 18:03:25 +13:00
David Cole
6fd31621f2
Add preliminary PHP 8.1 support (#109)
* Bump PHP API version, remove flags removed from PHP API

See following commits:
- 70195c3561
- b5746a4c7f

* Add PHP 8.1 to CI

* Clippy lint

* Fix PHP 8.1 support with new features
2021-11-21 20:00:51 +13:00
David Cole
d9e40ea4d2
Ensure CLI-Extension compatibility (#108)
* Change describe types to C ABI, check ext-php-rs version

Rust doesn't have a stable ABI so the describe function and types are
now C ABI compatible, however, the internal types `Cow`, `Vec`, `Option`
aren't.

Check `ext-php-rs` versions to check compatibility between the CLI and
the extension.

* CLI requires 0.7.1

* Bump versions

* Replace standard library types with ABI-stable replacements

* Change option type
2021-11-21 17:27:12 +13:00
David Cole
6df362b714
Added CLI crate for stubs, installation and removal (#107)
* started work on stub generator

* Worked on stub CLI tool

* Unused import

* Account for namespaces in function and class names

* Add support for docblocks on structs

* Push Rust comments to stubs

* Add indentation to stub generation

* Add CLI application to install and generate stubs

This time CLI application is defined on user side, called with `cargo
run -- ..args..`

* Export anyhow result

* Add constants to stub file

* Removed stub symbols

No longer required as we are now building while also linking to PHP.
Keeping the stubs causes the stubs to override the real symbols in the
extension.

* Fix stubs for real this time

Removed stub symbols as they were being included in the extension
dylib, fix by loading the PHP executable as a dylib, loading the
required symbols globally.

* Maybe actually fix stubs this time

* Forgot to remove PHP binary loading

* let's give this another go... cargo subcommand

Now called via `cargo php <install,stubs>`.

* Added `remove` command

* Tidied up cargo-php, commented, set up CI

* Fix return types with non-ident types

* define namespace ordering

* Fix tests, replace `Self` when in outer context

* Moved allowed bindings into separate file

* Update guide with CLI instructions
2021-11-20 14:19:02 +13:00
David Cole
bedade8bb3
Fix builds on aarch64 Linux (#106) 2021-11-14 16:01:00 +13:00
David Cole
d0d05678b3 Replace box with in-struct object when building class 2021-11-05 23:41:30 +13:00
David Cole
88b84b9a17
Disable serialization and unserialization on classes (#105)
* Disable serialization and unserialization on classes

Classes that have associated Rust types cannot be serialized for
obvious reasons so these need to be disabled. Disabling these actions
changes in PHP 8.1 to use a flag, so that will need to be solved with
PHP 8.1 support. Closes #97

* update docs stubs
2021-10-19 02:10:02 +13:00
David Cole
b47f8ba160
Allow ZendClassObject as self parameter (#103)
* Allow `ZendClassObject` as `self` parameter

* Fixed tests

* Updated guide

* Updated changelog
2021-10-10 22:46:26 +13:00
David Cole
466c1658e3
Refactor module paths (#101)
* Refactor module layout

* Fixed documentation tests

* Removed skel, moved macro crate

* Ignore folders for crate publish

* Fix builder for zts

* Add `rustfmt.toml`, wrap all comments #96

* Fixed up documentation links, tidied up

* Add `Zend` prefix to callable and hashtable

* Updated guide types

* Updated changelog
2021-10-10 17:51:55 +13:00
David Cole
39790191e8
Fixed ArgParser lifetimes (#100)
* Fixed `ArgParser` lifetimes

Now stores a mutable reference to the underlying zval. Instead of
passing the execution data to the arg parser, a vector of args is now
passed to prevent a shared lifetime issue with the `$this` object.

* Implememt from traits for classes
2021-10-07 04:02:59 +13:00
David
3403cba7bf
Add ZBox<T> to replace owned variants (#94)
* Added `ZBox` and `ZBoxable`

* Implement `ZBoxable` for `ZendStr`

Build for all f eatures on CI

* Replace `OwnedZendObject` with `ZBox<ZendObject>`

* Replace `ClassObject` with `ZBox<ZendClassObject>`

Fixed deserialization bug

Panic when uninitialized - better than UB

* Replace `OwnedHashTable` with `ZBox<HashTable>`

* Remove `MaybeUninit` from `ZendClassObject`
2021-10-05 16:59:41 +13:00
David
125ec3bc94
Add ability to add proper object constructor (#83) 2021-10-03 19:53:54 +13:00
David
c16c5d48cb
Add #[derive(ZvalConvert)] macro (#78) 2021-10-03 18:00:50 +13:00