全局变量

pull/1/head
韩天峰 8 months ago
parent 730e0e45f1
commit 04f265ae24
  1. 5
      examples/div.php
  2. 4
      main.cc
  3. 1
      run-tests.php
  4. 19
      src/Php/Translator.php
  5. 6
      tests/core/basic/001.phpt
  6. 6
      tests/core/basic/006.phpt
  7. 6
      tests/core/basic/007.phpt
  8. 6
      tests/core/basic/008.phpt
  9. 6
      tests/core/basic/009.phpt
  10. 6
      tests/core/basic/010.phpt
  11. 23
      tests/core/basic/012.phpt
  12. 28
      tests/core/basic/bug80384.phpt
  13. 31
      tests/core/basic/consistent_float_string_casts.phpt
  14. 38
      tests/core/basic/encoding.phpt
  15. 14
      tests/zend/indirect_function_call/indirect_call_array_001.phpt
  16. 14
      tests/zend/indirect_function_call/indirect_call_array_002.phpt

@ -1,8 +1,7 @@
<?php
function main()
{
$array = [1, 232, 333, 4, 35];
sort($array);
var_dump($array);
var_dump($_SERVER);
var_dump($_ENV);
}

@ -10,6 +10,7 @@ extern php::Var argc;
extern php::Var argv;
extern void php_init_constant_vars();
extern void php_init_global_vars();
extern void php_unset_global_vars();
static void throw_exception(zend_object *ex) {
@ -33,8 +34,7 @@ int main(int cpp_argc, char **cpp_argv) {
ProfilerStart("myapp.prof");
#endif
zend_first_try {
argc = php::global("argc");
argv = php::global("argv");
php_init_global_vars();
php_init_constant_vars();
php_main();
}

@ -2432,6 +2432,7 @@ TEST $file
global $no_aot;
if (!$no_aot) {
$bin_file = compile_php_file($test_file);
$args = substr($args, strlen(' -- '));
$cmd = './' . $bin_file . ' ' . $args . $cmdRedirect;
}

@ -1250,6 +1250,9 @@ class Translator extends \PhpAot\Core\Translator
$type = $this->detectVarType($node->var);
$rightExprStr = $this->convertExprType($expr, $type, $this->detectExprType($node->expr));
if ($this->isAssignOpConcat($op)) {
if ($this->isArrayVar($node->var)) {
$this->fatalError($node->var, 'Cannot concat string to array');
}
return $var . '.append(' . $rightExprStr . ')';
} elseif ($this->isAssignOpPow($op)) {
$powExpr = 'php::call(php::pow, {' . $var . ', ' . $rightExprStr . '})';
@ -2272,6 +2275,17 @@ class Translator extends \PhpAot\Core\Translator
$this->indentLevel--;
$code .= $this->genFunction(self::PREFIX . 'init_constant_vars', 'void', [], $lines);
// 生成全局变量
$code .= PHP_EOL;
$this->indentLevel++;
$lines = [];
foreach ($this->globalVars as $name => $type) {
$lines[] = $this->getIndent() . $name . ' = php::global("' . $name . '");';
}
$this->indentLevel--;
$code .= $this->genFunction(self::PREFIX . 'init_global_vars', 'void', [], $lines);
// 销毁全局变量
$code .= PHP_EOL;
$this->indentLevel++;
$lines = [];
@ -2588,4 +2602,9 @@ class Translator extends \PhpAot\Core\Translator
}
return self::TYPE_VAR;
}
private function isArrayVar($var): bool
{
return $var->getType() == self::EXPR_VARIABLE and $this->hasVar($var->name) and $this->getVarType($var->name) == self::TYPE_ARRAY;
}
}

@ -0,0 +1,6 @@
--TEST--
Trivial "Hello World" test
--FILE--
<?php echo "Hello World";?>
--EXPECT--
Hello World

@ -0,0 +1,6 @@
--TEST--
Add 3 variables together and print result
--FILE--
<?php $a=1; $b=2; $c=3; $d=$a+$b+$c; echo $d;?>
--EXPECT--
6

@ -0,0 +1,6 @@
--TEST--
Multiply 3 variables and print result
--FILE--
<?php $a=2; $b=4; $c=8; $d=$a*$b*$c; echo $d;?>
--EXPECT--
64

@ -0,0 +1,6 @@
--TEST--
Divide 3 variables and print result
--FILE--
<?php $a=27; $b=3; $c=3; $d=$a/$b/$c; echo $d;?>
--EXPECT--
3

@ -0,0 +1,6 @@
--TEST--
Subtract 3 variables and print result
--FILE--
<?php $a=27; $b=7; $c=10; $d=$a-$b-$c; echo $d;?>
--EXPECT--
10

