]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/SpiderMonkey.pm
simplified eval'ing code
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Utils / SpiderMonkey.pm
1 package OpenILS::Utils::SpiderMonkey;
2 use strict; use warnings;
3 use OpenSRF::Utils::Logger qw(:logger);
4 use OpenILS::Utils::ScriptRunner;
5 use base 'OpenILS::Utils::ScriptRunner';
6 use JavaScript::SpiderMonkey;
7
8 sub new {
9         my ( $class, $file ) = @_;
10         $class = ref($class) || $class;
11         my $self = { file => $file };
12         return bless( $self, $class );
13 }
14
15 sub context {
16         my( $self, $context ) = @_;
17         $self->{ctx} = $context if $context;
18         return $self->{ctx};
19 }
20
21 sub init {
22         my $self = shift;
23         my $js = JavaScript::SpiderMonkey->new();
24         $js->init();
25         $js->function_set("perl_print",         sub { print "@_\n"; } );
26         $js->function_set("log_error",          sub { $logger->error(@_); return 1;} );
27         $js->function_set("log_warn",                   sub { $logger->warn(@_); return 1;} );
28         $js->function_set("log_info",                   sub { $logger->info(@_); return 1;} );
29         $js->function_set("log_debug",          sub { $logger->debug(@_); return 1;} );
30         $js->function_set("log_internal",       sub { $logger->internal(@_); return 1;} );
31         $self->context($js);
32         return $self;
33 }
34
35
36 sub load {
37         my( $self, $filename ) = @_;
38         $self->{file} = $filename;
39 }
40
41 sub run {
42         my $self = shift;
43         my $file = shift() || $self->{file};
44         my $js = $self->context;
45
46         if( ! open(F, $file) ) {
47                 $logger->error("Error opening script file: $file");
48                 return 0;
49         }
50
51         if( ! $js->eval(join("\n", <F>)) ) {
52                 $logger->error("$file Eval failed: $@");  
53                 return 0;
54         }
55
56         close(F);
57         return 1;
58 }
59
60 sub load_lib { 
61         my( $self, $file ) = @_;
62         $self->run( $file );
63 }
64
65 sub _js_prop_name {
66         my $name = shift;
67         $name =~ s/^.*\.//o;
68         return $name;
69 }
70
71 sub insert_fm {
72
73         my( $self, $key, $fm ) = @_;
74         my $ctx = $self->context;
75         return undef unless ($ctx and $key and $fm);
76         my $o = $ctx->object_by_path($key);
77         
78         for my $f ( $fm->properties ) {
79                 $ctx->property_by_path("$key.$f", $fm->$f(),
80
81                         sub {
82                                 my $k = _js_prop_name(shift());
83                                 $fm->$k();
84                         }, 
85
86                         sub {
87                                 my $k = _js_prop_name(shift());
88                                 $fm->ischanged(1);
89                                 $fm->$k(@_);
90                         }
91                 );
92         }
93 }
94
95 sub insert_hash {
96
97         my( $self, $key, $hash ) = @_;
98         my $ctx = $self->context;
99         return undef unless ($ctx and $key and $hash);
100         $ctx->object_by_path($key);
101         
102         for my $k ( keys %$hash ) {
103                 $ctx->property_by_path(
104                         "$key.$k", $hash->{$k},
105                         sub { $hash->{_js_prop_name(shift())} },
106                         sub { 
107                                 my( $key, $val ) = @_;
108                                 $hash->{_js_prop_name($key)} = $val; }
109                 );
110         }
111 }
112
113 1;