Skip to content

Commit

Permalink
WIP sourcemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
alehander92 committed Apr 2, 2020
1 parent 7d17cd3 commit 997a700
Show file tree
Hide file tree
Showing 4 changed files with 423 additions and 10 deletions.
4 changes: 4 additions & 0 deletions compiler/commands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,10 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
processOnOffSwitchG(conf, {optPanics}, arg, pass, info)
if optPanics in conf.globalOptions:
defineSymbol(conf.symbols, "nimPanics")
of "sourcemap":
conf.globalOptions.incl optSourcemap
conf.options.incl optLineDir
# processOnOffSwitchG(conf, {optSourcemap, opt}, arg, pass, info)
of "": # comes from "-" in for example: `nim c -r -` (gets stripped from -)
handleStdinInput(conf)
else:
Expand Down
43 changes: 33 additions & 10 deletions compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import
nversion, msgs, idents, types, tables,
ropes, math, passes, ccgutils, wordrecg, renderer,
intsets, cgmeth, lowerings, sighashes, modulegraphs, lineinfos, rodutils,
transf, injectdestructors
transf, injectdestructors, sourcemap, json, sets


from modulegraphs import ModuleGraph, PPassContext
Expand Down Expand Up @@ -205,6 +205,8 @@ proc mapType(typ: PType): TJSTypeKind =
proc mapType(p: PProc; typ: PType): TJSTypeKind =
result = mapType(typ)

var mangled = initSet[string]()

proc mangleName(m: BModule, s: PSym): Rope =
proc validJsName(name: string): bool =
result = true
Expand Down Expand Up @@ -259,6 +261,11 @@ proc mangleName(m: BModule, s: PSym): Rope =
result.add(rope(s.id))
s.loc.r = result

# TODO: optimize
let s = $result
if '_' in s:
mangled.incl(s)

proc escapeJSString(s: string): string =
result = newStringOfCap(s.len + s.len shr 2)
result.add("\"")
Expand Down Expand Up @@ -641,11 +648,16 @@ proc hasFrameInfo(p: PProc): bool =
({optLineTrace, optStackTrace} * p.options == {optLineTrace, optStackTrace}) and
((p.prc == nil) or not (sfPure in p.prc.flags))

proc lineDir(config: ConfigRef, info: TLineInfo, line: int): Rope =
ropes.`%`("// line $2 \"$1\"$n",
[rope(toFullPath(config, info)), rope(line)])

proc genLineDir(p: PProc, n: PNode) =
let line = toLinenumber(n.info)
if optLineDir in p.options:
lineF(p, "// line $2 \"$1\"$n",
[rope(toFilename(p.config, n.info)), rope(line)])
if line < 0:
return
if optLineDir in p.options or optLineDir in p.config.options:
lineF(p, "$1", [lineDir(p.config, n.info, line)])
if hasFrameInfo(p):
lineF(p, "F.line = $1;$n", [rope(line)])

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

p.nested: genStmt(p, transformedBody)


if optLineDir in p.config.options:
result = lineDir(p.config, prc.info, toLinenumber(prc.info))

var def: Rope
if not prc.constraint.isNil:
def = runtimeFormat(prc.constraint.strVal & " {$n$#$#$#$#$#",
Expand All @@ -2253,7 +2269,8 @@ proc genProc(oldProc: PProc, prc: PSym): Rope =
optionalLine(genProcBody(p, prc)),
optionalLine(p.indentLine(returnStmt))])
else:
result = ~"\L"
# if optLineDir in p.config.options:
# result.add(~"\L")

if p.config.hcrOn:
# Here, we introduce thunks that create the equivalent of a jump table
Expand Down Expand Up @@ -2336,6 +2353,7 @@ proc gen(p: PProc, n: PNode, r: var TCompRes) =
if r.kind != resCallee: r.kind = resNone
#r.address = nil
r.res = nil

case n.kind
of nkSym:
genSym(p, n, r)
Expand Down Expand Up @@ -2382,14 +2400,15 @@ proc gen(p: PProc, n: PNode, r: var TCompRes) =
else: r.res = rope(f.toStrMaxPrecision)
r.kind = resExpr
of nkCallKinds:
if isEmptyType(n.typ): genLineDir(p, n)
if isEmptyType(n.typ):
genLineDir(p, n)
if (n[0].kind == nkSym) and (n[0].sym.magic != mNone):
genMagic(p, n, r)
elif n[0].kind == nkSym and sfInfixCall in n[0].sym.flags and
n.len >= 1:
genInfixCall(p, n, r)
else:
genCall(p, n, r)
genCall(p, n, r)
of nkClosure: gen(p, n[0], r)
of nkCurly: genSetConstr(p, n, r)
of nkBracket: genArrayConstr(p, n, r)
Expand Down Expand Up @@ -2585,10 +2604,14 @@ proc myClose(graph: ModuleGraph; b: PPassContext, n: PNode): PNode =
n.add destructorCall
if passes.skipCodegen(m.config, n): return n
if sfMainModule in m.module.flags:
let code = wholeCode(graph, m)
var code = genHeader() & wholeCode(graph, m)
let outFile = m.config.prepareToWriteOutput()
discard writeRopeIfNotEqual(genHeader() & code, outFile)

if optSourcemap in m.config.globalOptions:
var map: SourceMap
(code, map) = genSourceMap($(code), mangled, outFile.string)
writeFile(outFile.string & ".map", $(%map))
discard writeRopeIfNotEqual(code, outFile)

proc myOpen(graph: ModuleGraph; s: PSym): PPassContext =
result = newModule(graph, s)

Expand Down
2 changes: 2 additions & 0 deletions compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type # please make sure we have under 32 options
optOldAst,
optSinkInference # 'sink T' inference


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

TGlobalOptions* = set[TGlobalOption]

Expand Down
Loading

0 comments on commit 997a700

Please sign in to comment.