Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/Parser/TypeConversionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ protected function convertIntExpr(string $expr, string $fromType = ''): string
default => null,
};
if ($bigConversion !== null) {
return $bigConversion . '(' . $expr . ')';
// The Big* conversions return a php::Variant; unwrap it to the native scalar.
return 'php::toInt(' . $bigConversion . '(' . $expr . '))';
}
if (!$this->isClosedExpr($expr, 'php::toInt')) {
return 'php::toInt(' . $expr . ')';
Expand All @@ -74,7 +75,8 @@ protected function convertFloatExpr(string $expr, string $fromType = ''): string
default => null,
};
if ($bigConversion !== null) {
return $bigConversion . '(' . $expr . ')';
// The Big* conversions return a php::Variant; unwrap it to the native scalar.
return 'php::toFloat(' . $bigConversion . '(' . $expr . '))';
}
if (!$this->isClosedExpr($expr, 'php::toFloat')) {
return 'php::toFloat(' . $expr . ')';
Expand Down Expand Up @@ -184,7 +186,8 @@ protected function convertBoolExpr(string $expr, string $fromType = ''): string
default => null,
};
if ($bigConversion !== null) {
return $bigConversion . '(' . $expr . ')';
// The Big* conversions return a php::Variant; unwrap it to the native scalar.
return 'php::toBool(' . $bigConversion . '(' . $expr . '))';
}
if (!$this->isClosedExpr($expr, 'php::toBool')) {
return 'php::toBool(' . $expr . ')';
Expand All @@ -203,16 +206,25 @@ protected function convertConditionExpr(NodeAbstract $node, string $expr): strin
return $this->convertBoolExpr($expr, $type);
}

protected function bigNumberTypeOf(mixed $type): ?string
{
return in_array($type, [Type::BIGINT, Type::BIGFLOAT, Type::DECIMAL], true) ? $type : null;
}

protected function convertExprType(string $expr, $leftType, $rightType): string
{
// A BigInt/BigFloat/Decimal operand is a php::Box resource. The generic
// php::toFloat()/php::toInt() would read its resource handle, so the
// conversion must go through the high-precision type itself.
$bigType = $this->bigNumberTypeOf($rightType) ?? $this->bigNumberTypeOf($leftType) ?? '';
if ($leftType === Type::FLOAT or $rightType === Type::FLOAT) {
return $this->convertFloatExpr($expr);
return $this->convertFloatExpr($expr, $bigType);
}
if ($leftType === Type::INT or $rightType === Type::INT) {
return $this->convertIntExpr($expr);
return $this->convertIntExpr($expr, $bigType);
}
if ($leftType === Type::BOOL or $rightType === Type::BOOL) {
return $this->convertBoolExpr($expr);
return $this->convertBoolExpr($expr, $bigType);
}

return $expr;
Expand Down
46 changes: 46 additions & 0 deletions tests/compiler/bignumber/native-scalar-conversions.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
--TEST--
BigInt/BigFloat/Decimal values convert by value (not by box resource handle) into native int/float/bool slots
--FILE--
<?php
function takesFloat(float $x): float { return $x; }
function takesInt(int $x): int { return $x; }
function takesBool(bool $x): bool { return $x; }

// A float literal with 16+ fractional digits is recognized as a Decimal literal.
function returnsFloatFromDecimalLiteral(): float { return 0.123456789012345678; }
function returnsIntFromBigInt(): int { return std::bigInt('42'); }

function main(): void
{
// Arguments
var_dump(takesFloat(0.123456789012345678));
var_dump(takesFloat(std::decimal('1.5')));
var_dump(takesFloat(std::bigInt('42')));
var_dump(takesFloat(std::bigFloat('2.25')));
var_dump(takesInt(std::bigInt('42')));
var_dump(takesInt(std::decimal('7.9')));
var_dump(takesBool(std::bigInt('0')));
var_dump(takesBool(std::decimal('0.5')));

// Returns
var_dump(returnsFloatFromDecimalLiteral());
var_dump(returnsIntFromBigInt());

// A fixed native local receiving a Decimal literal
$f = 0.0;
$f = 0.123456789012345678;
var_dump($f);
}
?>
--EXPECT--
float(0.12345678901234568)
float(1.5)
float(42)
float(2.25)
int(42)
int(7)
bool(false)
bool(true)
float(0.12345678901234568)
int(42)
float(0.12345678901234568)
Loading