feat(generator): 添加常量符号注册功能

- 启用数组和对象常量定义支持
- 实现符号注册函数自动生成
- 添加 register_symbols 扩展模板支持
- 优化常量解析跳过复杂类型处理
- 新增模块符号注册函数匹配逻辑
pull/1/head
韩天峰 7 months ago
parent 47e85c9de4
commit 8257f8a1c4
  1. 37
      bin/gen_stub.php
  2. 5
      examples/const.php
  3. 12
      src/Php/Translator.php
  4. 7
      src/template/extension.cc.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;
}

@ -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()
{

@ -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;
}

@ -88,6 +88,13 @@ foreach ($this->classCeList as $ce):
<?=$ce?> = <?= $info['func'] ?>(<?= $info['args'] ?>);
<?php endforeach; ?>
// register symbols
<?php
foreach ($this->registerSymbols as $registerSymbolFn):
?>
<?= $registerSymbolFn ?>(module_number);
<?php endforeach; ?>
return SUCCESS;
}

Loading…
Cancel
Save