feat(closure): 添加 C++ lambda 捕获局部变量支持

- 在 ClosureGenerator 中实现 & 捕获当前函数所有局部变量作为 arguments 处理
- 将原始上下文的局部变量赋值给 context 的 arguments 属性
- 添加静态变量测试用例验证 C++ lambda 捕获功能
pull/1/head
韩天峰 5 months ago
parent f226382d52
commit 5260559345
  1. 3
      src/Php/Generator/ClosureGenerator.php
  2. 16
      tests/aot/static-vars-02.phpt

@ -101,6 +101,9 @@ trait ClosureGenerator
// 使用 lambda 函数来对 static 变量进行赋值
$this->context = new FunctionContext();
// C++ lambda 使用 & 捕获了当前函数的所有局部变量,可直接使用,不需要再声明,将其作为 arguments 来处理,隐式使用
$this->context->arguments = $oriCtx->localVars;
$code .= '([&](){' . PHP_EOL;
$body = $cb();
$code .= $this->genLocalVarDecl();

@ -0,0 +1,16 @@
--TEST--
static variables
--FILE--
<?php
function main() {
$c = 100;
static $arr3 = array($c => 'ten');
print_r($arr3);
}
?>
--EXPECT--
Array
(
[100] => ten
)
Loading…
Cancel
Save