Skip to content

Commit

Permalink
Merge pull request #41 from m3m0r7/refactor-3
Browse files Browse the repository at this point in the history
Refactor 3
  • Loading branch information
m3m0r7 authored Oct 5, 2023
2 parents bb6785b + 94f9578 commit 9e32d71
Show file tree
Hide file tree
Showing 25 changed files with 166 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ public function divide(RubyClassInterface $object): Float_
$this->valueOf() / $object->valueOf(),
);
}

public function inspect(): RubyClassInterface
{
return String_::createBy((string) $this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,9 @@ public static function createBy(mixed $value = 0): self
{
return new self(new NumberSymbol($value));
}

public function inspect(): RubyClassInterface
{
return String_::createBy((string) $this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,12 @@ public function isIncluding(String_ $string): TrueClass|FalseClass
? TrueClass::createBy()
: FalseClass::createBy();
}

public function inspect(): RubyClassInterface
{
return String_::createBy(sprintf(
'"%s"',
(string) $this,
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public static function createBy(mixed $value = ''): self
{
return new self(new SymbolSymbol($value));
}

public function inspect(): RubyClassInterface
{
return String_::createBy((string) $this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use RubyVM\VM\Core\Helper\ClassHelper;
use RubyVM\VM\Core\Runtime\Attribute\BindAliasAs;
use RubyVM\VM\Core\Runtime\Attribute\WithContext;
use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Comparable\String_;
use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\NilClass;
use RubyVM\VM\Core\Runtime\BasicObject\Symbolizable;
use RubyVM\VM\Core\Runtime\BasicObject\SymbolizeInterface;
Expand Down Expand Up @@ -156,4 +157,9 @@ public function plus(RubyClassInterface $object): Array_
[...$this->valueOf(), ...$object->valueOf()],
);
}

public function inspect(): RubyClassInterface
{
return String_::createBy((string) $this);
}
}
23 changes: 21 additions & 2 deletions src/VM/Core/Runtime/BasicObject/Kernel/Object_/Enumerable/Hash.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Enumerable;

use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Comparable\String_;
use RubyVM\VM\Core\Runtime\Essential\RubyClassInterface;
use RubyVM\VM\Core\YARV\Essential\Symbol\SymbolInterface;

class Hash extends Enumerable implements RubyClassInterface
{
/**
* @param array<SymbolInterface> $hash
* @param array<RubyClassInterface> $hash
*/
public function __construct(protected array $hash = []) {}

Expand Down Expand Up @@ -53,4 +53,23 @@ public function count(): int
{
return count($this->hash);
}

public function inspect(): RubyClassInterface
{
return String_::createBy(sprintf(
'{%s}',
implode(
', ',
array_map(
static fn (string $key, RubyClassInterface $value) => sprintf(
':%s=>%s',
$key,
$value->inspect(),
),
array_keys($this->hash),
array_values($this->hash),
)
),
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use RubyVM\VM\Core\Runtime\Attribute\BindAliasAs;
use RubyVM\VM\Core\Runtime\Attribute\WithContext;
use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Comparable\Integer_;
use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Comparable\String_;
use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\FalseClass;
use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\NilClass;
use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\TrueClass;
Expand Down Expand Up @@ -131,4 +132,9 @@ public function compareStrictEquals(RubyClassInterface $object): TrueClass|False
? TrueClass::createBy()
: FalseClass::createBy();
}

public function inspect(): RubyClassInterface
{
return String_::createBy((string) $this);
}
}
5 changes: 5 additions & 0 deletions src/VM/Core/Runtime/BasicObject/Kernel/Object_/FalseClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public function __toString(): string
{
return (string) $this->symbol;
}

public function inspect(): RubyClassInterface
{
return String_::createBy((string) $this);
}
}
6 changes: 6 additions & 0 deletions src/VM/Core/Runtime/BasicObject/Kernel/Object_/NilClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_;

use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Comparable\String_;
use RubyVM\VM\Core\Runtime\BasicObject\Symbolizable;
use RubyVM\VM\Core\Runtime\Essential\RubyClassInterface;
use RubyVM\VM\Core\YARV\Essential\Symbol\NilSymbol;
Expand All @@ -29,4 +30,9 @@ public static function createBy(mixed $value = null): self

return $cache ??= new self(new NilSymbol());
}

public function inspect(): RubyClassInterface
{
return String_::createBy((string) $this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use RubyVM\VM\Core\Runtime\Option;
use RubyVM\VM\Core\YARV\Criterion\InstructionSequence\InstructionSequenceInterface;

class Lambda extends Object_ implements RubyClassInterface
class Proc extends Object_ implements RubyClassInterface
{
public function __construct(private readonly InstructionSequenceInterface $instructionSequence) {}

Expand Down
5 changes: 5 additions & 0 deletions src/VM/Core/Runtime/BasicObject/Kernel/Object_/TrueClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,9 @@ public function __toString(): string
{
return (string) $this->symbol;
}

public function inspect(): RubyClassInterface
{
return String_::createBy((string) $this);
}
}
13 changes: 6 additions & 7 deletions src/VM/Core/Runtime/Executor/CallBlockHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,17 @@ private function callSimpleMethod(ContextInterface $context, CallInfoInterface $
parentContext: $calleeContext ?? $context,
));

$environmentTable = $calleeContext?->environmentTable() ?? $context->environmentTable();

$executor->context()
->renewEnvironmentTable();

if ($environmentTable->has(Option::VM_ENV_DATA_SIZE - 1)) {
if ($calleeContext?->environmentTable()?->has(Option::VM_ENV_DATA_INDEX_SPECVAL)) {
$executor->context()
->environmentTable()
->set(
Option::VM_ENV_DATA_SIZE - 1,
$environmentTable
->get(Option::VM_ENV_DATA_SIZE - 1),
Option::VM_ENV_DATA_INDEX_SPECVAL,
$calleeContext
->environmentTable()
->get(Option::VM_ENV_DATA_INDEX_SPECVAL),
);
}

Expand Down Expand Up @@ -187,7 +186,7 @@ private function callBlockWithArguments(CallInfoInterface $callInfo, Integer_ $b
$executor->context()
->environmentTable()
->set(
Option::VM_ENV_DATA_SIZE - 1,
Option::VM_ENV_DATA_INDEX_SPECVAL,
$executor->context(),
);

Expand Down
2 changes: 1 addition & 1 deletion src/VM/Core/Runtime/Executor/Insn/Insn.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public function operandSize(): int
Insn::SEND => 2,
Insn::DEFINECLASS => 3,
Insn::SETLOCAL, Insn::GETLOCAL => 2,
Insn::OPT_CASE_DISPATCH, Insn::GETBLOCKPARAMPROXY => 2,
Insn::OPT_CASE_DISPATCH, Insn::GETBLOCKPARAMPROXY, Insn::GETBLOCKPARAM => 2,
default => 1,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@

namespace RubyVM\VM\Core\Runtime\Executor\Insn\Processor;

use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Proc;
use RubyVM\VM\Core\Runtime\Executor\Context\ContextInterface;
use RubyVM\VM\Core\Runtime\Executor\Insn\Insn;
use RubyVM\VM\Core\Runtime\Executor\LocalTable;
use RubyVM\VM\Core\Runtime\Executor\Operation\Operand;
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;
use RubyVM\VM\Core\Runtime\Option;

class BuiltinGetblockparam implements OperationProcessorInterface
{
use LocalTable;
use OperandHelper;

private Insn $insn;

private ContextInterface $context;
Expand All @@ -30,6 +35,17 @@ public function after(): void {}

public function process(): ProcessedStatus
{
throw new OperationProcessorException(sprintf('The `%s` (opcode: 0x%02x) processor is not implemented yet', strtolower($this->insn->name), $this->insn->value));
$this->operandAsNumber();
$level = $this->operandAsNumber();

$context = $this->localTable(Option::VM_ENV_DATA_INDEX_SPECVAL, $level->valueOf());

assert($context instanceof ContextInterface);

$this->context->vmStack()->push(new Operand(
new Proc($context->instructionSequence()),
));

return ProcessedStatus::SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace RubyVM\VM\Core\Runtime\Executor\Insn\Processor;

use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Lambda;
use RubyVM\VM\Core\Runtime\BasicObject\Kernel\Object_\Proc;
use RubyVM\VM\Core\Runtime\Executor\Context\ContextInterface;
use RubyVM\VM\Core\Runtime\Executor\Insn\Insn;
use RubyVM\VM\Core\Runtime\Executor\LocalTable;
Expand Down Expand Up @@ -35,11 +35,11 @@ public function after(): void {}

public function process(): ProcessedStatus
{
$slotIndex = $this->operandAsNumber()->valueOf();
$this->operandAsNumber()->valueOf();
$level = $this->operandAsNumber()->valueOf();

$context = $this->getLocalTableToStack(
Option::VM_ENV_DATA_SIZE - 1,
$context = $this->localTable(
Option::VM_ENV_DATA_INDEX_SPECVAL,
$level,
);

Expand All @@ -53,7 +53,7 @@ public function process(): ProcessedStatus
assert($context instanceof ContextInterface);

$this->context->vmStack()->push(new Operand(
new Lambda($context->instructionSequence()),
new Proc($context->instructionSequence()),
));

return ProcessedStatus::SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function process(): ProcessedStatus
$slotIndex = $this->operandAsNumber()->valueOf();
$level = $this->operandAsNumber()->valueOf();

$this->context->vmStack()->push(new Operand($this->getLocalTableToStack($slotIndex, $level)));
$this->context->vmStack()->push(new Operand($this->localTable($slotIndex, $level)));

return ProcessedStatus::SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,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\Core\Runtime\Option;

class BuiltinGetlocalWC0 implements OperationProcessorInterface
{
Expand All @@ -36,7 +35,7 @@ public function process(): ProcessedStatus
{
$slotIndex = $this->operandAsNumber()->valueOf();

$this->context->vmStack()->push(new Operand($this->getLocalTableToStack($slotIndex, Option::RSV_TABLE_INDEX_0)));
$this->context->vmStack()->push(new Operand($this->localTable($slotIndex)));

return ProcessedStatus::SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,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\Core\Runtime\Option;

class BuiltinGetlocalWC1 implements OperationProcessorInterface
{
Expand All @@ -35,7 +34,7 @@ public function after(): void {}
public function process(): ProcessedStatus
{
$slotIndex = $this->operandAsNumber()->valueOf();
$this->context->vmStack()->push(new Operand($this->getLocalTableToStack($slotIndex, Option::RSV_TABLE_INDEX_1)));
$this->context->vmStack()->push(new Operand($this->localTable($slotIndex, 1)));

return ProcessedStatus::SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ public function process(): ProcessedStatus
{
// This is an operation processor context including instruction sequence context
$processorContext = $this
->getLocalTableToStack(
Option::VM_ENV_DATA_SIZE - 1,
0,
->localTable(
Option::VM_ENV_DATA_INDEX_SPECVAL,
);

assert($processorContext instanceof ContextInterface);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use RubyVM\VM\Core\Runtime\Executor\Operation\Processor\OperationProcessorInterface;
use RubyVM\VM\Core\Runtime\Executor\ProcessedStatus;
use RubyVM\VM\Core\Runtime\Executor\Validatable;
use RubyVM\VM\Core\Runtime\Option;

class BuiltinSetlocalWC0 implements OperationProcessorInterface
{
Expand All @@ -36,7 +35,7 @@ public function after(): void {}
public function process(): ProcessedStatus
{
$slotIndex = $this->operandAsNumber()->valueOf();
$this->setLocalTableFromStack($slotIndex, Option::RSV_TABLE_INDEX_0);
$this->setLocalTableFromStack($slotIndex);

return ProcessedStatus::SUCCESS;
}
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\Core\Runtime\Option;

class BuiltinSetlocalWC1 implements OperationProcessorInterface
{
Expand All @@ -34,7 +33,7 @@ public function after(): void {}
public function process(): ProcessedStatus
{
$slotIndex = $this->operandAsNumber()->valueOf();
$this->setLocalTableFromStack($slotIndex, Option::RSV_TABLE_INDEX_1);
$this->setLocalTableFromStack($slotIndex, 1);

return ProcessedStatus::SUCCESS;
}
Expand Down
6 changes: 3 additions & 3 deletions src/VM/Core/Runtime/Executor/LocalTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ trait LocalTable
use Validatable;
use OperandHelper;

public function hasLocalTable(int $slotIndex, int $level): bool
public function hasLocalTable(int $slotIndex, int $level = 0): bool
{
return $this
->targetContextByLevel($level)
Expand All @@ -31,7 +31,7 @@ public function hasLocalTable(int $slotIndex, int $level): bool
);
}

public function getLocalTableToStack(int $slotIndex, int $level): ContextInterface|RubyClassInterface
public function localTable(int $slotIndex, int $level = 0): ContextInterface|RubyClassInterface
{
return $this
->targetContextByLevel($level)
Expand All @@ -49,7 +49,7 @@ public function getLocalTableToStack(int $slotIndex, int $level): ContextInterfa
);
}

public function setLocalTableFromStack(int $slotIndex, int $level, bool $forcibly = false): void
public function setLocalTableFromStack(int $slotIndex, int $level = 0, bool $forcibly = false): void
{
$operand = $this->stackAsObject();

Expand Down
Loading

0 comments on commit 9e32d71

Please sign in to comment.