File: Runtime.pm

package info (click to toggle)
libjavascript-perl 1.08-1%2Blenny1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 412 kB
  • ctags: 203
  • sloc: perl: 1,686; ansic: 1,620; makefile: 55
file content (216 lines) | stat: -rw-r--r-- 4,812 bytes parent folder | download
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
package JavaScript::Runtime;

use strict;
use warnings;

use Carp qw(croak);

use JavaScript;

sub new {
    my ($pkg, @args) = @_;

    $pkg = ref $pkg || $pkg;

    my $maxbytes = $JavaScript::MAXBYTES;
    $maxbytes = shift @args if (@args && $args[0] =~ /^\d+$/);
    
    my @does;
    for (@args) {
        if (my ($type) = $_ =~ /^-(\w+)$/) {
            my $does = "JavaScript::Runtime::" . $type;
            if (!exists $JavaScript::Runtime::{$type . '::'}) {
                eval "require $does;";
                croak $@ if $@;
            }
            push @does, $does;
        }
    }
    
    my $runtime = jsr_create($maxbytes);
    my $self = bless { _impl => $runtime, _does => \@does }, $pkg;
   
    for (@does) {
        my $init = $_->can('_init');
        $init->($self) if $init;
    }
 
    return $self;
}

sub _destroy {
    my $self = shift;

    for (@{$self->{_does}}) {
        my $destroy = $_->can('_destroy');
        $destroy->($self);
    }
    
    if ($self->{_perl_interrupt_handler}) {
        # Remove the current one
        $self->set_interrupt_handler();
    }

    return unless $self->{'_impl'};
    jsr_destroy($self->{'_impl'});
    delete $self->{'_impl'};
    return 1;
}

sub DESTROY {
    my ($self) = @_;
    $self->_destroy();
}

sub create_context {
	my $self = shift;
	
	warn "Requesting a custom stacksize is not longer supported" if @_ && $_[0];
	
    my $context = JavaScript::Context->new($self);

    return $context;
}

sub _add_interrupt_handler {
    my ($self, $handler) = @_;
    jsr_add_interrupt_handler($self->{_impl}, $handler);
}

sub _remove_interrupt_handler {
    my ($self, $handler) = @_;
    jsr_remove_interrupt_handler($self->{_impl}, $handler); 
}

sub set_interrupt_handler {
    my ($self, $handler) = @_;

    if ($handler && ref $handler eq '') {
        my $caller_pkg = caller;
        $handler = $caller_pkg->can($handler);
    }
    
    if ($handler) {
        $self->{_perl_interrupt_handler} = jsr_init_perl_interrupt_handler($handler);
        $self->_add_interrupt_handler($self->{_perl_interrupt_handler});
        
    }
    elsif ($self->{_perl_interrupt_handler}) {
        $self->_remove_interrupt_handler($self->{_perl_interrupt_handler});
        jsr_destroy_perl_interrupt_handler($self->{_perl_interrupt_handler});
        delete $self->{_perl_interrupt_handler};
    }
    
    1;
}

our $AUTOLOAD;
sub AUTOLOAD {
    my $self = shift;
    my ($method_name) = $AUTOLOAD =~ /::([A-Za-z0-9_]+)$/;
    
    for my $does (@{$self->{_does}}) {
        if (defined (my $method = $does->can($method_name))) {
            return $method->($self, @_);
        }
    }
    
    my $isa = join(", ", @{$self->{_does}});
    croak "Can't call method '$method_name' because it's not defined in $isa";
}

1;
__END__

=head1 NAME

JavaScript::Runtime -

=head1 DESCRIPTION

=head1 INTERFACE

=head2 CLASS METHODS

=over 4

=item new ( $maxbytes )

Creates a new runtime object. The optional argument I<$maxbytes> specifies the number
of bytes that can be allocated before garbage collection is runned. If ommited it
defaults to 1MB.

=back

=head2 INSTANCE METHODS

=over 4

=item create_context ()

Creates a new C<JavaScript::Context>-object in the runtime. 

=item set_interrupt_handler ( $handler )

Attaches an interrupt handler (a function that is called before each op is
executed ) to the runtime. The argument I<$handler> must be either a code-reference
or the name of a subroutine in the calling package.

To remove the handler call this method with an undef as argument.

Note that attaching an interrupt handler to the runtime causes a slowdown in
execution speed since we must execute some Perl code between each op.

In order to abort execution your handler should a false value (such as 0). All true values will continue
execution. Any exceptions thrown by the handler are ignored and $@ is cleared.

=back

=begin PRIVATE

=head1 PRIVATE INTERFACE

=over 4

=item _destroy

Method that deallocates the runtime.

=item DESTORY

Called when the runtime is destroyed by Perl.

=item jsr_create ( int maxbytes )

Creates a runtime and returns a pointer to a C<PJS_Runtime> structure.

=item jsr_destroy ( PJS_Runtime *runtime )

Destorys the runtime and deallocates the memory occupied by it.

=item jsr_add_interrupt_handler ( PJS_Runtime *runtime, PJS_TrapHandler *handler )

Adds an interrupt handler. 

=item jsr_remove_interrupt_handler ( PJS_Runtime *runtime, PJS_TrapHandler *handler )

Removes an interrupt handler

=item jsr_init_perl_interrupt_handler ( CV *callback )

Initializes a new Perl level interrupt handler.

=item jsr_destroy_perl_interrupt_handler ( PJS_TrapHandler *handler )

Destroys a Perl level interrupt handler

=back

=end PRIVATE

=head1 SEE ALSO

L<JavaScript::Context>

=cut