diff --git a/src/Php/AstNodeType.php b/src/Php/AstNodeType.php index f93fe4a9..75d7b1eb 100644 --- a/src/Php/AstNodeType.php +++ b/src/Php/AstNodeType.php @@ -100,6 +100,16 @@ trait AstNodeType return $expr instanceof Node\Scalar\Int_; } + protected function isScalarBool(NodeAbstract $expr): bool + { + return $expr instanceof Node\Expr\ConstFetch and in_array($expr->name->toString(), ['true', 'false']); + } + + protected function getBoolValue(Node\Expr\ConstFetch $expr): string + { + return $expr->name->toString() === 'true' ? self::VALUE_TRUE : self::VALUE_FALSE; + } + protected function isMatchExpr(NodeAbstract $expr): bool { return $expr instanceof Expr\Match_; diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index dd2b6a67..1137eaaa 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -76,6 +76,8 @@ class CompilerBase extends \PhpAot\Core\Translator public const string VALUE_INF = 'std::numeric_limits::infinity()'; public const string VALUE_NULL = 'php::null'; public const string VALUE_ZERO = 'php::zero'; + public const string VALUE_FALSE = 'php::false_value'; + public const string VALUE_TRUE = 'php::true_value'; public const string LITERAL_STRINGS = '_literal_strings'; public const string ANON_CLASS = '_anon_class_'; public const string STATIC_VAR = '_static_var_'; @@ -3659,14 +3661,33 @@ class CompilerBase extends \PhpAot\Core\Translator return $code . PHP_EOL; } + protected function parseCompareExpr(NodeAbstract $expr): string + { + // PHPX 与 bool 值比较会出现重载错误,所以需要转换成 bool 值 + if ($this->isScalarBool($expr)) { + return $this->getBoolValue($expr); + } + return $this->parseIdentifier($expr); + } + protected function parseBinaryOpEqual(Expr\BinaryOp\Equal $expr): string { - return 'php::equals(' . $this->parseExpr($expr->left) . ', ' . $this->parseExpr($expr->right) . ')'; + return 'php::equals(' . $this->parseCompareExpr($expr->left) . ', ' . $this->parseCompareExpr($expr->right) . ')'; } protected function parseBinaryOpNotEqual(Expr\BinaryOp\NotEqual $expr): string { - return '!php::equals(' . $this->parseExpr($expr->left) . ', ' . $this->parseExpr($expr->right) . ')'; + return '!php::equals(' . $this->parseCompareExpr($expr->left) . ', ' . $this->parseCompareExpr($expr->right) . ')'; + } + + protected function parseBinaryOpIdentical(Expr\BinaryOp $expr): string + { + $left = $this->parseCompareExpr($expr->left); + $right = $this->parseCompareExpr($expr->right); + if ($right === 'nullptr') { + return $left . '.isNull()'; + } + return 'php::same(' . $left . ', ' . $right . ')'; } /** @@ -3793,18 +3814,6 @@ class CompilerBase extends \PhpAot\Core\Translator return $code; } - protected function parseBinaryOpIdentical(Expr\BinaryOp $expr): string - { - $left = $this->parseIdentifier($expr->left); - $right = $this->parseIdentifier($expr->right); - - if ($right === 'nullptr') { - return $left . '.isNull()'; - } - - return 'php::same(' . $left . ', ' . $right . ')'; - } - protected function parseBinaryOpSpaceship(Expr\BinaryOp\Spaceship $expr): string { $left = $this->parseIdentifier($expr->left); diff --git a/tests/aot/std-array/004.phpt b/tests/aot/std-array/004.phpt new file mode 100644 index 00000000..1f50f3a9 --- /dev/null +++ b/tests/aot/std-array/004.phpt @@ -0,0 +1,34 @@ +--TEST-- +std array: 004 +--FILE-- + +--EXPECT-- +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) +bool(true) \ No newline at end of file