@ -0,0 +1,6 @@
--TEST--
Testing | and & operators
--FILE--
<?php $a=8; $b=4; $c=8; echo $a|$b&$c;?>
--EXPECT--
8

@ -0,0 +1,23 @@
--TEST--
Testing $argc and $argv handling (cli)
--INI--
register_argc_argv=1
variables_order=GPS
--ARGS--
ab cd ef 123 test
--FILE--
<?php
$argc = $_SERVER['argc'];
$argv = $_SERVER['argv'];
for ($i=1; $i<$argc; $i++) {
echo ($i-1).": ".$argv[$i]."\n";
}
?>
--EXPECT--
0: ab
1: cd
2: ef
3: 123
4: test

@ -0,0 +1,28 @@
--TEST--
Bug #80384 large reads cause filters to internally buffer large amounts of memory
--FILE--
<?php
/* First, create a file to read */
$tmp_filename = __DIR__ . "/bug80384.tmp";
$fp = fopen($tmp_filename, 'w');
for ($i=0; $i<1024; $i++) {
fwrite($fp, str_repeat('ABCDEFGH', 1024));
}
fclose($fp);
/* Stream the file through a filter */
$fp = fopen($tmp_filename, 'r');
$filter = stream_filter_append($fp, "string.rot13");
$mem_start = memory_get_usage();
fread($fp, 8 * 1024 * 1024);
$mem_final = memory_get_usage();
fclose($fp);
var_dump($mem_final - $mem_start < 32768);
?>
--CLEAN--
<?php
unlink(__DIR__ . "/bug80384.tmp");
?>
--EXPECT--
bool(true)

@ -0,0 +1,31 @@
--TEST--
Test that float to string and string to float casts are consistent
--SKIPIF--
<?php
if (setlocale(LC_ALL, 'invalid') === 'invalid') { die('skip setlocale() is broken /w musl'); }
if (!setlocale(
LC_ALL,
"german", "de", "de_DE", "de_DE.ISO8859-1", "de_DE.ISO_8859-1", "de_DE.UTF-8",
"french", "fr", "fr_FR", "fr_FR.ISO8859-1", "fr_FR.ISO_8859-1", "fr_FR.UTF-8",
)) {
die("skip locale needed for this test is not supported on this platform");
}
?>
--FILE--
<?php
setlocale(
LC_ALL,
"german", "de", "de_DE", "de_DE.ISO8859-1", "de_DE.ISO_8859-1", "de_DE.UTF-8",
"french", "fr", "fr_FR", "fr_FR.ISO8859-1", "fr_FR.ISO_8859-1", "fr_FR.UTF-8",
);
$float = 1/3;
$string = (string) $float;
$float = (float) $string;
printf("%.2f", $float);
?>
--EXPECT--
0,33

@ -0,0 +1,38 @@
--TEST--
PHP encoding setting test
--FILE--
<?php
var_dump(ini_get('default_charset'));
var_dump(ini_get('input_encoding'));
var_dump(ini_get('internal_encoding'));
var_dump(ini_get('output_encoding'));
var_dump(ini_set('default_charset', 'ISO-8859-1'));
var_dump(ini_get('default_charset'));
var_dump(ini_get('input_encoding'));
var_dump(ini_get('internal_encoding'));
var_dump(ini_get('output_encoding'));
var_dump(ini_set('input_encoding', 'EUC-JP'));
var_dump(ini_set('internal_encoding', 'EUC-JP'));
var_dump(ini_set('output_encoding', 'EUC-JP'));
var_dump(ini_get('input_encoding'));
var_dump(ini_get('internal_encoding'));
var_dump(ini_get('output_encoding'));
?>
--EXPECT--
string(5) "UTF-8"
string(0) ""
string(0) ""
string(0) ""
string(5) "UTF-8"
string(10) "ISO-8859-1"
string(0) ""
string(0) ""
string(0) ""
string(0) ""
string(0) ""
string(0) ""
string(6) "EUC-JP"
string(6) "EUC-JP"
string(6) "EUC-JP"

@ -0,0 +1,14 @@
--TEST--
Indirect method call by array - Invalid class name
--FILE--
<?php
$arr = array('a', 'b');
$arr();
?>
--EXPECTF--
Fatal error: Uncaught Error: Invalid callback a::b, class "a" not found in %s
Stack trace:
#0 {main}
thrown in %s on line %d

@ -0,0 +1,14 @@
--TEST--
Indirect method call by array - Invalid method name
--FILE--
<?php
$arr = array('stdclass', 'b');
$arr();
?>
--EXPECTF--
Fatal error: Uncaught Error: Invalid callback stdclass::b, class stdClass does not have a method "b" in %s
Stack trace:
#0 {main}
thrown in %s on line %d
Loading…
Cancel
Save