From 8257f8a1c4c626536dddf7f043b78adc61a26c63 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Wed, 4 Feb 2026 16:42:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(generator):=20=E6=B7=BB=E5=8A=A0=E5=B8=B8?= =?UTF-8?q?=E9=87=8F=E7=AC=A6=E5=8F=B7=E6=B3=A8=E5=86=8C=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 启用数组和对象常量定义支持 - 实现符号注册函数自动生成 - 添加 register_symbols 扩展模板支持 - 优化常量解析跳过复杂类型处理 - 新增模块符号注册函数匹配逻辑 --- bin/gen_stub.php | 37 +++++++++++++++++++---------------- examples/const.php | 5 ++--- src/Php/Translator.php | 12 +++++++++++- src/template/extension.cc.php | 7 +++++++ 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/bin/gen_stub.php b/bin/gen_stub.php index 8350ddd6..813e8349 100755 --- a/bin/gen_stub.php +++ b/bin/gen_stub.php @@ -2430,8 +2430,8 @@ class EvaluatedValue } else { if ($this->expr instanceof Expr\ConstFetch) { $value = constant($this->expr->name->__toString()); - $expr = strval($value); if (is_int($value)) { + $expr = strval($value); if ($value === PHP_INT_MIN) { $expr = 'LONG_MIN'; } elseif ($value === PHP_INT_MAX) { @@ -2440,8 +2440,6 @@ class EvaluatedValue $expr = $expr . 'L'; } } - } else { - $expr = $prettyPrinter->prettyPrintExpr($this->expr); } } return $expr[0] == '"' ? $expr : preg_replace('(\bnull\b)', 'NULL', str_replace('\\', '', $expr)); @@ -4377,20 +4375,25 @@ class FileInfo { } if ($stmt instanceof Stmt\Const_) { -// foreach ($stmt->consts as $const) { -// $this->constInfos[] = parseConstLike( -// $prettyPrinter, -// new ConstName($const->namespacedName, $const->name->toString()), -// $const, -// 0, -// null, -// $stmt->getComments(), -// $cond, -// $this->isUndocumentable, -// $this->getMinimumPhpVersionIdCompatibility(), -// AttributeInfo::createFromGroups($stmt->attrGroups) -// ); -// } + foreach ($stmt->consts as $const) { + if ($const->value instanceof PhpParser\Node\Expr\Array_ or + $const->value instanceof PhpParser\Node\Expr\New_ + ) { + continue; + } + $this->constInfos[] = parseConstLike( + $prettyPrinter, + new ConstName($const->namespacedName, $const->name->toString()), + $const, + 0, + null, + $stmt->getComments(), + $cond, + $this->isUndocumentable, + $this->getMinimumPhpVersionIdCompatibility(), + AttributeInfo::createFromGroups($stmt->attrGroups) + ); + } continue; } diff --git a/examples/const.php b/examples/const.php index 0e3d4a1b..31b5e165 100644 --- a/examples/const.php +++ b/examples/const.php @@ -5,12 +5,11 @@ const C_F = 1.1; const C_S = "str"; const C_B = true; const C_N = null; -//const C_A = [1, 2, 3]; -//const C_O = new stdClass(); +const C_A = [1, 2, 3]; +const C_O = new stdClass(); const C_I2 = -C_I; const C_F2 = C_F * 2; -const C_S2 = "hello"; function main() { diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 00dfe27f..23cb5958 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -30,6 +30,8 @@ class Translator extends Preprocessor protected bool $verbose = false; protected array $phpSrcFiles = []; protected array $argInfoHeaderFiles = []; + protected array $registerSymbols = []; + protected bool $useRegisterSymbolsFn = false; protected array $unsupportedFunctions = [ 'compact', 'extract', @@ -633,10 +635,18 @@ class Translator extends Preprocessor $this->climate->info('generate stub file: ' . $file); $this->climate->comment($genStubCmd); $stubFilenameWithoutExtension = str_replace(['.stub.php', '.php'], '', $file); - $headerFile = $this->getArgInfoHeaderFile($stubFilenameWithoutExtension, true); + $headerFile = $this->getArgInfoHeaderFile($stubFilenameWithoutExtension, true); if (!str_contains($output, 'Saved')) { $this->error("failed to generate arginfo header file: `{$headerFile}`, output: {$output}"); } + if ($this->useRegisterSymbolsFn) { + preg_match('/php_(.*)_arginfo.h/', $headerFile, $matches); + $registerSymbolFn = 'register_' . $matches[1] . '_symbols'; + $registerSymbol = PHP_EOL . 'static void ' . $registerSymbolFn . '(int module_number)' . PHP_EOL; + if (str_contains(file_get_contents($this->getBuildDir() . '/include/' . $headerFile), $registerSymbol)) { + $this->registerSymbols[] = $registerSymbolFn; + } + } $this->argInfoHeaderFiles[] = $headerFile; } diff --git a/src/template/extension.cc.php b/src/template/extension.cc.php index 5f3d0a7e..606973a6 100644 --- a/src/template/extension.cc.php +++ b/src/template/extension.cc.php @@ -88,6 +88,13 @@ foreach ($this->classCeList as $ce): = (); +// register symbols +registerSymbols as $registerSymbolFn): +?> + (module_number); + + return SUCCESS; }