1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
package JavaScript::Script;
use strict;
sub new {
my $class = shift;
my $self = bless {}, $class;
my $context = shift;
my $source = shift;
$self->{impl} = CompileScriptImpl($context, $source);
return $self;
}
sub exec {
my $self = shift;
my $rval = ExecuteScriptImpl($self->{impl});
return $rval;
}
package JavaScript::Context;
use strict;
use Carp qw(croak);
sub new {
my ($class, $rt, $stacksize) = @_;
$stacksize = $JavaScript::STACKSIZE unless(defined $stacksize);
my $self = bless {}, $class;
$self->{impl} = CreateContext($rt, $stacksize);
return $self;
}
sub eval {
my ($self, $script) = @_;
my $rval = EvaluateScriptImpl($self->{impl}, $script);
return $rval;
}
sub eval_file {
my ($self, $file) = @_;
open(JS, "<$file") || die "$!\n";
local($/);
my $js = <JS>;
close(JS);
my $rval = EvaluateScriptImpl($self->{impl}, $js);
return $rval;
}
sub call {
my $self = shift;
my $func_name = shift;
my $args = [];
push(@$args, $_) foreach(@_);
my $rval = CallFunctionImpl($self->{impl}, $func_name, $args);
return $rval;
}
sub can {
my $self = shift;
my $func_name = shift;
return CanFunctionImpl($self->{impl}, $func_name);
}
# Functions for binding perl stuff into JS namespace
sub bind_function {
my $self = shift;
my %args = @_;
# Check for name
die "Missing argument 'name'\n" unless(exists $args{name});
die "Argument 'name' must match /^[A-Za-z0-9_]+\$/" unless($args{name} =~ /^[A-Za-z0-9\_]+$/);
# Check for func
die "Missing argument 'func'\n" unless(exists $args{func});
die "Argument 'func' is not a CODE reference\n" unless(ref($args{func}) eq 'CODE');
my $rval = BindPerlFunctionImpl($self->{impl}, $args{name}, $args{func});
return $rval;
}
sub bind_class {
my $self = shift;
my %args = @_;
# Check if name argument is valid
die "Missing argument 'name'\n" unless(exists $args{name});
die "Argument 'name' must match /^[A-Za-z0-9_]+\$/" unless($args{name} =~ /^[A-Za-z0-9\_]+$/);
# Check if constructor is supplied and it's an coderef
die "Missing argument 'constructor'\n" unless(exists $args{constructor});
die "Argument 'constructor' is not a code reference\n" unless(ref($args{constructor}) eq 'CODE');
# Check if we've supplied a methods mapping
if(exists $args{methods}) {
die "Argument 'methods' is not a hash reference\n" unless(ref($args{methods}) eq 'HASH');
# Make sure that all methods are coderefs
foreach(keys %{$args{methods}}) {
die "Defined method '$_' is not a code reference\n" unless(ref($args{methods}->{$_}) eq 'CODE');
}
} else {
# BindPerlClassImpl always expects a hash reference
$args{methods} = {};
}
# Check properties we've supplied
if(exists $args{properties}) {
die "Argument 'properties' must be a hash reference\n" unless(ref($args{properties}) eq 'HASH');
# Make sure that all methods are valid, ie. they must be of integer type
while (my($name,$opts) = each %{$args{properties}}) {
if (!ref($opts)) { # flags only
$opts ||= 0;
croak "Defined property '$name' is not numeric" unless($opts =~ /^\d+$/);
$opts = { flags => $opts};
$args{properties}->{$name} = $opts;
}
if ($opts->{JavaScript::JS_PROP_ACCESSOR()}) {
unless ( ref($opts->{getter}) eq 'CODE' and ref ($opts->{setter}) eq 'CODE') {
croak("getter and setter for propery '$name' must be code-refs");
}
}
}
} else {
$args{properties} = {};
}
if(exists $args{flags}) {
die "Argument 'flags' is not numeric\n" unless($args{flags} =~ /^\d+$/);
} else {
$args{flags} = 0;
}
unless(exists $args{package}) {
$args{package} = undef;
}
my $rval = BindPerlClassImpl($self->{impl}, $args{name}, $args{constructor}, $args{methods}, $args{properties}, $args{package}, $args{flags});
return $rval;
}
sub bind_object {
my ($self, $name, $object) = @_;
my $rval = BindPerlObject($self->{impl}, $name, $object);
return $rval;
}
sub set_error_handler {
my $self = shift;
my $sub = shift;
die "Argument isn't a CODE reference\n" unless(ref($sub) eq 'CODE');
$self->{_error_handler} = $sub;
SetErrorCallbackImpl($self->{impl}, $self->{_error_handler});
}
sub compile {
my $self = shift;
my $source = shift;
my $script = new JavaScript::Script($self->{impl}, $source);
return $script;
}
package JavaScript::Runtime;
sub new {
my ($class, $maxbytes) = @_;
$maxbytes = $JavaScript::MAXBYTES unless(defined $maxbytes);
my $self = bless {}, $class;
$self->{'impl'} = JavaScript::Runtime::CreateRuntime($maxbytes);
return $self;
}
sub DESTROY {
my ($self) = @_;
delete $self->{_error_handler};
}
sub create_context {
my $self = shift;
my $stacksize = shift;
$stacksize = $JavaScript::STACKSIZE unless(defined($stacksize));
my $context = new JavaScript::Context($self->{'impl'}, $stacksize);
return $context;
}
package JavaScript;
use 5.006;
use strict;
use warnings;
use Carp;
require Exporter;
require DynaLoader;
use AutoLoader;
our @ISA = qw(Exporter DynaLoader);
our %EXPORT_TAGS = ( 'all' => [ qw(
JS_PROP_PRIVATE
JS_PROP_READONLY
JS_CLASS_NO_INSTANCE
) ] );
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(
JS_PROP_PRIVATE
JS_PROP_READONLY
JS_PROP_ACCESSOR
JS_CLASS_NO_INSTANCE
);
our $VERSION = '0.55';
use vars qw($STACKSIZE $MAXBYTES $INITIALIZED);
use constant JS_PROP_PRIVATE => 0x1;
use constant JS_PROP_READONLY => 0x2;
use constant JS_PROP_ACCESSOR => 0x4;
use constant JS_CLASS_NO_INSTANCE => 0x1;
BEGIN {
$MAXBYTES = 1024 ** 2;
$STACKSIZE = 32 * 1024;
}
sub AUTOLOAD {
my $constname;
our $AUTOLOAD;
($constname = $AUTOLOAD) =~ s/.*:://;
croak "& not defined" if $constname eq 'constant';
my $val = constant($constname, @_ ? $_[0] : 0);
if ($! != 0) {
if ($! =~ /Invalid/ || $!{EINVAL}) {
$AutoLoader::AUTOLOAD = $AUTOLOAD;
goto &AutoLoader::AUTOLOAD;
}
else {
croak "Your vendor has not defined JavaScript macro $constname";
}
}
{
no strict 'refs';
# Fixed between 5.005_53 and 5.005_61
if ($] >= 5.00561) {
*$AUTOLOAD = sub () { $val };
}
else {
*$AUTOLOAD = sub { $val };
}
}
goto &$AUTOLOAD;
}
bootstrap JavaScript $VERSION;
1;
__END__
=head1 NAME
JavaScript - Perl extension for executing embedded JavaScript
|