-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Objects support #8
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace PHPPHP\Engine; | ||
|
||
use PHPPHP\Engine\Objects\ClassEntry; | ||
|
||
class ClassStore { | ||
/** @var ClassEntry[] */ | ||
protected $classes = array(); | ||
|
||
public function register(ClassEntry $ce) { | ||
$lcname = strtolower($ce->getName()); | ||
if (isset($this->classes[$lcname])) { | ||
throw new \RuntimeException(sprintf("Class %s already defined", $ce->getName())); | ||
} | ||
$this->classes[$lcname] = $ce; | ||
} | ||
|
||
public function exists($name) { | ||
return isset($this->classes[strtolower($name)]); | ||
} | ||
|
||
public function get($name) { | ||
$name = strtolower($name); | ||
if (!isset($this->classes[$name])) { | ||
throw new \RuntimeException(sprintf('Undefined class %s', $name)); | ||
} | ||
|
||
return $this->classes[$name]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace PHPPHP\Engine\Objects; | ||
|
||
use PHPPHP\Engine\ExecuteData; | ||
use PHPPHP\Engine\Objects\ClassInstance; | ||
use PHPPHP\Engine\FunctionData; | ||
use PHPPHP\Engine\Zval\Ptr; | ||
use PHPPHP\Engine\Zval; | ||
|
||
class ClassEntry | ||
{ | ||
private $name; | ||
private $methods = array(); | ||
|
||
public function __construct($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function getName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function instanciate(ExecuteData $data, array $properties, array $args = array()) | ||
{ | ||
return new ClassInstance($this, $properties); | ||
} | ||
|
||
public function addMethod($name, FunctionData $method) | ||
{ | ||
$this->methods[$name] = $method; | ||
} | ||
|
||
public function callMethod(ExecuteData $data, ClassInstance $ci, $name, array $args, Ptr $result = null) | ||
{ | ||
if (!isset($this->methods[$name])) { | ||
throw new \RuntimeException(sprintf('Call to undefined method %s::%s()', $this->getName(), $name)); | ||
} | ||
$method = $this->methods[$name]; | ||
if (!$result) { | ||
$result = Zval::ptrFactory(); | ||
} | ||
$method->execute($data->executor, $args, $result); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace PHPPHP\Engine\Objects; | ||
|
||
use PHPPHP\Engine\ExecuteData; | ||
use PHPPHP\Engine\Zval\Ptr; | ||
use PHPPHP\Engine\Objects\ClassEntry; | ||
use PHPPHP\Engine\Zval; | ||
|
||
class ClassInstance | ||
{ | ||
private $ce; | ||
private $properties = array(); | ||
|
||
public function __construct(ClassEntry $ce, array $properties) { | ||
$this->ce = $ce; | ||
$this->properties = $properties; | ||
|
||
array_map(function($property) { | ||
$property->addRef(); | ||
}, $this->properties); | ||
} | ||
|
||
public function getProperty($name) { | ||
if (!isset($this->properties[$name])) { | ||
$value = Zval::ptrFactory(); | ||
$this->properties[$name] = $value; | ||
} else { | ||
$value = $this->properties[$name]; | ||
} | ||
return $value; | ||
} | ||
|
||
public function callMethod(ExecuteData $data, $name, array $args, Ptr $result = null) { | ||
$this->ce->callMethod($data, $this, $name, $args, $result); | ||
} | ||
|
||
public function __destruct() { | ||
array_map(function($property) { | ||
$property->delRef(); | ||
}, $this->properties); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace PHPPHP\Engine\OpLines; | ||
|
||
class CastObject extends \PHPPHP\Engine\OpLine { | ||
|
||
public function execute(\PHPPHP\Engine\ExecuteData $data) { | ||
$this->result->setValue($this->op1->toObject($data)); | ||
|
||
$data->nextOp(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace PHPPHP\Engine\OpLines; | ||
|
||
use PHPPHP\Engine\FunctionData; | ||
|
||
class ClassDef extends \PHPPHP\Engine\OpLine { | ||
|
||
public function execute(\PHPPHP\Engine\ExecuteData $data) { | ||
$ce = $this->op1; | ||
|
||
$data->executor->getClassStore()->register($ce); | ||
|
||
$data->nextOp(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace PHPPHP\Engine\OpLines; | ||
|
||
class MethodCall extends \PHPPHP\Engine\OpLine { | ||
|
||
private $objectOp; | ||
|
||
public function setObjectOp($objectOp) { | ||
$this->objectOp = $objectOp; | ||
} | ||
|
||
public function execute(\PHPPHP\Engine\ExecuteData $data) { | ||
$object = $this->objectOp->toObject($data); | ||
$methodName = $this->op1->toString(); | ||
$args = $this->op2->toArray(); | ||
$object->callMethod($data, $methodName, $args, $this->result); | ||
$data->nextOp(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*nit
instantiate