Skip to content

Commit

Permalink
Support passing emitter-augmenting class names to CompilingClassLoade…
Browse files Browse the repository at this point in the history
…r::instanceFor()
  • Loading branch information
thekid committed Jan 30, 2022
1 parent 0e26a1f commit 056e79d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
6 changes: 6 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ XP Compiler ChangeLog

## ?.?.? / ????-??-??

## 8.2.0 / 2022-01-30

* Support passing emitter-augmenting class names to the instanceFor()
method of `lang.ast.CompilingClassLoader`.
(@thekid)

## 8.1.0 / 2022-01-29

* Merged PR #131: Inline nullable checks when casting - @thekid
Expand Down
11 changes: 6 additions & 5 deletions src/main/php/lang/ast/CompilingClassloader.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static function __static() {

/** Creates a new instances with a given PHP runtime */
private function __construct($emit) {
$this->version= str_replace('', '+', $emit->getSimpleName());
$this->version= strtr($emit->getSimpleName(), ['' => '+', '·' => '.']);

Compiled::$emit[$this->version]= $emit->newInstance();
stream_wrapper_register($this->version, Compiled::class);
Expand Down Expand Up @@ -242,13 +242,14 @@ public function instanceId() {
}

/**
* Fetch instance of classloader by path
* Fetch instance of classloader by version
*
* @param string path the identifier
* @return lang.IClassLoader
* @param string $version
* @return lang.IClassLoader
*/
public static function instanceFor($version) {
$emit= Emitter::forRuntime($version, [XpMeta::class]);
sscanf($version, "%[^+]+%[^\r]", $emitter, $augmented);
$emit= Emitter::forRuntime($emitter, $augmented ? explode('+', $augmented) : [XpMeta::class]);

$id= $emit->getName();
if (!isset(self::$instance[$id])) {
Expand Down
5 changes: 2 additions & 3 deletions src/main/php/lang/ast/Emitter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ public static function forRuntime($runtime, $emitters= []) {
$extended= ['kind' => 'class', 'extends' => [$p->loadClass($impl)], 'implements' => [], 'use' => []];
foreach ($emitters as $class) {
if ($class instanceof XPClass) {
$impl.= ''.$class->getSimpleName();
$impl.= ''.strtr($class->getName(), ['.' => '·']);
$extended['use'][]= $class;
} else {
$d= strrpos(strtr($class, '\\', '.'), '.');
$impl.= ''.(false === $d ? $class : substr($class, $d + 1));
$impl.= ''.strtr($class, ['.' => '·', '\\' => '·']);
$extended['use'][]= XPClass::forName($class);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public function supports_php($version) {

#[Test]
public function string_representation() {
Assert::equals('CompilingCL<PHP70+XpMeta>', CompilingClassLoader::instanceFor('php:7.0.0')->toString());
Assert::equals('CompilingCL<PHP70+lang.ast.emit.php.XpMeta>', CompilingClassLoader::instanceFor('php:7.0.0')->toString());
}

#[Test]
public function hashcode() {
Assert::equals('CPHP70+XpMeta', CompilingClassLoader::instanceFor('php:7.0.0')->hashCode());
Assert::equals('CPHP70+lang.ast.emit.php.XpMeta', CompilingClassLoader::instanceFor('php:7.0.0')->hashCode());
}

#[Test]
Expand All @@ -77,6 +77,14 @@ public function compare() {
Assert::equals(1, $cl->compareTo(null), 'does not equal null');
}

#[Test]
public function instanced_for_augmented() {
Assert::equals(
'PHP80+lang.ast.emit.php.XpMeta',
CompilingClassLoader::instanceFor('php:8.0.0+lang.ast.emit.php.XpMeta')->instanceId()
);
}

#[Test]
public function package_contents() {
$contents= $this->compile(['Tests' => '<?php namespace %s; class Tests { }'], function($loader, $types) {
Expand Down

0 comments on commit 056e79d

Please sign in to comment.