| 1 | package JavaScript::V8::CommonJS; | ||
| 2 | |||
| 3 | # use strictures 2; | ||
| 4 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 3 | ▶ | use JavaScript::V8; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use JavaScript::V8::CommonJS::Exception; | ||
| 8 | use File::ShareDir 'dist_dir'; | ||
| 9 | use File::Basename 'dirname'; | ||
| 10 | use File::Spec::Functions qw' rel2abs catdir catfile '; | ||
| 11 | use Cwd qw' getcwd realpath '; | ||
| 12 | use Data::Dumper; | ||
| 13 | # use Data::Printer; | ||
| 14 | use Carp qw' croak confess '; | ||
| 15 | |||
| 16 | our $VERSION = "0.04"; | ||
| 17 | |||
| 18 | my $scripts_dir = realpath catdir(dirname(rel2abs(__FILE__)), '../../../share'); | ||
| 19 | $scripts_dir = catdir(dist_dir('JavaScript-V8-CommonJS')) | ||
| 20 | unless -d $scripts_dir; | ||
| 21 | |||
| 22 | sub new { | ||
| 23 | my $class = shift; | ||
| 24 | my $args = @_ == 0 ? {} | ||
| 25 | : @_ > 1 ? {@_} : $_[0]; | ||
| 26 | |||
| 27 | croak "invalid arguments" unless ref $args eq 'HASH'; | ||
| 28 | |||
| 29 | bless my $self = { | ||
| 30 | paths => $args->{paths} || [getcwd()], | ||
| 31 | modules => $args->{modules} || {}, | ||
| 32 | }, $class; | ||
| 33 | |||
| 34 | $self->{c} = $self->_build_ctx; | ||
| 35 | $self; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub c { shift->{c} } | ||
| 39 | sub modules { shift->{modules} } | ||
| 40 | sub paths { shift->{paths} } | ||
| 41 | |||
| 42 | |||
| 43 | sub _build_ctx { | ||
| 44 | my $self = shift; | ||
| 45 | my $c = JavaScript::V8::Context->new; | ||
| 46 | |||
| 47 | # global functions | ||
| 48 | for my $name (qw/ resolveModule requireNative evalModuleFile /) { | ||
| 49 | |||
| 50 | $c->bind($name => sub { | ||
| 51 | $self->can("_$name")->($self, @_); | ||
| 52 | }); | ||
| 53 | } | ||
| 54 | |||
| 55 | $c->bind( | ||
| 56 | console => { | ||
| 57 | log => \&_log, | ||
| 58 | warn => \&_log, | ||
| 59 | error => \&_log, | ||
| 60 | info => \&_log, | ||
| 61 | } | ||
| 62 | ); | ||
| 63 | |||
| 64 | # require.js | ||
| 65 | my $require_js = catfile($scripts_dir, "require.js"); | ||
| 66 | _eval($c, _slurp($require_js), $require_js); | ||
| 67 | |||
| 68 | $c; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub add_module { | ||
| 72 | my ($self, $name, $module) = @_; | ||
| 73 | my $mods = $self->modules; | ||
| 74 | croak "add_module() error: '$name' already exists'" if exists $mods->{$name}; | ||
| 75 | $mods->{$name} = $module; | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | sub _requireNative { | ||
| 80 | my ($self, $id) = @_; | ||
| 81 | $self->modules->{$id}; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _resolveModule { | ||
| 85 | my ($self, $id, $current_module_file) = @_; | ||
| 86 | |||
| 87 | confess "Can't traverse up the module paths." if $id =~ /\.\./; | ||
| 88 | |||
| 89 | # relative | ||
| 90 | if ($id =~ /^\.\//) { | ||
| 91 | |||
| 92 | confess "Can't load relative module '$id' without current_module_file" unless $current_module_file; | ||
| 93 | my $dir = dirname($current_module_file); | ||
| 94 | my $file = catfile($dir, $id.'.js'); | ||
| 95 | return -e $file ? $file : undef; | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | # convert absolute path into module id | ||
| 100 | $id =~ s/^\/+//g; | ||
| 101 | |||
| 102 | # module id | ||
| 103 | foreach my $path (@{$self->paths}) { | ||
| 104 | my $file = catfile($path, $id.".js"); | ||
| 105 | return "$file" if -f $file; | ||
| 106 | } | ||
| 107 | |||
| 108 | return undef; | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | sub _readFile { | ||
| 113 | my ($self, $path) = @_; | ||
| 114 | return undef unless -f $path; | ||
| 115 | _slurp($path); | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | sub _log { | ||
| 120 | my (@lines) = @_; | ||
| 121 | @lines = map { defined $_ ? $_ : 'undef' } map { ref $_ ? Dumper($_) : $_ } @lines; | ||
| 122 | printf STDERR "# [console.log] @lines\n"; | ||
| 123 | } | ||
| 124 | |||
| 125 | sub eval_file { | ||
| 126 | my ($self, $file) = @_; | ||
| 127 | croak "do not exist: $file" unless -f $file; | ||
| 128 | $self->eval(_slurp($file), $file); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub _evalModuleFile { | ||
| 132 | my ($self, $file) = @_; | ||
| 133 | croak "module '$file' do not exist" unless -f $file; | ||
| 134 | my $module_code = _slurp($file); | ||
| 135 | |||
| 136 | my $wrapper = qq! | ||
| 137 | |||
| 138 | require.__modules["$file"] = { | ||
| 139 | exports: {}, | ||
| 140 | __filename: "$file" | ||
| 141 | }; | ||
| 142 | |||
| 143 | try { | ||
| 144 | require.__callStack.push(require.__modules["$file"]); | ||
| 145 | (function (require, module, exports, __filename, __dirname) { "use strict"; \%s })(require, require.__modules["$file"], require.__modules["$file"].exports, "$file"); | ||
| 146 | require.__modules["$file"].__is_compiled = true; | ||
| 147 | } | ||
| 148 | finally{ | ||
| 149 | require.__callStack.pop(); | ||
| 150 | if (require.__modules["$file"].__is_compiled) { | ||
| 151 | delete require.__modules["$file"].__is_compiled; | ||
| 152 | } else { | ||
| 153 | delete require.__modules["$file"]; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | 1; | ||
| 158 | !; | ||
| 159 | |||
| 160 | # make oneline | ||
| 161 | $wrapper =~ s/\n//g; | ||
| 162 | |||
| 163 | # eval | ||
| 164 | my $rv = $self->c->eval(sprintf($wrapper, $module_code), $file); | ||
| 165 | |||
| 166 | if (!defined $rv && $@) { | ||
| 167 | $@ =~ s/^Error: //; | ||
| 168 | die $@ | ||
| 169 | } | ||
| 170 | |||
| 171 | $rv; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub eval { | ||
| 175 | my $self = shift; | ||
| 176 | _eval($self->c, @_); | ||
| 177 | }; | ||
| 178 | |||
| 179 | sub _eval { | ||
| 180 | my ($c, $code, $source) = @_; | ||
| 181 | local $@ = undef; | ||
| 182 | my $rv = $c->eval("try {$code} catch(e) {throw e.stack}", $source || ()); | ||
| 183 | if (!defined $rv && $@) { | ||
| 184 | # warn "# eval error((($@)))"; | ||
| 185 | my $exception = JavaScript::V8::CommonJS::Exception->new_from_string($@); | ||
| 186 | my $ourStackCall = $exception->stack->[-1]; | ||
| 187 | $ourStackCall->{column} = $ourStackCall->{column} - 5 if ref $ourStackCall eq 'HASH'; # subtract "try {" length | ||
| 188 | die $exception; | ||
| 189 | } | ||
| 190 | $rv; | ||
| 191 | }; | ||
| 192 | |||
| 193 | |||
| 194 | |||
| 195 | sub _slurp { | ||
| 196 | my $path = shift; | ||
| 197 | |||
| 198 | CORE::open my $file, '<:encoding(UTF-8)', $path or croak qq{Can't open file "$path": $!}; | ||
| 199 | my $ret = my $content = ''; | ||
| 200 | while ($ret = $file->sysread(my $buffer, 131072, 0)) { $content .= $buffer } | ||
| 201 | croak qq{Can't read from file "$path": $!} unless defined $ret; | ||
| 202 | |||
| 203 | return $content; | ||
| 204 | } | ||
| 205 | |||
| 206 | 1; | ||
| 207 | __END__ | ||