Re: [PHP-DEV] Persistent zvals

2012-04-09 Thread Flavius Aspra

On 04/07/2012 05:21 AM, Luke Scott wrote:

 From what I've gathered thus far, it is impossible to do without copying the

non-persistent memory into persistent memory, and then back again.


Hi, glad to see you again StackOverflow user:-)

I think I've shown you the route by that [1] project, and yes, the emalloc() 
call is hardcoded in so many places that you'd have to do it this way.


But as Pierre said, an application server for PHP (PHP-FPM?) would be great. The 
stub part of a .phar file could play a big role there.


Regards,
Flavius

[1] https://github.com/flavius/php-persist


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



Re: [PHP-DEV] PHP Script Compile System

2012-02-22 Thread Flavius Aspra

On 02/22/2012 07:29 AM, Rasmus Lerdorf wrote:

complicated optimization passes or any of those things


Would such things be welcome/needed in the engine or as an extension?

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



[PHP-DEV] Re: Exceptions for method on non-object rather than fatal (desired feature?)

2012-02-21 Thread Flavius Aspra

On 02/21/2012 03:18 PM, Ralf Lang wrote:


In PHP I would rather do

$mother = $baby-getMother();

if ($mother) {
  $granny = $mother-getMother();
  if ($granny) {
  $granny_name  = $granny-getName();
  }
}

because I have no way to catch if $mother or $granny cannot be retrieved.



There is a way:


 1  ?php
 2  class NonCallable {
 3  public function __call($meth, $args) {
 4  throw new Exception($meth does not exist);
 5  }
 6  }
 7  
 8  class Foo {
 9  protected $bar;
10  public function __construct($bar=NULL) {
11  $this-bar = $bar;
12  }
13  public function bar() {
14  if($this-bar) {
15  return $this;
16  }
17  return new NonCallable;
18  }
19  }
20  
21  class Bar {
22  public function bark() {
23  echo 'bark';
24  return $this;
25  }
26  }
27  
28  $foo = new Foo;
29  
30  try {
31  $foo-bar()-bark();
32  } catch(Exception $e) {
33  echo 'something went wrong';
34  return 1;
35  }
36  

Liebe Grüße,
Flavius

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



Re: [PHP-DEV] PHP Script Compile System

2012-02-21 Thread Flavius Aspra

On 02/22/2012 06:49 AM, Deepak Balani wrote:

Hello all,

I am think(actually drafting) about the compilation system of PHP scripts.
I want to make a native C extension which is able to compile the scripts in
the Zend Engines opcodes and execute directly when called.

The extension may have two functions.

bool gpc_compile($source, $target): compile file to opcodes.
mixed gpc_import($target) Include file to current script.

gpc_import function accepting path to the compiled file and execute file
into the zend engine. I want to know perception of you all about this.

Thank you.
Deepak Balani



Hi

Do you mean something like apc_compile_file()?

I think we already have it.

Or perhaps you mean runkit?

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



[PHP-DEV] ksort an array internally (OR: interate a numerically-indexed array with respect to the indeces)

2011-10-01 Thread Flavius Aspra

Hi

How can I ksort() an array?

The default implementation does basically this: 
zend_hash_sort(Z_ARRVAL_P(array), zend_qsort, php_array_key_compare, 0 
TSRMLS_CC) but php_array_key_compare() is static in ext/standard/array.c [1]


Actually I wouldn't need to sort by key at all if add_index_zval() would 
put the zval at the right [2] place from the beginning. Is there any 
better alternative to add_index_zval() in this regard?


Preferably without going through the hassle of making a round-trip via 
zend_call_function() directly to zif_ksort().


What I need is to zend_hash_move_forward()  co but to get the elements 
in the order indicated by the numeric indeces.


Thanks,
Flavius


[1] http://lxr.php.net/opengrok/xref/PHP_5_3/ext/standard/array.c#250
[2] I know the HT implementation is made to be generalized enough for 
other use cases, and as such, there is no right order. Thus I'm 
thinking maybe there's some way to add a new numerically-indexed element 
to the HT and teach the HT to do the bookkeeping in respect to the 
numeric order. Or maybe a better way to iterate such an array.


--
What I cannot create, I do not understand. -- Feynman

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



