Skip to content

[WIP] Type initalization ordering in JS #1379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 50 additions & 21 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ import (
"go/token"
"go/types"
"io"
"sort"
"strings"
"time"

"golang.org/x/tools/go/gcexportdata"

"github.com/gopherjs/gopherjs/compiler/internal/dce"
"github.com/gopherjs/gopherjs/compiler/internal/grouper"
"github.com/gopherjs/gopherjs/compiler/linkname"
"github.com/gopherjs/gopherjs/compiler/prelude"
"golang.org/x/tools/go/gcexportdata"
)

var (
Expand Down Expand Up @@ -118,6 +121,8 @@ func WriteProgramCode(pkgs []*Archive, w *SourceMapFilter, goVersion string) err
gls.Add(pkg.GoLinknames)
}

// Perform dead code elimination (DCE) on the declarations
// to get the selection of the declarations that are actually used.
sel := &dce.Selector[*Decl]{}
for _, pkg := range pkgs {
for _, d := range pkg.Declarations {
Expand All @@ -134,6 +139,10 @@ func WriteProgramCode(pkgs []*Archive, w *SourceMapFilter, goVersion string) err
}
dceSelection := sel.AliveDecls()

// Set the Decl.Grouper().Group values for each declaration.
// The group number is used to determine the type initialization order.
groupCount := grouper.Group(dceSelection)

if _, err := w.Write([]byte("\"use strict\";\n(function() {\n\n")); err != nil {
return err
}
Expand All @@ -159,7 +168,7 @@ func WriteProgramCode(pkgs []*Archive, w *SourceMapFilter, goVersion string) err
}
}

if _, err := w.Write([]byte("$synthesizeMethods();\n$initAllLinknames();\nvar $mainPkg = $packages[\"" + string(mainPkg.ImportPath) + "\"];\n$packages[\"runtime\"].$init();\n$go($mainPkg.$init, []);\n$flushConsole();\n\n}).call(this);\n")); err != nil {
if _, err := fmt.Fprintf(w, "$initializeTypes(%d);\n$synthesizeMethods();\n$initAllLinknames();\nvar $mainPkg = $packages[\"%s\"];\n$packages[\"runtime\"].$init();\n$go($mainPkg.$init, []);\n$flushConsole();\n\n}).call(this);\n", groupCount, string(mainPkg.ImportPath)); err != nil {
return err
}
return nil
Expand All @@ -175,43 +184,63 @@ func WritePkgCode(pkg *Archive, dceSelection map[*Decl]struct{}, gls linkname.Go
if _, err := w.Write(removeWhitespace([]byte(fmt.Sprintf("$packages[\"%s\"] = (function() {\n", pkg.ImportPath)), minify)); err != nil {
return err
}

vars := []string{"$pkg = {}", "$init"}
var filteredDecls []*Decl
groupMap := make(map[int][]*Decl)
for _, d := range pkg.Declarations {
if _, ok := dceSelection[d]; ok {
vars = append(vars, d.Vars...)
filteredDecls = append(filteredDecls, d)
group := d.Grouper().Group
groupMap[group] = append(groupMap[group], d)
}
}
if _, err := w.Write(removeWhitespace([]byte(fmt.Sprintf("\tvar %s;\n", strings.Join(vars, ", "))), minify)); err != nil {
return err
}
for _, d := range filteredDecls {
if _, err := w.Write(d.DeclCode); err != nil {

groups := make([]int, 0, len(groupMap))
for group := range groupMap {
groups = append(groups, group)
}
sort.Ints(groups)

for _, group := range groups {
groupDecls := groupMap[group]
if _, err := w.Write(removeWhitespace([]byte(fmt.Sprintf("\t$addTypeInit(%d, this, function() {\n", group)), minify)); err != nil {
return err
}
if gls.IsImplementation(d.LinkingName) {
// This decl is referenced by a go:linkname directive, expose it to external
// callers via $linkname object (declared in prelude). We are not using
// $pkg to avoid clashes with exported symbols.
var code string
if recv, method, ok := d.LinkingName.IsMethod(); ok {
code = fmt.Sprintf("\t$linknames[%q] = $unsafeMethodToFunction(%v,%q,%t);\n", d.LinkingName.String(), d.NamedRecvType, method, strings.HasPrefix(recv, "*"))
} else {
code = fmt.Sprintf("\t$linknames[%q] = %s;\n", d.LinkingName.String(), d.RefExpr)
for _, d := range groupDecls {
if _, err := w.Write(d.DeclCode); err != nil {
return err
}
if _, err := w.Write(removeWhitespace([]byte(code), minify)); err != nil {
if gls.IsImplementation(d.LinkingName) {
// This decl is referenced by a go:linkname directive, expose it to external
// callers via $linkname object (declared in prelude). We are not using
// $pkg to avoid clashes with exported symbols.
var code string
if recv, method, ok := d.LinkingName.IsMethod(); ok {
code = fmt.Sprintf("\t$linknames[%q] = $unsafeMethodToFunction(%v,%q,%t);\n", d.LinkingName.String(), d.NamedRecvType, method, strings.HasPrefix(recv, "*"))
} else {
code = fmt.Sprintf("\t$linknames[%q] = %s;\n", d.LinkingName.String(), d.RefExpr)
}
if _, err := w.Write(removeWhitespace([]byte(code), minify)); err != nil {
return err
}
}
}
for _, d := range groupDecls {
if _, err := w.Write(d.MethodListCode); err != nil {
return err
}
}
}
for _, d := range filteredDecls {
if _, err := w.Write(d.MethodListCode); err != nil {
return err
for _, d := range groupDecls {
if _, err := w.Write(d.TypeInitCode); err != nil {
return err
}
}
}
for _, d := range filteredDecls {
if _, err := w.Write(d.TypeInitCode); err != nil {
if _, err := w.Write([]byte("\t});\n")); err != nil {
return err
}
}
Expand Down
Loading
Loading