#!/usr/bin/perl

$REVISION = '$Id: jvm,v 1.9 2004/06/08 06:51:00 eric Exp $ ';

use strict;

#BEGIN {unshift@INC,('../../..');}
package testJvm;
use vars qw(@ISA);
@ISA = qw(); # W3C::Util::NamedParmObject);

use W3C::Util::Exception;
use W3C::Util::Object;
use W3C::JVM::JVM;

eval {
    my $tester = new testJvm(\@ARGV);
    $tester->execute();
}; if ($@) {if ($@) {if (my $ex = &catch('W3C::Util::Exception')) {
    die $ex->toString;
#    die $@.' at line '.$tester->{XML_PARSER}->getLineNumber.' column '.$tester->{XML_PARSER}->getColumnNumber;
} else {die $@;}}}

sub new {
    my ($proto, $args) = @_;
    my $class = ref($proto) || $proto;
    my $self = {}; # $class->SUPER::new(@parms);
    bless ($self, $class);
    $self->{ORDINAL} = 10;
    for (my $i = 0; $i < @$args; $i++) {
	if ($args->[$i] =~ m/^-v$/) {
	    # verbosely dump classfiles
	    $self->{DUMP_CLASSES} = 1;
	} elsif ($args->[$i] =~ m/^-h$/) {
	    # verbosely dump classfiles
	    $self->{ORDINAL} = 16;
	} elsif ($args->[$i] =~ m/^-x$/) {
	    # don't actually execute anything
	    $self->{NO_EXEC} = 1;
	} elsif ($args->[$i] =~ m/^-c$/) {
	    # complile to perl and execute that
	    $self->{COMPILE} = 1;
	} elsif ($args->[$i] =~ m/^-cl$/) {
	    # complile to perl and check referenced classes at compile time
	    $self->{COMPILE} = 1;
	    $self->{COMPILE_LOCAL} = 1;
	} else {
	    # call this classes main
	    $self->{MAIN_CLASS} = $args->[$i];
	}
    }
    $self->{JVM} = new W3C::JVM::JVM($self->{MAIN_CLASS}, -compile => $self->{COMPILE}, -resolveLocal => $self->{COMPILE_LOCAL});
    if ($self->{DUMP_CLASSES}) {
	foreach my $name (keys %{$self->{JVM}{BY_NAME}}) {
	    print "===== class $name =====\n";
	    print $self->{JVM}{BY_NAME}{$name}->toString({-ordinal => $self->{ORDINAL}}), "\n";
	}
    }
    return $self;
}

sub execute {
    my ($self, $args) = @_;
    if (!$self->{NO_EXEC}) {
	$self->{JVM}->execute;
    }
}

