Re: Generators in PHP

From: Date: Fri, 08 Jun 2012 11:51:14 +0000
Subject: Re: Generators in PHP
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On Fri, 8 Jun 2012 13:24:49 +0200, Nikita Popov wrote:
Are you planning on any internal API for functions to implement generators (though I really haven't thought how that would work)?
I don't think that this is possible. Generators require that the execution context is suspended in some way and we have to that control only over userland code, not internal C code.
Couldn't we simulate this by saving the state in a heap allocated structure (whose exact form would depend on the generator implementation). Something like: struct generator_context {
    zval *(*yield_next)(struct generator_context*);
} struct spec_generator_context {
    struct generator_context parent;
    int foo;
} PHP_FUNCTION(get_generator) {
    struct spec_generator_context *ctx = emalloc(*ctx);
    ctx->parent.yield_next = foo_bar();
    return_value = make_internal_generator(ctx);
} And you could also change the yield_next pointer in foo_bar() to avoid going through goto's or switch statements. Possibly yield_next could be take the arg with double indirection and you could also completely replace the context. I understand this has the problem that the internal and userspace implementations would be markedly different. -- Gustavo Lopes

Thread (142 messages)

« previous php.internals (#60782) next »