@@ -532,9 +532,9 @@ func (c *funcContext) translateToplevelFunction(fun *ast.FuncDecl, info *analysi
532
532
}
533
533
534
534
var joinedParams string
535
- primaryFunction := func (lhs string ) []byte {
535
+ primaryFunction := func (funcRef string ) []byte {
536
536
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 ()))
538
538
}
539
539
540
540
var initStmts []ast.Stmt
@@ -561,17 +561,20 @@ func (c *funcContext) translateToplevelFunction(fun *ast.FuncDecl, info *analysi
561
561
},
562
562
}, initStmts ... )
563
563
}
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 )
565
565
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 ))
567
567
}
568
568
569
+ code := bytes .NewBuffer (nil )
570
+
569
571
if fun .Recv == nil {
570
- lhs := c .objectName (o )
572
+ funcRef := c .objectName (o )
573
+ code .Write (primaryFunction (funcRef ))
571
574
if fun .Name .IsExported () {
572
- lhs += " = $pkg." + fun .Name .Name
575
+ fmt . Fprintf ( code , " \t $pkg.%s = %s; \n " , fun .Name .Name , funcRef )
573
576
}
574
- return primaryFunction ( lhs )
577
+ return code . Bytes ( )
575
578
}
576
579
577
580
recvType := sig .Recv ().Type ()
@@ -586,8 +589,6 @@ func (c *funcContext) translateToplevelFunction(fun *ast.FuncDecl, info *analysi
586
589
funName += "$"
587
590
}
588
591
589
- code := bytes .NewBuffer (nil )
590
-
591
592
if _ , isStruct := namedRecvType .Underlying ().(* types.Struct ); isStruct {
592
593
code .Write (primaryFunction (typeName + ".ptr.prototype." + funName ))
593
594
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
612
613
return code .Bytes ()
613
614
}
614
615
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 ) {
616
617
c := & funcContext {
617
618
FuncInfo : info ,
618
619
p : outerContext .p ,
@@ -684,18 +685,17 @@ func translateFunction(typ *ast.FuncType, initStmts []ast.Stmt, body *ast.BlockS
684
685
685
686
if len (c .Blocking ) != 0 {
686
687
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"
690
691
}
691
- functionName = " " + b
692
692
var stores , loads string
693
693
for _ , v := range c .localVars {
694
694
loads += fmt .Sprintf ("%s = $f.%s; " , v , v )
695
695
stores += fmt .Sprintf ("$f.%s = %s; " , v , v )
696
696
}
697
697
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
699
699
}
700
700
701
701
if c .HasDefer {
0 commit comments