Commit graph

252 commits

Author SHA1 Message Date
William S Fulton
319442a8c4 More move semantics improvements
More removal of casts in the out typemaps when copying objects to enable
C++ compilers to possibly make use of move semantics.
2022-07-04 11:19:50 +01:00
William S Fulton
71cd6a38fe Performance optimisation for directors for classes passed by value
The directorin typemaps in the director methods now use std::move on the
input parameter when copying the object from the stack to the heap prior
to the callback into the target language, thereby taking advantage of
move semantics if available.
2022-07-04 11:19:29 +01:00
William S Fulton
bf36bf7d8a Movable and move-only types supported in "out" typemaps.
Enhance SWIGTYPE "out" typemaps to use std::move when copying
objects, thereby making use of move semantics when wrapping a function returning
by value if the returned type supports move semantics.

Wrapping functions that return move only types 'by value' now work out the box
without having to provide custom typemaps.

The implementation removed all casts in the "out" typemaps to allow the compiler to
appropriately choose calling a move constructor, where possible, otherwise a copy
constructor. The implementation alsoand required modifying SwigValueWrapper to
change a cast operator from:

  SwigValueWrapper::operator T&() const;

to

  #if __cplusplus >=201103L
    SwigValueWrapper::operator T&&() const;
  #else
    SwigValueWrapper::operator T&() const;
  #endif

This is not backwards compatible for C++11 and later when using the valuewrapper feature
if a cast is explicitly being made in user supplied "out" typemaps. Suggested change
in custom "out" typemaps for C++11 and later code:

1. Try remove the cast altogether to let the compiler use an appropriate implicit cast.
2. Change the cast, for example, from static_cast<X &> to static_cast<X &&>, using the
   __cplusplus macro if all versions of C++ need to be supported.

Issue #999
Closes #1044

More about the commit:
Added some missing "varout" typemaps for Ocaml which was falling back to
use "out" typemaps as they were missing.

