Re: [PHP] 7.1 extension writing, output parameter remains unchanged

[email protected] (Jan Vávra)
Newsgroups php.general
Message-ID <[email protected]>
Hi
> Hi Jan
>
> 2017-07-19 12:17 GMT+02:00 Jan Vávra <[email protected]>:
>> 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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.