Skip to content

Commit d5d4815

Browse files
committed
interpreter/wasm,internal/filetypes: fix wasm awkwardness
There currently this comment in the code: ``` // The CUE command works just fine without this (how?), // but the API tests require this for some reason. ``` Having a special case for "wasm" in the filetypes code doesn't seem quite right, and it turns out that the fix is straightforward: add knowledge to `internal/filetypes` about WASM files. The WASM files that are added are then added to `OrphanedFiles` not `UnknownFiles` because the files are not actually unknown any more, and the names are entered as full path names so the search is slightly different. No behavior change should result from this refactor. Signed-off-by: Roger Peppe <[email protected]> Change-Id: Icc18d0770c3f12aecc1a6a3274778b5fdf24f4ab Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1197522 TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Paul Jolly <[email protected]>
1 parent 4a8f673 commit d5d4815

File tree

4 files changed

+12
-33
lines changed

4 files changed

+12
-33
lines changed

cue/interpreter/wasm/wasm.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,19 @@ type compiler struct {
7171
// attribute and returns it as an [adt.Builtin] with the given function
7272
// name.
7373
func (c *compiler) Compile(funcName string, scope adt.Value, a *internal.Attr) (adt.Expr, errors.Error) {
74-
file, err := fileName(a)
74+
baseFile, err := fileName(a)
7575
if err != nil {
7676
return nil, errors.Promote(err, "invalid attribute")
7777
}
7878
// TODO: once we have position information, make this
7979
// error more user-friendly by returning the position.
80-
if !strings.HasSuffix(file, "wasm") {
81-
return nil, errors.Newf(token.NoPos, "load %q: invalid file name", file)
80+
if !strings.HasSuffix(baseFile, "wasm") {
81+
return nil, errors.Newf(token.NoPos, "load %q: invalid file name", baseFile)
8282
}
8383

84-
file, found := findFile(file, c.b)
84+
file, found := findFile(baseFile, c.b)
8585
if !found {
86-
return nil, errors.Newf(token.NoPos, "load %q: file not found", file)
86+
return nil, errors.Newf(token.NoPos, "load %q: file not found", baseFile)
8787
}
8888

8989
inst, err := c.instance(file)
@@ -118,16 +118,15 @@ func (c *compiler) instance(filename string) (inst *instance, err error) {
118118
return inst, nil
119119
}
120120

121-
// findFile searches the build.Instance for name. If found, it returnes
122-
// its full path name and true, otherwise it returns the original name
123-
// and false.
121+
// findFile searches the build.Instance for the given file name
122+
// and reports its full name and whether it was found.
124123
func findFile(name string, b *build.Instance) (path string, found bool) {
125-
for _, f := range b.UnknownFiles {
126-
if f.Filename == name {
127-
return filepath.Join(b.Dir, name), true
124+
for _, f := range b.OrphanedFiles {
125+
if filepath.Base(f.Filename) == name {
126+
return f.Filename, true
128127
}
129128
}
130-
return name, false
129+
return "", false
131130
}
132131

133132
// fileName returns the file name of the external module specified in a,

cue/interpreter/wasm/wasm_test.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,6 @@ func TestWasm(t *testing.T) {
5454

5555
bi := t.Instance()
5656

57-
// the BuildInstance created above doesn't know about
58-
// the .wasm files (why not?), so add them here.
59-
for _, f := range t.Archive.Files {
60-
if strings.HasSuffix(f.Name, ".wasm") {
61-
bf := &build.File{
62-
Filename: f.Name,
63-
}
64-
bi.UnknownFiles = append(bi.UnknownFiles, bf)
65-
}
66-
}
67-
6857
v := ctx.BuildInstance(bi)
6958
err := v.Validate()
7059

internal/filetypes/filetypes.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,6 @@ func ParseArgs(args []string) (files []*build.File, err error) {
182182
continue
183183
}
184184

185-
// The CUE command works just fine without this (how?),
186-
// but the API tests require this for some reason.
187-
//
188-
// This is almost certainly wrong, and in the wrong place.
189-
//
190-
// TODO(aram): why do we need this here?
191-
if len(a) == 1 && strings.HasSuffix(a[0], ".wasm") {
192-
continue
193-
}
194-
195185
modeVal, fileVal, err = parseType("", Input)
196186
if err != nil {
197187
return nil, err

internal/filetypes/types.cue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ modes: [string]: {
170170
".yml": tags.yaml
171171
".txt": tags.text
172172
".go": tags.go
173+
".wasm": tags.binary
173174
".proto": tags.proto
174175
".textproto": tags.textproto
175176
".textpb": tags.textproto // perhaps also pbtxt

0 commit comments

Comments
 (0)