MCPcopy
hub / github.com/tinygo-org/tinygo / Load

Function Load

loader/loader.go:107–266  ·  view source on GitHub ↗

Load loads the given package with all dependencies (including the runtime package). Call .Parse() afterwards to parse all Go files (including CGo processing, if necessary).

(config *compileopts.Config, inputPkg string, typeChecker types.Config)

Source from the content-addressed store, hash-verified

105// package). Call .Parse() afterwards to parse all Go files (including CGo
106// processing, if necessary).
107func Load(config *compileopts.Config, inputPkg string, typeChecker types.Config) (*Program, error) {
108 goroot, err := GetCachedGoroot(config)
109 if err != nil {
110 return nil, err
111 }
112 var wd string
113 if config.Options.Directory != "" {
114 wd = config.Options.Directory
115 } else {
116 wd, err = os.Getwd()
117 if err != nil {
118 return nil, err
119 }
120 }
121 p := &Program{
122 config: config,
123 typeChecker: typeChecker,
124 goroot: goroot,
125 workingDir: wd,
126 Packages: make(map[string]*Package),
127 fset: token.NewFileSet(),
128 }
129
130 // List the dependencies of this package, in raw JSON format.
131 extraArgs := []string{"-json", "-deps", "-e"}
132 if config.TestConfig.CompileTestBinary {
133 extraArgs = append(extraArgs, "-test")
134 }
135 cmd, err := List(config, extraArgs, []string{inputPkg})
136 if err != nil {
137 return nil, err
138 }
139 buf := &bytes.Buffer{}
140 cmd.Stdout = buf
141 cmd.Stderr = os.Stderr
142 err = cmd.Run()
143 if err != nil {
144 if exitErr, ok := err.(*exec.ExitError); ok {
145 os.Exit(exitErr.ExitCode())
146 }
147 return nil, fmt.Errorf("failed to run `go list`: %s", err)
148 }
149
150 // Parse the returned json from `go list`.
151 decoder := json.NewDecoder(buf)
152 var pkgErrors []error
153 for {
154 pkg := &Package{
155 program: p,
156 FileHashes: make(map[string][]byte),
157 EmbedGlobals: make(map[string][]*EmbedFile),
158 info: types.Info{
159 Types: make(map[ast.Expr]types.TypeAndValue),
160 Instances: make(map[*ast.Ident]types.Instance),
161 Defs: make(map[*ast.Ident]types.Object),
162 Uses: make(map[*ast.Ident]types.Object),
163 Implicits: make(map[ast.Node]types.Object),
164 Scopes: make(map[ast.Node]*types.Scope),

Callers 3

testCompilePackageFunction · 0.92
BuildFunction · 0.92
compileGoFileForTestingFunction · 0.92

Calls 6

getOriginalPathMethod · 0.95
GetCachedGorootFunction · 0.85
ExitCodeMethod · 0.80
ListFunction · 0.70
ErrorfMethod · 0.65
RunMethod · 0.45

Tested by 2

testCompilePackageFunction · 0.74
compileGoFileForTestingFunction · 0.74