Re: [PHP-DEV] ZE2 broken by newer gcc

2011-09-12 Thread Flavius Aspra

On 09/12/2011 08:45 AM, Dmitry Stogov wrote:

Hi Flavius,

Unfortunately, the proposed fix is wrong.
It changes the operators precedence and it's definitely wrong.
I suppose the crash caused by side effect of some other bug.

Try to run the same script with valgrind.

Thanks. Dmitry.





Please test the following POC with gcc 4.6.1 on x64 first (perhaps 
slightly earlier versions behave the same, but this one is for sure), 
the dump_container() function simulates what the ZE does in 
zend_call_function() and many other places. You can also look at what 
valgrind says too and compare. I haven't dwelt on it at the ASM level, 
but certainly something has changed. My bet is that GCC has had wrong 
the operator precedence, and now they've fixed it.


I'm not speaking from my imagination, I'm speaking from what I've seen 
and what I've tested, in practice. No theory. In the meta extension I 
am calling zend_call_function(), which is how I've discovered this. The 
POC resembles what is happenning as close as possible.



#include stdio.h
#include stdlib.h

#define NUMS 10

/* a zval */
typedef struct _foo {
char *t;
int i;
} foo;

/* a zend_fcall_info */
typedef struct _foo_container {
foo ***foos;
} container;

foo** init_foos(void) {
foo** f, *f2;
int i;
f = malloc(sizeof(foo*)*NUMS);
for(i=0; i  NUMS; i++) {
f2 = malloc(sizeof(foo));
printf(allocated %p , f2);
f[i] = f2;
f[i]-i = i*11+1;
printf(with value %d\n, i*11+1);
}
return f;
}

void dump_container(container *c) {
int i;
for(i=0; i  NUMS; i++) {
//working:
printf(foo[%d]=%d at %p\n, i, (*c-foos)[i]-i, (*c-foos)[i]);
//similar to ZE, crashing (not at i=0, but on my machine at i=1 
it already delivers garbage, at i=2 it finnaly crashes

//if you're lucky, increase NUMS at the top
printf(foo[%d]=%d at %p\n, i, (*c-foos)[i]-i, *c-foos[i]);
}
}

int main(void) {
container c;
foo **foos;

foos = init_foos();

c.foos = foos;
int i;
for(i=0; i  NUMS; i++) {
printf(%d %d at %p\n, i, foos[i]-i, foos[i]);
}
printf(--\n);
dump_container(c);
free(foos);
return EXIT_SUCCESS;
}



--
What I cannot create, I do not understand. -- Feynman

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



[PHP-DEV] Re: [RESULT] Choosing a distributed version control system for PHP

2011-09-08 Thread Flavius Aspra

On 09/07/2011 11:57 PM, David Soria Parra wrote:



For everyone else, go read http:/progit.org, make yourself familar with Git.



As a former svn-only user, I can tell you how I've learned (and still 
learn) about git:


1. http://git.or.cz/course/svn.html
2. progit
3. Git from the Bottom up: 
http://ftp.newartisans.com/pub/git.from.bottom.up.pdf
4. Git for the confused is a good reference: 
http://www.gelato.unsw.edu.au/archives/git/0512/13748.html

5. progit again, to wrap it up, you may have missed a few details
6. Reset demystified http://progit.org/2011/07/11/reset.html (and other 
articles, but this one stood out to me)


You shouldn't skip 2, 3 and 6. Don't forget you can test it all locally, 
so just:


cd /tmp
mkdir repo.git  cd repo.git
git init --bare

Then you can clone it:

cd ~/projects
git clone /tmp/repo.git

This kind of setup is especially useful when playing around with 3.
Take your time to experiment with the plumbing commands. You'll probably 
have some a-ha! moments.


Cheers,
Flavius

--
What I cannot create, I do not understand. -- Feynman

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



Re: [PHP-DEV] How to collaborate?

2011-09-08 Thread Flavius Aspra

On 09/08/2011 04:54 PM, Adir Kuhn wrote:

Hello I am new to the list, I live in Brazil and PHP program for 5
years. I have basic knowledge in C, would like to increase my
knowledge in this language and collaborate with the PHP code.

