Skip to content

Commit

Permalink
Merge branch 'master' of github.com:xp-framework/compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Jun 16, 2018
2 parents c06d3cf + a666ec0 commit 76e1836
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
6 changes: 5 additions & 1 deletion src/main/php/lang/ast/Emitter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,11 @@ protected function emitSwitch($switch) {
}

protected function emitCatch($catch) {
$this->out->write('catch('.implode('|', $catch->types).' $'.$catch->variable.') {');
if (empty($catch->types)) {
$this->out->write('catch(\\Throwable $'.$catch->variable.') {');
} else {
$this->out->write('catch('.implode('|', $catch->types).' $'.$catch->variable.') {');
}
$this->emit($catch->body);
$this->out->write('}');
}
Expand Down
14 changes: 9 additions & 5 deletions src/main/php/lang/ast/emit/HHVM320.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ protected function emitParameter($parameter) {
}

protected function emitCatch($catch) {
$last= array_pop($catch->types);
$label= sprintf('c%u', crc32($last));
foreach ($catch->types as $type) {
$this->out->write('catch('.$type.' $'.$catch->variable.') { goto '.$label.'; }');
if (empty($catch->types)) {
$this->out->write('catch(\\Throwable $'.$catch->variable.') {');
} else {
$last= array_pop($catch->types);
$label= sprintf('c%u', crc32($last));
foreach ($catch->types as $type) {
$this->out->write('catch('.$type.' $'.$catch->variable.') { goto '.$label.'; }');
}
$this->out->write('catch('.$last.' $'.$catch->variable.') { '.$label.':');
}

$this->out->write('catch('.$last.' $'.$catch->variable.') { '.$label.':');
$this->emit($catch->body);
$this->out->write('}');
}
Expand Down
14 changes: 9 additions & 5 deletions src/main/php/lang/ast/emit/PHP56.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,17 @@ function($matches) { return html_entity_decode('&#'.hexdec($matches[1]).';', ENT
}

protected function emitCatch($catch) {
$last= array_pop($catch->types);
$label= sprintf('c%u', crc32($last));
foreach ($catch->types as $type) {
$this->out->write('catch('.$type.' $'.$catch->variable.') { goto '.$label.'; }');
if (empty($catch->types)) {
$this->out->write('catch(\\Exception $'.$catch->variable.') {');
} else {
$last= array_pop($catch->types);
$label= sprintf('c%u', crc32($last));
foreach ($catch->types as $type) {
$this->out->write('catch('.$type.' $'.$catch->variable.') { goto '.$label.'; }');
}
$this->out->write('catch('.$last.' $'.$catch->variable.') { '.$label.':');
}

$this->out->write('catch('.$last.' $'.$catch->variable.') { '.$label.':');
$this->emit($catch->body);
$this->out->write('}');
}
Expand Down
14 changes: 9 additions & 5 deletions src/main/php/lang/ast/emit/PHP70.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ class PHP70 extends \lang\ast\Emitter {
];

protected function emitCatch($catch) {
$last= array_pop($catch->types);
$label= sprintf('c%u', crc32($last));
foreach ($catch->types as $type) {
$this->out->write('catch('.$type.' $'.$catch->variable.') { goto '.$label.'; }');
if (empty($catch->types)) {
$this->out->write('catch(\\Throwable $'.$catch->variable.') {');
} else {
$last= array_pop($catch->types);
$label= sprintf('c%u', crc32($last));
foreach ($catch->types as $type) {
$this->out->write('catch('.$type.' $'.$catch->variable.') { goto '.$label.'; }');
}
$this->out->write('catch('.$last.' $'.$catch->variable.') { '.$label.':');
}

$this->out->write('catch('.$last.' $'.$catch->variable.') { '.$label.':');
$this->emit($catch->body);
$this->out->write('}');
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/php/lang/ast/unittest/emit/ExceptionsTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,19 @@ public function run() {

$this->assertEquals(4, $t->newInstance()->run());
}

#[@test]
public function catch_without_type() {
$t= $this->type('class <T> {
public function run() {
try {
throw new \\lang\\IllegalArgumentException("test");
} catch ($e) {
return get_class($e);
}
}
}');

$this->assertEquals(IllegalArgumentException::class, $t->newInstance()->run());
}
}

0 comments on commit 76e1836

Please sign in to comment.