Skip to content
Merged
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
41 changes: 37 additions & 4 deletions ext/standard/similar_text.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
use PHPCompiler\Func\Internal;
use PHPCompiler\JIT\Builtin\StringSimilarText;
use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\InternalStrictArg as JitInternalStrictArg;
use PHPCompiler\JIT\JitStringBuiltinArg;
use PHPCompiler\JIT\JitValueBox;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\InternalStrictArg;
use PHPLLVM\Builder;
use PHPLLVM\Value;

Expand All @@ -30,8 +32,8 @@ public function execute(Frame $frame): void
if ($argc < 2 || $argc > 3) {
throw new \LogicException('similar_text() accepts two or three arguments in this compiler build');
}
$s1 = VmString::coerceStringBuiltinArg($frame->calledArgs[0], 'similar_text', 0, 'string1');
$s2 = VmString::coerceStringBuiltinArg($frame->calledArgs[1], 'similar_text', 1, 'string2');
$s1 = self::vmStringArg($frame, 0, 'string1');
$s2 = self::vmStringArg($frame, 1, 'string2');
if (3 === $argc) {
$percent = 0.0;
$sim = VmString::similar_text($s1, $s2, $percent);
Expand All @@ -55,8 +57,8 @@ public function call(Context $context, JITVariable ...$args): Value
throw new \LogicException('similar_text() accepts two or three arguments in this compiler build');
}
StringSimilarText::ensureLinked($context);
$str0 = JitStringBuiltinArg::lower($context, $args[0], 'similar_text', 0, 'string1');
$str1 = JitStringBuiltinArg::lower($context, $args[1], 'similar_text', 1, 'string2');
$str0 = self::jitStringArg($context, $args[0], 1, 'string1');
$str1 = self::jitStringArg($context, $args[1], 2, 'string2');
$p0 = $this->stringDataPtr($context, $str0);
$p1 = $this->stringDataPtr($context, $str1);
$fn = $context->lookupFunction('phpc_similar_text');
Expand All @@ -71,6 +73,37 @@ public function call(Context $context, JITVariable ...$args): Value
return $sim;
}

private static function vmStringArg(Frame $frame, int $argIndex, string $paramName): string
{
if (null !== $frame->parent && $frame->parent->block->strictTypes) {
return InternalStrictArg::requireString($frame, $argIndex, 'similar_text', $paramName)->toString();
}

return VmString::coerceStringBuiltinArg(
$frame->calledArgs[$argIndex],
'similar_text',
$argIndex,
$paramName
);
}

private static function jitStringArg(
Context $context,
JITVariable $arg,
int $argNumber,
string $paramName
): Value {
JitInternalStrictArg::requireString($context, $arg, 'similar_text', $paramName, $argNumber);

return JitStringBuiltinArg::lower(
$context,
$arg,
'similar_text',
$argNumber - 1,
$paramName
);
}

private function jitWriteSimilarityPercent(
Context $context,
Value $strData0,
Expand Down
4 changes: 4 additions & 0 deletions test/compliance/SimilarTextJITTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public static function providePHPTests(): \Generator
__DIR__.'/cases/stdlib/similar_text_coerce_jit.phpt',
'similar_text_coerce_jit.phpt'
);
yield 'similar_text_strict_types_jit.phpt' => self::parsePHPT(
__DIR__.'/cases/stdlib/similar_text_strict_types_jit.phpt',
'similar_text_strict_types_jit.phpt'
);
}

public function setUp(): void
Expand Down
4 changes: 4 additions & 0 deletions test/compliance/SimilarTextVMTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public static function providePHPTests(): \Generator
__DIR__.'/cases/stdlib/similar_text_type_error.phpt',
'similar_text_type_error.phpt'
);
yield 'similar_text_strict_types.phpt' => self::parsePHPT(
__DIR__.'/cases/stdlib/similar_text_strict_types.phpt',
'similar_text_strict_types.phpt'
);
}

public function setUp(): void
Expand Down
18 changes: 18 additions & 0 deletions test/compliance/cases/stdlib/similar_text_strict_types.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
stdlib similar_text() — strict call-site TypeError on int operands (#9519, ext/standard/string.c)
--FILE--
<?php
declare(strict_types=1);
try {
similar_text(123, '123');
echo "uncaught\n";
} catch (Throwable $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
$p = 0.0;
similar_text('hello', 'hello world', $p);
var_export($p > 99.0);
echo "\n";
--EXPECT--
TypeError: similar_text(): Argument #1 ($string1) must be of type string, int given
false
14 changes: 14 additions & 0 deletions test/compliance/cases/stdlib/similar_text_strict_types_jit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
stdlib similar_text() JIT — strict call-site TypeError on int operands (#9519)
--JIT--
--FILE--
<?php
declare(strict_types=1);
try {
similar_text(123, '123');
echo "uncaught\n";
} catch (Throwable $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
--EXPECT--
TypeError: similar_text(): Argument #1 ($string1) must be of type string, int given
6 changes: 2 additions & 4 deletions test/repro/issue_4543_similar_text.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php
declare(strict_types=1);
// Zend coerces scalars to string
echo similar_text(123, '123'), "\n";

// Zend TypeErrors on int operands when strict_types=1 is in effect at the call site
try {
similar_text([], 'x');
similar_text(123, '123');
} catch (Throwable $e) {
echo get_class($e), "\n";
}
Expand Down