I wish someone could help me with the basic steps, suggesting bugs
that are relatively easy and could review my code.

Thanks,

Adir Kuhn



The best way is to learn how to write extensions first, I guess. That's 
what has worked for me at least. So grab yourself a hard copy of Sara 
Golemon's book, and learn from there.


The IRC channel #php.pecl @ efnet is also quite helpful.

If that feels overwhelming, there's still plenty to do on pear2.php.net 
and qa.php.net.


Cheers,
Flavius

--
What I cannot create, I do not understand. -- Feynman

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



Re: [PHP-DEV] [RFC] New extension proposal: meta

2011-09-04 Thread Flavius Aspra

On 09/04/2011 09:21 AM, Nikita Popov wrote:

Hi Flavius!


Hi Nikita


First, you seem to try to lex the PHP code yourself too. Why don't you
use PHP's internal lexing capabilities?


I can't. This is documented in the README.


Second, you seem to try to parse PHP using your own grammar. From my
experience, the PHP grammar
(http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_language_parser.y) is
really messed up and it's already quite hard to strictly adhere to
that grammar (as it has lots of strange quirks) and make it AST-able.
If you write up your own grammar I doubt that it will ever match the
PHP grammar.


This will become a non-issue, once we've formalized the grammar in 
standard EBNF, which is already on the TODO list (TODO.txt). I've heard 
there's an old need to do this among other developers, and I can do it, 
as I work on meta.



I think you should stick as much as possible to the PHP
grammar and only introduce changes where required.


I can't. This is documented in the README.



For example you already
have at least one error in the little grammar you have: The opening
and closing tags should be filtered away before being passed to the
parser


It's a feature, and it's documented in the README. There will be flags 
to instruct it to skip those tokens if desired - it's documented in the 
code.



as the may occur anywhere, even in the middle of an expression
(`echo $hi . ??php $hi;` is valid).



I know, there is no error handling yet. I've put it on the TODO list.

Thanks,
Flavius

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



Re: [PHP-DEV] [RFC] New extension proposal: meta

2011-09-04 Thread Flavius Aspra

On 09/03/2011 11:41 PM, Arpad Ray wrote:



Haven't had much of a look yet but I really like the concept :)



Glad to hear that. I had this idea for a long time, but I've never got 
time to look at it.




A couple of minor things to note:
- Now would be a good time to make it follow the PHP coding standards,
before it gets much larger or other devs get involved.


Done (but I could have missed a couple of things here and there).


- Variable declarations should always begin a block, some platforms
will bork otherwise.


Done.

Thanks.

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



Re: [PHP-DEV] Turn on error on mixed declarations (Was: New extension proposal: meta)

2011-09-04 Thread Flavius Aspra

On 09/04/2011 05:28 AM, Gustavo Lopes wrote:

On Sun, 04 Sep 2011 04:05:32 +0100, Kalle Sommer Nielsenka...@php.net
wrote:


2011/9/3 Arpad Rayarray...@gmail.com:

- Variable declarations should always begin a block, some platforms
will bork otherwise.


Actually this is required for the C standard, the early ones that we
use in PHP, whereas C++ allows them anywhere in a block, like PHP does
:)



By the way, since committing code with mixed declarations is a frequent
problem -- GCC accepts it even without -std=C99 yet the Windows builds are
broken --, we could make GCC treat this as an error with:

-Werror=declaration-after-statement


Thanks, I've compiled it with this parameter and I've fixed those mistakes.

DISCLAIMER: I know playing around with CFLAGS in config.m4 is bad, those 
bits will be removed as documented in the lines right above that


What else could I improve, beside the bits I already know about, which 
are documented in the code (as TODOs) and in TODO.txt?


Cheers,
Flavius

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



[PHP-DEV] [RFC] New extension proposal: meta

2011-09-03 Thread Flavius Aspra

Hi

I'm Flavius Aspra and over the past weeks [1] I've worked on a small
extension, which I think it has a lot of potential. Thanks to everyone
for being patient with me on #php.pecl during this time - I've learned a
lot!

Meta will enable userland scripts to get the AST of a PHP source code,
to manipulate it, and if desired, to serialize it back to valid PHP code.

