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
25 changes: 16 additions & 9 deletions ext/standard/str_shuffle.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use PHPCompiler\Frame;
use PHPCompiler\Func\Internal;
use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\JitStringBuiltinArg;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\BuiltinExecute;
use PHPCompiler\VM\Variable;
use PHPLLVM\Value;

Expand All @@ -21,14 +23,16 @@ public function execute(Frame $frame): void
if (1 !== \count($frame->calledArgs)) {
throw new \LogicException('str_shuffle() requires exactly one argument');
}
$v = $frame->calledArgs[0]->resolveIndirect();
if (null === $frame->returnVar) {
return;
}
if (Variable::TYPE_STRING !== $v->type) {
throw new \LogicException('str_shuffle() only supports strings in this compiler build');
}
$frame->returnVar->string(VmString::strShuffle($v->toString()));
$subject = VmString::coerceStringBuiltinArg(
$frame->calledArgs[0],
'str_shuffle',
0,
'string'
);
BuiltinExecute::writeReturn(
$frame,
static fn (Variable $ret) => $ret->string(VmString::strShuffle($subject))
);
}

public function call(Context $context, JITVariable ...$args): Value
Expand All @@ -37,6 +41,9 @@ public function call(Context $context, JITVariable ...$args): Value
throw new \LogicException('str_shuffle() requires exactly one argument');
}

return JitStrShuffle::shuffle($context, $this->jitString($context, $args[0], 'str_shuffle() argument #1'));
return JitStrShuffle::shuffle(
$context,
JitStringBuiltinArg::lower($context, $args[0], 'str_shuffle', 0, 'string')
);
}
}
21 changes: 21 additions & 0 deletions test/compliance/cases/stdlib/str_shuffle_type_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
stdlib str_shuffle() — TypeError for non-string operand (#4551, ext/standard/string.c)
--FILE--
<?php
try {
$unused = str_shuffle([]);
echo "uncaught\n";
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
try {
$unused = str_shuffle(new stdClass());
echo "uncaught\n";
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
echo strlen(str_shuffle('abc')), "\n";
--EXPECT--
TypeError: str_shuffle(): Argument #1 ($string) must be of type string, array given
TypeError: str_shuffle(): Argument #1 ($string) must be of type string, stdClass given
3
14 changes: 14 additions & 0 deletions test/compliance/cases/stdlib/str_shuffle_type_error_jit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
stdlib str_shuffle() JIT — TypeError for non-string operand (#4551)
--FILE--
<?php
try {
$unused = str_shuffle([]);
echo "uncaught\n";
} catch (Throwable $e) {
echo $e::class, ': ', $e->getMessage(), "\n";
}
echo strlen(str_shuffle('abc')), "\n";
--EXPECT--
TypeError: str_shuffle(): Argument #1 ($string) must be of type string, array given
3
8 changes: 8 additions & 0 deletions test/fixtures/aot/cases/str_shuffle_type_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
AOT: str_shuffle() — TypeError for non-string operand (#4551)
--FILE--
<?php
str_shuffle([]);
--EXPECT--
--EXPECT_EXIT--
134