Skip to content

Support embed package. #1145

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

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
61801ed
compiler/natives/src: add embed
visualfc Sep 9, 2022
b0db6f5
compiler/natives: go generate
visualfc Sep 9, 2022
522585d
build: check and generate embed file for build/test
visualfc Sep 9, 2022
561171d
build: BuildFiles support check embed file for build/run
visualfc Sep 10, 2022
7dd9288
tests/testdata/legacy_syscall: fix go:build
visualfc Sep 10, 2022
2a8f084
goembed v0.2.3
visualfc Sep 11, 2022
092a24b
checkEmbed: check load file error
visualfc Sep 11, 2022
4316b6d
build: move checkEmbed to BuildPackage
visualfc Sep 11, 2022
b4fad18
build: checkEmbed unknown type check error
visualfc Sep 11, 2022
7c483c8
build: embedFiles
visualfc Sep 12, 2022
aec0b57
compiler/natives/src: prealloc embed
visualfc Sep 12, 2022
7cb07f3
build: embedFiles rewrite ast for go:embed vars
visualfc Sep 12, 2022
f4c2418
compiler/natives/src: add embed/internal/embedtest
visualfc Sep 12, 2022
0c19332
build: BuildFiles ctx.ReadDir prealloc
visualfc Sep 12, 2022
625d4cc
build: joinEmbedPatternPos
visualfc Sep 12, 2022
1e0f65a
goembed v0.2.5
visualfc Sep 13, 2022
a80e720
goembed v0.2.7
visualfc Sep 14, 2022
2a6f9de
goembed v0.2.8
visualfc Sep 15, 2022
fd891b2
goembed v0.3.0 go:embed check multiple file error if kind not EmbedFiles
visualfc Sep 15, 2022
ffe4555
goembed v0.3.1
visualfc Sep 16, 2022
c5140d5
build: embedFiles check goembed.EmbedMaybeAlias
visualfc Sep 16, 2022
b612190
x
visualfc Sep 16, 2022
a9d764a
goembed v0.3.2
visualfc Sep 16, 2022
b799041
Merge remote-tracking branch 'upstream/master' into embed
visualfc Sep 19, 2022
8fb8e58
goembed v0.3.3
visualfc Sep 19, 2022
c552a3d
remove compiler/natives/src/embed/internal/embedtest
visualfc Sep 19, 2022
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
Prev Previous commit
Next Next commit
goembed v0.3.3
  • Loading branch information
visualfc committed Sep 19, 2022
commit 8fb8e58bba47ab9105956e4117d19b2591d081b6
14 changes: 0 additions & 14 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,20 +395,6 @@ func (p *PackageData) InternalBuildContext() *build.Context {
return p.bctx
}

func joinEmbedPatternPos(m1, m2 map[string][]token.Position) map[string][]token.Position {
if len(m1) == 0 && len(m2) == 0 {
return nil
}
m := make(map[string][]token.Position)
for k, v := range m1 {
m[k] = v
}
for k, v := range m2 {
m[k] = append(m[k], v...)
}
return m
}

// TestPackage returns a variant of the package with "internal" tests.
func (p *PackageData) TestPackage() *PackageData {
return &PackageData{
Expand Down
42 changes: 28 additions & 14 deletions build/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,44 @@ func __gopherjs_embed_buildFS__(list []struct {
`

// embedFiles generates an additional source file, which initializes all variables in the package with a go:embed directive.
func embedFiles(bp *PackageData, fset *token.FileSet, files []*ast.File) (*ast.File, error) {
if len(bp.EmbedPatternPos) == 0 {
func embedFiles(pkg *PackageData, fset *token.FileSet, files []*ast.File) (*ast.File, error) {
if len(pkg.EmbedPatternPos) == 0 {
return nil, nil
}

ems, err := goembed.CheckEmbed(bp.EmbedPatternPos, fset, files)
ems, err := goembed.CheckEmbed(pkg.EmbedPatternPos, fset, files)
if err != nil {
return nil, err
}

r := goembed.NewResolve()
for _, v := range ems {
fs, err := r.Load(bp.Dir, fset, v)
for _, em := range ems {
fs, err := r.Load(pkg.Dir, fset, em)
if err != nil {
return nil, err
}
switch v.Kind {
switch em.Kind {
case goembed.EmbedMaybeAlias:
// value = Type(data)
// valid alias string or []byte type used by types.check
v.Spec.Values = []ast.Expr{
em.Spec.Values = []ast.Expr{
&ast.CallExpr{
Fun: v.Spec.Type,
Fun: em.Spec.Type,
Args: []ast.Expr{
&ast.Ident{Name: buildIdent(fs[0].Name),
NamePos: v.Spec.Names[0].NamePos},
NamePos: em.Spec.Names[0].NamePos},
},
}}
case goembed.EmbedBytes:
// value = []byte(data)
v.Spec.Values = []ast.Expr{
em.Spec.Values = []ast.Expr{
&ast.CallExpr{
Fun: v.Spec.Type,
Fun: em.Spec.Type,
Args: []ast.Expr{ast.NewIdent(buildIdent(fs[0].Name))},
}}
case goembed.EmbedString:
// value = data
v.Spec.Values = []ast.Expr{ast.NewIdent(buildIdent(fs[0].Name))}
em.Spec.Values = []ast.Expr{ast.NewIdent(buildIdent(fs[0].Name))}
case goembed.EmbedFiles:
// value = __gopherjs_embed_buildFS__([]struct{name string; data string; hash [16]byte}{...})
fs = goembed.BuildFS(fs)
Expand Down Expand Up @@ -138,12 +138,12 @@ func embedFiles(bp *PackageData, fset *token.FileSet, files []*ast.File) (*ast.F
},
},
}
v.Spec.Values = []ast.Expr{call}
em.Spec.Values = []ast.Expr{call}
}
}

var buf bytes.Buffer
fmt.Fprintf(&buf, embed_head, bp.Name)
fmt.Fprintf(&buf, embed_head, pkg.Name)
buf.WriteString("\nconst (\n")
for _, f := range r.Files() {
if len(f.Data) == 0 {
Expand All @@ -159,3 +159,17 @@ func embedFiles(bp *PackageData, fset *token.FileSet, files []*ast.File) (*ast.F
}
return f, nil
}

func joinEmbedPatternPos(m1, m2 map[string][]token.Position) map[string][]token.Position {
if len(m1) == 0 && len(m2) == 0 {
return nil
}
m := make(map[string][]token.Position)
for k, v := range m1 {
m[k] = v
}
for k, v := range m2 {
m[k] = append(m[k], v...)
}
return m
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/visualfc/goembed v0.3.2
github.com/visualfc/goembed v0.3.3
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/visualfc/goembed v0.3.2 h1:a9m6o9VTzNk3mEF98C8cHp8f8P8BblyjsjajXGfTp8w=
github.com/visualfc/goembed v0.3.2/go.mod h1:jCVCz/yTJGyslo6Hta+pYxWWBuq9ADCcIVZBTQ0/iVI=
github.com/visualfc/goembed v0.3.3 h1:pOL02L715tHKsLQVMcZz06tTzRDAHkJKJLRnCA22G9Q=
github.com/visualfc/goembed v0.3.3/go.mod h1:jCVCz/yTJGyslo6Hta+pYxWWBuq9ADCcIVZBTQ0/iVI=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down