Commit Graph

70426 Commits

Author SHA1 Message Date
Nikita Popov
7b3bfa5784 Improve backtraces from generators
The current situation is still not perfect, as the generator function itself
does not appear in the stack trace. This makes sense in some way, but it
would probably be more helpful if it would show up (with the bound arguments)
after the $generator->xyz() call. This could be misleading too though as the
function is not *really* called there.
2012-06-03 02:00:11 +02:00
Nikita Popov
6117f4c7c0 Add cloning support for generators
Generators can now be cloned. I'm pretty sure that my current code does not
yet cover all the edge cases of cloning the execution context, so there are
probably a few bugs in there :)
2012-06-02 20:40:58 +02:00
Nikita Popov
1477be9aa8 Make $generator->send() return the current value
This makes the API easier to use (and is consistent with Python and JS).
2012-05-31 20:03:18 +02:00
Nikita Popov
ee89e228f6 Allow yielding during function calls
During function calls arguments are pushed onto the stack. Now these are
backed up on yield and restored on resume. This requires memcpy'ing them,
but there doesn't seem to be any better way to do it.

Also this fixes the issue with exceptions thrown during function calls.
2012-05-31 00:00:30 +02:00
Nikita Popov
0033a52521 Allow throwing exceptions from generators
The missing piece is how one can find the next stack frame, which is
required for dtor'ing arguments pushed to the stack. As the generator
execute_data does not live on the stack one can't use it to figure out the
start of the next stack frame. So there must be some other method.
2012-05-30 16:28:33 +02:00
Nikita Popov
8790160235 Add auto-increment keys
When no key is explicitely yielded PHP will used auto-incrementing keys
as a fallback. They behave the same as with arrays, i.e. the key is the
successor of the largest previously used integer key.
2012-05-30 05:05:49 +02:00
Nikita Popov
bc08c2cf94 Add support for yielding keys
Keys are yielded using the

    yield $key => $value

syntax. Currently this is implemented as a statement only and not as an
expression, because conflicts arise considering nesting and use in arrays:

    yield yield $a => $b;
    // could be either
    yield (yield $a) => $b;
    // or
    yield (yield $a => $b);

Once I find some way to resolve these conflicts this should be available
as an expression too.

Also the key yielding code is rather copy-and-past-y for the value yielding
code, so that should be factored out.
2012-05-30 02:44:06 +02:00
Nikita Popov
72a91d08e7 Add $generator->close() method
Calling $generator->close() is equivalent to executing a return statement
at the current position in the generator.
2012-05-29 18:11:18 +02:00
Nikita Popov
12e928314f Fix segfault when send()ing to a closed generator 2012-05-29 18:01:08 +02:00
Nikita Popov
ad525c288a Allow to use yield without value
If the generator is used as a coroutine it often doesn't make sense to yield
anything. In this case one can simply receive values using

    $value = yield;

The yield here will simply yield NULL.
2012-05-29 17:53:11 +02:00
Nikita Popov
3600914ced Add support for $generator->send()
Yield now is an expression and the return value is the value passed to
$generator->send(). By default (i.e. if ->next() is called) the value is
NULL.

Unlike in Python ->send() can be run without priming the generator with a
->next() call first.
2012-05-29 17:34:33 +02:00
Nikita Popov
b770b221e0 Make the GOTO and SWITCH VMs work again 2012-05-29 02:31:56 +02:00
Nikita Popov
4aab08b64c Properly free resources when generator return value not used
To keep things clean two new functions are introduced:

zend_clean_and_cache_symbol_table(HashTable *symbol_table)
zend_free_compiled_variables(zval ***CVs, int num)
2012-05-28 06:43:18 +02:00
Nikita Popov
bcc7d976f3 Set EG(current_execute_data)
This fixes several issues. In particular it makes method generators work
properly and also allows generators using a symbol table.
2012-05-28 06:06:30 +02:00
Nikita Popov
9f52c5c226 Fix generator creation when execute_data is not nested
This happens primarily when the generator is invoked from some internal
place like a dynamic function call.
2012-05-27 22:48:21 +02:00
Nikita Popov
64a643a4e3 Free loop variables
If the generator is closed before it has finished running, it may happen
that some FREE or SWITCH_FREE opcodes haven't been executed and memory is
leaked.

This fixes it by walking the brk_cont_array and manually freeing the
variables.
2012-05-27 17:14:20 +02:00
Nikita Popov
247bb737d5 Add support for generator methods 2012-05-27 03:50:31 +02:00
Nikita Popov
39d3d5ec13 Add first real generator test
The test implements an xrange() function (the generator version of range()).
2012-05-27 00:50:27 +02:00
Nikita Popov
cbfa96cad5 Remove wrong dtor call 2012-05-27 00:25:12 +02:00
Nikita Popov
d49d3971e6 Close generator on return 2012-05-26 23:59:22 +02:00
Nikita Popov
5bb3a995c2 Implement return for generators
For generators ZEND_RETURN directly calls ZEND_VM_RETURN(), thus passing
execution back to the caller (zend_generator_resume).

This commit also adds a check that only return; is used in generators and
not return $value;.
2012-05-26 23:07:40 +02:00
Nikita Popov
fafce58683 Add YIELD opcode implementation 2012-05-26 22:44:53 +02:00
Nikita Popov
1a99d1c887 Add way to pass generator object to opcode handlers
The generator zval is put into the return_value_ptr_ptr.
2012-05-26 19:40:29 +02:00
Nikita Popov
f627be5254 Add support for executing a zend_execute_data
This adds another function execute_ex(), which accepts a zend_execute_data
struct to run (contrary to execute(), which accepts a zend_op_array from
which it initialized the execute_data).

