[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-general
Subject:    Re: [PHP] 7.1 extension writing, output parameter remains unchanged
From:       Jan_Vávra <vavra () 602 ! cz>
Date:       2017-07-19 11:56:19
Message-ID: 46647da9-510b-d5d8-d60b-90ca7d38b13d () 602 ! cz
[Download RAW message or body]

Hi
> Hi Jan
>
> 2017-07-19 12:17 GMT+02:00 Jan Vávra <vavra@602.cz>:
>> Hello,
>>   I'm porting our extension from php5.6 to php7.1 x64 Windows, Visual Studio
>> 2015.
>> I'd like to write an extension function that has one input / output
>> parameter type of long. But the parameter value changed in extension is not
>> changed in php. What I'm doing wrong?
>> The php code is:
>>
>> $a = 3;
>> $r = hello_square($a);
>> var_export($a); echo "\n"; //value of $a still remains 3, should be 9.
>>
>> The c code is:
>>
>> PHP_FUNCTION(hello_square)
>> {
>>    long a = 0;
>>    zval *b;
>>
>>    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &b) == FAILURE)
>> {
>>      return;
>>    }
>>
>>    a = Z_LVAL_P(b);
>>    ZVAL_LONG(b, a*a);
>>
>>    RETURN_BOOL(1);
>> }
>>
>> ----
>>
>> I also implemented hello_square this way
>> PHP_FUNCTION(hello_square)
>> {
>>    zend_long b;
>>
>>    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &b) == FAILURE)
>> {
>>      return;
>>    }
>>
>>    b = b * b;
>>    RETURN_BOOL(1);
>> }
>>
>> And the value of $a remains unchanged.
> What you should do is to make the parameter sent to hello_square() a
> reference, this is done by the following (keep in mind this uses the
> new and fast parameter parsing API, but it is still do-able with the
> old zend_parse_parameters()):
>
> https://gist.github.com/KalleZ/69f9ca1895bcac945e110b7777104203
>
> Take a look at the README.PARAMETER_PARSING_API in the root of
> php-src, this includes all possible values that
> zend_parse_parameters() can accept
I've fixed line 10 from your sample to
ZVAL_LONG(b, Z_LVAL_P(b) * Z_LVAL_P(b));

or put a constant
ZVAL_LONG(b, 20);

But in php $a variable remains unchanged.

Full c source is:

PHP_FUNCTION(hello_square)
{
   zval *b;

   ZEND_PARSE_PARAMETERS_START(1, 1)
   Z_PARAM_ZVAL_DEREF(b)
   ZEND_PARSE_PARAMETERS_END();

   zval_dtor(b);
   ZVAL_LONG(b, Z_LVAL_P(b) * Z_LVAL_P(b));

   RETURN_TRUE;
}

Jan.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic