Re: [PHP-DEV] trying to understand zvals

2002-10-20 Thread Stanislav Malyshev
TDJ Could you go into a little more depth on the problem, just so I
TDJ understand?

The problem is that passing argument passes only zval *, i.e. pointer to 
variable itself. To make reference assignment, you need zval **, i.e. the 
place in the symbol table. 

-- 
Stanislav Malyshev, Zend Products Engineer   
[EMAIL PROTECTED]  http://www.zend.com/ +972-3-6139665 ext.109





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




Re: [PHP-DEV] trying to understand zvals

2002-10-18 Thread Tim Daly, Jr.


Brad LaFountain [EMAIL PROTECTED] writes:

 PHP_FUNCTION(ref_assign)
 {
   zval *bar, *foo;
 
   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, zz, bar, foo) ==
 FAILURE) {
   return;
   }
 
   ZVAL_ADDREF(foo);
   *bar = *foo;
   bar-is_ref = TRUE;
   bar-refcount = 1;
 }
 
 this works, but i don't know if its the best way to do it.
 

Thanks, but it doesn't actually work for me.  Witness:

[tim@myst php-4.2.3]$ ./php -f ext/foo/foo.php
foo
foo
baz
-*-
^@\204Í
FATAL:  erealloc():  Unable to allocate 1515870811 bytes
Segmentation fault (core dumped)
[tim@myst php-4.2.3]$ 


ext/foo/foo.php contains:

?
if(!extension_loaded('foo')) {
dl('foo.so');
}

//make two variables
$foo = foo;
$bar = zonk;

//reference assignment
$bar = $foo;

//should be identical
echo $foo\n;
echo $bar\n;

//set foo and bar to baz
$bar = baz;

//should print baz
echo $foo\n;

echo -*-\n;

//make two variables
$wibb = foo;
$wobb = zonk;

//reference assignment
ref_assign($wobb, $wibb);

//set wibb and wobb to baz
echo $wibb\n;
echo $wobb\n;

//should print baz
$wobb = baz;

echo $wibb\n;

?


-Tim

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




Re: [PHP-DEV] trying to understand zvals

2002-10-18 Thread Tim Daly, Jr.

Stanislav Malyshev [EMAIL PROTECTED] writes:

 OK, looking again on the matter in depth, it seems to me that you cannot 
 do it from C function either, due to the way in which parameters are 
 passed in the engine. 

Could you go into a little more depth on the problem, just so I
understand?

Thanks,
Tim


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




[PHP-DEV] trying to understand zvals

2002-10-18 Thread Tim Daly, Jr.

I'm trying to do some extension programming, and I'm pretty confused
by the whole zval thing.  In particular, references are a little
mysterious.  If I have

$foo = zonk;
$bar = $foo;

in PHP, what actually happens?  Specifically, if I wanted a function
that did such a reference assignment:

$foo = zonk;
$bar = baz;

ref_assign($bar, $foo); // $bar = $foo;

what has to happen in ref_assign?

Thanks,
Tim


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




Re: [PHP-DEV] trying to understand zvals

2002-10-18 Thread Stanislav Malyshev
TDJ I'm trying to do some extension programming, and I'm pretty confused
TDJ by the whole zval thing.  In particular, references are a little
TDJ mysterious.  If I have
TDJ 
TDJ $foo = zonk;
TDJ $bar = $foo;
TDJ 
TDJ in PHP, what actually happens?  Specifically, if I wanted a function

What actually happens is that both entries in symbol table with names 
foo and bar point to the same zval (having refcount of 2 now) 
containing string zonk.

TDJ that did such a reference assignment:
TDJ 
TDJ $foo = zonk;
TDJ $bar = baz;
TDJ 
TDJ ref_assign($bar, $foo); // $bar = $foo;
TDJ 
TDJ what has to happen in ref_assign?

That's problematic, due to the way PHP variables work. The best you can do 
is something like this:

