Skip to content

Commit

Permalink
Fixed guard mechanism that forbids instantiating builtin types via ob…
Browse files Browse the repository at this point in the history
…ject.__new__()
  • Loading branch information
smuuf committed Dec 8, 2022
1 parent 10d67b7 commit d66ad6a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/Stdlib/TypeExtensions/ObjectTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use \Smuuf\Primi\Values\TypeValue;
use \Smuuf\Primi\Values\AbstractValue;
use \Smuuf\Primi\Values\InstanceValue;
use \Smuuf\Primi\Values\StaticTypeValue;
use \Smuuf\Primi\Extensions\PrimiFunc;
use \Smuuf\Primi\Extensions\TypeExtension;
use \Smuuf\Primi\Structures\CallArgs;
use Smuuf\Primi\Values\BuiltinTypeValue;

class ObjectTypeExtension extends TypeExtension {

Expand All @@ -35,8 +35,8 @@ public static function __new__(
// can be instantiated into PHP `InstanceValue` objects. Otherwise
// things would get messy (for example `DictValue::__construct()` would
// not get used in any way, etc.)
if ($type instanceof StaticTypeValue) {
throw new TypeError("First argument for object.__new__() must not be a native type");
if ($type instanceof BuiltinTypeValue) {
throw new TypeError("First argument for object.__new__() must not be a builtin type");
}

return new InstanceValue($type, $ctx);
Expand Down
6 changes: 5 additions & 1 deletion src/Values/TypeValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,13 @@ public function invoke(
// than the "type" type itself - and calling this object should
// represent instantiation of new object that will have this type.
if ($fn = Types::attr_lookup($this, MagicStrings::MAGICMETHOD_NEW, $this)) {

// The static '__new__()' will receive type object as the first
// argument.
$newArgs = $args
? new CallArgs([$this, ...$args->getArgs()], $args->getKwargs())
? (new CallArgs([$this]))->withExtra($args)
: new CallArgs([$this]);

$result = $fn->invoke($context, $newArgs, $callsite);

if ($init = $result->attrGet(MagicStrings::MAGICMETHOD_INIT)) {
Expand Down

0 comments on commit d66ad6a

Please sign in to comment.