| 1 | package Zuzu::Module::Perl; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | |||
| 5 | our $VERSION = '0.007002'; | ||
| 6 | |||
| 7 | use JSON::PP (); | ||
| 8 | use Scalar::Util qw( blessed ); | ||
| 9 | |||
| 10 | use Zuzu::Util::NativeHelpers qw( | ||
| 11 | native_class | ||
| 12 | native_function | ||
| 13 | native_object | ||
| 14 | perl_to_zuzu | ||
| 15 | ); | ||
| 16 | |||
| 17 | sub _eval_scalar { | ||
| 18 | my ( $code, $topic ) = @_; | ||
| 19 | |||
| 20 | $code = defined $code ? "$code" : ''; | ||
| 21 | my $value; | ||
| 22 | if ( @_ > 1 ) { | ||
| 23 | local $_ = $topic; | ||
| 24 | $value = eval $code; | ||
| 25 | } | ||
| 26 | else { | ||
| 27 | $value = eval $code; | ||
| 28 | } | ||
| 29 | die $@ if $@; | ||
| 30 | |||
| 31 | return $value; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub _is_safe_perl_value { | ||
| 35 | my ( $value, $seen ) = @_; | ||
| 36 | |||
| 37 | return 1 if not defined $value; | ||
| 38 | return 1 if not ref $value; | ||
| 39 | return 0 if blessed( $value ); | ||
| 40 | |||
| 41 | $seen //= {}; | ||
| 42 | my $addr = "$value"; | ||
| 43 | return 1 if $seen->{$addr}++; | ||
| 44 | |||
| 45 | if ( ref($value) eq 'ARRAY' ) { | ||
| 46 | for my $item ( @{ $value } ) { | ||
| 47 | return 0 if not _is_safe_perl_value( $item, $seen ); | ||
| 48 | } | ||
| 49 | return 1; | ||
| 50 | } | ||
| 51 | |||
| 52 | if ( ref($value) eq 'HASH' ) { | ||
| 53 | for my $key ( CORE::keys %{ $value } ) { | ||
| 54 | return 0 if ref $key; | ||
| 55 | return 0 if not _is_safe_perl_value( $value->{$key}, $seen ); | ||
| 56 | } | ||
| 57 | return 1; | ||
| 58 | } | ||
| 59 | |||
| 60 | return 0; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub _wrap_result { | ||
| 64 | my ( $class_obj, $raw ) = @_; | ||
| 65 | |||
| 66 | return native_object( | ||
| 67 | class => $class_obj, | ||
| 68 | slots => { | ||
| 69 | _raw => $raw, | ||
| 70 | }, | ||
| 71 | const => { | ||
| 72 | _raw => 1, | ||
| 73 | }, | ||
| 74 | ); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub IMPORT { | ||
| 78 | my ( $class, $runtime ) = @_; | ||
| 79 | |||
| 80 | my $perl_result_class = native_class( | ||
| 81 | name => 'PerlResult', | ||
| 82 | ); | ||
| 83 | my $perl_class = native_class( | ||
| 84 | name => 'Perl', | ||
| 85 | ); | ||
| 86 | |||
| 87 | $perl_class->static_methods->{version} = native_function( | ||
| 88 | name => 'version', | ||
| 89 | native => sub { | ||
| 90 | return "$^V"; | ||
| 91 | }, | ||
| 92 | ); | ||
| 93 | |||
| 94 | $perl_class->static_methods->{eval} = native_function( | ||
| 95 | name => 'eval', | ||
| 96 | native => sub { | ||
| 97 | my ( $self, $code ) = @_; | ||
| 98 | $runtime->assert_capability( 'perl', "Perl.eval is denied by runtime policy" ); | ||
| 99 | my $value = _eval_scalar( $code ); | ||
| 100 | return _wrap_result( $perl_result_class, $value ); | ||
| 101 | }, | ||
| 102 | ); | ||
| 103 | |||
| 104 | $perl_result_class->methods->{isSafe} = native_function( | ||
| 105 | name => 'isSafe', | ||
| 106 | native => sub { | ||
| 107 | my ( $self ) = @_; | ||
| 108 | return _is_safe_perl_value( $self->slots->{_raw} ) ? 1 : 0; | ||
| 109 | }, | ||
| 110 | ); | ||
| 111 | |||
| 112 | $perl_result_class->methods->{value} = native_function( | ||
| 113 | name => 'value', | ||
| 114 | native => sub { | ||
| 115 | my ( $self ) = @_; | ||
| 116 | my $raw = $self->slots->{_raw}; | ||
| 117 | die "PerlResult is not safe to convert" if not _is_safe_perl_value( $raw ); | ||
| 118 | return perl_to_zuzu( $raw ); | ||
| 119 | }, | ||
| 120 | ); | ||
| 121 | |||
| 122 | $perl_result_class->methods->{toJSON} = native_function( | ||
| 123 | name => 'toJSON', | ||
| 124 | native => sub { | ||
| 125 | my ( $self ) = @_; | ||
| 126 | my $encoder = JSON::PP->new | ||
| 127 | ->allow_nonref(1) | ||
| 128 | ->canonical(1); | ||
| 129 | return $encoder->encode( $self->slots->{_raw} ); | ||
| 130 | }, | ||
| 131 | ); | ||
| 132 | |||
| 133 | $perl_result_class->methods->{eval} = native_function( | ||
| 134 | name => 'eval', | ||
| 135 | native => sub { | ||
| 136 | my ( $self, $code ) = @_; | ||
| 137 | $runtime->assert_capability( 'perl', "PerlResult.eval is denied by runtime policy" ); | ||
| 138 | my $raw = $self->slots->{_raw}; | ||
| 139 | my $next = _eval_scalar( $code, $raw ); | ||
| 140 | return _wrap_result( $perl_result_class, $next ); | ||
| 141 | }, | ||
| 142 | ); | ||
| 143 | |||
| 144 | return { | ||
| 145 | Perl => $perl_class, | ||
| 146 | PerlResult => $perl_result_class, | ||
| 147 | }; | ||
| 148 | } | ||
| 149 | |||
| 150 | 1; | ||
| 151 | |||
| 152 | =pod | ||
| 153 | |||
| 154 | =head1 NAME | ||
| 155 | |||
| 156 | Zuzu::Module::Perl - perl builtin bridge for ZuzuScript. | ||
| 157 | |||
| 158 | =head1 DESCRIPTION | ||
| 159 | |||
| 160 | Implements the C<perl> builtin module. | ||
| 161 | |||
| 162 | Exports: | ||
| 163 | |||
| 164 | =over | ||
| 165 | |||
| 166 | =item * C<Perl> class with static methods C<version> and C<eval> | ||
| 167 | |||
| 168 | =item * C<PerlResult> class wrapping evaluated Perl values | ||
| 169 | |||
| 170 | =back | ||
| 171 | |||
| 172 | C<Perl.eval> runs Perl source code in scalar context and always returns | ||
| 173 | C<PerlResult>. | ||
| 174 | |||
| 175 | C<PerlResult> provides: | ||
| 176 | |||
| 177 | =over | ||
| 178 | |||
| 179 | =item * C<isSafe> | ||
| 180 | |||
| 181 | True when the wrapped value contains only undef, scalar, array, and | ||
| 182 | hash data recursively. | ||
| 183 | |||
| 184 | =item * C<value> | ||
| 185 | |||
| 186 | Converts the wrapped value to a native Zuzu value. Dies if unsafe. | ||
| 187 | |||
| 188 | =item * C<toJSON> | ||
| 189 | |||
| 190 | Attempts JSON serialization of the wrapped Perl value. | ||
| 191 | |||
| 192 | =item * C<eval> | ||
| 193 | |||
| 194 | Evaluates further Perl code with the current result available as C<$_> | ||
| 195 | in scalar context. | ||
| 196 | |||
| 197 | =back | ||
| 198 | |||
| 199 | =head1 COPYRIGHT AND LICENCE | ||
| 200 | |||
| 201 | B<< Zuzu::Module::Perl >> is copyright Toby Inkster. | ||
| 202 | |||
| 203 | It is free software; you may redistribute it and/or modify it under | ||
| 204 | the terms of either the Artistic License 1.0 or the GNU General Public | ||
| 205 | License version 2. | ||
| 206 | |||
| 207 | =cut |