Skip to content

Commit 87535d4

Browse files
committed
[js] Repair source map support and make it accesible from rakudo
1 parent 51b5124 commit 87535d4

File tree

6 files changed

+24
-11
lines changed

6 files changed

+24
-11
lines changed

src/HLL/Compiler.nqp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class HLL::Compiler does HLL::Backend::Default {
2525
# Command options and usage.
2626
@!cmdoptions := nqp::split(' ', 'e=s help|h target=s trace|t=s encoding=s output|o=s source-name=s combine version|v show-config verbose-config|V stagestats=s? ll-exception rxtrace nqpevent=s profile=s? profile-compile=s? profile-filename=s profile-stage=s'
2727
#?if js
28-
~ ' substagestats beautify nqp-runtime=s perl6-runtime=s libpath=s shebang execname=s'
28+
~ ' substagestats beautify nqp-runtime=s perl6-runtime=s libpath=s shebang execname=s source-map'
2929
#?endif
3030
);
3131
%!config := nqp::hash();

src/NQP/Compiler.nqp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ $nqpcomp.addstage('classname', :after<start>);
3737
@clo.push('bootstrap');
3838
#?endif
3939
#?if js
40-
@clo.push('source-map');
4140
@clo.push('source-map-debug');
4241
@clo.push('nyi=s');
4342
#?endif

src/vm/js/Chunk.nqp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,27 @@ class Chunk does Joinable {
5959
}
6060
}
6161

62-
method with_source_map_info() {
62+
method with_source_map_info($hll-compiler) {
6363
my @parts;
6464
if nqp::isnull($!setup) {
6565
}
6666
elsif nqp::istype($!setup, Chunk) {
67-
nqp::push(@parts, $!setup.with_source_map_info);
67+
nqp::push(@parts, $!setup.with_source_map_info($hll-compiler));
6868
}
6969
else {
7070
for $!setup -> $part {
7171
if nqp::isstr($part) {
7272
nqp::push(@parts,quote_string($part, :json));
7373
}
7474
else {
75-
nqp::push(@parts,$part.with_source_map_info);
75+
nqp::push(@parts,$part.with_source_map_info($hll-compiler));
7676
}
7777
}
7878
}
7979
my $parts := '[' ~ nqp::join(',', @parts) ~ ']';
8080
if nqp::defined($!node) && $!node.node {
8181
my $node := $!node.node;
82-
my $location := HLL::Compiler.line_and_column_of($node.orig(), $node.from(), :cache(1));
82+
my $location := $hll-compiler.line_and_column_of($node.orig(), $node.from(), :cache(1));
8383
"\{\"line\": {nqp::atpos_i($location, 0)}, \"column\": {nqp::atpos_i($location, 1)}, \"parts\": $parts\}";
8484
}
8585
else {

src/vm/js/Compiler.nqp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,8 +2014,8 @@ class QAST::CompilerJS does DWIMYNameMangling does SerializeOnce {
20142014
}
20152015

20162016
# return a json datastructure we later process into a source map
2017-
method emit_with_source_map($ast, *%named) {
2018-
self.as_js_with_prelude($ast, |%named).with_source_map_info
2017+
method emit_with_source_map($ast, $hll-compiler, *%named) {
2018+
self.as_js_with_prelude($ast, |%named).with_source_map_info($hll-compiler);
20192019
}
20202020

20212021
method emit_with_source_map_debug($ast, *%named) {

src/vm/js/HLL/Backend.nqp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class JavaScriptBackend {
180180
if %adverbs<source-map-debug> {
181181
$backend.emit_with_source_map_debug($qast, :$instant, :$shebang, :$nqp-runtime);
182182
} elsif %adverbs<source-map> {
183-
$backend.emit_with_source_map($qast, :$instant, :$shebang, :$nqp-runtime);
183+
$backend.emit_with_source_map($qast, HLL::Compiler, :$instant, :$shebang, :$nqp-runtime);
184184
} else {
185185
my $code := $backend.emit($qast, :$instant, :$substagestats, :$shebang, :$nqp-runtime);
186186
$code := self.beautify($code) if %adverbs<beautify>;

src/vm/js/bin/gen_sourcemap.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,24 @@ var p6source = process.argv[3]
66
var outputJs = process.argv[4];
77
var outputSourceMap = process.argv[5];
88

9+
var SourceNode = map.SourceNode;
10+
11+
function toNode(chunk) {
12+
if (chunk.line) {
13+
return new SourceNode(chunk.line, chunk.column-1, p6source , chunk.parts.map(function(c) {return toNode(c)}));
14+
} else if (typeof chunk == 'string') {
15+
return chunk;
16+
} else if (chunk instanceof Array) {
17+
return chunk.map(function(c) {return toNode(c)});
18+
} else {
19+
console.error(chunk);
20+
throw "incorrect chunk";
21+
}
22+
}
23+
924
var data = JSON.parse(fs.readFileSync(input,'utf-8'));
1025

11-
var toNode = require('nqp-loader').toNode;
12-
var node = new SourceNode(null, null, p6source, toNode(data));
26+
var node = new SourceNode(null, null, p6source, ['//# sourceMappingURL=' + outputSourceMap + '\n', toNode(data)]);
1327
var sourceAndMap = node.toStringWithSourceMap({file: outputJs});
1428
fs.writeFileSync(outputJs,sourceAndMap.code);
1529
fs.writeFileSync(outputSourceMap,sourceAndMap.map.toString());

0 commit comments

Comments
 (0)