The current status is not really usable, but it has already reached
around 2k LOCs and I think it's easier for you to review it now, than later.

The code is still hosted on github, please let me know what needs to be
done to make it PECL-compatible. Advices, bug fixes and improvements are
welcome. Pending TODOs are listed inline, in the code, as they fit, and
longer-term TODOs are in TODO.txt.

I've already asked for a first thought on IRC, and people seem to see
the need for this extension.

The project's page is at [2], it's called phpmeta there, so no
confusion arises. The extension is simply called meta. Many other
details can be found in the README. Feel free to ask me anything.

[1] Actually, I wanted to work on this for GSoC, but I was a little bit
late, so I've worked on it in my spare time
[2] https://github.com/OriginalCopy/phpmeta

Cheers,
Flavius
--
What I cannot create, I do not understand. -- Feynman

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



[PHP-DEV] Array as class member and refcounting

2011-08-25 Thread Flavius Aspra
There is something that confuses me. Let there be a class member 
Foo::$bar, which has to be initialized as an empty array in the 
constructor. If I do that (via zend_update_property), its refcount is 
increased (from 1, which it gets after alloc + array_init, to 2). It is 
obvious why this happens, since from zend_update_property gets a 
variable from somone in the outside world, and so it is right to ++ it.


But in this particular case, the array being initialized in the 
constructor, it does not need a refcount of 2, 1 is right, since it's 
used only by the object (yet).


So I thought I will Z_DELREF_P() it. And it worked. Until I started 
valgrind, which was reporting:


==4538== Invalid read of size 4
==4538==at 0x822D3C6: _zval_ptr_dtor (zend.h:385)
==4538==by 0x823C1FF: _zval_ptr_dtor_wrapper (zend_variables.c:189)
==4538==by 0x824E1A1: zend_hash_destroy (zend_hash.c:529)
==4538==by 0x826655A: zend_object_std_dtor (zend_objects.c:45)
==4538==by 0x8266A28: zend_objects_free_object_storage 
(zend_objects.c:126)
==4538==by 0x826C43D: zend_objects_store_del_ref_by_handle_ex 
(zend_objects_API.c:220)
==4538==by 0x826C0AC: zend_objects_store_del_ref 
(zend_objects_API.c:172)

==4538==by 0x823BD77: _zval_dtor_func (zend_variables.c:52)
==4538==by 0x822B99B: _zval_dtor (zend_variables.h:35)
==4538==by 0x822D463: _zval_ptr_dtor (zend_execute_API.c:443)
==4538==by 0x823C1FF: _zval_ptr_dtor_wrapper (zend_variables.c:189)
==4538==by 0x824E518: zend_hash_apply_deleter (zend_hash.c:614)
==4538==  Address 0x44c1718 is 8 bytes inside a block of size 20 free'd

So it looks like the ZE really needs the refcount to be 2. All other 
tests I've written work fine, no memleaks, no segfaults whatsoever.


Still I am a little bit confused: WHY does it need it be higher than 
(from my understanding) it should be?


Thanks,
Flavius

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



[PHP-DEV] [Q] references and objects

2011-08-02 Thread Flavius Aspra
I have a zval *obj holding an object of class B, which I then add to an 
array member of another object of class A. Then I create a new instance 
*obj2 of class B, to which I want to add a reference to *obj. How would 
I best go about it?


So A works as a pool of B's, and B's may reference to each other.

Please tell me key functions and macros to use to accomplish 
specifically this thing, as the API is quite new to me.


I have thought about setting Z_SET_ISREF_P() manually, but what else 
would I need? From my understanding, I'd still need a new zval* for 
that. Any other better ways?


Regards,
Flavius

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



[PHP-DEV] zend_call_function() and co.

2011-07-29 Thread Flavius Aspra

Hi

I have a few questions about zend_fcall_info and zend_fcall_info_cache.

Regarding zend_fcall_info

What is function_name useful for? I have a feeling it's for error 
reporting, but I'm not sure


What is symbol_table for? Maybe to put in the function's context other 
variables beside the parameters, like $this?


What is zend_fcall_info_cache for? I've seen in some places that 
zend_call_function() sometimes takes a NULL for it. When is it useful to 
cache information about the function call?



Regards,
Flavius

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