Fix directorout SWIGTYPE typemaps

This commit is contained in:
Olly Betts 2021-04-22 17:49:57 +12:00
commit 837dfa1e7e
3 changed files with 46 additions and 7 deletions

View file

@ -99,6 +99,12 @@ namespace Swig {
swig_owner[vptr] = new GCItem_T<Type>(vptr); swig_owner[vptr] = new GCItem_T<Type>(vptr);
} }
} }
void swig_acquire_ownership_obj(void *vptr, int own) const {
if (vptr && own) {
swig_owner[vptr] = new GCItem_Object(own);
}
}
}; };
/* base class for director exceptions */ /* base class for director exceptions */

View file

@ -93,8 +93,8 @@
%typemap(directorout) SWIGTYPE ($&1_ltype tmp) %typemap(directorout) SWIGTYPE ($&1_ltype tmp)
%{ %{
if (SWIG_ConvertPtr($input, (void **) &tmp, $1_descriptor, 0) < 0 || tmp == NULL) { if (SWIG_ConvertPtr($input, (void **) &tmp, $&1_descriptor, 0) < 0 || tmp == NULL) {
SWIG_PHP_Error(E_ERROR, "Type error in argument $argnum of $symname. Expected $1_descriptor"); SWIG_PHP_Error(E_ERROR, "Type error in argument $argnum of $symname. Expected $&1_descriptor");
} }
$result = *tmp; $result = *tmp;
%} %}
@ -107,6 +107,15 @@
} }
%} %}
%typemap(directorout) SWIGTYPE * (swig_owntype own),
SWIGTYPE [] (swig_owntype own)
%{
if (SWIG_ConvertPtrAndOwn($input, (void **)&$result, $1_descriptor, SWIG_POINTER_DISOWN, &own) < 0) {
SWIG_PHP_Error(E_ERROR, "Type error in argument $argnum of $symname. Expected $1_descriptor");
}
swig_acquire_ownership_obj((void*)$result, own);
%}
%typemap(in) SWIGTYPE &, %typemap(in) SWIGTYPE &,
SWIGTYPE && SWIGTYPE &&
%{ %{
@ -115,6 +124,15 @@
} }
%} %}
%typemap(directorout) SWIGTYPE & ($1_ltype tmp),
SWIGTYPE && ($1_ltype tmp)
%{
if (SWIG_ConvertPtr($input, (void **) &tmp, $1_descriptor, 0) < 0 || tmp == NULL) {
SWIG_PHP_Error(E_ERROR, "Type error in argument $argnum of $symname. Expected $1_descriptor");
}
$result = tmp;
%}
%typemap(in) SWIGTYPE *const& ($*ltype temp) %typemap(in) SWIGTYPE *const& ($*ltype temp)
%{ %{
if (SWIG_ConvertPtr(&$input, (void **) &temp, $*1_descriptor, 0) < 0) { if (SWIG_ConvertPtr(&$input, (void **) &temp, $*1_descriptor, 0) < 0) {

View file

@ -4,6 +4,8 @@
* PHP runtime library * PHP runtime library
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
#define swig_owntype int
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -130,7 +132,7 @@ SWIG_SetPointerZval(zval *z, void *ptr, swig_type_info *type, int newobject) {
This is called by SWIG_ConvertPtr which gets the type name from the This is called by SWIG_ConvertPtr which gets the type name from the
swig_object_wrapper. */ swig_object_wrapper. */
static void * static void *
SWIG_ConvertPtrData(void * p, const char *type_name, swig_type_info *ty) { SWIG_ConvertPtrData(void * p, const char *type_name, swig_type_info *ty, int *own) {
swig_cast_info *tc; swig_cast_info *tc;
void *result = 0; void *result = 0;
@ -149,14 +151,21 @@ SWIG_ConvertPtrData(void * p, const char *type_name, swig_type_info *ty) {
if (tc) { if (tc) {
int newmemory = 0; int newmemory = 0;
result = SWIG_TypeCast(tc, p, &newmemory); result = SWIG_TypeCast(tc, p, &newmemory);
assert(!newmemory); /* newmemory handling not yet implemented */ if (newmemory == SWIG_CAST_NEW_MEMORY) {
assert(own); /* badly formed typemap which will lead to a memory leak - it must set and use own to delete *ptr */
if (own)
*own |= SWIG_CAST_NEW_MEMORY;
}
} }
return result; return result;
} }
/* We wrap C/C++ pointers as PHP objects. */ /* We wrap C/C++ pointers as PHP objects. */
static int static int
SWIG_ConvertPtr(zval *z, void **ptr, swig_type_info *ty, int flags) { SWIG_ConvertPtrAndOwn(zval *z, void **ptr, swig_type_info *ty, int flags, swig_owntype *own) {
if (own)
*own = 0;
if (z == NULL) { if (z == NULL) {
*ptr = 0; *ptr = 0;
return (flags & SWIG_POINTER_NO_NULL) ? SWIG_NullReferenceError : SWIG_OK; return (flags & SWIG_POINTER_NO_NULL) ? SWIG_NullReferenceError : SWIG_OK;
@ -165,11 +174,12 @@ SWIG_ConvertPtr(zval *z, void **ptr, swig_type_info *ty, int flags) {
switch (Z_TYPE_P(z)) { switch (Z_TYPE_P(z)) {
case IS_OBJECT: { case IS_OBJECT: {
swig_object_wrapper *value = SWIG_Z_FETCH_OBJ_P(z); swig_object_wrapper *value = SWIG_Z_FETCH_OBJ_P(z);
*ptr = SWIG_ConvertPtrData(value->ptr, value->type->name, ty, own);
if (*ptr == NULL) return SWIG_ERROR;
if (flags & SWIG_POINTER_DISOWN) { if (flags & SWIG_POINTER_DISOWN) {
value->newobject = 0; value->newobject = 0;
} }
*ptr = SWIG_ConvertPtrData(value->ptr, value->type->name, ty); return SWIG_OK;
return (*ptr == NULL ? SWIG_ERROR : SWIG_OK);
} }
case IS_NULL: case IS_NULL:
*ptr = 0; *ptr = 0;
@ -179,6 +189,11 @@ SWIG_ConvertPtr(zval *z, void **ptr, swig_type_info *ty, int flags) {
return -1; return -1;
} }
static int
SWIG_ConvertPtr(zval *z, void **ptr, swig_type_info *ty, int flags) {
return SWIG_ConvertPtrAndOwn(z, ptr, ty, flags, 0);
}
static const char const_name[] = "swig_runtime_data_type_pointer"; static const char const_name[] = "swig_runtime_data_type_pointer";
static swig_module_info *SWIG_Php_GetModule() { static swig_module_info *SWIG_Php_GetModule() {
zval *pointer = zend_get_constant_str(const_name, sizeof(const_name) - 1); zval *pointer = zend_get_constant_str(const_name, sizeof(const_name) - 1);