function ref_assign($a, $b) {
$GLOBALS[$a] = $GLOBALS[$b];
}

ref_assign(bar, foo);

That's assuming they are globals. If they are not, I cannot now think of a 
good way to make variable-binding function. 
-- 
Stanislav Malyshev, Zend Products Engineer   
[EMAIL PROTECTED]  http://www.zend.com/ +972-3-6139665 ext.109



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




Re: [PHP-DEV] trying to understand zvals

2002-10-18 Thread Tim Daly, Jr.

Stanislav Malyshev [EMAIL PROTECTED] writes:

*snip*
 TDJ that did such a reference assignment:
 TDJ 
 TDJ $foo = zonk;
 TDJ $bar = baz;
 TDJ 
 TDJ ref_assign($bar, $foo); // $bar = $foo;
 TDJ 
 TDJ what has to happen in ref_assign?
 
 That's problematic, due to the way PHP variables work. The best you can do 
 is something like this:
*snip*

Sorry, I wasn't clear.  I mean ref_assign to be a C function, in my
extension.

-Tim

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




Re: [PHP-DEV] trying to understand zvals

2002-10-18 Thread Brad LaFountain
PHP_FUNCTION(ref_assign)
{
zval *bar, *foo;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, zz, bar, foo) ==
FAILURE) {
return;
}

ZVAL_ADDREF(foo);
*bar = *foo;
bar-is_ref = TRUE;
bar-refcount = 1;
}

this works, but i don't know if its the best way to do it.

 - Brad

--- Tim Daly, Jr. [EMAIL PROTECTED] wrote:
 
 I'm trying to do some extension programming, and I'm pretty confused
 by the whole zval thing.  In particular, references are a little
 mysterious.  If I have
 
 $foo = zonk;
 $bar = $foo;
 
 in PHP, what actually happens?  Specifically, if I wanted a function
 that did such a reference assignment:
 
 $foo = zonk;
 $bar = baz;
 
 ref_assign($bar, $foo); // $bar = $foo;
 
 what has to happen in ref_assign?
 
 Thanks,
 Tim
 
 
 -- 
 PHP Development Mailing List http://www.php.net/
 To unsubscribe, visit: http://www.php.net/unsub.php
 


__
Do you Yahoo!?
Faith Hill - Exclusive Performances, Videos  More
http://faith.yahoo.com

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




Re: [PHP-DEV] trying to understand zvals

2002-10-18 Thread Andrey Hristov
- Original Message -
From: Stanislav Malyshev [EMAIL PROTECTED]
To: Tim Daly, Jr. [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Sent: Thursday, October 17, 2002 5:34 PM
Subject: Re: [PHP-DEV] trying to understand zvals


 TDJ I'm trying to do some extension programming, and I'm pretty confused
 TDJ by the whole zval thing.  In particular, references are a little
 TDJ mysterious.  If I have
 TDJ
 TDJ $foo = zonk;
 TDJ $bar = $foo;
 TDJ
 TDJ in PHP, what actually happens?  Specifically, if I wanted a function

 What actually happens is that both entries in symbol table with names
 foo and bar point to the same zval (having refcount of 2 now)
 containing string zonk.

 TDJ that did such a reference assignment:
 TDJ
 TDJ $foo = zonk;
 TDJ $bar = baz;
 TDJ
 TDJ ref_assign($bar, $foo); // $bar = $foo;
 TDJ
 TDJ what has to happen in ref_assign?

 That's problematic, due to the way PHP variables work. The best you can do
 is something like this:

 function ref_assign($a, $b) {
 $GLOBALS[$a] = $GLOBALS[$b];
 }

 ref_assign(bar, foo);

 That's assuming they are globals. If they are not, I cannot now think of a
 good way to make variable-binding function.
 --

I think he wants to know how to do that in C.

Andrey


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




Re: [PHP-DEV] trying to understand zvals

2002-10-18 Thread Tim Daly, Jr.


Stanislav Malyshev [EMAIL PROTECTED] writes:

 Ah. Then you need to accept the first parameter by reference (this is 
 defined in ZEND_FE definition, put first_arg_force_ref as second 
 parameter). Then you get parameters the usual way (zend_get_parameters_ex, 
 etc.) and have to zval ** variables, something like this:

Thanks for your response!  While it does not cause a crash, your
function doesn't do what I expected.  Do you know where I went wrong?

This script:

?
if(!extension_loaded('foo')) {
dl('foo.so');
}

//make two variables
$foo = foo;
$bar = zonk;

//reference assignment
stas_ref_assign($bar, $foo);

//should be identical
echo $foo\n;
echo $bar\n;

//set foo and bar to baz
$bar = baz;

//should print baz
echo $foo\n;

?

Outputs foo zonk foo, and I expected foo foo baz. 

I made the following function:

PHP_FUNCTION(stas_ref_assign)
{
zval **dest, **src;

if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, dest, src)) {
WRONG_PARAM_COUNT;
}
SEPARATE_ZVAL_TO_MAKE_IS_REF(src);
(*src)-refcount++;
zval_ptr_dtor(dest);
*dest = *src;
}

