Created
December 10, 2009 23:20
-
-
Save konobi/253801 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use JavaScript; | |
use Data::Dumper; | |
my $runtime = JavaScript::Runtime->new(); | |
my $ctx = $runtime->create_context(); | |
$ctx->set_version('1.8'); | |
my $system = {}; | |
$system->{digest} = { sha1 => sub { "sha1" }, md5 => sub { "md5" } }; | |
my $system2 = {}; | |
$system2->{http} = { request => sub { "requested" } }; | |
$ctx->bind_value('recur', sub {}); | |
my $bootstrap = <<EOJS; | |
(function(){ | |
var merge_recursively = function (obj1, obj2) { | |
for (var p in obj2) { | |
try { | |
// Property in destination object set; update its value. | |
if ( obj2[p].constructor==Object ) { | |
obj1[p] = merge_recursively(obj1[p], obj2[p]); | |
} else { | |
obj1[p] = obj2[p]; | |
} | |
} catch(e) { | |
// Property in destination object not set; create it and set its value. | |
obj1[p] = obj2[p]; | |
} | |
} | |
return obj1; | |
} | |
recur = merge_recursively; | |
})(); | |
EOJS | |
$ctx->bind_value('extensions', {}); | |
$ctx->bind_value('extensions.digest', $system); | |
$ctx->bind_value('extensions.http', $system2); | |
$ctx->bind_value('system', {}); | |
$ctx->eval($bootstrap); | |
$ctx->eval("system = recur(system, extensions.digest);"); | |
$ctx->eval("system = recur(system, extensions.http);"); | |
print Dumper($ctx->eval("this")); | |
$ctx->unbind_value('extensions'); | |
$ctx->unbind_value('recur'); | |
print Dumper( $ctx->eval("this;") ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment