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
1 change: 1 addition & 0 deletions docs/capabilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ Auto-generated by `script/capability-matrix.php`. Do not edit by hand.
| `timezone_identifiers_list` | yes | yes | yes | standard | JIT PHPT |
| `timezone_location_get` | yes | no | no | standard | not implemented for JIT in this compiler build (issue #6041) |
| `timezone_offset_get` | yes | no | no | standard | not implemented for JIT in this compiler build (issue #6041) |
| `timezone_open` | yes | yes | yes | standard | JIT PHPT |
| `timezone_transitions_get` | yes | no | no | standard | not implemented for JIT in this compiler build (issue #6041) |
| `timezone_version_get` | yes | yes | yes | standard | JIT PHPT |
| `tmpfile` | yes | yes | yes | standard | JIT PHPT; AOT PHPT |
Expand Down
85 changes: 85 additions & 0 deletions ext/standard/JitTimezoneOpen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

namespace PHPCompiler\ext\standard;

use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\JitStringBuiltinArg;
use PHPCompiler\JIT\JitValueBox;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\DateTimeSupport;
use PHPCompiler\VM\NativeDateInvalidTimeZoneException;
use PHPLLVM\Value;

/** LLVM lowering for timezone_open() (#4634, ext/date/php_date.c). */
final class JitTimezoneOpen
{
public static function invoke(Context $context, JITVariable ...$args): Value
{
if (1 !== \count($args)) {
throw new \ArgumentCountError(
\sprintf('timezone_open() expects exactly 1 argument, %d given', \count($args))
);
}

$literal = JitStringBuiltinArg::compileTimeLiteral($args[0]);
if (null === $literal) {
throw new \LogicException(
'timezone_open() requires a compile-time timezone string in this compiler build (issue #4634)'
);
}

$vmCtx = $context->runtime->vmContext;
if (null === $vmCtx) {
throw new \LogicException('timezone_open() requires VM context at JIT compile time');
}
try {
DateTimeSupport::newDateTimeZoneVariable($vmCtx, $literal);
} catch (NativeDateInvalidTimeZoneException) {
return self::returnFalse($context);
}

return self::returnZoneForValidatedName($context, $literal);
}

private static function returnZoneForValidatedName(Context $context, string $timezone): Value
{
$objectType = $context->type->object;
$classId = $objectType->lookup('DateTimeZone');
$obj = $objectType->allocate($classId);
$objectType->markObjectConstructed($obj);

$propSlot = $objectType->propertySlotFor($obj, 'DateTimeZone', DateTimeSupport::TZ_NAME_PROPERTY);
$strVar = new JITVariable(
$context,
JITVariable::TYPE_STRING,
JITVariable::KIND_VALUE,
$context->builder->load($context->constantStringFromString($timezone))
);
$objectType->propertyStore($propSlot, $strVar, JITVariable::TYPE_STRING);

$slot = JitValueBox::alloc($context);
$ptr = JitValueBox::pointer($context, $slot);
$context->builder->call(
$context->lookupFunction('__value__writeObject'),
$ptr,
$obj
);

return $ptr;
}

private static function returnFalse(Context $context): Value
{
$slot = JitValueBox::alloc($context);
$ptr = JitValueBox::pointer($context, $slot);
JitValueBox::writeBool(
$context,
$slot,
$context->getTypeFromString('int1')->constInt(0, false)
);

return $ptr;
}
}
1 change: 1 addition & 0 deletions ext/standard/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ public function getFunctions(): array
new date(),
new timezone_version_get(),
new timezone_identifiers_list(),
new timezone_open(),
new timezone_offset_get(),
new timezone_location_get(),
new timezone_transitions_get(),
Expand Down
70 changes: 70 additions & 0 deletions ext/standard/timezone_open.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace PHPCompiler\ext\standard;

use PHPCompiler\Frame;
use PHPCompiler\Func\Internal;
use PHPCompiler\JIT\Context;
use PHPCompiler\JIT\Variable as JITVariable;
use PHPCompiler\VM\BuiltinExecute;
use PHPCompiler\VM\DateTimeSupport;
use PHPCompiler\VM\ErrorReporter;
use PHPCompiler\VM\NativeDateInvalidTimeZoneException;
use PHPLLVM\Value;

/**
* timezone_open() — procedural DateTimeZone factory (ext/date/php_date.c, #4634).
*
* php-src: ext/date/php_date.c — PHP_FUNCTION(timezone_open)
*/
final class timezone_open extends Internal
{
public function __construct()
{
parent::__construct('timezone_open');
}

public function execute(Frame $frame): void
{
if (1 !== \count($frame->calledArgs)) {
throw new \ArgumentCountError(
\sprintf('timezone_open() expects exactly 1 argument, %d given', \count($frame->calledArgs))
);
}
if (null === $frame->vmContext) {
throw new \LogicException('timezone_open() requires VM context in this compiler build');
}
$timezone = VmString::coerceStringBuiltinArg(
$frame->calledArgs[0],
'timezone_open',
0,
'timezone'
);
try {
$zone = DateTimeSupport::newDateTimeZoneVariable($frame->vmContext, $timezone);
BuiltinExecute::writeReturn($frame, static function ($ret) use ($zone): void {
$ret->copyFrom($zone);
});
} catch (NativeDateInvalidTimeZoneException) {
if (null !== $frame->vmContext) {
$frame->vmContext->errors->triggerError(
"timezone_open(): Unknown or bad timezone ({$timezone})",
ErrorReporter::E_WARNING,
'' !== $frame->scriptPath ? $frame->scriptPath : null,
$frame->vmContext,
$frame
);
}
BuiltinExecute::writeReturn($frame, static function ($ret): void {
$ret->bool(false);
});
}
}

public function call(Context $context, JITVariable ...$args): Value
{
return JitTimezoneOpen::invoke($context, ...$args);
}
}
19 changes: 19 additions & 0 deletions lib/VM/DateTimeSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,25 @@ public static function initDateTimeZone(ObjectEntry $zone, string $timezone): vo
$zone->constructed = true;
}

/**
* Allocate a DateTimeZone object (ext/date/php_date.c — PHP_FUNCTION(timezone_open), #4634).
*
* @throws NativeDateInvalidTimeZoneException when the timezone id is invalid
*/
public static function newDateTimeZoneVariable(Context $ctx, string $timezone): Variable
{
$class = $ctx->classes[self::CLASS_DATETIMEZONE] ?? null;
if (null === $class) {
throw new \LogicException('DateTimeZone is not registered in this compiler build');
}
$entry = new ObjectEntry($class);
self::initDateTimeZone($entry, $timezone);
$var = new Variable(Variable::TYPE_OBJECT);
$var->object($entry);

return $var;
}

/** php-src ext/date/php_datetimezone.c — invalid id throws DateInvalidTimeZoneException (#7279). */
public static function throwDateInvalidTimeZoneException(string $timezone): void
{
Expand Down
16 changes: 16 additions & 0 deletions test/compliance/cases/stdlib/timezone_open.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
stdlib timezone_open() returns DateTimeZone (#4634, ext/date/php_date.c)
--FILE--
<?php
var_dump(function_exists('timezone_open'));
$tz = timezone_open('UTC');
var_dump($tz instanceof DateTimeZone);
echo $tz->getName(), "\n";
$bad = timezone_open('Invalid/Zone');
var_dump($bad);
--EXPECT--
PHP Warning: timezone_open(): Unknown or bad timezone (Invalid/Zone)
bool(true)
bool(true)
UTC
bool(false)
11 changes: 11 additions & 0 deletions test/compliance/cases/stdlib/timezone_open_jit.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--TEST--
stdlib timezone_open() JIT returns DateTimeZone (#4634, ext/date/php_date.c)
--JIT--
--FILE--
<?php
$tz = timezone_open('UTC');
var_dump($tz instanceof DateTimeZone);
echo $tz->getName(), "\n";
--EXPECT--
bool(true)
UTC
9 changes: 9 additions & 0 deletions test/repro/timezone_open.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
// Repro for #4634 — timezone_open() procedural ext/date (php_date.c).
var_dump(function_exists('timezone_open'));
var_dump(function_exists('date_default_timezone_get'));
$tz = timezone_open('UTC');
var_dump($tz instanceof DateTimeZone);
echo date_default_timezone_get(), "\n";
date_default_timezone_set('Europe/Berlin');
echo date_default_timezone_get(), "\n";