Commit Graph

343 Commits

Author SHA1 Message Date
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
1875e99e93
Pass args to startup function (#226) 2023-02-07 22:24:33 +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
7ed31ebda9
chore: Prepare 0.10.0 (#215) 2022-12-22 22:55:07 +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
4ca5c0d06e
honour PHP_CONFIG & rebuild automatically when env vars change (#210)
Closes https://github.com/davidcole1340/ext-php-rs/issues/208
Closes https://github.com/davidcole1340/ext-php-rs/issues/209

## Summary of the changes

### Build scripts
* the `unix_build.rs` script now honors the `PHP_CONFIG` environment variable, like `cargo php install`
* use `cargo:rerun-if-env-changed` for the `PHP`, `PHP_CONFIG` and `PATH` environment variables, to avoid needless recompilation of the whole dependency tree.

### Documentation
While trying to document the aforementioned changes, I realized that there was no chapter about installing and setting up a PHP environment to develop PHP extensions. So, I refactored the first chapters of the book into a `Getting Started` section, including instructions on how to quickly set up a PHP environment.
2022-12-11 20:08:50 +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
Pierre Tondereau
9e08e253dc
Update Cargo.toml (#207) 2022-11-28 21:08:00 +01:00
Pierre Tondereau
7423da060d
Revert "chore: use php-discovery to find matching PHP build" (#206) 2022-11-28 14:57:34 +01:00
Saif Eddin Gmati
31712066c8
chore: use php-discovery to find matching PHP build (#201) 2022-11-26 13:09:59 -08: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
580ad9f462
Describes restrictions on generic parameters for php_class (#194) 2022-11-24 11:11:23 +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
3b1e2e3848
check docs warnings in CI (#180)
* check docs warnings in CI

* works

* format
2022-11-13 21:13:25 +13:00
David Cole
a160f2a2a0 Bump version to v0.8.2 2022-11-11 12:11:33 +13:00
David Cole
eafd3b4471
fix type links in docs.rs (#179)
[ci skip]
2022-11-11 12:08:23 +13:00
Pierre Tondereau
b72d0555d6
chore(cli): Bump Clap for CLI tool (#177) 2022-11-10 10:59:49 +01: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
Niklas Mollenhauer
997fded715
Add example that shows how to implement an interface (#167)
* Add example that shows how to implement an interface

* Add missing uses

* Fix some compilation issues
2022-10-23 13:09:56 +13:00
David Cole
76358ede3c rustfmt... 2022-10-23 12:06:22 +13:00
David Cole
1f0582b10d
fix describe when using #[implements] (#169)
[ci skip]
2022-10-23 12:02:00 +13:00
Denzyl Dick
ad048d0e05
Update lib.rs (#168)
Correct field name.

[ci skip]
2022-10-22 21:33:48 +13:00
David Cole
296c3add84 add note to update docs.rs bindings
[ci skip]
2022-10-16 13:13:17 +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
David Cole
6a598ded3d
fix building docs on docs.rs (#165)
* fix building docs on docs.rs

accidentally removed the docs.rs stub bindings feature in
664981f4fb. docs.rs only has php 7.4 and
therefore cannot build ext-php-rs, so stub bindings are generated prior.

* update docs.rs stub bindings
2022-10-16 12:49:02 +13:00
Dirk Stolle
6766786db5
Update changelog for latest versions (#161)
[ci skip]
2022-10-13 10:37:17 +13:00
David Cole
669e024feb Bump version to v0.8.1 2022-10-12 11:18:22 +13:00
Robert O'Rourke
8a81c4b7f8
Allow passing --yes parameter to bypass prompts (#135)
* Allow passing `--yes` parameter to bypass prompts

Makes this tool usable in automated builds such as Docker containers.

Addresses https://github.com/davidcole1340/ext-php-rs/issues/133

* Update readme and guides

* rustfmt

Co-authored-by: David Cole <david.cole1340@gmail.com>
2022-10-01 12:14:15 +13:00
Robert O'Rourke
7dac4010a0
Fix INI file truncation and duplication (#136)
Addresses https://github.com/davidcole1340/ext-php-rs/issues/134

Also prevents adding the extension include line twice.
2022-10-01 11:24:36 +13:00
Bradley Schofield
1da812cf78
Add php-scrypt as a example project (#146) 2022-10-01 11:24:19 +13:00
Joe Hoyle
d4ef116b31
Support marking methods as abstract (#154)
For classes that are registered with `#[php_impl]` this allows functions to be marked as abstract.
2022-10-01 11:24:04 +13:00
Joe Hoyle
5d1fda4666
Support marking classes as interfaces (#155)
* Support marking classes as interfaces

This allows passing flags as part of `#[php_class(flags=Interface]` etc, which allows one to mark a class as being an interface.

When a class is an interface, it also shouldn't get a constructor created for it.

* rustfmt
2022-10-01 11:23:48 +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
Tobias Bengtsson
73902ef017
Merge pull request #153 from joehoyle/add-name-function-macro
Add ability to set function name on php_function macro
2022-09-30 13:04:22 +02:00
Joe Hoyle
18ede97712 Add ability to set function name on php_function macro
This is much the same as `php_class(name="ABC")`
2022-09-30 12:01:35 +02:00
Tobias Bengtsson
199962ce3f
Merge pull request #151 from striezel-stash/gh-action-update
update actions/checkout in GitHub Actions workflows to v3
2022-09-28 23:04:44 +02:00
Tobias Bengtsson
0d8aec2a77
Merge pull request #159 from striezel-stash/run-clippy-only-on-stable
ci: run clippy only on stable Rust channel
2022-09-28 23:02:39 +02:00
Dirk Stolle
a13f2b99cc update actions/checkout in GitHub Actions workflows to v3 2022-09-28 22:11:20 +02:00
Dirk Stolle
ca5a7c21f7 ci: run clippy only on stable Rust channel
Sparked by the comments about nightly clippy breaking the build
too often (see <https://github.com/davidcole1340/ext-php-rs/pull/151#issuecomment-1261151479>),
this change will limit clippy to builds with a stable Rust
version only.
2022-09-28 22:08:10 +02:00
Tobias Bengtsson
81d1861a1d
Merge pull request #158 from striezel-stash/fix-clap-errors
Use arg instead of deprecated clap attribute
2022-09-28 22:01:28 +02:00
Dirk Stolle
7b238eede1 attempt to fix errors related to clap
This should fix the errors from the build at
<https://github.com/davidcole1340/ext-php-rs/actions/runs/3145521955/jobs/5112909446>:

       Compiling clap v4.0.0
    error: Unknown `#[clap(long)]` attribute (`#[arg(long)] exists)
      --> crates/cli/src/lib.rs:92:12
       |
    92 |     #[clap(long)]
       |            ^^^^

    error: Unknown `#[clap(long)]` attribute (`#[arg(long)] exists)
       --> crates/cli/src/lib.rs:115:12
        |
    115 |     #[clap(long)]
        |            ^^^^

    error: Unknown `#[clap(short)]` attribute (`#[arg(short)] exists)
       --> crates/cli/src/lib.rs:134:12
        |
    134 |     #[clap(short, long)]
        |            ^^^^^

    error: could not compile `cargo-php` due to 3 previous errors
    warning: build failed, waiting for other jobs to finish...
    Error: Process completed with exit code 101.

These errors were probably caused by the release of `clap` 4.0.0
a few hours ago.
2022-09-28 21:05:56 +02:00
Tobias Bengtsson
04dd0cc990
Merge pull request #157 from striezel-stash/clippy-strikes-again
fix more causes of clippy warnings
2022-09-28 18:21:08 +02:00
Dirk Stolle
ee45e8dc73 fix more causes of clippy warnings
Clippy linting had more errors:

    error: iterating on a map's values
    Error:   --> crates/macros/src/module.rs:56:36
       |
    56 |       let registered_classes_impls = state
       |  ____________________________________^
    57 | |         .classes
    58 | |         .iter()
    59 | |         .map(|(_, class)| generate_registered_class_impl(class))
       | |________________________________________________________________^
       |
       = note: `-D clippy::iter-kv-map` implied by `-D warnings`
       = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#iter_kv_map
    help: try
       |
    56 ~     let registered_classes_impls = state
    57 +         .classes.values().map(|class| generate_registered_class_impl(class))
       |

    error: iterating on a map's values
    Error:    --> crates/macros/src/module.rs:366:23
        |
    366 |         let classes = self.classes.iter().map(|(_, class)| class.describe());
        |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `self.classes.values().map(|class| class.describe())`
        |
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#iter_kv_map

    error: could not compile `ext-php-rs-derive` due to 2 previous errors
2022-09-28 12:34:12 +02:00