Skip to content

Commit

Permalink
Fixes #627
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutierrez committed Nov 17, 2014
1 parent fe333b1 commit 7835713
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
33 changes: 33 additions & 0 deletions Library/Operators/BaseOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,45 @@ public function setExpectReturn($expecting, Variable $expectingVariable = null)
$this->_expectingVariable = $expectingVariable;
}

/**
* Returns the expected variable for assignment or creates a temporary variable to
* store the result. This method returns a variable that is always stored in the heap
*
* @param CompilationContext $compilationContext
* @param array $expression
* @param boolean $init
* @return Variable
*/
public function getExpectedNonLiteral(CompilationContext $compilationContext, $expression, $init = true)
{
$isExpecting = $this->_expecting;
$symbolVariable = $this->_expectingVariable;

if ($isExpecting) {
if (is_object($symbolVariable)) {
if ($symbolVariable->getType() == 'variable' && !$symbolVariable->isLocalOnly()) {
if (!$init) {
return $symbolVariable;
}
$symbolVariable->initVariant($compilationContext);
} else {
$symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext, $expression);
}
} else {
$symbolVariable = $compilationContext->symbolTable->getTempVariableForWrite('variable', $compilationContext, $expression);
}
}
return $symbolVariable;
}

/**
* Returns the expected variable for assignment or creates a temporary variable to
* store the result
*
* @param CompilationContext $compilationContext
* @param array $expression
* @param boolean $init
* @return Variable
*/
public function getExpected(CompilationContext $compilationContext, $expression, $init = true)
{
Expand Down
8 changes: 7 additions & 1 deletion Library/Operators/Other/NewInstanceOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*/
class NewInstanceOperator extends BaseOperator
{
protected $_literalOnly = false;

/**
* Creates a new instance
Expand All @@ -51,11 +52,16 @@ public function compile(array $expression, CompilationContext $compilationContex
/**
* Resolves the symbol that expects the value
*/
$symbolVariable = $this->getExpected($compilationContext, $expression);
$this->_literalOnly = false;
$symbolVariable = $this->getExpectedNonLiteral($compilationContext, $expression);
if (!$symbolVariable->isVariable()) {
throw new CompilerException("Objects can only be instantiated into dynamic variables", $expression);
}

if ($symbolVariable->isLocalOnly()) {
throw new CompilerException("Cannot use non-heap variable to store new instance", $expression);
}

if ($symbolVariable->hasDifferentDynamicType(array('unknown', 'undefined', 'object', 'null'))) {
$compilationContext->logger->warning('Possible attempt to use non-object in "new" operator', 'non-valid-new', $expression);
}
Expand Down

0 comments on commit 7835713

Please sign in to comment.