From 5260559345d63d561aceff374ca879a8f3a2e318 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 7 Apr 2026 13:09:08 +0800 Subject: [PATCH] =?UTF-8?q?feat(closure):=20=E6=B7=BB=E5=8A=A0=20C++=20lam?= =?UTF-8?q?bda=20=E6=8D=95=E8=8E=B7=E5=B1=80=E9=83=A8=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 ClosureGenerator 中实现 & 捕获当前函数所有局部变量作为 arguments 处理 - 将原始上下文的局部变量赋值给 context 的 arguments 属性 - 添加静态变量测试用例验证 C++ lambda 捕获功能 --- src/Php/Generator/ClosureGenerator.php | 3 +++ tests/aot/static-vars-02.phpt | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) create mode 100644 tests/aot/static-vars-02.phpt diff --git a/src/Php/Generator/ClosureGenerator.php b/src/Php/Generator/ClosureGenerator.php index 518da8ed..b72fe82c 100644 --- a/src/Php/Generator/ClosureGenerator.php +++ b/src/Php/Generator/ClosureGenerator.php @@ -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(); diff --git a/tests/aot/static-vars-02.phpt b/tests/aot/static-vars-02.phpt new file mode 100644 index 00000000..57535817 --- /dev/null +++ b/tests/aot/static-vars-02.phpt @@ -0,0 +1,16 @@ +--TEST-- +static variables +--FILE-- + 'ten'); + print_r($arr3); +} +?> +--EXPECT-- +Array +( + [100] => ten +) \ No newline at end of file