Merge pull request '支持declare(strict_types=1);,这样通过zendVM调用其他函数的时候可以触发类型检查' (#46) from strict_types into master

Reviewed-on: #46
pull/47/head
韩天峰 2 weeks ago
commit f630fcf661
  1. 12
      src/Translator.php
  2. 4
      src/gen_stub.php
  3. 38
      tests/compiler/declare/strict_types-default.phpt
  4. 17
      tests/compiler/declare/strict_types.phpt

@ -901,11 +901,11 @@ CODE;
}
$fullName = $functionDef->getNamespacedName();
$zifName = $this->escapeZendFnName($fullName);
if ($functionDef->namespace) {
$code .= $this->getIndent() . 'ZEND_NAMED_FE("' . $this->escapeString($fullName) . '", ZEND_FN(' . $zifName . '), arginfo_' . $zifName . ')' . PHP_EOL;
} else {
$code .= $this->getIndent() . 'ZEND_FE(' . $zifName . ', arginfo_' . $zifName . ')' . PHP_EOL;
}
// TypePHP is always strict. Store the flag in the registered
// zend_function instead of rewriting shared metadata on every call.
$code .= $this->getIndent() . 'ZEND_RAW_FENTRY("' . $this->escapeString($fullName)
. '", ZEND_FN(' . $zifName . '), arginfo_' . $zifName
. ', ZEND_ACC_STRICT_TYPES, NULL, NULL)' . PHP_EOL;
}
$code .= $this->getIndent() . "ZEND_FE_END\n};\n// clang-format on" . PHP_EOL . PHP_EOL;
@ -3308,6 +3308,7 @@ CODE;
if ($functionDef->argCountRequired > 0) {
$cppCode .= $this->genWrapperRequiredArgCountCheck($functionDef, $displayName);
}
foreach ($functionDef->argInfoList as $k => $argInfo) {
$var = 'arg_' . $argInfo->name;
if ($argInfo->variadic) {
@ -4821,4 +4822,5 @@ CODE;
return $code;
}
}

@ -1688,7 +1688,9 @@ class FuncInfo {
private function getArginfoFlagsByPhpVersions(): VersionFlags
{
$flags = [];
// TypePHP is always strict. Register functions and methods with the
// flag once so dynamic calls made by native bodies inherit that mode.
$flags = ["ZEND_ACC_STRICT_TYPES"];
if ($this->isMethod()) {
if ($this->flags & Modifiers::PROTECTED) {

@ -0,0 +1,38 @@
--TEST--
TypePHP enables strict types without a declare directive
--FILE--
<?php
function checkStrictCaller(string $label): void
{
$callable = 'strlen';
try {
$callable(123);
echo $label, "=accepted\n";
} catch (TypeError $error) {
echo $label, "=TypeError\n";
}
}
class StrictMethodCaller
{
public function check(): void
{
checkStrictCaller('method');
}
}
function main(): void
{
checkStrictCaller('main');
$function = 'checkStrictCaller';
$function('function');
$method = [new StrictMethodCaller(), 'check'];
$method();
}
?>
--EXPECT--
main=TypeError
function=TypeError
method=TypeError

@ -0,0 +1,17 @@
--TEST--
declare: strict types 1
--FILE--
<?php
declare(strict_types=1);
function main() {
$callable = 'strlen';
try {
$callable(123);
echo "accepted\n";
} catch (TypeError $error) {
echo "TypeError\n";
}
}
?>
--EXPECT--
TypeError
Loading…
Cancel
Save