Skip to content

Commit fc61fa2

Browse files
committed
nicer function names in stack traces
1 parent d0f4875 commit fc61fa2

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

compiler/compiler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,15 @@ func WritePkgCode(pkg *Archive, minify bool, w *SourceMapFilter) error {
202202
}
203203
}
204204

205-
if _, err := w.Write(removeWhitespace([]byte("\t$init = $pkg.$init = function() {\n\t\t$pkg.$init = function() {};\n\t\t/* */ var $f, $c = false, $s = 0, $r; if (this !== undefined && this.$blk !== undefined) { $f = this; $c = true; $s = $f.$s; $r = $f.$r; } s: while (true) { switch ($s) { case 0:\n"), minify)); err != nil {
205+
if _, err := w.Write(removeWhitespace([]byte("\t$init = function() {\n\t\t$pkg.$init = function() {};\n\t\t/* */ var $f, $c = false, $s = 0, $r; if (this !== undefined && this.$blk !== undefined) { $f = this; $c = true; $s = $f.$s; $r = $f.$r; } s: while (true) { switch ($s) { case 0:\n"), minify)); err != nil {
206206
return err
207207
}
208208
for _, d := range filteredDecls {
209209
if _, err := w.Write(d.InitCode); err != nil {
210210
return err
211211
}
212212
}
213-
if _, err := w.Write(removeWhitespace([]byte("\t\t/* */ } return; } if ($f === undefined) { $f = { $blk: $init }; } $f.$s = $s; $f.$r = $r; return $f;\n\t};\n\treturn $pkg;\n})();"), minify)); err != nil {
213+
if _, err := w.Write(removeWhitespace([]byte("\t\t/* */ } return; } if ($f === undefined) { $f = { $blk: $init }; } $f.$s = $s; $f.$r = $r; return $f;\n\t};\n\t$pkg.$init = $init;\n\treturn $pkg;\n})();"), minify)); err != nil {
214214
return err
215215
}
216216
if _, err := w.Write([]byte("\n")); err != nil { // keep this \n even when minified

compiler/package.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,9 @@ func (c *funcContext) translateToplevelFunction(fun *ast.FuncDecl, info *analysi
532532
}
533533

534534
var joinedParams string
535-
primaryFunction := func(lhs string) []byte {
535+
primaryFunction := func(funcRef string) []byte {
536536
if fun.Body == nil {
537-
return []byte(fmt.Sprintf("\t%s = function() {\n\t\t$panic(\"Native function not implemented: %s\");\n\t};\n", lhs, o.FullName()))
537+
return []byte(fmt.Sprintf("\t%s = function() {\n\t\t$panic(\"Native function not implemented: %s\");\n\t};\n", funcRef, o.FullName()))
538538
}
539539

540540
var initStmts []ast.Stmt
@@ -561,17 +561,20 @@ func (c *funcContext) translateToplevelFunction(fun *ast.FuncDecl, info *analysi
561561
},
562562
}, initStmts...)
563563
}
564-
params, fun := translateFunction(fun.Type, initStmts, fun.Body, c, sig, info, fun.Name.Name)
564+
params, fun := translateFunction(fun.Type, initStmts, fun.Body, c, sig, info, funcRef)
565565
joinedParams = strings.Join(params, ", ")
566-
return []byte(fmt.Sprintf("\t%s = %s;\n", lhs, fun))
566+
return []byte(fmt.Sprintf("\t%s = %s;\n", funcRef, fun))
567567
}
568568

569+
code := bytes.NewBuffer(nil)
570+
569571
if fun.Recv == nil {
570-
lhs := c.objectName(o)
572+
funcRef := c.objectName(o)
573+
code.Write(primaryFunction(funcRef))
571574
if fun.Name.IsExported() {
572-
lhs += " = $pkg." + fun.Name.Name
575+
fmt.Fprintf(code, "\t$pkg.%s = %s;\n", fun.Name.Name, funcRef)
573576
}
574-
return primaryFunction(lhs)
577+
return code.Bytes()
575578
}
576579

577580
recvType := sig.Recv().Type()
@@ -586,8 +589,6 @@ func (c *funcContext) translateToplevelFunction(fun *ast.FuncDecl, info *analysi
586589
funName += "$"
587590
}
588591

589-
code := bytes.NewBuffer(nil)
590-
591592
if _, isStruct := namedRecvType.Underlying().(*types.Struct); isStruct {
592593
code.Write(primaryFunction(typeName + ".ptr.prototype." + funName))
593594
fmt.Fprintf(code, "\t%s.prototype.%s = function(%s) { return this.$val.%s(%s); };\n", typeName, funName, joinedParams, funName, joinedParams)
@@ -612,7 +613,7 @@ func (c *funcContext) translateToplevelFunction(fun *ast.FuncDecl, info *analysi
612613
return code.Bytes()
613614
}
614615

615-
func translateFunction(typ *ast.FuncType, initStmts []ast.Stmt, body *ast.BlockStmt, outerContext *funcContext, sig *types.Signature, info *analysis.FuncInfo, name string) ([]string, string) {
616+
func translateFunction(typ *ast.FuncType, initStmts []ast.Stmt, body *ast.BlockStmt, outerContext *funcContext, sig *types.Signature, info *analysis.FuncInfo, funcRef string) ([]string, string) {
616617
c := &funcContext{
617618
FuncInfo: info,
618619
p: outerContext.p,
@@ -684,18 +685,17 @@ func translateFunction(typ *ast.FuncType, initStmts []ast.Stmt, body *ast.BlockS
684685

685686
if len(c.Blocking) != 0 {
686687
c.localVars = append(c.localVars, "$r")
687-
b := "$b"
688-
if name != "" && !c.p.minify {
689-
b = "$blocking_" + name
688+
if funcRef == "" {
689+
funcRef = "$b"
690+
functionName = " $b"
690691
}
691-
functionName = " " + b
692692
var stores, loads string
693693
for _, v := range c.localVars {
694694
loads += fmt.Sprintf("%s = $f.%s; ", v, v)
695695
stores += fmt.Sprintf("$f.%s = %s; ", v, v)
696696
}
697697
prefix = prefix + " var $f, $c = false; if (this !== undefined && this.$blk !== undefined) { $f = this; $c = true; " + loads + "}"
698-
suffix = " if ($f === undefined) { $f = { $blk: " + b + " }; } " + stores + "return $f;" + suffix
698+
suffix = " if ($f === undefined) { $f = { $blk: " + funcRef + " }; } " + stores + "return $f;" + suffix
699699
}
700700

701701
if c.HasDefer {

compiler/prelude/goroutines.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,40 +114,40 @@ var $curGoroutine = $dummyGoroutine, $totalGoroutines = 0, $awakeGoroutines = 0,
114114
var $go = function(fun, args, direct) {
115115
$totalGoroutines++;
116116
$awakeGoroutines++;
117-
var goroutine = function() {
117+
var $goroutine = function() {
118118
var rescheduled = false;
119119
try {
120-
$curGoroutine = goroutine;
120+
$curGoroutine = $goroutine;
121121
var r = fun.apply(undefined, args);
122122
if (r && r.$blk !== undefined) {
123123
fun = function() { r.$blk(); };
124124
args = [];
125125
rescheduled = true;
126126
return;
127127
}
128-
goroutine.exit = true;
128+
$goroutine.exit = true;
129129
} catch (err) {
130-
goroutine.exit = true;
130+
$goroutine.exit = true;
131131
throw err;
132132
} finally {
133133
$curGoroutine = $dummyGoroutine;
134-
if (goroutine.exit && !rescheduled) { /* also set by runtime.Goexit() */
134+
if ($goroutine.exit && !rescheduled) { /* also set by runtime.Goexit() */
135135
$totalGoroutines--;
136-
goroutine.asleep = true;
136+
$goroutine.asleep = true;
137137
}
138-
if (goroutine.asleep && !rescheduled) {
138+
if ($goroutine.asleep && !rescheduled) {
139139
$awakeGoroutines--;
140140
if ($awakeGoroutines === 0 && $totalGoroutines !== 0 && $checkForDeadlock) {
141141
console.error("fatal error: all goroutines are asleep - deadlock!");
142142
}
143143
}
144144
}
145145
};
146-
goroutine.asleep = false;
147-
goroutine.exit = false;
148-
goroutine.deferStack = [];
149-
goroutine.panicStack = [];
150-
$schedule(goroutine, direct);
146+
$goroutine.asleep = false;
147+
$goroutine.exit = false;
148+
$goroutine.deferStack = [];
149+
$goroutine.panicStack = [];
150+
$schedule($goroutine, direct);
151151
};
152152
153153
var $scheduled = [], $schedulerLoopActive = false;

0 commit comments

Comments
 (0)