Skip to content

Commit ab917e0

Browse files
committed
include natives in gopherjs binary via virtual file system
1 parent f627518 commit ab917e0

File tree

40 files changed

+786
-2
lines changed

40 files changed

+786
-2
lines changed

build/build.go

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@ import (
88
"go/scanner"
99
"go/token"
1010
"go/types"
11+
"io"
1112
"io/ioutil"
1213
"os"
1314
"os/exec"
15+
"path"
1416
"path/filepath"
1517
"strconv"
1618
"strings"
1719
"time"
1820

1921
"github.com/fsnotify/fsnotify"
2022
"github.com/gopherjs/gopherjs/compiler"
23+
"github.com/gopherjs/gopherjs/compiler/natives"
2124
"github.com/kardianos/osext"
2225
"github.com/neelance/sourcemap"
2326
)
@@ -153,7 +156,48 @@ func parse(pkg *build.Package, isTest bool, fileSet *token.FileSet) ([]*ast.File
153156
if isXTest {
154157
importPath = importPath[:len(importPath)-5]
155158
}
156-
if nativesPkg, err := Import("github.com/gopherjs/gopherjs/compiler/natives/"+importPath, 0, "", nil); err == nil {
159+
160+
nativesContext := &build.Context{
161+
GOROOT: "/",
162+
GOOS: build.Default.GOOS,
163+
GOARCH: "js",
164+
Compiler: "gc",
165+
JoinPath: path.Join,
166+
SplitPathList: func(list string) []string {
167+
if list == "" {
168+
return nil
169+
}
170+
return strings.Split(list, "/")
171+
},
172+
IsAbsPath: path.IsAbs,
173+
IsDir: func(name string) bool {
174+
dir, err := natives.FS.Open(name)
175+
if err != nil {
176+
return false
177+
}
178+
defer dir.Close()
179+
info, err := dir.Stat()
180+
if err != nil {
181+
return false
182+
}
183+
return info.IsDir()
184+
},
185+
HasSubdir: func(root, name string) (rel string, ok bool) {
186+
panic("not implemented")
187+
},
188+
ReadDir: func(name string) (fi []os.FileInfo, err error) {
189+
dir, err := natives.FS.Open(name)
190+
if err != nil {
191+
return nil, err
192+
}
193+
defer dir.Close()
194+
return dir.Readdir(0)
195+
},
196+
OpenFile: func(name string) (r io.ReadCloser, err error) {
197+
return natives.FS.Open(name)
198+
},
199+
}
200+
if nativesPkg, err := nativesContext.Import(importPath, "", 0); err == nil {
157201
names := nativesPkg.GoFiles
158202
if isTest {
159203
names = append(names, nativesPkg.TestGoFiles...)
@@ -162,10 +206,16 @@ func parse(pkg *build.Package, isTest bool, fileSet *token.FileSet) ([]*ast.File
162206
names = nativesPkg.XTestGoFiles
163207
}
164208
for _, name := range names {
165-
file, err := parser.ParseFile(fileSet, filepath.Join(nativesPkg.Dir, name), nil, parser.ParseComments)
209+
fullPath := path.Join(nativesPkg.Dir, name)
210+
r, err := nativesContext.OpenFile(fullPath)
211+
if err != nil {
212+
panic(err)
213+
}
214+
file, err := parser.ParseFile(fileSet, fullPath, r, parser.ParseComments)
166215
if err != nil {
167216
panic(err)
168217
}
218+
r.Close()
169219
for _, decl := range file.Decls {
170220
switch d := decl.(type) {
171221
case *ast.FuncDecl:

compiler/natives/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Package natives provides native packages via a virtual filesystem.
2+
package natives
3+
4+
//go:generate vfsgendev -source="github.com/gopherjs/gopherjs/compiler/natives".FS

compiler/natives/fs.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// +build dev
2+
3+
package natives
4+
5+
import (
6+
"go/build"
7+
"log"
8+
"net/http"
9+
"os"
10+
"strings"
11+
12+
"github.com/shurcooL/httpfs/filter"
13+
)
14+
15+
func importPathToDir(importPath string) string {
16+
p, err := build.Import(importPath, "", build.FindOnly)
17+
if err != nil {
18+
log.Fatalln(err)
19+
}
20+
return p.Dir
21+
}
22+
23+
// FS is a virtual filesystem that contains native packages.
24+
var FS = filter.New(
25+
http.Dir(importPathToDir("github.com/gopherjs/gopherjs/compiler/natives")),
26+
func(path string, fi os.FileInfo) bool {
27+
return path != "/" && path != "/src" && !strings.HasPrefix(path, "/src/")
28+
},
29+
)

compiler/natives/fs_vfsdata.go

Lines changed: 701 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)