| 1 | package JSON::T::SpiderMonkey; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use utf8; | ||
| 7 | |||
| 8 | use JavaScript::SpiderMonkey; | ||
| 9 | |||
| 10 | use base qw[JSON::T]; | ||
| 11 | |||
| 12 | BEGIN | ||
| 13 | { | ||
| 14 | $JSON::T::SpiderMonkey::AUTHORITY = 'cpan:TOBYINK'; | ||
| 15 | $JSON::T::SpiderMonkey::VERSION = '0.103'; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub init | ||
| 19 | { | ||
| 20 | my ($self, @args) = @_; | ||
| 21 | |||
| 22 | my $JS = $self->{engine} = JavaScript::SpiderMonkey->new(); | ||
| 23 | |||
| 24 | $JS->init; | ||
| 25 | $JS->function_set("return_to_perl", sub | ||
| 26 | { | ||
| 27 | $self->_accept_return_value(@_); | ||
| 28 | }); | ||
| 29 | $JS->function_set("print_to_perl", sub | ||
| 30 | { | ||
| 31 | print @_; | ||
| 32 | }); | ||
| 33 | |||
| 34 | $self->SUPER::init(@args); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub engine_eval | ||
| 38 | { | ||
| 39 | my ($self, $code) = @_; | ||
| 40 | $self->{engine}->eval($code); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub parameters | ||
| 44 | { | ||
| 45 | my ($self, %args) = @_; | ||
| 46 | while (my ($k,$v) = each %args) | ||
| 47 | { | ||
| 48 | if (ref $v eq 'ARRAY') | ||
| 49 | { | ||
| 50 | $v = $v->[1]; | ||
| 51 | } | ||
| 52 | $self->{'engine'}->property_by_path($k, "$v"); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | 1; | ||
| 57 | |||
| 58 | =head1 NAME | ||
| 59 | |||
| 60 | JSON::T::SpiderMonkey - transform JSON using JsonT and SpiderMonkey (libjs) | ||
| 61 | |||
| 62 | =head1 DESCRIPTION | ||
| 63 | |||
| 64 | This module uses L<JavaScript::SpiderMonkey> to provide JavaScript support. | ||
| 65 | |||
| 66 | Implements: | ||
| 67 | |||
| 68 | =over | ||
| 69 | |||
| 70 | =item C<init> | ||
| 71 | |||
| 72 | =item C<engine_eval> | ||
| 73 | |||
| 74 | =item C<parameters> | ||
| 75 | |||
| 76 | =back | ||
| 77 | |||
| 78 | =head1 BUGS | ||
| 79 | |||
| 80 | Please report any bugs to L<http://rt.cpan.org/>. | ||
| 81 | |||
| 82 | =head1 SEE ALSO | ||
| 83 | |||
| 84 | L<JSON::T>. | ||
| 85 | |||
| 86 | =head1 AUTHOR | ||
| 87 | |||
| 88 | Toby Inkster E<lt>tobyink@cpan.orgE<gt>. | ||
| 89 | |||
| 90 | =head1 COPYRIGHT AND LICENCE | ||
| 91 | |||
| 92 | Copyright 2008-2011, 2013 Toby Inkster. | ||
| 93 | |||
| 94 | Licensed under the Lesser GPL: | ||
| 95 | L<http://creativecommons.org/licenses/LGPL/2.1/>. | ||
| 96 | |||
| 97 | =head1 DISCLAIMER OF WARRANTIES | ||
| 98 | |||
| 99 | THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED | ||
| 100 | WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | ||
| 101 | MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
| 102 | |||
| 103 | =cut |