Skip to content

Commit

Permalink
Debugger interface, changed the interpreter constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
tuqqu committed Oct 7, 2023
1 parent 919ab00 commit 086c319
Show file tree
Hide file tree
Showing 14 changed files with 278 additions and 94 deletions.
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

Golang interpreter written in PHP.

# Example
## Example

```php
use GoPhp\Interpreter;

$interpreter = new Interpreter(<<<GO
$interp = Interpreter::create(<<<GO
package main

type person struct {
Expand All @@ -29,11 +29,10 @@ $interpreter = new Interpreter(<<<GO
}
GO);

$interpreter->run();
$result = $interp->run();
```

See [examples](examples/) for more.

To run examples:

```
Expand All @@ -43,11 +42,9 @@ php main.php

## WIP

This is a work-in-progress project.

Already implemented:
This is a toy project, currently work-in-progress.

* see [tests](tests/Functional/files/)
To see what is already implemented, refer to [tests](tests/Functional/files/).

## Development

Expand All @@ -63,4 +60,4 @@ run tests:
make test
```

run `make help` for more commands.
run `make help` for more commands.
4 changes: 2 additions & 2 deletions bin/go-php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ function main(array $argv): never
$stderr = new ResourceOutputStream(STDERR);
$errorHandler = new OutputToStream($stderr);

$runtime = new Interpreter(
$runtime = Interpreter::create(
source: $src,
errorHandler: $errorHandler,
envVars: new EnvVarSet(
Expand All @@ -196,7 +196,7 @@ function main(array $argv): never

$result = $runtime->run();

exit($result->value);
exit($result->exitCode->value);
}

error_reporting(E_ALL);
Expand Down
6 changes: 3 additions & 3 deletions examples/helloworld/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use GoPhp\Stream\StringStreamProvider;

$stdout = '';
$interp = new Interpreter(
$interp = Interpreter::create(
source: <<<'GO'
package main
Expand All @@ -19,7 +19,7 @@
streams: new StringStreamProvider($stdout, $stdout),
);

$exitCode = $interp->run();
$result = $interp->run();

print "Output:\n$stdout\n";
print "Exit code: $exitCode->value\n";
print "Exit code: $result->exitCode->value\n";
7 changes: 3 additions & 4 deletions examples/sorting/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
$goRoot = __DIR__;
$goFile = __DIR__ . '/src/main.go';
$goSrc = file_get_contents($goFile);

$stdout = '';

$interp = new Interpreter(
$interp = Interpreter::create(
source: $goSrc,
streams: new StringStreamProvider($stdout, $stdout),
envVars: new EnvVarSet($goRoot)
);

$exitCode = $interp->run();
$result = $interp->run();

print "Output:\n$stdout\n";
print "Exit code: $exitCode->value\n";
print "Exit code: $result->exitCode->value\n";
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@

<issueHandlers>
<ImplicitToStringCast errorLevel="suppress" />
<TypeDoesNotContainType errorLevel="suppress" />
</issueHandlers>
</psalm>
44 changes: 44 additions & 0 deletions src/CallStackCollectorDebugger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace GoPhp;

use GoPhp\Error\PanicError;

use function count;
use function array_shift;

final class CallStackCollectorDebugger implements Debugger
{
public const DEFAULT_STACK_TRACE_DEPTH = 128;

/** @var list<InvokableCall|PanicError> */
private array $stackTrace = [];

public function __construct(
private readonly bool $enableDebug = true,
private readonly int $maxTraceDepth = self::DEFAULT_STACK_TRACE_DEPTH,
) {}

public function addStackTrace(InvokableCall|PanicError $call): void
{
if (!$this->enableDebug) {
return;
}

$this->stackTrace[] = $call;

if (count($this->stackTrace) > $this->maxTraceDepth) {
array_shift($this->stackTrace);
}
}

/**
* @return list<InvokableCall|PanicError>
*/
public function getStackTrace(): array
{
return $this->stackTrace;
}
}
17 changes: 17 additions & 0 deletions src/Debugger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace GoPhp;

use GoPhp\Error\PanicError;

interface Debugger
{
public function addStackTrace(InvokableCall|PanicError $call): void;

/**
* @return list<InvokableCall|PanicError>
*/
public function getStackTrace(): array;
}
2 changes: 1 addition & 1 deletion src/Error/InterfaceTypeError.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function cannotUseAsInterfaceType(WrappedValue|WrappedType $value,
public static function fromOther(self $error, WrappedType $interfaceType): self
{
if (!isset($error->value, $error->missingMethod)) {
throw InternalError::unreachable('Cannot convert error from other error');
throw InternalError::unreachable('cannot convert error from other error');
}

return self::cannotUseAsType(
Expand Down
4 changes: 3 additions & 1 deletion src/GoValue/Slice/SliceValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ public function append(GoValue $value): void
$this->grow();
}

/** @psalm-suppress PossiblyNullReference */
$this->values[$this->len++] = $value;
}

Expand Down Expand Up @@ -268,6 +267,9 @@ private function exceedsCapacity(): bool
return $this->len - $this->pos + 1 > $this->cap;
}

/**
* @psalm-assert !null $this->values
*/
private function grow(): void
{
$copies = [];
Expand Down
Loading

0 comments on commit 086c319

Please sign in to comment.