Commit Graph

352 Commits

Author SHA1 Message Date
Olly Betts 1f1349741f
[php] Generate PHP type declarations
We now automatically generate PHP type declarations for PHP >= 8.0.

The generated code still compiles with PHP 7.x but without type declarations.
2022-01-20 10:07:44 +13:00
Olly Betts 499fb74707 Remove unused Printf parameters 2021-12-18 11:24:00 +13:00
Olly Betts 4672318b42 Make some generated variables static 2021-12-17 22:32:34 +13:00
Olly Betts cf802c63f2 Improve naming of zend_class_entry structs
Previously the zend_class_entry for Foo was named SWIGTYPE_Foo_ce, but
this can collide in some cases - e.g. if there's a class named p_Foo
then its zend_class entry will be SWIGTYPE_p_Foo_ce, but that's the same
as the swig_type_info for a class named p_Foo_ce.
2021-12-17 17:14:54 +13:00
Olly Betts f976927d52 Fix source code comment typo 2021-12-17 14:54:05 +13:00
Olly Betts 78f5404727 Improve generated object handlers
Do more initialisation at module load time.

Use a shared set of handlers for cases when the C/C++ object is
destroyed with free().

Most of the code in the free_obj and create_object handlers is the
same for every wrapped class so factor that out into common functions.
2021-12-17 14:50:49 +13:00
Olly Betts 5da86a1413 Make some generated functions static 2021-12-17 09:38:44 +13:00
Olly Betts 6253583809 Rename php_fetch_object with swig_ prefix
We shouldn't be using symbols starting `php` as that risks collisions
with future symbols defined by PHP.
2021-12-17 09:37:00 +13:00
Olly Betts 9ffa15b307 Tweak source whitespace to match SWIG conventions 2021-12-15 19:40:06 +13:00
Olly Betts 6ef820acf9 [php] Simplify creating overload dispatch name 2021-12-15 19:30:17 +13:00
Olly Betts 98b8578731 [php] Tidy up code which processes in typemaps
The only functional change is that we now recover after
WARN_TYPEMAP_IN_UNDEF better (or at least like most of the other
SWIG backends do).
2021-12-13 16:02:12 +13:00
Olly Betts 48bb7e0e95 Simplify PHP backend code 2021-12-13 09:27:58 +13:00
Olly Betts 7d70402306 Update PHP source comment
We no longer wrap anything as a PHP resource.
2021-12-13 09:27:58 +13:00
Olly Betts bf68b377e6 Remove unused code from PHP backend
These are leftovers from the work on wrapping using only PHP's C API.
2021-12-13 09:27:58 +13:00
Olly Betts fd013c344a [php] Remove unused variable
SWIG_module_entry hasn't actually been used for any of git history
which is over 19 years.
2021-12-10 17:10:35 +13:00
Olly Betts c25df74807 [php7] Use destructor action if present
If there's a destructor, use its action instead of free(ptr)
(for C)/delete ptr (for C++).

Fixes #2108
2021-12-02 19:15:34 +13:00
Olly Betts 17a294cec4 Replace remaining PHP errors with PHP exceptions
`SWIG_ErrorCode()`, `SWIG_ErrorMsg()`, `SWIG_FAIL()` and `goto thrown;`
are no longer supported (these are really all internal implementation
details and none are documented aside from brief mentions in CHANGES
for the first three).  I wasn't able to find any uses at least in FOSS
code via code search tools.

If you are using these:

Use `SWIG_PHP_Error(code,msg);` instead of `SWIG_ErrorCode(code);
SWIG_ErrorMsg(msg);` (which will throw a PHP exception in SWIG >= 4.1
and do the same as the individual calls in older SWIG).

`SWIG_FAIL();` and `goto thrown;` can typically be replaced with
`SWIG_fail;`.  This will probably also work with older SWIG, but
please test with your wrappers if this is important to you.

Fixes #2014
2021-05-26 09:39:43 +12:00
Olly Betts cdc69f9843 php: Throw exceptions instead of using errors
Parameter type errors and some other cases in SWIG-generated wrappers
now throw a PHP exception, which is how PHP's native parameter handling
deals with similar situations.

See #2014, but not closing yet as there may be more cases to convert.
2021-05-25 16:42:12 +12:00
Olly Betts 3c168ef332 Map known PHP interfaces to zend_class_entry*
Most pre-defined interfaces are accessible via zend_class_entry*
variables declared in the PHP C API - we can use these to add
an interface at MINIT time (rather than having to wait until RINIT to
look up by name) by having a mapping from PHP interface name to them.

This will also be a little faster than looking up by name.

