| 1 | 1 | ▶ | use 5.008003; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | package JavaScript::Any::Context::Duktape; | ||
| 6 | |||
| 7 | our $AUTHORITY = 'cpan:TOBYINK'; | ||
| 8 | our $VERSION = '0.001'; | ||
| 9 | |||
| 10 | 1 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use Class::Tiny qw( _engine ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use Role::Tiny::With; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | with qw( JavaScript::Any::Context ); | ||
| 14 | |||
| 15 | 1 | use Ref::Util qw( | |
| 16 | is_plain_arrayref is_plain_hashref is_ref | ||
| 17 | is_blessed_ref is_plain_coderef | ||
| 18 | 1 | ▶ | ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 1 | ▶ | use JavaScript::Duktape qw(); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 20 | |||
| 21 | sub BUILD { | ||
| 22 | my $self = shift; | ||
| 23 | my ($args) = @_; | ||
| 24 | |||
| 25 | $self->_engine("JavaScript::Duktape"->new( | ||
| 26 | timeout => $args->{timeout}, | ||
| 27 | max_memory => $args->{max_memory}, | ||
| 28 | )); | ||
| 29 | |||
| 30 | return; | ||
| 31 | } | ||
| 32 | |||
| 33 | # delegated methods | ||
| 34 | sub timeout { shift->_engine->timeout(@_) } | ||
| 35 | sub max_memory { shift->_engine->max_memory(@_) } | ||
| 36 | sub eval { shift->_engine->eval(@_) } | ||
| 37 | |||
| 38 | sub define { | ||
| 39 | my $self = shift; | ||
| 40 | my ($name, $value) = @_; | ||
| 41 | |||
| 42 | $self->_throw_if_bad_name($name); | ||
| 43 | |||
| 44 | # XXX | ||
| 45 | # need to install a wrapper | ||
| 46 | # to process params and return values | ||
| 47 | |||
| 48 | if (is_plain_coderef($value)) { | ||
| 49 | $self->_engine->set($name, $value); | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | |||
| 53 | if (is_plain_hashref($value)) { | ||
| 54 | $self->_engine->set($name, $value); | ||
| 55 | return; | ||
| 56 | } | ||
| 57 | |||
| 58 | if (is_plain_arrayref($value)) { | ||
| 59 | $self->_engine->set($name, $value); | ||
| 60 | return; | ||
| 61 | } | ||
| 62 | |||
| 63 | if (is_blessed_ref($value)) { | ||
| 64 | $self->_engine->set($name, $value); | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | if ($self->is_true($value)) { | ||
| 69 | $self->_engine->eval("$name = true;"); # UGLY | ||
| 70 | return; | ||
| 71 | } | ||
| 72 | |||
| 73 | if ($self->is_false($value)) { | ||
| 74 | $self->_engine->eval("$name = false;"); # UGLY | ||
| 75 | return; | ||
| 76 | } | ||
| 77 | |||
| 78 | if ($self->is_null($value)) { | ||
| 79 | $self->_engine->eval("$name = null;"); # UGLY | ||
| 80 | return; | ||
| 81 | } | ||
| 82 | |||
| 83 | if (not is_ref($value)) { | ||
| 84 | $self->_engine->set($name, $value); | ||
| 85 | return; | ||
| 86 | } | ||
| 87 | |||
| 88 | $self->_throw_because_bad_value($value); | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | 1; | ||
| 93 | |||