Skip to content

Commit

Permalink
removing duplicates json/functions
Browse files Browse the repository at this point in the history
  • Loading branch information
niden committed Dec 3, 2023
1 parent df3d9e9 commit 94e9506
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 69 deletions.
15 changes: 13 additions & 2 deletions src/Optimizers/FunctionCall/FuncGetArgsOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,20 @@ public function optimize(array $expression, Call $call, CompilationContext $cont
$this->checkInitSymbolVariable($call, $symbolVariable, $context);

$symbol = $context->backend->getVariableCode($symbolVariable);
$context->headersManager->add('kernel/main');
$context->codePrinter->output('zephir_get_args(' . $symbol . ');');
$this->printOutput($context, $symbol);

return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression);
}

/**
* @param CompilationContext $context
* @param string $symbol
*
* @return void
*/
protected function printOutput(CompilationContext $context, string $symbol): void
{
$context->headersManager->add('kernel/main');
$context->codePrinter->output('zephir_get_args(' . $symbol . ');');
}
}
22 changes: 4 additions & 18 deletions src/Optimizers/FunctionCall/GetDefinedVarsOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,24 @@

namespace Zephir\Optimizers\FunctionCall;

use Zephir\Call;
use Zephir\CompilationContext;
use Zephir\CompiledExpression;
use Zephir\Optimizers\OptimizerAbstract;

/**
* GetDefinedVars.
*
* Optimizes calls to 'get_defined_vars' using internal function
*/
class GetDefinedVarsOptimizer extends OptimizerAbstract
class GetDefinedVarsOptimizer extends FuncGetArgsOptimizer
{
/**
* @param array $expression
* @param Call $call
* @param CompilationContext $context
* @param string $symbol
*
* @return bool|CompiledExpression|mixed
* @return void
*/
public function optimize(array $expression, Call $call, CompilationContext $context)
protected function printOutput(CompilationContext $context, string $symbol): void
{
$call->processExpectedReturn($context);
$symbolVariable = $call->getSymbolVariable(true, $context);
$this->checkNotVariableString($symbolVariable, $expression);

$this->checkInitSymbolVariable($call, $symbolVariable, $context);


$symbol = $context->backend->getVariableCode($symbolVariable);
$context->headersManager->add('kernel/variables');
$context->codePrinter->output('zephir_get_defined_vars(' . $symbol . ');');

return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression);
}
}
37 changes: 29 additions & 8 deletions src/Optimizers/FunctionCall/JsonDecodeOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Zephir\CompilationContext;
use Zephir\CompiledExpression;
use Zephir\Optimizers\OptimizerAbstract;
use Zephir\Variable\Variable;

use function count;

Expand All @@ -27,6 +28,8 @@
*/
class JsonDecodeOptimizer extends OptimizerAbstract
{
protected string $zephirMethod = 'zephir_json_decode';

/**
* @param array $expression
* @param Call $call
Expand All @@ -40,17 +43,12 @@ public function optimize(array $expression, Call $call, CompilationContext $cont
return false;
}

/*
/**
* Process the expected symbol to be returned
*/
$call->processExpectedReturn($context);

$symbolVariable = $call->getSymbolVariable();
$this->checkNotVariable($symbolVariable, $expression);
if (!$symbolVariable) {
$symbolVariable = $context->symbolTable->addTemp('variable', $context);
$symbolVariable->initVariant($context);
}
$symbolVariable = $this->processSymbolVariable($call, $expression, $context);

$context->headersManager->add('kernel/string');

Expand All @@ -71,9 +69,32 @@ public function optimize(array $expression, Call $call, CompilationContext $cont

$symbol = $context->backend->getVariableCode($symbolVariable);
$context->codePrinter->output(
'zephir_json_decode(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $options . ');'
$this->zephirMethod
. '(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $options . ');'
);

return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression);
}

/**
* @param Call $call
* @param array $expression
* @param CompilationContext $context
*
* @return Variable|null
*/
private function processSymbolVariable(
Call $call,
array $expression,
CompilationContext $context
): ?Variable {
$symbolVariable = $call->getSymbolVariable();
$this->checkNotVariable($symbolVariable, $expression);
if (!$symbolVariable) {
$symbolVariable = $context->symbolTable->addTemp('variable', $context);
$symbolVariable->initVariant($context);
}

return $symbolVariable;
}
}
53 changes: 12 additions & 41 deletions src/Optimizers/FunctionCall/JsonEncodeOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,32 @@

use Zephir\Call;
use Zephir\CompilationContext;
use Zephir\CompiledExpression;
use Zephir\Optimizers\OptimizerAbstract;

use function count;
use Zephir\Variable\Variable;

/**
* JsonEncodeOptimizer.
*
* Optimizes calls to 'json_encode' using internal function
*/
class JsonEncodeOptimizer extends OptimizerAbstract
class JsonEncodeOptimizer extends JsonDecodeOptimizer
{
protected string $zephirMethod = 'zephir_json_encode';

/**
* @param array $expression
* @param Call $call
* @param array $expression
* @param CompilationContext $context
*
* @return bool|CompiledExpression|mixed
* @return Variable|null
*/
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
return false;
}

/*
* Process the expected symbol to be returned
*/
$call->processExpectedReturn($context);

private function processSymbolVariable(
Call $call,
array $expression,
CompilationContext $context
): ?Variable {
$symbolVariable = $call->getSymbolVariable(true, $context);
$this->checkNotVariable($symbolVariable, $expression);

$context->headersManager->add('kernel/string');

$resolvedParams = $call->getReadOnlyResolvedParams($expression['parameters'], $context, $expression);

/*
* Process encode options
*/
if (count($resolvedParams) >= 2) {
$context->headersManager->add('kernel/operators');
$options = 'zephir_get_intval(' . $resolvedParams[1] . ') ';
} else {
$options = '0 ';
}

$this->checkInitSymbolVariable($call, $symbolVariable, $context);


$symbol = $context->backend->getVariableCode($symbolVariable);
$context->codePrinter->output(
'zephir_json_encode(' . $symbol . ', ' . $resolvedParams[0] . ', ' . $options . ');'
);

return new CompiledExpression('variable', $symbolVariable->getRealName(), $expression);
return $symbolVariable;
}
}

0 comments on commit 94e9506

Please sign in to comment.