Ruby std::set fix for SwigValueWrapper C++11 changes.
2022-06-30 17:26:48 +01: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
William S Fulton
e4cdf9d98f argcargv.i cosmetic updates 2022-05-15 18:32:53 +01:00
William S Fulton
0307d0732c Typemaps for (int ARGC, char **ARGV) fixup
The default typemap should not be in this library file - this is for
users to add in if they want C default argument support.
2022-05-15 18:21:59 +01:00
Erez Geva
b88fe498ca Fix argcargv.i in Perl5, Tcl, PHP
Add missing type map for type check.
Add testing of argcargv.i for Perl5, Tcl, PHP and Ruby.

Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
2022-05-11 01:10:12 +02:00
Erez Geva
464d548d71 Add argcargv.i to more languages: Perl 5, Tcl, PHP
Signed-off-by: Erez Geva <ErezGeva2@gmail.com>
2022-05-06 23:15:06 +02: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
89a0a3c5f7
Merge pull request #2197 from swig-fortran/rename-warn
Print rename warnings except anonymous template methods
2022-02-10 16:15:10 +13:00
Dimitris Apostolou
40c3bf30b2 Fix typos 2022-02-10 16:12:24 +13:00
Seth R Johnson
de78b80de9 Renames performed by %namewarn with rename= are printed in warning message
This is necessary for regex-like renames (where you can't use the #define trick
as is done in many of the %keywordwarn directives). It's now unnecessary to print
the "renaming to '`x`'" code explicitly by the kw.swg files.
2022-02-06 13:51:37 -05:00
Olly Betts
0cdbbf326b Fix -Wstrict-prototypes warning in generated PHP wrappers 2022-02-03 17:21:17 +13:00
Olly Betts
5f5a8f2d4a [php] Initialise zval in directorin typemaps
SWIG_SetPointerZval() requires the zval structure passed to be
initialised (so it can handle the constructor case where the zval is
already initialised as a PHP object.

I couldn't think of a suitable regression test for this, but it fixes 2
issues detected by running director_basic under valgrind.
2022-01-24 13:32:22 +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
87c2e7d71a [php] Fix long long handling on 32 bit platforms
The typemaps for long long and unsigned long long didn't handle a
string input correctly, and long_long_runme.php had a flawed test
in this case.

Fixes #2155
2022-01-22 13:09:34 +13:00
Olly Betts
4f5dfd596d [php] Provide zend_class_implements_interface() for 7.x
It seems instanceof_function_ex() isn't a viable option for PHP 7.3 and
earlier - the implementation is strangely wrong.

So let's just provide a compatibility implementation which does what
zend_class_implements_interface() does in 8.x and use that for all 7.x
so there are fewer variants to worry about testing.
2022-01-21 14:32:36 +13:00
Olly Betts
772ea68084 [php] Fix minor version test 2022-01-21 13:09:47 +13:00
Olly Betts
c197ed719a Fix unterminated comment in previous commit
I'm an idiot - I noticed I'd used a // comment after testing, but failed
to retest after fixing that.
2022-01-21 12:34:38 +13:00
Olly Betts
ce4a9923ed [php] Fix zend_class_implements_interface() compat
The sense of parameter 3 of instanceof_function_ex() appears to have
changed in PHP 7.4 and we need to pass 0 for older versions and 1 for
PHP 7.4.  Words fail me.
2022-01-21 12:11:39 +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
69b580dfb6 [php] Use SWIG_TypeCheckStruct to check types
We have the swig_type_info available and SWIG_TypeCheckStruct
is more efficient because it uses a pointer comparison instead of the
string comparison SWIG_TypeCheck uses (this change speeds up `make
check-php-test-suite` by about 10%).
2021-12-19 13:45:25 +13:00
Olly Betts
7d75e3eb7e [PHP] Add new PHP 8.1 keyword
Add PHP keyword 'readonly' (added in 8.1) to the list SWIG knows to
automatically rename.  This keyword is special in that PHP allows it to
be used as a function (or method) name.
2021-12-18 15:30:38 +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
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
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
94ad89284d [php] Remove redundant in typemap for bool
This typemap which would wrap C++ bool as PHP int is later overridden
by another which wraps it as PHP bool.  The current result is what
we want so just remove the redundant one.
2021-12-13 09:31:11 +13:00
Olly Betts
c0c7a8dba0 [php] Fix two incorrect PHP 8 conditionals
The correct macro to test is PHP_MAJOR_VERSION so these two PHP 8 cases
weren't ever used, which hid that the PHP8 version of the code was
broken in one of them.

Highlighted in #2113.
2021-12-08 13:10:38 +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
b671a37e89 [php] Fix reserved class names TRUE, FALSE, NULL
PHPCN(x) does a string compare of x with the lower-cased class name,
so x needs to be in lowercase or else the entry has no effect.  The
entries for TRUE, FALSE and NULL weren't working as a result.
2021-05-12 16:45:42 +12:00
Olly Betts
853c511057 [php] Update keyword list
Add some missing entries, remove some long obsolete entries (from
the "ming" extension for generating SWF files, which was split
out from PHP core in 2008), and entry for "static" as a reserved class
name (`static::` is used for late static bindings, but attempting to
name a PHP class `static` fails because `static` is a keyword and we
also list it as such).
2021-05-12 16:43:32 +12:00
Olly Betts
542f6ca440 Fix uses of uninitialised zval
SWIG_SetPointerZval() now checks if the passed zval is an object,
so use ZVAL_UNDEF() before in cases where we create a zval to pass.
2021-05-05 11:55:07 +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
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
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
04bacf689b Implement director-disown for PHP 2021-05-03 16:00:30 +12:00
Olly Betts
837dfa1e7e Fix directorout SWIGTYPE typemaps 2021-04-22 17:51:14 +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
ad61e33e22 Wrap pointer to member as object not resource 2021-04-22 07:56:25 +12:00
Olly Betts
50b13275dc Fix mixed declarations and code 2021-04-22 07:42:39 +12:00
Olly Betts
2920ba1cf6 Use malloc() not emalloc() when free() gets used 2021-04-21 18:18:59 +12:00
Olly Betts
49d5909b08 Use $1 instead of result in out typemaps 2021-04-21 18:16:08 +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
5577257301 Eliminate $needNewFlow from factory.i
$needNewFlow in an output typemap is now only relevant when wrapping
to a PHP __construct method, and there the return type is known so
factory.i isn't useful.
2021-04-21 16:32:56 +12:00