| 1 | package JavaScript::V8::CommonJS; | ||
| 2 | |||
| 3 | # use strictures 2; | ||
| 4 | 4 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 4 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | 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.09"; | ||
| 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 | v8_params => $args->{v8_params}, | ||
| 33 | }, $class; | ||
| 34 | |||
| 35 | $self->{c} = $self->_build_ctx; | ||
| 36 | $self; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub c { shift->{c} } | ||
| 40 | sub modules { shift->{modules} } | ||
| 41 | sub paths { shift->{paths} } | ||
| 42 | |||
| 43 | |||
| 44 | sub _build_ctx { | ||
| 45 | my $self = shift; | ||
| 46 | my $c = JavaScript::V8::Context->new($self->{v8_params} ? %{$self->{v8_params}} : ()); | ||
| 47 | |||
| 48 | # global functions | ||
| 49 | for my $name (qw/ resolveModule requireNative evalModuleFile /) { | ||
| 50 | |||
| 51 | $c->bind($name => sub { | ||
| 52 | $self->can("_$name")->($self, @_); | ||
| 53 | }); | ||
| 54 | } | ||
| 55 | |||
| 56 | $c->bind( | ||
| 57 | console => { | ||
| 58 | log => \&_log, | ||
| 59 | warn => \&_log, | ||
| 60 | error => \&_log, | ||
| 61 | info => \&_log, | ||
| 62 | } | ||
| 63 | ); | ||
| 64 | |||
| 65 | # require.js | ||
| 66 | my $require_js = catfile($scripts_dir, "require.js"); | ||
| 67 | _eval($c, _slurp($require_js), $require_js); | ||
| 68 | |||
| 69 | $c; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub add_module { | ||
| 73 | my ($self, $name, $module) = @_; | ||
| 74 | my $mods = $self->modules; | ||
| 75 | croak "add_module() error: '$name' already exists'" if exists $mods->{$name}; | ||
| 76 | $mods->{$name} = $module; | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | sub _requireNative { | ||
| 81 | my ($self, $id) = @_; | ||
| 82 | $self->modules->{$id}; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _resolveModule { | ||
| 86 | my ($self, $id, $current_module_file) = @_; | ||
| 87 | |||
| 88 | |||
| 89 | # relative | ||
| 90 | if ($id =~ /^\.\.?\//) { | ||
| 91 | |||
| 92 | confess "Can't load relative module '$id' without current_module_file" unless $current_module_file; | ||
| 93 | |||
| 94 | my $dir = dirname($current_module_file); | ||
| 95 | return $self->_resolve_relative_module_node($dir, $id); | ||
| 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 | # for my $file (catfile($path, $id.".js"), catfile($path, $id.".json")) { | ||
| 105 | # return $file if -f $file; | ||
| 106 | # } | ||
| 107 | my $file = $self->_resolve_relative_module_node($path, $id); | ||
| 108 | return $file if $file; | ||
| 109 | } | ||
| 110 | |||
| 111 | return undef; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub _resolve_relative_module_node { | ||
| 115 | my ($self, $dir, $path) = @_; | ||
| 116 | |||
| 117 | my $basepath = catdir($dir, $path); | ||
| 118 | |||
| 119 | # warn "# basepath($basepath)\n"; | ||
| 120 | |||
| 121 | # module.js | ||
| 122 | if (-f "$basepath.js") { | ||
| 123 | |||
| 124 | return $self->_validate_module_path("$basepath.js"); | ||
| 125 | } | ||
| 126 | |||
| 127 | # module.json | ||
| 128 | if (-f "$basepath.json") { | ||
| 129 | |||
| 130 | return $self->_validate_module_path("$basepath.json"); | ||
| 131 | } | ||
| 132 | |||
| 133 | # module/package.json | ||
| 134 | if (-f (my $package_file = catfile($basepath, "package.json"))) { | ||
| 135 | |||
| 136 | # parse JSON | ||
| 137 | my $json = _slurp($package_file); | ||
| 138 | my $main = $self->eval("($json).main"); | ||
| 139 | |||
| 140 | if ($main) { | ||
| 141 | my $file = catfile($basepath, $main); | ||
| 142 | return $self->_validate_module_path($file) | ||
| 143 | if -f $file; | ||
| 144 | } | ||
| 145 | else { | ||
| 146 | warn "module package file exists but no main entry defined ($package_file)"; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | # module/index.js | ||
| 151 | if (-f (my $file = catfile($basepath, "index.js"))) { | ||
| 152 | return $self->_validate_module_path($file); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | sub _validate_module_path { | ||
| 157 | my ($self, $path) = @_; | ||
| 158 | $path = realpath $path; | ||
| 159 | foreach (@{$self->paths}) { | ||
| 160 | if (index($path, $_) == 0) { | ||
| 161 | return $path; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | warn "Requested commonjs module '$path' is outside of valid root paths! Returning nothing."; | ||
| 166 | return; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub _readFile { | ||
| 170 | my ($self, $path) = @_; | ||
| 171 | return undef unless -f $path; | ||
| 172 | _slurp($path); | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | sub _log { | ||
| 177 | my (@lines) = @_; | ||
| 178 | @lines = map { defined $_ ? $_ : 'undef' } map { ref $_ ? Dumper($_) : $_ } @lines; | ||
| 179 | printf STDERR "# [console.log] @lines\n"; | ||
| 180 | } | ||
| 181 | |||
| 182 | sub eval_file { | ||
| 183 | my ($self, $file) = @_; | ||
| 184 | croak "do not exist: $file" unless -f $file; | ||
| 185 | $self->eval(_slurp($file), $file); | ||
| 186 | } | ||
| 187 | |||
| 188 | sub _evalModuleFile { | ||
| 189 | my ($self, $file) = @_; | ||
| 190 | croak "module '$file' do not exist" unless -f $file; | ||
| 191 | my $module_code = _slurp($file); | ||
| 192 | |||
| 193 | # JSON | ||
| 194 | if ($file =~ /\.json$/) { | ||
| 195 | |||
| 196 | # validate json | ||
| 197 | my $json = _slurp($file); | ||
| 198 | |||
| 199 | # inject as module | ||
| 200 | my $rv = $self->c->eval(qq! | ||
| 201 | |||
| 202 | require.__modules["$file"] = { | ||
| 203 | exports: null, | ||
| 204 | __filename: "$file" | ||
| 205 | }; | ||
| 206 | |||
| 207 | (function (module) { "use strict"; module.exports = $json; })(require.__modules["$file"]); | ||
| 208 | |||
| 209 | !, $file); | ||
| 210 | |||
| 211 | if (!defined $rv && $@) { | ||
| 212 | $@ =~ s/^Error: //; | ||
| 213 | die $@ | ||
| 214 | } | ||
| 215 | |||
| 216 | return; | ||
| 217 | } | ||
| 218 | |||
| 219 | # js module | ||
| 220 | my $wrapper = qq! | ||
| 221 | |||
| 222 | require.__modules["$file"] = { | ||
| 223 | exports: {}, | ||
| 224 | __filename: "$file" | ||
| 225 | }; | ||
| 226 | |||
| 227 | try { | ||
| 228 | require.__callStack.push(require.__modules["$file"]); | ||
| 229 | (function (require, module, exports, __filename, __dirname) { "use strict"; \%s })(require, require.__modules["$file"], require.__modules["$file"].exports, "$file"); | ||
| 230 | require.__modules["$file"].__is_compiled = true; | ||
| 231 | } | ||
| 232 | finally{ | ||
| 233 | require.__callStack.pop(); | ||
| 234 | if (require.__modules["$file"].__is_compiled) { | ||
| 235 | delete require.__modules["$file"].__is_compiled; | ||
| 236 | } else { | ||
| 237 | delete require.__modules["$file"]; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | 1; | ||
| 242 | !; | ||
| 243 | |||
| 244 | # make oneline | ||
| 245 | $wrapper =~ s/\n//g; | ||
| 246 | |||
| 247 | # eval | ||
| 248 | my $rv = $self->c->eval(sprintf($wrapper, $module_code), $file); | ||
| 249 | |||
| 250 | if (!defined $rv && $@) { | ||
| 251 | $@ =~ s/^Error: //; | ||
| 252 | die $@ | ||
| 253 | } | ||
| 254 | |||
| 255 | $rv; | ||
| 256 | } | ||
| 257 | |||
| 258 | sub eval { | ||
| 259 | my $self = shift; | ||
| 260 | _eval($self->c, @_); | ||
| 261 | }; | ||
| 262 | |||
| 263 | sub _eval { | ||
| 264 | my ($c, $code, $source) = @_; | ||
| 265 | local $@ = undef; | ||
| 266 | my $rv = $c->eval("try {$code} catch(e) {throw e.stack}", $source || ()); | ||
| 267 | if (!defined $rv && $@) { | ||
| 268 | # warn "# eval error((($@)))"; | ||
| 269 | my $exception = JavaScript::V8::CommonJS::Exception->new_from_string($@); | ||
| 270 | my $ourStackCall = $exception->stack->[-1]; | ||
| 271 | $ourStackCall->{column} = $ourStackCall->{column} - 5 if ref $ourStackCall eq 'HASH'; # subtract "try {" length | ||
| 272 | die $exception; | ||
| 273 | } | ||
| 274 | $rv; | ||
| 275 | }; | ||
| 276 | |||
| 277 | |||
| 278 | |||
| 279 | sub _slurp { | ||
| 280 | my $path = shift; | ||
| 281 | |||
| 282 | CORE::open my $file, '<:encoding(UTF-8)', $path or croak qq{Can't open file "$path": $!}; | ||
| 283 | my $ret = my $content = ''; | ||
| 284 | while ($ret = $file->sysread(my $buffer, 131072, 0)) { $content .= $buffer } | ||
| 285 | croak qq{Can't read from file "$path": $!} unless defined $ret; | ||
| 286 | |||
| 287 | return $content; | ||
| 288 | } | ||
| 289 | |||
| 290 | 1; | ||
| 291 | __END__ | ||