Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFR] Support coroutine or yield #1849

Open
dreamsxin opened this issue Apr 21, 2019 · 9 comments
Open

[NFR] Support coroutine or yield #1849

dreamsxin opened this issue Apr 21, 2019 · 9 comments
Assignees
Labels
nfr New Feature Request

Comments

@dreamsxin
Copy link
Contributor

dreamsxin commented Apr 21, 2019

I don't think it's very meaningful to implement yield because curl and the IO wait of PDO can't be solved. We can add libuv and boost.context implementation a lower level symmetric coroutine.

@sergeyklay

@sergeyklay sergeyklay added the nfr New Feature Request label Apr 21, 2019
@sergeyklay
Copy link
Contributor

Yes, it seems so. Good idea, thank you!

@niden
Copy link
Contributor

niden commented Apr 21, 2019

I will insert a picture of a dancing bear here!

@dreamsxin
Copy link
Contributor Author

dreamsxin commented Apr 22, 2019

class AsyncTest {

    async plubic function test() {
        sleep(10);
        return 1;
    }
}
test = new AsyncTest;
test->test(); // nonblock
wait test->test(); // block
let ret = test->test(); // block

@dreamsxin
Copy link
Contributor Author

<?php

Generator implements Iterator {

	public current() {
	}

	public key() {
	
	}

	public next() {
	}

	public rewind() {
	}

	public send(mixed $value) {
	}

	public throw(Exception $exception) {
	}

	public valid () {
	}

	public __wakeup () {
	
	}
}

@dreamsxin
Copy link
Contributor Author

Reference zend source code

void zend_compile_yield(znode *result, zend_ast *ast) /* {{{ */
{
	zend_ast *value_ast = ast->child[0];
	zend_ast *key_ast = ast->child[1];

	znode value_node, key_node;
	znode *value_node_ptr = NULL, *key_node_ptr = NULL;
	zend_op *opline;
	zend_bool returns_by_ref = (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) != 0;

	zend_mark_function_as_generator();

	if (key_ast) {
		zend_compile_expr(&key_node, key_ast);
		key_node_ptr = &key_node;
	}

	if (value_ast) {
		if (returns_by_ref && zend_is_variable(value_ast) && !zend_is_call(value_ast)) {
			zend_compile_var(&value_node, value_ast, BP_VAR_W);
		} else {
			zend_compile_expr(&value_node, value_ast);
		}
		value_node_ptr = &value_node;
	}

	opline = zend_emit_op(result, ZEND_YIELD, value_node_ptr, key_node_ptr);

	if (value_ast && returns_by_ref && zend_is_call(value_ast)) {
		opline->extended_value = ZEND_RETURNS_FUNCTION;
	}
}
/* }}} */

void zend_compile_yield_from(znode *result, zend_ast *ast) /* {{{ */
{
	zend_ast *expr_ast = ast->child[0];
	znode expr_node;

	zend_mark_function_as_generator();

	if (CG(active_op_array)->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
		zend_error_noreturn(E_COMPILE_ERROR,
			"Cannot use \"yield from\" inside a by-reference generator");
	}

	zend_compile_expr(&expr_node, expr_ast);
	zend_emit_op_tmp(result, ZEND_YIELD_FROM, &expr_node, NULL);
}
/* }}} */

@niden
Copy link
Contributor

niden commented Dec 10, 2019

Any updates on this boys or we are doing other stuff?

@sergeyklay
Copy link
Contributor

Not yet

@Jeckerson Jeckerson added this to the 0.14.x milestone Apr 14, 2021
@JWprogrammer
Copy link

Generators (using yield) are a very important feature to convert PHP projects to Zephir.
Thank you for developing a good tool and trying to make it as similar to PHP as possible!

@Jeckerson Jeckerson changed the title [NFR]Support coroutine or yield [NFR] Support coroutine or yield Sep 10, 2021
@Jeckerson
Copy link
Member

Jeckerson commented Sep 11, 2021

Blocked by zephir-lang/php-zephir-parser#118

@Jeckerson Jeckerson modified the milestones: 0.14.x, Backlog Oct 3, 2021
@Jeckerson Jeckerson self-assigned this Oct 12, 2021
@Jeckerson Jeckerson removed this from the Backlog milestone Oct 12, 2021
@Jeckerson Jeckerson added this to the 0.16.0 milestone Oct 24, 2021
Jeckerson added a commit that referenced this issue Nov 7, 2021
@Jeckerson Jeckerson modified the milestones: 0.16.0, Backlog Apr 24, 2022
@Jeckerson Jeckerson removed this from the Backlog milestone Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
nfr New Feature Request
Projects
None yet
Development

No branches or pull requests

5 participants