Skip to content

Commit 66bf593

Browse files
committed
WIP sourcemaps
1 parent fd20213 commit 66bf593

File tree

4 files changed

+423
-8
lines changed

4 files changed

+423
-8
lines changed

compiler/commands.nim

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ proc testCompileOption*(conf: ConfigRef; switch: string, info: TLineInfo): bool
312312
of "patterns", "trmacros": result = contains(conf.options, optTrMacros)
313313
of "excessivestacktrace": result = contains(conf.globalOptions, optExcessiveStackTrace)
314314
of "nilseqs": result = contains(conf.options, optNilSeqs)
315-
of "oldast": result = contains(conf.options, optOldAst)
315+
of "sourcemap": result = contains(conf.globalOptions, optSourcemap)
316316
else: invalidCmdLineOption(conf, passCmd1, switch, info)
317317

318318
proc processPath(conf: ConfigRef; path: string, info: TLineInfo,
@@ -890,6 +890,10 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
890890
processOnOffSwitchG(conf, {optPanics}, arg, pass, info)
891891
if optPanics in conf.globalOptions:
892892
defineSymbol(conf.symbols, "nimPanics")
893+
of "sourcemap":
894+
conf.globalOptions.incl optSourcemap
895+
conf.options.incl optLineDir
896+
# processOnOffSwitchG(conf, {optSourcemap, opt}, arg, pass, info)
893897
of "": # comes from "-" in for example: `nim c -r -` (gets stripped from -)
894898
handleStdinInput(conf)
895899
else:

compiler/jsgen.nim

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import
3333
nversion, msgs, idents, types, tables,
3434
ropes, math, passes, ccgutils, wordrecg, renderer,
3535
intsets, cgmeth, lowerings, sighashes, modulegraphs, lineinfos, rodutils,
36-
transf, injectdestructors
36+
transf, injectdestructors, sourcemap, json, sets
3737

3838

3939
from modulegraphs import ModuleGraph, PPassContext
@@ -205,6 +205,8 @@ proc mapType(typ: PType): TJSTypeKind =
205205
proc mapType(p: PProc; typ: PType): TJSTypeKind =
206206
result = mapType(typ)
207207

208+
var mangled = initSet[string]()
209+
208210
proc mangleName(m: BModule, s: PSym): Rope =
209211
proc validJsName(name: string): bool =
210212
result = true
@@ -259,6 +261,11 @@ proc mangleName(m: BModule, s: PSym): Rope =
259261
result.add(rope(s.id))
260262
s.loc.r = result
261263

264+
# TODO: optimize
265+
let s = $result
266+
if '_' in s:
267+
mangled.incl(s)
268+
262269
proc escapeJSString(s: string): string =
263270
result = newStringOfCap(s.len + s.len shr 2)
264271
result.add("\"")
@@ -641,11 +648,16 @@ proc hasFrameInfo(p: PProc): bool =
641648
({optLineTrace, optStackTrace} * p.options == {optLineTrace, optStackTrace}) and
642649
((p.prc == nil) or not (sfPure in p.prc.flags))
643650

651+
proc lineDir(config: ConfigRef, info: TLineInfo, line: int): Rope =
652+
ropes.`%`("// line $2 \"$1\"$n",
653+
[rope(toFullPath(config, info)), rope(line)])
654+
644655
proc genLineDir(p: PProc, n: PNode) =
645656
let line = toLinenumber(n.info)
657+
if line < 0:
658+
return
646659
if optLineDir in p.options:
647-
lineF(p, "// line $2 \"$1\"$n",
648-
[rope(toFilename(p.config, n.info)), rope(line)])
660+
lineF(p, "$1", [lineDir(p.config, n.info, line)])
649661
if hasFrameInfo(p):
650662
lineF(p, "F.line = $1;$n", [rope(line)])
651663

@@ -2241,6 +2253,10 @@ proc genProc(oldProc: PProc, prc: PSym): Rope =
22412253

22422254
p.nested: genStmt(p, transformedBody)
22432255

2256+
2257+
if optLineDir in p.config.options:
2258+
result = lineDir(p.config, prc.info, toLinenumber(prc.info))
2259+
22442260
var def: Rope
22452261
if not prc.constraint.isNil:
22462262
def = runtimeFormat(prc.constraint.strVal & " {$n$#$#$#$#$#",
@@ -2253,7 +2269,8 @@ proc genProc(oldProc: PProc, prc: PSym): Rope =
22532269
optionalLine(genProcBody(p, prc)),
22542270
optionalLine(p.indentLine(returnStmt))])
22552271
else:
2256-
result = ~"\L"
2272+
if optLineDir in p.config.options:
2273+
result = ~"\L"
22572274

22582275
if p.config.hcrOn:
22592276
# Here, we introduce thunks that create the equivalent of a jump table
@@ -2585,10 +2602,15 @@ proc myClose(graph: ModuleGraph; b: PPassContext, n: PNode): PNode =
25852602
n.add destructorCall
25862603
if passes.skipCodegen(m.config, n): return n
25872604
if sfMainModule in m.module.flags:
2588-
let code = wholeCode(graph, m)
2605+
var code = wholeCode(graph, m)
25892606
let outFile = m.config.prepareToWriteOutput()
2590-
discard writeRopeIfNotEqual(genHeader() & code, outFile)
2591-
2607+
if optSourcemap in m.config.globalOptions:
2608+
var map: SourceMap
2609+
# TODO why do we pass file ""
2610+
(code, map) = genSourceMap($(genHeader() & code), mangled, "", outFile.string)
2611+
writeFile(outFile.string & ".map", $(%map))
2612+
discard writeRopeIfNotEqual(code, outFile)
2613+
25922614
proc myOpen(graph: ModuleGraph; s: PSym): PPassContext =
25932615
result = newModule(graph, s)
25942616

compiler/options.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type # please make sure we have under 32 options
4444
optOldAst,
4545
optSinkInference # 'sink T' inference
4646

47+
4748
TOptions* = set[TOption]
4849
TGlobalOption* = enum # **keep binary compatible**
4950
gloptNone, optForceFullMake,
@@ -94,6 +95,7 @@ type # please make sure we have under 32 options
9495
optProduceAsm # produce assembler code
9596
optPanics # turn panics (sysFatal) into a process termination
9697
optNimV1Emulation # emulate Nim v1.0
98+
optSourcemap
9799

98100
TGlobalOptions* = set[TGlobalOption]
99101

0 commit comments

Comments
 (0)