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
3939from modulegraphs import ModuleGraph , PPassContext
@@ -205,6 +205,8 @@ proc mapType(typ: PType): TJSTypeKind =
205205proc mapType (p: PProc ; typ: PType ): TJSTypeKind =
206206 result = mapType (typ)
207207
208+ var mangled = initSet [string ]()
209+
208210proc 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+
262269proc 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+
644655proc 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+
25922614proc myOpen (graph: ModuleGraph ; s: PSym ): PPassContext =
25932615 result = newModule (graph, s)
25942616
0 commit comments