Force enable type checking

pull/46/head
NathanFreeman 3 weeks ago
parent 7f46445a38
commit b69582011a
  1. 29
      src/Translator.php

@ -94,7 +94,6 @@ class Translator extends Preprocessor
*/ */
protected array $classCeList = []; protected array $classCeList = [];
protected array $classCeInfo = []; protected array $classCeInfo = [];
private int $strictTypes = 0;
protected function isConstructorNativeFunction(FunctionDef $func): bool protected function isConstructorNativeFunction(FunctionDef $func): bool
{ {
@ -2417,12 +2416,6 @@ CODE;
$this->resetMethod(); $this->resetMethod();
$this->resetFunction(); $this->resetFunction();
/**
* Here, the value of strict_types needs to be reset,
* as not every file has declare(strict_types=1); set.
*/
$this->setStrictTypes(0);
$cppCode = ''; $cppCode = '';
foreach ($stmts as $v) { foreach ($stmts as $v) {
$type = $v->getType(); $type = $v->getType();
@ -3257,9 +3250,9 @@ CODE;
} }
/** /**
* If the current file declares `declare(strict_types=1);`, the current function must be marked with the `ZEND_ACC_STRICT_TYPES` flag. * Tthe current function must be marked with the `ZEND_ACC_STRICT_TYPES` flag.
* This flag is used to ensure that when the function calls other functions through the ZendVM, it strictly validates argument types * This flag is used to ensure that when the function calls other functions through the ZendVM,
* according to the declared mode. * it strictly validates argument types according to the declared mode.
* *
* It is important to note that this mechanism only applies to functions dynamically invoked by the ZendVM. * It is important to note that this mechanism only applies to functions dynamically invoked by the ZendVM.
* If the target function is compiled native code, type checking will not be triggered—this is by design in the ZendVM, * If the target function is compiled native code, type checking will not be triggered—this is by design in the ZendVM,
@ -5005,20 +4998,6 @@ CODE;
private function genStrictTypesCode(): string private function genStrictTypesCode(): string
{ {
/** return "execute_data->func->common.fn_flags |= ZEND_ACC_STRICT_TYPES;" . PHP_EOL;
* Type checking is enabled only in binary mode. Since binary mode runs as a standalone executable,
* it must preserve behavioral semantics consistent with directly executed PHP code,
* so type checking must be retained to ensure safety.
* Conversely, when compiled as an extension (e.g., ext/curl), the compilation side does not need to
* enforce built-in type checking; whether it is enabled should be left to the calling PHP code to decide.
*/
return $this->strictTypes == 1 && $this->isBuildModeBin() && !$this->isWasiTarget()
? "execute_data->func->common.fn_flags |= ZEND_ACC_STRICT_TYPES;" . PHP_EOL
: '';
}
private function setStrictTypes(int $value): void
{
$this->strictTypes = $value;
} }
} }

Loading…
Cancel
Save