Skip to content

Commit

Permalink
Adding is_php_version optimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutierrez committed Oct 21, 2014
1 parent 9ff4e6b commit 4dfbb7d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 8 deletions.
67 changes: 67 additions & 0 deletions Library/Optimizers/FunctionCall/IsPhpVersionOptimizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/*
+--------------------------------------------------------------------------+
| Zephir Language |
+--------------------------------------------------------------------------+
| Copyright (c) 2013-2014 Zephir Team and contributors |
+--------------------------------------------------------------------------+
| This source file is subject the MIT license, that is bundled with |
| this package in the file LICENSE, and is available through the |
| world-wide-web at the following url: |
| http://zephir-lang.com/license.html |
| |
| If you did not receive a copy of the MIT license and are unable |
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+--------------------------------------------------------------------------+
*/

namespace Zephir\Optimizers\FunctionCall;

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

/**
* IsPhpVersionOptimizer
*
* Checks if PHP has a specific version
*/
class IsPhpVersionOptimizer extends OptimizerAbstract
{
/**
*
* @param array $expression
* @param Call $call
* @param CompilationContext $context
*/
public function optimize(array $expression, Call $call, CompilationContext $context)
{
if (!isset($expression['parameters'])) {
throw new CompilerException("This function requires parameters", $expression);
}

if (count($expression['parameters']) != 1) {
throw new CompilerException("This function only requires one parameter", $expression);
}

if ($expression['parameters'][0]['parameter']['type'] != 'string') {
throw new CompilerException("This function requires a string parameter", $expression);
}

preg_match('/^([0-9]+)\.([0-9]+)\.?([0-9]+)?$/', $expression['parameters'][0]['parameter']['value'], $matches);
if (!count($matches)) {
throw new CompilerException("Could not parse PHP version", $expression);
}

$major = $matches[1] * 10000;
$minor = $matches[2] * 100;

$version = $major + $minor;

return new CompiledExpression('bool', 'zephir_is_php_version(' . $version . ')', $expression);
}
}
2 changes: 1 addition & 1 deletion ext/kernel/fcall.h
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ int zephir_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache
} \
}

#if PHP_VERSION_ID < 50600
#ifdef ZEPHIR_RELEASE
#define ZEPHIR_TEMP_PARAM_COPY 0
#define zephir_check_temp_parameter(param) do { if (Z_REFCOUNT_P(param) > 1) zval_copy_ctor(param); else ZVAL_NULL(param); } while(0)
#else
Expand Down
2 changes: 2 additions & 0 deletions ext/kernel/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,4 +539,6 @@ static inline char *_str_erealloc(char *str, size_t new_len, size_t old_len) {

#define ZEPHIR_CHECK_POINTER(v) if (!v) fprintf(stderr, "%s:%d\n", __PRETTY_FUNCTION__, __LINE__);

#define zephir_is_php_version(id) ((PHP_VERSION_ID >= id && PHP_VERSION_ID <= (id + 10000)) ? 1 : 0)

#endif /* ZEPHIR_KERNEL_MAIN_H */
10 changes: 5 additions & 5 deletions ext/kernel/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1919,6 +1919,7 @@ int zephir_create_instance(zval *return_value, const zval *class_name TSRMLS_DC)

ce = zend_fetch_class(Z_STRVAL_P(class_name), Z_STRLEN_P(class_name), ZEND_FETCH_CLASS_DEFAULT TSRMLS_CC);
if (!ce) {
ZVAL_NULL(return_value);
return FAILURE;
}

Expand Down Expand Up @@ -1950,6 +1951,7 @@ int zephir_create_instance_params(zval *return_value, const zval *class_name, zv

ce = zend_fetch_class(Z_STRVAL_P(class_name), Z_STRLEN_P(class_name), ZEND_FETCH_CLASS_DEFAULT TSRMLS_CC);
if (!ce) {
ZVAL_NULL(return_value);
return FAILURE;
}

Expand All @@ -1969,21 +1971,19 @@ int zephir_create_instance_params(zval *return_value, const zval *class_name, zv

if (likely(param_count) <= 10) {
params_ptr = static_params;
}
else {
} else {
params_arr = emalloc(param_count * sizeof(zval*));
params_ptr = &params;
}

for (
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(params), &pos);
zend_hash_get_current_data_ex(Z_ARRVAL_P(params), (void**)&item, &pos) == SUCCESS;
zend_hash_get_current_data_ex(Z_ARRVAL_P(params), (void**) &item, &pos) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_P(params), &pos), ++i
) {
params_ptr[i] = *item;
}
}
else {
} else {
params_ptr = NULL;
}

Expand Down
4 changes: 2 additions & 2 deletions unit-tests/Extension/BuiltIn/CharMethodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testModifications()
{
$t = new CharMethods();

//$this->assertEquals('61', $t->getHex());
//$this->assertEquals('68656C6C6F', $t->getHexForString('hello'));
$this->assertEquals('61', $t->getHex());
$this->assertEquals('68656C6C6F', $t->getHexForString('hello'));
}
}

0 comments on commit 4dfbb7d

Please sign in to comment.