Skip to content

Commit

Permalink
Sourcemaps for JS
Browse files Browse the repository at this point in the history
  • Loading branch information
alehander92 committed Sep 25, 2018
1 parent 4b2f003 commit 73073d5
Show file tree
Hide file tree
Showing 4 changed files with 412 additions and 6 deletions.
3 changes: 3 additions & 0 deletions compiler/commands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ proc testCompileOption*(conf: ConfigRef; switch: string, info: TLineInfo): bool
of "patterns": result = contains(conf.options, optPatterns)
of "excessivestacktrace": result = contains(conf.globalOptions, optExcessiveStackTrace)
of "nilseqs": result = contains(conf.options, optNilSeqs)
of "sourcemap": result = contains(conf.globalOptions, optSourceMap)
else: invalidCmdLineOption(conf, passCmd1, switch, info)

proc processPath(conf: ConfigRef; path: string, info: TLineInfo,
Expand Down Expand Up @@ -740,6 +741,8 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
else:
conf.cppCustomNamespace = "Nim"
defineSymbol(conf.symbols, "cppCompileToNamespace", conf.cppCustomNamespace)
of "sourcemap":
incl(conf.globalOptions, optSourceMap)
else:
if strutils.find(switch, '.') >= 0: options.setConfigVar(conf, switch, arg)
else: invalidCmdLineOption(conf, pass, switch, info)
Expand Down
34 changes: 29 additions & 5 deletions compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import
ast, astalgo, strutils, hashes, trees, platform, magicsys, extccomp, options,
nversion, nimsets, msgs, std / sha1, bitsets, idents, types, os, tables,
times, ropes, math, passes, ccgutils, wordrecg, renderer,
intsets, cgmeth, lowerings, sighashes, lineinfos, rodutils, pathutils
intsets, cgmeth, lowerings, sighashes, lineinfos, rodutils, pathutils, sets, sourcemap, json

from modulegraphs import ModuleGraph

Expand Down Expand Up @@ -200,6 +200,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 @@ -254,6 +256,11 @@ proc mangleName(m: BModule, s: PSym): Rope =
add(result, 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 @@ -532,11 +539,17 @@ 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.`%`("$N// line $2 $1$N",
[rope(toFilename(config, info)), rope(line)])

proc genLineDir(p: PProc, n: PNode) =
let line = toLinenumber(n.info)
if line < 0:
return
if optLineDir in p.options:
lineF(p, "// line $2 \"$1\"$n",
[rope(toFilename(p.config, n.info)), rope(line)])
# softRnl = noRnl
line(p, lineDir(p.config, n.info, line))
if {optStackTrace, optEndb} * p.options == {optStackTrace, optEndb} and
((p.prc == nil) or sfPure notin p.prc.flags):
useMagic(p, "endb")
Expand Down Expand Up @@ -1961,6 +1974,10 @@ proc genProc(oldProc: PProc, prc: PSym): Rope =

p.nested: genStmt(p, prc.getBody)


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

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

if optHotCodeReloading in p.config.options:
# Here, we introduce thunks that create the equivalent of a jump table
Expand Down Expand Up @@ -2275,14 +2293,20 @@ proc myClose(graph: ModuleGraph; b: PPassContext, n: PNode): PNode =
let ext = "js"
let f = if globals.classes.len == 0: toFilename(m.config, FileIndex m.module.position)
else: "nimsystem"
let code = wholeCode(graph, m)
var code = wholeCode(graph, m)
let outfile =
if not m.config.outFile.isEmpty:
if m.config.outFile.string.isAbsolute: m.config.outFile
else: AbsoluteFile(getCurrentDir() / m.config.outFile.string)
else:
changeFileExt(completeCFilePath(m.config, AbsoluteFile f), ext)
if optSourceMap in m.config.globalOptions:
var map: SourceMap
(code, map) = genSourceMap($code, mangled, f, outfile.string)
writeFile(outfile.string & ".map", $(%map))

discard writeRopeIfNotEqual(genHeader() & code, outfile)

for obj, content in items(globals.classes):
genClass(m.config, obj, content, ext)

Expand Down
4 changes: 3 additions & 1 deletion compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type # please make sure we have under 32 options
optLaxStrings,
optNilSeqs


TOptions* = set[TOption]
TGlobalOption* = enum # **keep binary compatible**
gloptNone, optForceFullMake,
Expand Down Expand Up @@ -77,7 +78,8 @@ type # please make sure we have under 32 options
optMixedMode # true if some module triggered C++ codegen
optListFullPaths
optNoNimblePath
optDynlibOverrideAll
optDynlibOverrideAll,
optSourcemap

TGlobalOptions* = set[TGlobalOption]

Expand Down
Loading

0 comments on commit 73073d5

Please sign in to comment.