Permalink
Browse files

(bug 4634) Allow calling S2 constructors with new.

  • Loading branch information...
1 parent 108bb5f commit e92b7d5e3b7ac04e20efff30b509c35cbd756179 @anall anall committed Sep 8, 2012
Showing with 35 additions and 3 deletions.
  1. +1 −0 src/s2/S2/BackendJS/Codegen.pm
  2. +34 −3 src/s2/S2/NodeTerm.pm
@@ -690,6 +690,7 @@ sub asJS {
return;
}
+ # FIXME: Fix for S2 Constructors
if ($type == $NEW) {
$o->write("{\".type\": ".
$bp->quoteString($this->{'newClass'}->getIdent()) .
View
@@ -155,7 +155,20 @@ sub _getType {
unless ($nc) {
S2::error($this, "Can't instantiate unknown class.");
}
- return new S2::Type $clas;
+ $this->{funcID} = S2::Checker::functionID( $clas, $clas,
+ ( $this->{funcArgs} ? $this->{funcArgs}->typeList($ck) : undef ) );
+ $this->{funcBuiltin} = $ck->isFuncBuiltin( $this->{funcID} );
+
+ my $t = $ck->functionType($this->{funcID});
+ my $clasType = S2::Type->new( $clas );
+
+ S2::error($this, "Unknown constructor '$this->{funcID}'")
+ if $this->{funcArgs} && ! $t;
+ S2::error($this, "Constructor '$this->{funcID}' returns '" . $t->toString() . "', expected '$clas'")
+ if $t && ! $t->equals( $clasType );
+ $this->{funcID} = undef unless $t;
+
+ return $clasType;
}
else {
if (defined($wanted) && !$wanted->isPrimitive()) {
@@ -438,7 +451,12 @@ sub parse {
# the 'null' keyword, but it is no longer required and it is ignored.
my $nextToken = $toker->peek;
if (UNIVERSAL::isa($nextToken, 'S2::TokenIdent')) {
- $nt->{'newClass'} = $nt->getIdent($toker);
+ $nt->{newClass} = $nt->getIdent($toker);
+ $nextToken = $toker->peek;
+ if ( $nextToken == $S2::TokenPunct::LPAREN ) {
+ $nt->{funcArgs} = parse S2::NodeArguments $toker;
+ $nt->addNode($nt->{funcArgs});
+ }
}
elsif ($t == $S2::TokenKeyword::NEW) {
# A type is *required* for new, but not for null
@@ -536,9 +554,22 @@ sub asPerl {
}
if ($type == $NEW) {
- $o->write("S2::Object->new(" .
+ if ( $this->{funcID} && $this->{funcBuiltin} ) {
+ my $pkg = $bp->getBuiltinPackage() || "S2::Builtin";
+ my $clas = $this->{newClass}->getIdent();
+ $o->write($pkg . '::' . $clas . '__' . $clas);
+
+ # FIXME: I think S2 builtin constructors should at least get $ctx.
+ $o->write("(");
+ $this->{funcArgs}->asPerl($bp, $o, 0) if $this->{funcArgs};
+ $o->write(")");
+ } elsif ( $this->{funcID} ) {
+ S2::error($this, "Can't use non-builtin constructor '$this->{funcID}'");
+ } else {
+ $o->write("S2::Object->new(" .
$bp->quoteString($this->{'newClass'}->getIdent()) .
")");
+ }
return;
}

0 comments on commit e92b7d5

Please sign in to comment.