Skip to content

Commit

Permalink
Until the before, using String as a Symbol; but replace to a Symbol i…
Browse files Browse the repository at this point in the history
…s a Symbol
  • Loading branch information
m3m0r7 committed Sep 27, 2023
1 parent db001c0 commit 5d7c1db
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/VM/Core/Runtime/Entity/Class_.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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;

class Class_ extends Entity implements EntityInterface
{
Expand All @@ -26,7 +27,7 @@ public static function createBy(mixed $value = null): self
return new self(new ClassSymbol($value));
}

public static function of(StringSymbol $symbol, ContextInterface $context): RubyClassInterface
public static function of(StringSymbol|SymbolSymbol $symbol, ContextInterface $context): RubyClassInterface
{
return static::$classes[$context->modulePath((string) $symbol)] ??= (new self(new ClassSymbol($symbol)))
->toBeRubyClass();
Expand Down
2 changes: 2 additions & 0 deletions src/VM/Core/Runtime/Entity/EntityHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use RubyVM\VM\Core\YARV\Essential\Symbol\RangeSymbol;
use RubyVM\VM\Core\YARV\Essential\Symbol\StringSymbol;
use RubyVM\VM\Core\YARV\Essential\Symbol\SymbolInterface;
use RubyVM\VM\Core\YARV\Essential\Symbol\SymbolSymbol;
use RubyVM\VM\Core\YARV\Essential\Symbol\UndefinedSymbol;
use RubyVM\VM\Core\YARV\Essential\Symbol\VoidSymbol;
use RubyVM\VM\Exception\EntityException;
Expand All @@ -35,6 +36,7 @@ public static function createEntityBySymbol(SymbolInterface $symbol): EntityInte
StringSymbol::class => new String_($symbol),
UndefinedSymbol::class => new Undefined($symbol),
VoidSymbol::class => new Void_($symbol),
SymbolSymbol::class => new Symbol($symbol),
default => throw new EntityException(
sprintf(
'The specified entity was not implemented yet: %s',
Expand Down
25 changes: 25 additions & 0 deletions src/VM/Core/Runtime/Entity/Symbol.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace RubyVM\VM\Core\Runtime\Entity;

use RubyVM\VM\Core\YARV\Essential\Symbol\SymbolSymbol;

class Symbol extends Entity implements EntityInterface
{
public function __construct(SymbolSymbol $symbol)
{
$this->symbol = $symbol;
}

public function testValue(): bool
{
return (bool) $this->symbol->valueOf();
}

public static function createBy(mixed $value = ''): self
{
return new self(new SymbolSymbol($value));
}
}
5 changes: 3 additions & 2 deletions src/VM/Core/Runtime/Essential/RubyClassInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use RubyVM\VM\Core\YARV\Criterion\UserlandHeapSpaceInterface;
use RubyVM\VM\Core\YARV\Essential\Symbol\NumberSymbol;
use RubyVM\VM\Core\YARV\Essential\Symbol\StringSymbol;
use RubyVM\VM\Core\YARV\Essential\Symbol\SymbolSymbol;

interface RubyClassInterface extends RubyClassImplementationInterface, \Stringable
{
Expand All @@ -34,7 +35,7 @@ public function methods(): array;

public function hasMethod(string $name): bool;

public function class(NumberSymbol $flags, StringSymbol $className): void;
public function class(NumberSymbol $flags, StringSymbol|SymbolSymbol $className): void;

public function def(StringSymbol $methodName, ContextInterface $context): void;
public function def(StringSymbol|SymbolSymbol $methodName, ContextInterface $context): void;
}
3 changes: 2 additions & 1 deletion src/VM/Core/Runtime/Executor/OperatorCalculatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use RubyVM\VM\Core\Runtime\Executor\Operation\OperandHelper;
use RubyVM\VM\Core\YARV\Essential\Symbol\StringSymbol;
use RubyVM\VM\Core\YARV\Essential\Symbol\SymbolInterface;
use RubyVM\VM\Core\YARV\Essential\Symbol\SymbolSymbol;
use RubyVM\VM\Exception\OperationProcessorException;

trait OperatorCalculatable
Expand Down Expand Up @@ -44,7 +45,7 @@ private function processArithmetic(string $expectedOperator): ProcessedStatus
$rightOperand = $recv->operand->entity()->symbol();

$value = null;
if ($operator instanceof StringSymbol) {
if ($operator instanceof StringSymbol || $operator instanceof SymbolSymbol) {
if ((string) $operator !== $expectedOperator) {
throw new OperationProcessorException(sprintf('The `%s` (opcode: 0x%02x) processor cannot process %s operator because string concatenating was allowed only `%s`', strtolower((string) $this->insn->name), $this->insn->value, $operator, $expectedOperator));
}
Expand Down
22 changes: 19 additions & 3 deletions src/VM/Core/Runtime/Kernel/Ruby3_2/Loader/SymbolSymbolLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
namespace RubyVM\VM\Core\Runtime\Kernel\Ruby3_2\Loader;

use RubyVM\VM\Core\Runtime\Essential\KernelInterface;
use RubyVM\VM\Core\Runtime\Option;
use RubyVM\VM\Core\YARV\Criterion\Offset\Offset;
use RubyVM\VM\Core\YARV\Essential\Encoding;
use RubyVM\VM\Core\YARV\Essential\Symbol\SymbolInterface;
use RubyVM\VM\Core\YARV\Essential\Symbol\SymbolLoaderInterface;
use RubyVM\VM\Core\YARV\Essential\Symbol\SymbolSymbol;
use RubyVM\VM\Exception\RubyVMException;

class SymbolSymbolLoader implements SymbolLoaderInterface
{
Expand All @@ -18,8 +22,20 @@ public function __construct(

public function load(): SymbolInterface
{
// NOTE: The SymbolSymbolLoader is same at StringSymbolLoader
return (new StringSymbolLoader($this->kernel, $this->offset))
->load();
$reader = $this->kernel->stream()->duplication();
$reader->pos($this->offset->offset);

$encIndex = $reader->smallValue();
$len = $reader->smallValue();

// see: https://github.com/ruby/ruby/blob/2f603bc4/compile.c#L12567
if ($encIndex > Option::RUBY_ENCINDEX_BUILTIN_MAX) {
throw new RubyVMException('Not implemented yet in encIndex > RUBY_ENCINDEX_BUILTIN_MAX comparison');
}

return new SymbolSymbol(
string: $reader->read($len),
encoding: Encoding::of($encIndex),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use RubyVM\VM\Core\YARV\Criterion\UserlandHeapSpaceInterface;
use RubyVM\VM\Core\YARV\Essential\Symbol\NumberSymbol;
use RubyVM\VM\Core\YARV\Essential\Symbol\StringSymbol;
use RubyVM\VM\Core\YARV\Essential\Symbol\SymbolSymbol;
use RubyVM\VM\Exception\RuntimeException;

trait ProvideClassExtendableMethods
Expand Down Expand Up @@ -63,7 +64,7 @@ public function hasMethod(string $name): bool
return in_array($name, $this->methods(), true);
}

public function class(NumberSymbol $flags, StringSymbol $className): void
public function class(NumberSymbol $flags, StringSymbol|SymbolSymbol $className): void
{
$className = (string) $className;

Expand All @@ -77,7 +78,7 @@ public function class(NumberSymbol $flags, StringSymbol $className): void
);
}

public function def(StringSymbol $methodName, ContextInterface $context): void
public function def(StringSymbol|SymbolSymbol $methodName, ContextInterface $context): void
{
$context->self()
->userlandHeapSpace()
Expand Down
2 changes: 1 addition & 1 deletion src/VM/Core/YARV/Essential/Symbol/ClassSymbol.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class ClassSymbol implements SymbolInterface, \Stringable
{
public function __construct(
private readonly StringSymbol $class,
private readonly StringSymbol|SymbolSymbol $class,
) {}

public function valueOf(): string
Expand Down
6 changes: 5 additions & 1 deletion src/VM/Core/YARV/Essential/Symbol/StringSymbol.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class StringSymbol implements SymbolInterface, \Stringable
{
public function __construct(
private readonly string $string,
// @phpstan-ignore-next-line
private readonly Encoding $encoding = Encoding::RUBY_ENCINDEX_UTF_8,
) {}

Expand All @@ -23,4 +22,9 @@ public function __toString(): string
{
return $this->string;
}

public function encoding(): Encoding
{
return $this->encoding;
}
}
30 changes: 30 additions & 0 deletions src/VM/Core/YARV/Essential/Symbol/SymbolSymbol.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace RubyVM\VM\Core\YARV\Essential\Symbol;

use RubyVM\VM\Core\YARV\Essential\Encoding;

class SymbolSymbol implements SymbolInterface, \Stringable
{
public function __construct(
private readonly string $string,
private readonly Encoding $encoding = Encoding::RUBY_ENCINDEX_UTF_8,
) {}

public function valueOf(): string
{
return $this->string;
}

public function __toString(): string
{
return $this->string;
}

public function encoding(): Encoding
{
return $this->encoding;
}
}
2 changes: 1 addition & 1 deletion src/VM/Core/YARV/Essential/Symbol/SymbolType.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static function findBySymbol(SymbolInterface $symbol): self
NilSymbol::class => self::NIL,
ClassSymbol::class => self::CLASS_,
OffsetSymbol::class,
VoidSymbol::class => self::SYMBOL,
VoidSymbol::class, SymbolSymbol::class => self::SYMBOL,
NumberSymbol::class => self::FIXNUM,
StringSymbol::class => self::STRING,
default => self::NONE,
Expand Down

0 comments on commit 5d7c1db

Please sign in to comment.