Skip to content

Introduce FFI::Scope class #16327

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft

Introduce FFI::Scope class #16327

wants to merge 2 commits into from

Conversation

dstogov
Copy link
Member

@dstogov dstogov commented Oct 9, 2024

This is a refactoring of user level ext/FFI API.
The most important changes are in ffi.stub.php.

Now FFI::cdef, FFI::load and FFI::scope return an instance of new FFI\Scope class instead of FFI.

There is no way to instantiate an object of class FFI. Methods FFI::new, FFI:cast, FFI::type may be called only statically. The deprecation introduced by 4acf008 is removed. (I can't find the RFC corresponding to this deprecation).

FFI\Scope::new, FFI\Scope::cast, FFI\Scope::type can't be called as static methods.

Most tests are not affected by this change, but of course this patch may break some user code.

@nielsdos
Copy link
Member

nielsdos commented Oct 9, 2024

@dstogov This is the RFC that made the static methods deprecated: https://wiki.php.net/rfc/deprecate_functions_with_overloaded_signatures#fficast_ffinew_and_ffitype this happened in 8.3. It was part of a larger RFC.
This PR is a draft, so I suppose this isn't ready yet for review, and GitHub is just confusing me by saying it already did a request for review?

@dstogov
Copy link
Member Author

dstogov commented Oct 9, 2024

@nielsdos Thanks for pointing to the RFC.

The implementation should be good enough, but this PR requires RFC and I don't like to post it now.
It would be great to hear your initial opinion.

Copy link
Member

@nielsdos nielsdos left a comment

Choose a reason for hiding this comment

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

I think, this path forward makes sense.

@@ -71,6 +71,19 @@ public static function isNull(FFI\CData $ptr): bool {}

namespace FFI {

/** @not-serializable */
Copy link
Member

Choose a reason for hiding this comment

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

Often, @strict-properties is added to final non-serializable classes alike these. But no strong preference.

@arnaud-lb
Copy link
Member

I understand that the goal is to address https://wiki.php.net/rfc/deprecate_functions_with_overloaded_signatures#fficast_ffinew_and_ffitype in an alternative way?

This makes sense, and there are almost no BC breaks, except in at least these two cases:

  • Code with FFI type hints. Such code can be made compatible with <8.3..master by typing FFI|FFI\Scope (However static analyzers may not be happy about calls to ->new() on a variable of type FFI|FFI\Scope if the method does not exist in both classes. This could be annoying but not really a BC break.)
  • Code calling static methods with the syntax $instance::method() may break, as FFI\Scope does not have the static methods. We could mitigate this by adding the static methods to FFI\Scope.

One issue that is not solved is that the methods new, type, and cast collide with C function calls. All instance calls on FFI\Scope forward to C, except those, so it is not be possible to call C functions with these names. Also, adding new methods on Scope in the future would be a BC break. Maybe we could deprecated these methods on FFI\Scope?

@dstogov
Copy link
Member Author

dstogov commented Oct 11, 2024

I understand that the goal is to address https://wiki.php.net/rfc/deprecate_functions_with_overloaded_signatures#fficast_ffinew_and_ffitype in an alternative way?

Yes. This is an API re-factoring to avoid static/non-static mess.

* Code calling static methods with the syntax `$instance::method()` may break, as `FFI\Scope` does not have the static methods. We could mitigate this by adding the static methods to `FFI\Scope`.

There is no sense in static methods, because they can't reach symbols loaded into the instance of the Scope.

One issue that is not solved is that the methods new, type, and cast collide with C function calls. All instance calls on FFI\Scope forward to C, except those, so it is not be possible to call C functions with these names. Also, adding new methods on Scope in the future would be a BC break. Maybe we could deprecated these methods on FFI\Scope?

These non-static methods are necessary (now static methods are deprecated).
I don't see a big problem in reserving new, type, cast and prefer to keep API compatibility.
There are no plans to add new Scope methods.

This is a refactoring of user level ext/FFI API.
The most important changes are in ``ffi.stub.php``.

Now FFI::cdef, FFI::load and FFI::scope return an instance of new
FFI\Scope class instead of FFI.

There us no way to instantiate an object of class FFI.
Methods FFI::new, FFI:cast, FFI::type may be called only statically.
The deprecation introduced by
https://wiki.php.net/rfc/ffi-non-static-deprecated is removed.

FFI\Scope::new, FFI\Scope::cast, FFI\Scope::type can't be called as
static methods.

Most tests are not affcted by this change, but of course this may break
some user code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants