Skip to content

Commit

Permalink
Merge branch 'release/3.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Oct 20, 2014
2 parents 7d975e5 + a6bb307 commit e137a63
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Isolator Changelog

### 3.0.1 (2014-10-21)

* **[FIXED]** References are now preserved when calling functions with variable arguments

### 3.0.0 (2014-10-08)

* **[BC]** Removed `Isolator::getIsolator()` and `resetIsolator()`
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ PHP 5.6.

* Follow [@IcecaveStudios](https://twitter.com/IcecaveStudios) on Twitter
* Visit the [Icecave Studios website](http://icecave.com.au)
* Join `#icecave` on [irc.freenode.net](http://webchat.freenode.net?channels=icecave)

<!-- references -->
[Build Status]: http://img.shields.io/travis/IcecaveStudios/isolator/develop.svg?style=flat-square
[Test Coverage]: http://img.shields.io/coveralls/IcecaveStudios/isolator/develop.svg?style=flat-square
[SemVer]: http://img.shields.io/:semver-3.0.0-brightgreen.svg?style=flat-square
[SemVer]: http://img.shields.io/:semver-3.0.1-brightgreen.svg?style=flat-square
3 changes: 3 additions & 0 deletions benchmark/benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

$isolator = Icecave\Isolator\Isolator::get();

$reflector = new ReflectionClass($isolator);
echo $reflector->getFilename() . PHP_EOL;

class TestClass
{
public function __construct($param = null)
Expand Down
33 changes: 32 additions & 1 deletion src/Detail/CodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ public function generate($className, array $functions)
$code .= 'class ' . $shortName . ' extends AbstractIsolator' . PHP_EOL;
$code .= '{' . PHP_EOL;

$newLine = false;
foreach ($functions as $function) {
if ($newLine) {
$code .= PHP_EOL;
} else {
$newLine = true;
}

$code .= $this->generateMethod(
new ReflectionFunction($function)
);
Expand Down Expand Up @@ -70,12 +77,20 @@ private function generateMethod(ReflectionFunction $reflector)
$code = ' ' . $signature . PHP_EOL;
$code .= ' {' . PHP_EOL;
$code .= $this->generateSwitch($name, $minArity, $maxArity);
$code .= ' return \call_user_func_array(' . var_export($reflector->getName(), true) . ', \func_get_args());' . PHP_EOL;
$code .= PHP_EOL;
$code .= $this->generateFallbackReturn($name, $refIndices);
$code .= ' }' . PHP_EOL;

return $code;
}

/**
* @param string $name The function name.
* @param boolean $returnsReference True if the function returns a reference.
* @param integer $minArity The minimum number of arguments.
* @param integer $maxArity The maximum number of arguments present in the signature.
* @param array<integer> $refIndices An array containing the indices of parameters that are references.
*/
private function generateSignature(
$name,
$returnsReference,
Expand Down Expand Up @@ -138,6 +153,22 @@ private function generateReturn($name, $arity)
);
}

public function generateFallbackReturn($name, $refIndices)
{
$code = ' $arguments = \func_get_args();' . PHP_EOL;

foreach ($refIndices as $index => $isReference) {
if ($isReference) {
$code .= ' $arguments[' . $index . '] = &$p' . $index . ';' . PHP_EOL;
}
}

$code .= PHP_EOL;
$code .= ' return \call_user_func_array(' . var_export($name, true) . ', $arguments);' . PHP_EOL;

return $code;
}

private function inspectParameters(ReflectionFunction $reflector)
{
$minArity = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/PackageInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
class PackageInfo
{
const NAME = 'Isolator';
const VERSION = '3.0.0';
const VERSION = '3.0.1';
}
16 changes: 13 additions & 3 deletions test/suite/Detail/CodeGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ public function testGenerateMethod()
$expectedCode .= ' switch (\func_num_args()) {' . PHP_EOL;
$expectedCode .= ' case 1: return \strlen($p0);' . PHP_EOL;
$expectedCode .= ' }' . PHP_EOL;
$expectedCode .= ' return \call_user_func_array(\'strlen\', \func_get_args());' . PHP_EOL;
$expectedCode .= PHP_EOL;
$expectedCode .= ' $arguments = \func_get_args();' . PHP_EOL;
$expectedCode .= PHP_EOL;
$expectedCode .= ' return \call_user_func_array(\'strlen\', $arguments);' . PHP_EOL;
$expectedCode .= ' }' . PHP_EOL;
$expectedCode .= '}' . PHP_EOL;

Expand Down Expand Up @@ -100,7 +103,11 @@ public function testGenerateMethodWithReferenceParameter()
$expectedCode .= ' case 2: return \ereg($p0, $p1);' . PHP_EOL;
$expectedCode .= ' case 3: return \ereg($p0, $p1, $p2);' . PHP_EOL;
$expectedCode .= ' }' . PHP_EOL;
$expectedCode .= ' return \call_user_func_array(\'ereg\', \func_get_args());' . PHP_EOL;
$expectedCode .= PHP_EOL;
$expectedCode .= ' $arguments = \func_get_args();' . PHP_EOL;
$expectedCode .= ' $arguments[2] = &$p2;' . PHP_EOL;
$expectedCode .= PHP_EOL;
$expectedCode .= ' return \call_user_func_array(\'ereg\', $arguments);' . PHP_EOL;
$expectedCode .= ' }' . PHP_EOL;
$expectedCode .= '}' . PHP_EOL;

Expand Down Expand Up @@ -128,7 +135,10 @@ public function testGenerateMethodWithVarArgs()
$expectedCode .= ' switch (\func_num_args()) {' . PHP_EOL;
$expectedCode .= ' case 2: return \sprintf($p0, $p1);' . PHP_EOL;
$expectedCode .= ' }' . PHP_EOL;
$expectedCode .= ' return \call_user_func_array(\'sprintf\', \func_get_args());' . PHP_EOL;
$expectedCode .= PHP_EOL;
$expectedCode .= ' $arguments = \func_get_args();' . PHP_EOL;
$expectedCode .= PHP_EOL;
$expectedCode .= ' return \call_user_func_array(\'sprintf\', $arguments);' . PHP_EOL;
$expectedCode .= ' }' . PHP_EOL;
$expectedCode .= '}' . PHP_EOL;

Expand Down
16 changes: 16 additions & 0 deletions test/suite/IsolatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,20 @@ public function testGet()
Isolator::get()
);
}

/**
* @group regression
* @link https://github.com/IcecaveStudios/isolator/issues/17
*/
public function testCallVarArgsWithReferences()
{
$array = array();

$this->isolator->array_push($array, 1, 2);

$this->assertSame(
array(1, 2),
$array
);
}
}

0 comments on commit e137a63

Please sign in to comment.