with the function entry:

PHP_FE(stas_ref_assign, first_arg_force_ref)

Thanks,
-Tim

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




Re: [PHP-DEV] trying to understand zvals

2002-10-18 Thread Stanislav Malyshev
TDJ Thanks for your response!  While it does not cause a crash, your
TDJ function doesn't do what I expected.  Do you know where I went
TDJ wrong?

You are right, this doesn't work. I'll look a bit more into it...

-- 
Stanislav Malyshev, Zend Products Engineer   
[EMAIL PROTECTED]  http://www.zend.com/ +972-3-6139665 ext.109



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




Re: [PHP-DEV] trying to understand zvals

2002-10-18 Thread Stanislav Malyshev
TDJ Thanks for your response!  While it does not cause a crash, your
TDJ function doesn't do what I expected.  Do you know where I went
TDJ wrong?

OK, looking again on the matter in depth, it seems to me that you cannot 
do it from C function either, due to the way in which parameters are 
passed in the engine. What you can do is to pass names and then work with 
symbols tables (either global or local) directly. You'll also have to take 
care to properly destroy old destination value and properly separate the 
source value. You can look on ASSIGN_REF opcode in zend_execute.c for 
example (yes, it's a bit messy, I know). 

-- 
Stanislav Malyshev, Zend Products Engineer   
[EMAIL PROTECTED]  http://www.zend.com/ +972-3-6139665 ext.109



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




Re: [PHP-DEV] trying to understand zvals

2002-10-18 Thread Stanislav Malyshev
TDJ Sorry, I wasn't clear.  I mean ref_assign to be a C function, in my
TDJ extension.

Ah. Then you need to accept the first parameter by reference (this is 
defined in ZEND_FE definition, put first_arg_force_ref as second 
parameter). Then you get parameters the usual way (zend_get_parameters_ex, 
etc.) and have to zval ** variables, something like this:

zval **dest, src;
if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, dest, src)) {
WRONG_PARAM_COUNT;
}

Then if src wasn't reference, you need to separate it:

SEPARATE_ZVAL_TO_MAKE_IS_REF(src);
(*src)-refcount++;

This will also make src a reference. 
Then you have to free old value of dest:

zval_ptr_dtor(dest);

Then assign:

*dest = *src;

I think this should be enough...
Note that refcount is increased before calling dtor - to avoid problems 
with something like assign_ref($foo, $foo) (it's not wise to do this, but 
this way it won't crash/produce garbage). 
 
-- 
Stanislav Malyshev, Zend Products Engineer   
[EMAIL PROTECTED]  http://www.zend.com/ +972-3-6139665 ext.109



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