Closes #2013
2021-05-25 16:28:44 +12:00
Olly Betts 8dadbcc744 [php] Simplify naming of overloaded method wrappers
The underlying wrapper function is now always named using
ZEND_NAMED_FUNCTION even if it's a method (in PHP a function and
a method only differ in how they're used).
2021-05-12 15:05:09 +12:00
Olly Betts ead90be779 [php] Fix -prefix when there are subclasses
The calls to the parent class' magic __get, __set and __isset methods
weren't getting the prefix.
2021-05-12 15:04:31 +12:00
Olly Betts 228b04974d Clean up includes of PHP API headers
Eliminate redundant and unused includes.

Only include the minimum headers needed before the PHP_MAJOR_VERSION
check in case future PHP versions remove some of the headers we
include.
2021-05-05 10:58:07 +12:00
Olly Betts e59d34481d Improve comment 2021-05-05 10:34:20 +12:00
Olly Betts 32283991c5 Don't generate a .php wrapper file by default
It's now only generated if something to put in it is specified via:

%pragma(php) include=...

or

%pragma(php) code=...
2021-05-04 14:14:56 +12:00
Olly Betts 6e6d720d88 Fix segfault in exception class creation
We can't safely lookup the Exception class entry at MINIT time, but we
can just use zend_ce_exception instead, which will be a bit faster too.
2021-05-04 13:12:15 +12:00
Olly Betts 592f230295 Remove CG(active_class_entry) = NULL;
This has been in the code for a really long time, and doesn't seem
to be required now.  It's not documented by PHP as something we
need to do, and the value seems to always be NULL at this point
already.
2021-05-04 11:36:24 +12:00
Olly Betts 586eb24efe php: Stop using dl()
With modern PHP it only works with the CLI version of PHP, so it's
better to direct users to load the extension via "extension=" in
php.ini.

Suggested by ferdynator in #1529.
2021-05-03 18:42:28 +12:00
Olly Betts 1eabe1b29b Fix type in SWIG_DIRECTOR_CAST 2021-05-03 16:51:18 +12:00
Olly Betts c87047fd39 Merge branch 'master' into gsoc2017-php7-classes-via-c-api 2021-05-03 16:17:02 +12:00
Olly Betts 04bacf689b Implement director-disown for PHP 2021-05-03 16:00:30 +12:00
Olly Betts 9ddc9dceb7 Remove support for $source and $target
These were officially deprecated in 2001, and attempts to use them have
resulted in a warning (including a pointer to what to update them to)
for most if not all of that time.

