Skip to content

Commit

Permalink
Fix #1917 AssignSuperGlobals
Browse files Browse the repository at this point in the history
fix bug: #1917
  • Loading branch information
chrysanthemum committed Sep 22, 2019
1 parent 40c45a5 commit a22a342
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Library/Backends/ZendEngine2/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Zephir\Fcall\FcallManagerInterface;
use Zephir\GlobalConstant;
use Zephir\Variable;
use Zephir\Variable\Globals;

/**
* Zephir\Backends\ZendEngine2\Backend.
Expand Down Expand Up @@ -1186,7 +1187,12 @@ public function setSymbolIfSeparated(Variable $variableTempSeparated, Variable $

public function copyOnWrite(Variable $target, $var, CompilationContext $context)
{
$context->codePrinter->output('ZEPHIR_CPY_WRT('.$this->getVariableCode($target).', '.$this->resolveValue($var, $context).');');
$globalsManager = new Globals();
if ($globalsManager->isSuperGlobal($target->getName())) {
$context->codePrinter->output('ZEPHIR_HASH_COPY('.$this->getVariableCode($target).', '.$this->resolveValue($var, $context).');');
} else {
$context->codePrinter->output('ZEPHIR_CPY_WRT('.$this->getVariableCode($target).', '.$this->resolveValue($var, $context).');');
}
}

public function forStatement(Variable $exprVariable, $keyVariable, $variable, $duplicateKey, $duplicateHash, $statement, $statementBlock, CompilationContext $compilationContext)
Expand Down
5 changes: 5 additions & 0 deletions kernels/ZendEngine3/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ int zephir_set_symbol_str(char *key_name, unsigned int key_length, zval *value);
ZEPHIR_OBS_VAR_ONCE(z); \
ZVAL_COPY(z, v);

#define ZEPHIR_HASH_COPY(z, v) \
if (Z_TYPE_P(z) == IS_ARRAY && Z_TYPE_P(v) == IS_ARRAY) { \
zend_hash_copy(Z_ARRVAL_P(z), Z_ARRVAL_P(v), (copy_ctor_func_t) zval_add_ref); \
}

#define ZEPHIR_OBS_NVAR(z) \
if (Z_TYPE_P(z) != IS_UNDEF) { \
if (Z_REFCOUNTED_P(z) && Z_REFCOUNT_P(z) > 1) { \
Expand Down
16 changes: 16 additions & 0 deletions test/assign.zep
Original file line number Diff line number Diff line change
Expand Up @@ -923,4 +923,20 @@ class Assign
let _GET["stestint2"] = 2;
let _GET[v] = "testval";
}

/**
* @link https://github.com/phalcon/zephir/issues/1917
*/
public function testAssignSuperGlobalsSERVER()
{
let _SERVER = array_merge(_SERVER, ["g1": "aaa", "g2": "bbb"]);
}

/**
* @link https://github.com/phalcon/zephir/issues/1917
*/
public function testAssignSuperGlobalsGET()
{
let _GET = array_merge(_GET, ["g1": "aaa", "g2": "bbb"]);
}
}
20 changes: 20 additions & 0 deletions unit-tests/Extension/AssignTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,24 @@ public function testGlobalVarAssign()
$this->assertSame($_GET['stestint2'], 2);
$this->assertSame($_GET['stest2'], 'testval');
}

public function testGlobalVarAssignSERVER()
{
$serverCount = count($_SERVER);

$t = new Assign();
$t->testAssignSuperGlobalsSERVER();

$this->assertSame($serverCount + 2, count($_SERVER));
}

public function testGlobalVarAssignGET()
{
$getCount = count($_GET);

$t = new Assign();
$t->testAssignSuperGlobalsGET();

$this->assertSame($getCount + 2, count($_GET));
}
}

0 comments on commit a22a342

Please sign in to comment.