| 1 | # | ||
| 2 | # Copyright (c) 2015-2019 Christian Jaeger, copying@christianjaeger.ch | ||
| 3 | # | ||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||
| 7 | # bundled with this file. | ||
| 8 | # | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Chj::Serialize | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | |||
| 19 | =head1 NOTE | ||
| 20 | |||
| 21 | This is alpha software! Read the status section in the package README | ||
| 22 | or on the L<website|http://functional-perl.org/>. | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | package Chj::Serialize; | ||
| 27 | use strict; | ||
| 28 | use warnings; | ||
| 29 | use warnings FATAL => 'uninitialized'; | ||
| 30 | use Exporter "import"; | ||
| 31 | |||
| 32 | our @EXPORT = qw(new_Serialize_Closure); | ||
| 33 | our @EXPORT_OK = qw(); | ||
| 34 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | ||
| 35 | |||
| 36 | use Chj::TEST; | ||
| 37 | |||
| 38 | { | ||
| 39 | |||
| 40 | package Chj::Serializable::Closure; | ||
| 41 | use FP::Struct ["env", "code_id"], 'FP::Struct::Show', 'FP::Abstract::Pure'; | ||
| 42 | _END_ | ||
| 43 | } | ||
| 44 | |||
| 45 | { | ||
| 46 | |||
| 47 | package Chj::Serialize::Closure; | ||
| 48 | |||
| 49 | use PadWalker qw(closed_over set_closed_over); | ||
| 50 | use FP::Repl::WithRepl qw(WithRepl_eval); | ||
| 51 | use B::Deparse; | ||
| 52 | use FP::Predicates ":all"; | ||
| 53 | |||
| 54 | our $deparse = B::Deparse->new("-p", "-l", "-q"); | ||
| 55 | |||
| 56 | use FP::Struct [ | ||
| 57 | [\&is_hash, "_closure_generator_code_to_id"], | ||
| 58 | [\&is_hash, "_id_to_closure_generator_code"], | ||
| 59 | [\&is_hash, "_id_to_closure_generator"], | ||
| 60 | [\&is_natural0, "current_id"] | ||
| 61 | ], | ||
| 62 | 'FP::Struct::Show'; | ||
| 63 | |||
| 64 | sub next_id { | ||
| 65 | my ($self) = @_; | ||
| 66 | $$self{current_id}++ | ||
| 67 | } | ||
| 68 | |||
| 69 | sub id_to_closure_generator_code { | ||
| 70 | my ($self, $id) = @_; | ||
| 71 | $$self{_id_to_closure_generator_code}{$id} // die "unknown code_id $id"; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub id_to_closure_generator { | ||
| 75 | my ($self, $id) = @_; | ||
| 76 | $$self{_id_to_closure_generator}{$id} | ||
| 77 | //= &WithRepl_eval($self->id_to_closure_generator_code($id)) | ||
| 78 | // die "eval error: $@"; | ||
| 79 | |||
| 80 | #XX oh, want a WithRepl_xeval ? | ||
| 81 | } | ||
| 82 | |||
| 83 | sub serializable { | ||
| 84 | my ($self, $fn) = @_; | ||
| 85 | |||
| 86 | my $env = closed_over($fn); | ||
| 87 | my $code = $deparse->coderef2text($fn); | ||
| 88 | |||
| 89 | my $vars = [sort keys %$env]; | ||
| 90 | |||
| 91 | my $closure_generator_code | ||
| 92 | = 'sub { my (' . join(",", @$vars) . ') = @_; sub ' . $code . ' }'; | ||
| 93 | |||
| 94 | # use this as key right now. | ||
| 95 | |||
| 96 | my $id = $$self{_closure_generator_code_to_id}{$closure_generator_code} | ||
| 97 | //= do { | ||
| 98 | my $id = $self->next_id; | ||
| 99 | $$self{_id_to_closure_generator_code}{$id} | ||
| 100 | = $closure_generator_code; | ||
| 101 | $id | ||
| 102 | }; | ||
| 103 | |||
| 104 | # XX exclude web server handle (request) etc. [hw btw in schem? xhu] | ||
| 105 | |||
| 106 | Chj::Serializable::Closure->new($env, $id); | ||
| 107 | } | ||
| 108 | |||
| 109 | sub executable { | ||
| 110 | my ($self, $serializable_closure) = @_; | ||
| 111 | my ($env, $code_id) | ||
| 112 | = ($serializable_closure->env, $serializable_closure->code_id); | ||
| 113 | |||
| 114 | my @vals = map { $$env{$_} } sort keys %$env; | ||
| 115 | |||
| 116 | $self->id_to_closure_generator($code_id)->(map {$$_} @vals); | ||
| 117 | } | ||
| 118 | |||
| 119 | _END_ | ||
| 120 | } | ||
| 121 | |||
| 122 | sub new_Serialize_Closure { | ||
| 123 | Chj::Serialize::Closure->new({}, {}, {}, 0) | ||
| 124 | } | ||
| 125 | |||
| 126 | my $sc; | ||
| 127 | my ($sfn, $sfn2); | ||
| 128 | my $construct; | ||
| 129 | |||
| 130 | TEST { | ||
| 131 | $sc = new_Serialize_Closure; | ||
| 132 | my $hello = "Hello"; | ||
| 133 | my $unused = "Value"; | ||
| 134 | $construct = sub { | ||
| 135 | my ($a, $b) = @_; | ||
| 136 | sub { | ||
| 137 | my ($x) = @_; | ||
| 138 | "$hello $x $a $b" | ||
| 139 | } | ||
| 140 | }; | ||
| 141 | $sfn = $sc->serializable($construct->("the", "world", "so")); | ||
| 142 | } | ||
| 143 | bless( | ||
| 144 | { | ||
| 145 | 'env' => { '$hello' => \'Hello', '$a' => \'the', '$b' => \'world', }, | ||
| 146 | 'code_id' => 0 | ||
| 147 | }, | ||
| 148 | 'Chj::Serializable::Closure' | ||
| 149 | ); | ||
| 150 | |||
| 151 | TEST { | ||
| 152 | $sfn2 = $sc->serializable($construct->("fool", "!")); | ||
| 153 | } | ||
| 154 | bless( | ||
| 155 | { | ||
| 156 | 'env' => { '$hello' => \'Hello', '$a' => \'fool', '$b' => \'!', }, | ||
| 157 | 'code_id' => 0 | ||
| 158 | }, | ||
| 159 | 'Chj::Serializable::Closure' | ||
| 160 | ); | ||
| 161 | |||
| 162 | TEST { | ||
| 163 | $sc->executable($sfn)->("to") | ||
| 164 | } | ||
| 165 | 'Hello to the world'; | ||
| 166 | TEST { | ||
| 167 | $sc->executable($sfn2)->("you") | ||
| 168 | } | ||
| 169 | 'Hello you fool !'; | ||
| 170 | |||
| 171 | 1 |