From 1fc07ee7295dc6d82c79af2d1cf49f2c1c28cc8d Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Tue, 27 Jan 2026 17:47:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(compiler):=20=E6=B7=BB=E5=8A=A0=E5=AF=B9mi?= =?UTF-8?q?xed=E7=B1=BB=E5=9E=8B=E6=94=AF=E6=8C=81=E5=92=8C=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E5=87=BD=E6=95=B0=E8=B0=83=E7=94=A8=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在解析函数声明时调整了返回类型检查逻辑,先验证类型存在性再进行类型转换 - 新增对mixed类型的参数类型解析,映射到TYPE_VAR类型 - 添加对resource类型的错误处理机制 - 将原parseCallArgs方法替换为专门的parseNativeCallArgs方法用于本地函数调用 - 在C++主程序中添加php::request_init()初始化调用 - 新增埃拉托斯特尼筛法求素数示例程序及对应的vector扩展实现 - 添加vector.stub.php存根文件定义vector相关函数接口 --- examples/prime.php | 51 ++++++++++++++++++++++++++++++++ examples/prime/main.php | 54 ++++++++++++++++++++++++++++++++++ examples/prime/vector.cc | 32 ++++++++++++++++++++ examples/prime/vector.stub.php | 15 ++++++++++ src/Php/CompilerBase.php | 11 +++++-- src/cpp/main.cc | 1 + 6 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 examples/prime.php create mode 100644 examples/prime/main.php create mode 100644 examples/prime/vector.cc create mode 100644 examples/prime/vector.stub.php diff --git a/examples/prime.php b/examples/prime.php new file mode 100644 index 00000000..cd7aa939 --- /dev/null +++ b/examples/prime.php @@ -0,0 +1,51 @@ + + +using namespace php; + +class VectorBox : public Box { + public: + std::vector vec; + VectorBox(size_t size, bool init) { + vec.resize(size, init); + } + void checkOffset(Int offset) { + if (offset >= vec.size()) { + zend_throw_error(NULL, "index[%ld] is out of range()", offset); + } + } +}; + +var php_vector_new(Int size, Bool init) { + return {new VectorBox(size, init)}; +} + +Bool php_vector_get(var box, Int offset) { + auto vecbox = box.toBox(); + vecbox->checkOffset(offset); + return vecbox->vec.at(offset); +} + +void php_vector_set(var box, Int offset, Bool value) { + auto vecbox = box.toBox(); + vecbox->checkOffset(offset); + vecbox->vec.at(offset) = value; +} diff --git a/examples/prime/vector.stub.php b/examples/prime/vector.stub.php new file mode 100644 index 00000000..e2db0f8f --- /dev/null +++ b/examples/prime/vector.stub.php @@ -0,0 +1,15 @@ +returnType ? $this->getTypeFromZendType($this->parseIdentifier($v->returnType)) : self::TYPE_VOID; // .stub 存根定义 C++ Native 函数,必须设置返回值类型 - if ($returnType === self::TYPE_VOID && $this->stubFile) { + if (!$v->returnType && $this->stubFile) { throw new Exception('No return type for ' . $v->name); } + $returnType = $v->returnType ? $this->getTypeFromZendType($this->parseIdentifier($v->returnType)) : self::TYPE_VOID; $functionDef = new FunctionDef($this->parseIdentifier($v->name), $returnType); $this->functionDef = $functionDef; $this->parseParams($v->params, $functionDef); @@ -1182,6 +1182,11 @@ class CompilerBase extends \PhpAot\Core\Translator case 'void': $this->fatalError($param, 'Cannot use `void` as a parameter type.'); break; + case 'mixed': + return self::TYPE_VAR; + case 'resource': + $this->fatalError($param, 'Cannot use `resource` as a parameter type.'); + break; default: $this->objects[$var] = $name; return self::TYPE_OBJECT; @@ -1509,7 +1514,7 @@ class CompilerBase extends \PhpAot\Core\Translator } $nativeFn = $this->findNativeFunction($name); if ($nativeFn) { - return self::PREFIX . $nativeFn . '(' . $this->parseCallArgs($expr->args, $name) . ')'; + return self::PREFIX . $nativeFn . '(' . $this->parseNativeCallArgs($expr->args, $nativeFn) . ')'; } if ($this->isInternalFunction($name)) { $fn = 'php::' . $name; diff --git a/src/cpp/main.cc b/src/cpp/main.cc index 65b4708d..56c81119 100644 --- a/src/cpp/main.cc +++ b/src/cpp/main.cc @@ -35,6 +35,7 @@ int main(int cpp_argc, char **cpp_argv) { ProfilerStart("profile.out"); #endif zend_first_try { + php::request_init(); php_app_init(); php::eval("main();"); }