Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
compiler: break functions related to Decls out of Compile().
For each package-level entity we emit one or more Decl struct, which
contains all JS code fragments and metadata required to produce the
final executable script.

For each decl type (imports, vars, functions and types) I created a
separate function that contains the logic responsible for its creation
(and some auxiliary functions). The main objective is to keep the
Compile() function very high-level and clearly reflecting various
compilation stages we go through.

I tried to add comments to make the code more accessible for future
contributors (and future self...), although there are still some aspects
I don't fully grasp.

Ideally, we would have tests for all these new functions, but that's way
more work than I'm able to take on right now.

(based on commit 146735d)
  • Loading branch information
nevkontakte committed May 25, 2024
commit 57eb88648139e678fd6565e4a9155b346d5b3304
13 changes: 13 additions & 0 deletions compiler/analysis/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,26 @@ func (info *Info) newFuncInfo(n ast.Node) *FuncInfo {
return funcInfo
}

// IsBlocking returns true if the function may contain blocking calls or operations.
func (info *Info) IsBlocking(fun *types.Func) bool {
if funInfo := info.FuncDeclInfos[fun]; funInfo != nil {
return len(funInfo.Blocking) > 0
}
panic(fmt.Errorf(`info did not have function declaration for %s`, fun.FullName()))
}

// VarsWithInitializers returns a set of package-level variables that have
// explicit initializers.
func (info *Info) VarsWithInitializers() map[*types.Var]bool {
result := map[*types.Var]bool{}
for _, init := range info.InitOrder {
for _, o := range init.Lhs {
result[o] = true
}
}
return result
}

func AnalyzePkg(files []*ast.File, fileSet *token.FileSet, typesInfo *types.Info, typesPkg *types.Package, isBlocking func(*types.Func) bool) *Info {
info := &Info{
Info: typesInfo,
Expand Down
51 changes: 0 additions & 51 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"strings"
"time"

"github.com/gopherjs/gopherjs/compiler/internal/symbol"
"github.com/gopherjs/gopherjs/compiler/prelude"
"golang.org/x/tools/go/gcexportdata"
)
Expand Down Expand Up @@ -85,56 +84,6 @@ func (a *Archive) RegisterTypes(packages map[string]*types.Package) error {
return err
}

// Decl represents a package-level symbol (e.g. a function, variable or type).
//
// It contains code generated by the compiler for this specific symbol, which is
// grouped by the execution stage it belongs to in the JavaScript runtime.
type Decl struct {
// The package- or receiver-type-qualified name of function or method obj.
// See go/types.Func.FullName().
FullName string
// A logical equivalent of a symbol name in an object file in the traditional
// Go compiler/linker toolchain. Used by GopherJS to support go:linkname
// directives. Must be set for decls that are supported by go:linkname
// implementation.
LinkingName symbol.Name
// A list of package-level JavaScript variable names this symbol needs to declare.
Vars []string
// A JS expression by which the object represented by this decl may be
// referenced within the package context. Empty if the decl represents no such
// object.
RefExpr string
// NamedRecvType is method named recv declare.
NamedRecvType string
// JavaScript code that declares basic information about a symbol. For a type
// it configures basic information about the type and its identity. For a function
// or method it contains its compiled body.
DeclCode []byte
// JavaScript code that initializes reflection metadata about type's method list.
MethodListCode []byte
// JavaScript code that initializes the rest of reflection metadata about a type
// (e.g. struct fields, array type sizes, element types, etc.).
TypeInitCode []byte
// JavaScript code that needs to be executed during the package init phase to
// set the symbol up (e.g. initialize package-level variable value).
InitCode []byte
// Symbol's identifier used by the dead-code elimination logic, not including
// package path. If empty, the symbol is assumed to be alive and will not be
// eliminated. For methods it is the same as its receiver type identifier.
DceObjectFilter string
// The second part of the identified used by dead-code elimination for methods.
// Empty for other types of symbols.
DceMethodFilter string
// List of fully qualified (including package path) DCE symbol identifiers the
// symbol depends on for dead code elimination purposes.
DceDeps []string
// Set to true if a function performs a blocking operation (I/O or
// synchronization). The compiler will have to generate function code such
// that it can be resumed after a blocking operation completes without
// blocking the main thread in the meantime.
Blocking bool
}

type Dependency struct {
Pkg string
Type string
Expand Down
Loading