This needs a bit more cleanup.
2012-05-26 17:53:13 +02:00
Nikita Popov
ececcbce0e Allow calling zend_vm_gen from everywhere
Before one could only call it with cwd=Zend.
2012-05-23 20:40:21 +02:00
Nikita Popov
2c5ecb4fea Add dummy Iterator implementation
This simply adds dummy rewind/valid/current/key/next methods to Generator.
2012-05-23 16:44:58 +02:00
Nikita Popov
9ce9a7e639 Add initial code for suspending execution
This is just some initial code, which is still quite broken (and needs to be
moved so it can be reused.)
2012-05-23 14:20:25 +02:00
Nikita Popov
5e763d9420 Allocate execute_data using malloc for generators
Generators need to switch the execute_data very often. If the execute_data
is allocated on the VM stack this operation would require to always copy
the structure (which is quite large). That's why the execution context is
allocated on the heap instead (only for generators obviously).
2012-05-22 23:17:59 +02:00
Nikita Popov
46fa26ab85 Make generator functions return a Generator object
Right now generator functions simply immediately return a new Generator
object (no suspension yet).
2012-05-20 14:45:01 +02:00
Nikita Popov
40b7533576 Add some boilerplate code for Generator class
The Generator class now uses a zend_generator struct, so it'll be able to
store additional info.

This commit also ensures that Generator cannot be directly instantiated
and extended. The error tests are now in a separate folder from the
(yet-to-come) functional tests.
2012-05-20 14:19:16 +02:00
Nikita Popov
ca59e5464d Add empty Generator class 2012-05-20 00:03:27 +02:00
Nikita Popov
1cec3f12cc Add ZEND_SUSPEND_AND_RETURN_GENERATOR opcode
If the function is a generator this opcode will be invoked right after
receiving the function arguments.

The current implementation is just a dummy.
2012-05-19 23:19:21 +02:00
Nikita Popov
e14cfafcbf Add zend_do_suspend_if_generator calls
The execution of generator functions will be suspended right after the
arguments were RECVed. This will be done in zend_do_suspend_if_generator.
2012-05-19 20:19:45 +02:00
Nikita Popov
fd2a109f86 Add error if yield is used outside a generator
The yield statement can only be used in generator functions, which are
marked with an asterix.
2012-05-19 18:49:27 +02:00
Nikita Popov
9b51a3b96d Minor code cleanup
The block for the foreach separator was nested unnecessary. This commit
simply removes that nesting.
2012-05-19 14:21:49 +02:00
Nikita Popov
252f623464 Add flag for generator functions
Generator functions have to specify the * (asterix) modifier after the
function keyword. If they do so the ZEND_ACC_GENERATOR flag is added to
the fn_flags.
2012-05-19 14:18:20 +02:00
Nikita Popov
9b101ac8b3 Add T_YIELD "yield" keyword 2012-05-15 18:30:48 +02:00
Anatoliy Belsky
d0d7340d50 Merge branch 'PHP-5.4'
* PHP-5.4:
  Fix bug #61992 ext\standard\tests\general_functions\bug44295.phpt fails
2012-05-10 17:35:09 +02:00
Anatoliy Belsky
b43d6c8522 Merge branch 'PHP-5.3' into PHP-5.4
* PHP-5.3:
  Fix bug #61992 ext\standard\tests\general_functions\bug44295.phpt fails
2012-05-10 17:32:21 +02:00
Anatoliy Belsky
266578f584 Fix bug #61992 ext\standard\tests\general_functions\bug44295.phpt fails
Exception text differ on windows
2012-05-10 17:07:38 +02:00
Anatoliy Belsky
27685b7bb5 Merge branch 'PHP-5.4'
* PHP-5.4:
  updated NEWS
2012-05-10 15:58:30 +02:00
Anatoliy Belsky
fc0c9054e6 updated NEWS 2012-05-10 15:56:50 +02:00
Anatoliy Belsky
4184551b94 Merge branch 'PHP-5.4'
* PHP-5.4:
  updated NEWS
2012-05-10 15:53:08 +02:00
Anatoliy Belsky
19f85f8676 Merge branch 'PHP-5.3' into PHP-5.4
* PHP-5.3:
  updated NEWS
2012-05-10 15:52:37 +02:00
Anatoliy Belsky
652632a89b updated NEWS 2012-05-10 15:48:56 +02:00
Anatoliy Belsky
f6558a295d Merge branch 'PHP-5.4'
* PHP-5.4:
  Fix bug ext\standard\tests\file\realpath_cache_win32.phpt fails
2012-05-10 15:40:17 +02:00
Anatoliy Belsky
10d5f2301a Merge branch 'PHP-5.3' into PHP-5.4
* PHP-5.3:
  Fix bug ext\standard\tests\file\realpath_cache_win32.phpt fails
2012-05-10 15:36:47 +02:00
Anatoliy Belsky
f7d8b274c7 Fix bug ext\standard\tests\file\realpath_cache_win32.phpt fails
What happens here is trivial long overflow. Despite the bug attracted
attention on windows, the same story is on linux. Just wait for a big
anough bucket->key . The linux test had %i to check the key value
which should be %d all the way.
2012-05-10 15:27:44 +02:00
Anatoliy Belsky
e3f2c76989 Merge branch 'PHP-5.4'
* PHP-5.4:
  Fix bug 61901 ext\phar\tests\phar_buildfromdirectory2.phpt fails
2012-05-09 13:06:47 +02:00
Anatoliy Belsky
f76d7be876 Merge branch 'PHP-5.3' into PHP-5.4
* PHP-5.3:
  Fix bug 61901 ext\phar\tests\phar_buildfromdirectory2.phpt fails
2012-05-09 13:04:32 +02:00