Fixes #1984
2021-04-30 10:20:14 +12:00
Olly Betts 3c4342e66a Only emit custom free_obj handler if needed
If has_destructor isn't set then the default zend_object_std_dtor
does everything necessary.
2021-04-22 18:44:16 +12:00
Olly Betts a6a52f2f79 Eliminate remaining use of $needNewFlow 2021-04-22 15:14:38 +12:00
Olly Betts 50426aae20 Make PHP directors work more like other languages
A PHP exception now gets translated to a C++ exception to skips over C++
code to get back to PHP, avoiding the need to gate every directorout
typemap on EG(exception).
2021-04-22 14:40:21 +12:00
Olly Betts 10d87100ea Whitespace tweaks 2021-04-22 12:25:13 +12:00
Olly Betts 5e2114501f Pass NULL instead of an empty zend_function_entry 2021-04-21 19:11:21 +12:00
Olly Betts af5030bca1 Just call the internal class entry internal_ce
It's just a local variable, so no need to carefully name it after
the class.
2021-04-21 18:34:26 +12:00
Olly Betts 3f9723b896 Use PTR instead of zv for SWIG_remove() parameter
The parameter is a general pointer, not necessarily a zval.
2021-04-21 18:16:56 +12:00
Olly Betts 33feca7527 Eliminate SWIG_SetZval()
$needNewFlow is now only used for a different hack in a directorout
typemap.
2021-04-21 18:16:08 +12:00
Olly Betts ac676d1a6c Remove bogus zend_string_release() in magic methods
We shouldn't be freeing the property name here.
2021-04-21 18:16:08 +12:00
Olly Betts 508d9f7279 Eliminate unused code in generated __isset methods 2021-04-21 18:16:08 +12:00
Olly Betts c705ef8f32 Use PHP objects instead of resources to wrap pointers
Pointer to member is currently still wrapped as a resource.
2021-04-21 15:40:35 +12:00
Olly Betts f1aaeeea1c Fix -prefix to prefix PHP class names 2021-04-20 18:23:23 +12:00
Olly Betts 7f74c4c1cd Generate list of string compares for __isset()
Previously we checked for a <varname>_get() method instead, but that
will misfire for an method actually called foo_get() in the C++ API
being wrapped.
2021-04-19 16:04:02 +12:00
Olly Betts 84559bc441 Clean up code to generate magic property methods 2021-04-19 15:12:42 +12:00
Olly Betts ed86e68fe1 Remove variable which is set but never used 2021-04-19 11:37:21 +12:00
Olly Betts 0a437cddcc Simplify magic property methods 2021-04-17 19:01:32 +12:00
Olly Betts e76308be92 Simplify globalvariableHandler() further 2021-04-17 07:39:41 +12:00
Olly Betts 2629764e3f Remove remnants of attempts to wrap to PHP global vars
This isn't really workable since PHP doesn't support intercepting
accesses to global variables (nor to static class properties, so
we can't wrap C/C++ global variables that way either).

The _get() and _set() function wrappers actually work and have
been generated for a very long time.
2021-04-17 05:59:18 +12:00
Olly Betts 7ccf3b81df Drop support for long obsolete typemap vars
These were deprecated nearly 20 years ago and attempts to use them
have generated a deprecation warning for most of that time.

Addresses #1984 for PHP.
2021-04-16 16:33:51 +12:00
Olly Betts 4eb666d648 Remove set but not used variable 2021-04-16 16:33:25 +12:00
Olly Betts 54a879d2cc Remove unused typemap variable $classFlag 2021-04-16 15:52:21 +12:00
Olly Betts dcdaaba7ba Eliminate $zend_obj typemap variable
We can just get the zend_class_entry from the swig_type_info when
we need it.
2021-04-16 13:13:24 +12:00
Olly Betts c4ff1ed7cb Remove <module>_{alter,get}_newobject functions
These were added as part of the changes to add director support for
PHP, but have never actually been used by anything SWIG generates,
and they aren't documented so shouldn't be used externally.

Removing these exposed a bug in the arginfo generation where we emitted
a ZEND_ARG_INFO for a varargs "parameter", which this commit also fixes.
2021-04-16 09:42:27 +12:00
Olly Betts a80dad0369 Revert to call_user_function() for PHP 7
zend_call_known_instance_method() was new in PHP 8.0.
2021-04-14 17:01:43 +12:00
Olly Betts 5bd38004c9 Fix calling of protected director methods
Use zend_call_known_instance_method() instead of call_user_function(),
since this way PHP seems to know that context of the call is from within
the same object.

Fixes testcases director_nested and director_protected which were giving
errors for PHP 8 and warnings for PHP 7.
2021-04-14 16:14:10 +12:00
Olly Betts 50f92dca08 Mark wrapped abstract classes
This allows restoring reflection checks in testcase director_abstract.
2021-04-14 10:59:12 +12:00
Olly Betts 836258b9d3 Fix some cases of converting PHP NULL to C++ NULL
Fixes testcase overload_null for PHP 8.0
2021-04-13 16:53:52 +12:00
Olly Betts 37f575b7b4 Merge branch 'master' into gsoc2017-php7-classes-via-c-api 2021-04-13 15:15:09 +12:00
Olly Betts 623dd7a394 Eliminate CALL_METHOD and CALL_METHOD_PARAM_1 macros
These names lack a "SWIG_" prefix to help prevent collisions with code
being wrapped, but they're each only used in one place so just inline
them there.
2021-04-13 14:42:33 +12:00
Olly Betts a216f6ca3c Stop generating unused Zend resource destructors
These aren't used since we stopped using PHP resources to wrap classes.
2021-04-13 13:13:24 +12:00
Olly Betts 09b968474d Removed unused #define-s in generated code 2021-04-13 12:43:02 +12:00
Olly Betts d2542eadf6 Remove redundant cast 2021-04-13 11:16:59 +12:00
Olly Betts 0bf846f56f Eliminate unused Printf args 2021-04-13 10:43:51 +12:00
Olly Betts 0a72bfc630 Eliminate tempPointer variable 2021-04-13 10:18:47 +12:00
Olly Betts 93a288c85a Fix memory leak in SWIG tool 2021-04-13 10:14:29 +12:00
Olly Betts 60e7deda2c Eliminate non-standard $lower_param typemap variable 2021-04-13 10:03:03 +12:00
Olly Betts a0174ea7fe php: Fix testcase li_boost_shared_ptr_bits 2021-04-04 18:58:24 +12:00
Olly Betts 40da8bcbb6 php: Wrap classes using only swig_object_wrapper
We no longer use PHP resources to wrap classes, and the proxy classes no
longer has a _cPtr property.
2021-04-04 07:45:20 +12:00
Olly Betts 0267ee374b Merge branch 'master' into gsoc2017-php7-classes-via-c-api 2021-04-02 08:13:10 +13:00
Olly Betts 18bc3e287b php: Avoid double underscore in generated code
These are reserved by the C++ standard, but we were generating them
in the le_swig__... names.
2021-04-02 08:07:44 +13:00
Olly Betts d2a0956766 Remove NULL check which can never be NULL
A pointer to a struct member can't be NULL!
2021-04-02 07:59:35 +13:00
Olly Betts a1abc692d3 Eliminate per-class dtor function
These are all the same, and the NULL check performed is done inside
zend_objects_destroy_object() anyway, so we can just set the dtor
to zend_objects_destroy_object (which is what in-tree PHP extensions
do.)
2021-04-02 07:58:08 +13:00
Olly Betts c467a66668 Remove debug code 2021-04-01 14:40:32 +13:00
Olly Betts 5838f10aa0 wrap fake class constants via C API 2021-04-01 14:40:32 +13:00
Olly Betts 43457690ac Eliminate another global variable 2021-03-31 11:10:34 +13:00
Olly Betts 46ef0eb9a1 Fix value of $source typemap parameter
This is only present for ancient compatibility so nothing actually
tests it works.
2021-03-31 10:57:34 +13:00
Olly Betts 3b1cc00566 Eliminate 3 List variables
Rather than building up lists of classes and details about them to
generate from at the end, just generate into a new String variable as we
go along.
2021-03-31 10:55:26 +13:00
Olly Betts d24e09c57d Remove now-unused variables and code to set them 2021-03-31 05:02:13 +13:00
Olly Betts 904f5a65d8 Merge branch 'master' into gsoc2017-php7-classes-via-c-api 2021-03-31 04:53:40 +13:00
Olly Betts 2392f6146a Remove variables which are set but never used 2021-03-31 04:39:18 +13:00
Olly Betts c863ca8b1f Use zstring access macros
These are likely to be more future-proof than accessing struct members
directly.
2021-03-29 18:18:37 +13:00
Olly Betts 5156ad4f7b Remove unnecessary NULL check
SWIG_remove() calls either free() or delete, both of which handle
a NULL pointer.
2021-03-29 17:45:03 +13:00
Olly Betts 979d48b0b4 Remove obsolete FIXME
We no longer have the PHP code wrappers.
2021-03-29 17:41:01 +13:00
Olly Betts 3f1286ba4f Use standard SWIG overload dispatch
Instead of having a slightly modified PHP-specific variant of
Swig_overload_dispatch we now advance the ParmList over the this
pointer.
2021-03-29 17:41:01 +13:00
Olly Betts 7142acbf93 Fix whitespace oddities 2021-03-29 17:29:59 +13:00
Olly Betts 0e7d6a4c8d More PHP8 compatibility fixes 2021-03-29 09:37:44 +13:00
Olly Betts 73a149200f Remove more code which is no longer used 2021-03-28 19:47:16 +13:00
Olly Betts 1c5573d0d0 Remove code which is no longer used 2021-03-28 08:54:24 +13:00
Olly Betts c58149bc45 Simplify printing code a little
Remove unused Printf parameter, and use Printv where it's more readable.
2021-03-27 19:59:30 +13:00
Olly Betts dfa5353f27 Eliminate extras hash table
This was used to store custom properties, but we can just ask the PHP
object to store them like it normally would, after checking for our
custom pseudo-properties.
2021-03-27 19:57:37 +13:00
Olly Betts 799cb68a8a Use ZEND_THIS instead of getThis()
getThis(z) checks that z is a PHP object and returns ZEND_THIS if it
is, and NULL otherwise.  In all our uses we know that z is a PHP object
(and we'd try to dereference NULL if it were returned!)
2021-03-27 19:51:24 +13:00
Olly Betts 232308b494 Drop unnecessary casts to swig_object_wrapper* 2021-03-27 19:49:49 +13:00
Olly Betts c03679acef Improve how we allocate swig_object_wrapper
Use zend_object_alloc() and put the zend_object member last so that
Zend can put object properties after it.
2021-03-26 17:15:34 +13:00
Olly Betts 4aff93942a Improve arginfo generation
Fixes some failing examples and testcases.
2021-03-26 15:34:29 +13:00
Olly Betts f77113ea71 php: Eliminate SWIG_ZEND_NAMED_FE
It existed to work around const-correctness issues in older versions of
PHP's C API.  It's conceivable user code might be using it, but unlikely
and the switch to creating classes via the API is a natural time for a
compatibility break.
2021-03-26 13:58:02 +13:00
Olly Betts 2ba0f82720 Merge branch 'master' into gsoc2017-php7-classes-via-c-api 2021-03-26 12:00:59 +13:00
Olly Betts b840911410 Remove outdated FIXME/TODO from php.cxx
See #1529
2021-03-26 11:18:07 +13:00
Olly Betts 8ded9d8dae Merge branch 'master' into gsoc2017-php7-classes-via-c-api 2021-03-25 17:45:17 +13:00
Olly Betts d15fe23c5f Merge branch 'gsoc2017-php7-classes-via-c-api' of https://github.com/nihal95/swig into gsoc2017-php7-classes-via-c-api 2021-03-25 16:25:33 +13:00