Skip to content

Commit

Permalink
Use attribute for aliasing class name
Browse files Browse the repository at this point in the history
  • Loading branch information
m3m0r7 committed Sep 29, 2023
1 parent bea9505 commit e856eeb
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/VM/Core/Runtime/Entity/Array_.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
use RubyVM\VM\Core\YARV\Essential\Symbol\ArraySymbol;
use RubyVM\VM\Core\YARV\Essential\Symbol\SymbolInterface;
use RubyVM\VM\Exception\RuntimeException;
use RubyVM\VM\Core\Runtime\Attribute\BindAliasAs;

#[BindAliasAs('Array')]
class Array_ extends Entity implements EntityInterface
{
public function __construct(ArraySymbol $symbol)
Expand Down
1 change: 1 addition & 0 deletions src/VM/Core/Runtime/Entity/Boolean_.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use RubyVM\VM\Core\Runtime\Attribute\BindAliasAs;
use RubyVM\VM\Core\YARV\Essential\Symbol\BooleanSymbol;

#[BindAliasAs('Boolean')]
class Boolean_ extends Entity implements EntityInterface
{
public function __construct(BooleanSymbol $symbol)
Expand Down
2 changes: 2 additions & 0 deletions src/VM/Core/Runtime/Entity/Class_.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace RubyVM\VM\Core\Runtime\Entity;

use RubyVM\VM\Core\Runtime\Attribute\BindAliasAs;
use RubyVM\VM\Core\Runtime\Essential\RubyClassInterface;
use RubyVM\VM\Core\Runtime\Executor\Context\ContextInterface;
use RubyVM\VM\Core\YARV\Essential\Symbol\ClassSymbol;
use RubyVM\VM\Core\YARV\Essential\Symbol\StringSymbol;
use RubyVM\VM\Core\YARV\Essential\Symbol\SymbolSymbol;

#[BindAliasAs('Class')]
class Class_ extends Entity implements EntityInterface
{
/**
Expand Down
2 changes: 2 additions & 0 deletions src/VM/Core/Runtime/Entity/Float_.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace RubyVM\VM\Core\Runtime\Entity;

use RubyVM\VM\Core\Runtime\Attribute\BindAliasAs;
use RubyVM\VM\Core\YARV\Essential\Symbol\FloatSymbol;

#[BindAliasAs('Float')]
class Float_ extends Entity implements EntityInterface
{
public function __construct(FloatSymbol $symbol)
Expand Down
2 changes: 2 additions & 0 deletions src/VM/Core/Runtime/Entity/String_.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace RubyVM\VM\Core\Runtime\Entity;

use RubyVM\VM\Core\Runtime\Attribute\BindAliasAs;
use RubyVM\VM\Core\YARV\Essential\Symbol\StringSymbol;

#[BindAliasAs('String')]
class String_ extends Entity implements EntityInterface
{
public function __construct(StringSymbol $symbol)
Expand Down
2 changes: 2 additions & 0 deletions src/VM/Core/Runtime/Entity/Void_.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace RubyVM\VM\Core\Runtime\Entity;

use RubyVM\VM\Core\Runtime\Attribute\BindAliasAs;
use RubyVM\VM\Core\YARV\Essential\Symbol\VoidSymbol;

#[BindAliasAs('Void')]
class Void_ extends Entity implements EntityInterface
{
public function __construct(VoidSymbol $symbol)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use RubyVM\VM\Core\Runtime\Executor\Operation\OperandHelper;
use RubyVM\VM\Core\Runtime\Executor\Operation\Processor\OperationProcessorInterface;
use RubyVM\VM\Core\Runtime\Executor\ProcessedStatus;
use RubyVM\VM\Exception\OperationProcessorException;

class BuiltinJump implements OperationProcessorInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,47 @@

namespace RubyVM\VM\Core\Runtime\Kernel\Ruby3_2\HeapSpace;

use RubyVM\VM\Core\Runtime\Entity\Array_;
use RubyVM\VM\Core\Runtime\Attribute\BindAliasAs;
use RubyVM\VM\Core\Runtime\Entity\EntityInterface;
use RubyVM\VM\Core\Runtime\UserlandHeapSpace;

class DefaultInstanceHeapSpace extends UserlandHeapSpace
{
/**
* @var array<string, class-string<EntityInterface>>
* @var array<string>
*/
protected static array $bindClassNames = [
'Array' => Array_::class,
protected static array $bindClassDirectories = [
__DIR__ . '/../../../BasicObject',
__DIR__ . '/../../../Entity',
];

/**
* @var array{class-string<EntityInterface>[], string, string}[]
*/
protected static array $bindAliasesMethods = [];

public function __construct()
{
parent::__construct();

foreach (static::$bindClassNames as $originalClassName => $bindClassName) {
$this->userlandClasses
->alias($originalClassName, $bindClassName);
foreach (self::$bindClassDirectories as $directory) {
$this->loadClasses($directory);
}

foreach (get_declared_classes() as $className) {
$reflection = new \ReflectionClass($className);
foreach ($reflection->getAttributes(BindAliasAs::class) as $attribute) {
$this->userlandClasses
->alias($attribute->getArguments()[0], $className);
}
}
}

protected function loadClasses(string $directory): void
{
foreach ((glob("{$directory}/*") ?: []) as $file) {
if (is_dir($file)) {
$this->loadClasses($file);

continue;
}

require_once $file;
}
}
}

0 comments on commit e856eeb

Please sign in to comment.