@@ -20,8 +20,7 @@ import (
2020const pseudoFileName = `prog.go`
2121
2222type runner struct {
23- cache * packageCache
24- output common.Output
23+ cache * packageCache
2524}
2625
2726func New (fetcher common.Fetcher ) common.Runner {
@@ -30,24 +29,9 @@ func New(fetcher common.Fetcher) common.Runner {
3029 }
3130}
3231
33- // SetOutput sets the output to write errors to.
34- func (r * runner ) SetOutput (output common.Output ) {
35- r .output = output
36- }
37-
38- func (r * runner ) writeError (err error ) {
39- if r .output != nil {
40- r .output .AddError (err )
41- } else {
42- fmt .Printf ("Runner error: %v\n " , err )
43- }
44- }
45-
46- // Preload asynchronously starts loading standard library packages needed
47- // to run the given code.
48- func (r * runner ) Preload (code string ) {
32+ func (r * runner ) Preload (goCode string ) {
4933 fileSet := token .NewFileSet ()
50- file , err := parser .ParseFile (fileSet , pseudoFileName , code , parser .ImportsOnly )
34+ file , err := parser .ParseFile (fileSet , pseudoFileName , goCode , parser .ImportsOnly )
5135 if err != nil {
5236 // Ignore errors here. They will be reported during actual compilation.
5337 return
@@ -74,15 +58,15 @@ func (r *runner) preloadImports(srcs *sources.Sources) {
7458 }
7559}
7660
77- // Run compiles and runs the given code.
78- // If this is called again while a previous Run is still in progress,
79- // the previous Run is cancelled.
80- func (r * runner ) Run (code string ) {
61+ func (r * runner ) Compile (goCode string , then func (string , error )) {
62+ go func () { then (r .syncCompile (goCode )) }()
63+ }
64+
65+ func (r * runner ) syncCompile (goCode string ) (string , error ) {
8166 fileSet := token .NewFileSet ()
82- file , err := parser .ParseFile (fileSet , pseudoFileName , code , parser .ParseComments )
67+ file , err := parser .ParseFile (fileSet , pseudoFileName , goCode , parser .ParseComments )
8368 if err != nil {
84- r .writeError (err )
85- return
69+ return `` , err
8670 }
8771
8872 root := & sources.Sources {
@@ -96,24 +80,24 @@ func (r *runner) Run(code string) {
9680 // and we can synchronously wait for them later during actual compilation.
9781 r .preloadImports (root )
9882
99- allSources , ok := r .collectAllSources (root )
100- if ! ok {
101- return // Errors have already been reported.
83+ allSources , err := r .collectAllSources (root )
84+ if err != nil {
85+ return `` , err
10286 }
10387 archives , err := r .prepareAndCompilePackages (allSources )
10488 if err != nil {
105- r .writeError (err )
106- return
89+ return `` , err
10790 }
10891
10992 jsCode := r .write (archives )
110- r . eval ( jsCode )
93+ return jsCode , nil
11194}
11295
113- func (r * runner ) collectAllSources (root * sources.Sources ) ([]* sources.Sources , bool ) {
96+ func (r * runner ) collectAllSources (root * sources.Sources ) ([]* sources.Sources , error ) {
11497 allSrcs := map [string ]* sources.Sources {}
115- var collect func (srcs * sources.Sources ) bool
116- collect = func (srcs * sources.Sources ) bool {
98+
99+ var collect func (srcs * sources.Sources ) error
100+ collect = func (srcs * sources.Sources ) error {
117101 allSrcs [srcs .ImportPath ] = srcs
118102 for _ , path := range srcs .UnresolvedImports () {
119103 if _ , has := allSrcs [path ]; has {
@@ -123,24 +107,23 @@ func (r *runner) collectAllSources(root *sources.Sources) ([]*sources.Sources, b
123107 srcs , _ , err := r .cache .Load (path )
124108 if err != nil {
125109 // Failed to load an import.
126- r .writeError (fmt .Errorf (`failed to load package %q: %w` , path , err ))
127- return false
110+ return fmt .Errorf (`failed to load package %q: %w` , path , err )
128111 }
129- if ! collect (srcs ) {
130- return false
112+ if err := collect (srcs ); err != nil {
113+ return err
131114 }
132115 }
133- return true
116+ return nil
134117 }
135- if ! collect (root ) {
136- return nil , false
118+ if err := collect (root ); err != nil {
119+ return nil , err
137120 }
138121
139122 sourcesSlice := make ([]* sources.Sources , 0 , len (allSrcs ))
140123 for _ , srcs := range allSrcs {
141124 sourcesSlice = append (sourcesSlice , srcs )
142125 }
143- return sourcesSlice , true
126+ return sourcesSlice , nil
144127}
145128
146129func (r * runner ) sourcesForImport (path , _ string ) (* sources.Sources , error ) {
@@ -180,7 +163,7 @@ func (r *runner) write(allPkgs []*compiler.Archive) string {
180163 return jsCode .String ()
181164}
182165
183- func (r * runner ) eval (jsCode string ) {
166+ func (r * runner ) Run (jsCode string ) {
184167 js .Global .Set ("$checkForDeadlock" , true )
185168 js .Global .Call ("eval" , js .InternalObject (jsCode ))
186169}
0 commit comments