Commit graph

322 commits

Author SHA1 Message Date
Olly Betts
f5e2d044df Remove set but not used variables
Highlighted by clang-15 warnings such as:

warning: variable 'index' set but not used [-Wunused-but-set-variable]
2022-06-30 12:52:37 +12:00
Olly Betts
9cdf46e8c8 [php] Adjust #required params for subclass methods
If the same method name is implemented in a parent class then the
subclass can't have more required parameters than that or else we
get a compatibility error when the module is loaded.

The testsuite wasn't catching this problem because it was no longer
trying to load the modules for testcases without _runme.php, because
the mechanism to do that relied on there being a generated .php
wrapper, which we no longer have by default.  Fix that to provide a
regression test for this fix.

See #2151
2022-06-14 16:00:46 +12:00
Olly Betts
c95df57976 [php] Fix missing parameter type declarations
In some cases of overloaded functions the parameter type information was
missing for some or all parameters.

See #2151
2022-06-12 09:22:27 +12:00
Olly Betts
2df27036b7 [php] Skip directorout typemap if PHP exception pending
Otherwise can end up with a second PHP exception if the directorout
typemap doesn't accept PHP Null.  `SWIG_fail` in this case results
in us returning to PHP which then propagates the pending exception.

This commit fixes a failure in smoketest.php in Xapian's PHP bindings,
but I've not managed to come up with a reproducer which works for
SWIG's testsuite.
2022-06-10 16:01:05 +12:00
Olly Betts
2de2efb0bb Fix PHP parameter type declarations compatibility
Ensure PHP parameter type declarations are compatible with those for
the same method in any classes we inherit from.

See #2151
2022-06-10 11:12:54 +12:00
Olly Betts
d43d62cd7a Revert "Fix PHP parameter type declarations compatibility"
I missed that this change breaks cpp_static, which seems to be to do
with handling the combined getter and setter we emit for static
member variables.  Reverting while I figure out how to handle that.

This reverts commit a277748870.
2022-06-09 17:55:09 +12:00
Olly Betts
a277748870 Fix PHP parameter type declarations compatibility
Ensure PHP parameter type declarations are compatible with those for
the same method in any classes we inherit from.

See #2151
2022-06-09 17:36:11 +12:00
Olly Betts
a8994d4186 Rename zend_types to be clearer
It's actually tracking pointer types which aren't wrapped as classes.
2022-06-09 12:49:30 +12:00
Olly Betts
d7ed1a8b97 [php] Eliminate sprintf to temp buffer to create ZVAL
Use zend_strpprintf() which handles this more cleanly without risk of
buffer overflow and without a fixed length limit.
2022-06-09 09:06:12 +12:00
Olly Betts
97a109ecc9 WIP: Refactor pararent class tracking 2022-06-08 17:41:00 +12:00
William S Fulton
6939d91e4c Header file tidyup
Fix Visual C++ warning in scilab.cxx:
  warning C4996: 'strtok': This function or variable may be unsafe.
2022-05-07 11:56:06 +01:00
Olly Betts
55377bdc08 Add DOH Exit() and SetExitHandler()
Exit() is a wrapper for exit() by default, but SetExitHandler() allows
specifying a function to call instead.

This means that failures within DOH (e.g. Malloc() failing due to lack
of memory) will now perform cleanup such as removing output files.

This commit also cleans up exit statuses so SWIG should now reliably
exit with status 0 if the run was successful and status 1 if there was
an error (or a warning and -Werror was in effect).

Previously in some situations SWIG would try to exit with the status set
to the number of errors encountered, but that's problematic - for
example if there were 256 errors this would result in exit status 0 on
most platforms.  Also some error statuses have special meanings e.g.
those defined by <sysexits.h>.

Also SWIG/Javascript tried to exit with status -1 in a few places (which
typically results in exit status 255).
2022-03-06 12:33:54 +13:00
Olly Betts
1707d6b89b [PHP] Fix cleanup code handling issues
Fix to call cleanup code in exception situations and not to invoke
the freearg typemap twice in certain situations.

Fixes https://sourceforge.net/p/swig/bugs/1211/
2022-02-17 13:52:44 +13:00
Olly Betts
66488e677d [php] Make zend_function_entry arrays const 2022-01-24 14:09:36 +13:00
Olly Betts
7b929dce82 [php] Fix director upcall check
This resolves an issue uncovered by adding a _runme.php for testcase
director_alternating.
2022-01-22 20:01:00 +13:00
Olly Betts
9b10534879 [php] Make internal function wrappers static 2022-01-22 13:03:58 +13:00
Olly Betts
1c76000e38 [php] Add type declarations for optional params too
See #2151
2022-01-21 16:34:59 +13:00
Olly Betts
91470ca62d [php] Actually generate parameter type declarations 2022-01-21 13:27:54 +13:00
Olly Betts
d43f28a666 [php] Use -prefix setting in type declarations 2022-01-21 11:08:24 +13:00
Olly Betts
c417250b4e [php] Allow testing if an object is SWIG-wrapped
Since the switch to wrapping classes using PHP's C API, we now
internally need to be able to tell if a PHP object is of or derived
from a class that is wrapped by SWIG so we know if we can offset the
zend_object pointer to get to the swig_object_wrapper.  If we try to
do this to an object which isn't wrapped by SWIG then we invoke C/C++
undefined behaviour (and typically get a segmentation fault).

This check is implemented by having a SWIG\wrapped empty interface which
we make all SWIG-wrapped classes implement simply so we can test for it
to detect such classes.

Fixes #2125
2022-01-20 14:42:02 +13:00
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