Commit Graph

1073 Commits

Author SHA1 Message Date
Marc Vertes
79b7420ee1 interp: fix issue where a var is reused instead of redefined
Avoid a spurious optimisation which forces a variable to be reused instead of redefined for assignment operation. This ensures that a variable defined in a loop is re-allocated, preserving the previous instance when used by a closure for example.

Fix #1594
2023-09-21 23:00:06 +02:00
Ludovic Fernandez
8a6061cc86 chore: update linter
update golangci-lint to v1.53.3
2023-07-02 10:28:05 +02:00
bysir
c10e468d01 interp: fix fieldName method parsing embedded + generic fields
Fix https://github.com/traefik/yaegi/issues/1571
2023-07-01 12:58:05 +02:00
Hiro
75e5f99bc5 doc: fix go install cmd 2023-06-20 02:38:06 +02:00
Hiro
79e32b5a92 doc: install using go install
- Update markdown to document `go install` as the preferred way for the installation of yaegi REPL
2023-06-20 01:46:05 +02:00
Fernandez Ludovic
63b8cc42b9 doc: update readme 2023-06-19 14:07:06 +02:00
Marc Vertes
f4a9cd3cbe stdlib: remove embed wrapper
Embedding files using `//go:embed` and `embed` packages can not be supported in yaegi, so it is better to not generate the wrapper to embed and have a graceful error in case of usage of `embed.FS` in the interpreter.
Also update README about unsuported directives.

Fixes #1557.
2023-06-14 19:04:05 +02:00
laushunyu
6447a677f3 fix(src): use errors.Is(err, fs.ErrNotExist)
I implemented fs.FS. When the file does not exist, `xerrors.Wrap(fs.ErrNotExist, "")` will be returned. However, `os.IsNotExist` cannot handle this situation. I found the following comment:
```
// IsNotExist returns a boolean indicating whether the error is known to
// report that a file or directory does not exist. It is satisfied by
// ErrNotExist as well as some syscall errors.
//
// This function predates errors.Is. It only supports errors returned by
// the os package. New code should use errors.Is(err, fs.ErrNotExist).
```

Therefore, we should use `errors.Is(err, fs.ErrNotExist)` instead.
2023-06-14 17:00:12 +02:00
Marc Vertes
68a430f969 interp: fix support of type assert expressions in the global scope
Add the support of type assert expressions in type parsing
which allows to process type assertions in the global scope.

Fixes #1543.
2023-04-26 10:52:05 +02:00
Marc Vertes
dc7c64ba88 interp: improve support of unsafe
Unsafe functions such as `unsafe.Alignof`, `unsafe.Offsetof` and `unsafe.Sizeof` can be used for type declarations early on during compile, and as such, must be treated as builtins returning constants at compile time. It is still necessary to explicitely enable unsafe support in yaegi.

The support of `unsafe.Add` has also been added.

Fixes #1544.
2023-04-26 10:16:05 +02:00
Marc Vertes
d6ad13acea interp: improve handling of embedded fields with binary methods
Only structures with one embedded field can be marked anonymous, due to golang/go#15924. Also check only that the method is defined, do not verify its complete signature, as the receiver may or not be defined in the arguments of the method.

Fixes #1537.
2023-04-13 18:16:05 +02:00
Marc Vertes
d124954a7d interp: fix computation of array size from constant expression
The result of the expression giving the size of an array may be an `int` instead of `constant.Value` in case of an out of order declaration. Handle both cases.

Fixes #1536.
2023-04-11 17:54:05 +02:00
Senan Kelly
8de3add6fa extract: escape ~ in package names
consider packages from git.sr.ht, the user namespace is prefixed with `~`

so running something like `yaegi extract git.sr.ht/~emersion/scfg`

was producing syntax errors with `~` in identifiers. and also `~` in filenames which worked but probably best not to have it there either

thanks!
2023-03-27 19:08:05 +02:00
Marc Vertes
20c8f5ef7c interp: correctly init variables assigned from function call
In the case of a Go short definition (i.e. `a, b := f()`), the new defined variables must be (re-)created in order to preserve the previous value (if in a loop) which can be still in use in the context of a closure. This must not apply to redeclared variables which simply see their value reassigned.

The problem was both occuring in callBin() for runtime functions and assignFromCall() for functions created in the interpreter.

Fixes #1497.
v0.15.1
2023-03-24 11:46:05 +01:00
a
ce2bb794fa Equality is incorrect when nil is used as the left argument of ==
hi!

this issue is sorta blocking for me so i thought i would try to fix it. 

im still learning the codebase and understanding how yaegi works, but I thought I would attempt to add a test in the style of other tests as a start.

please let me know if there is anything i need to change / run, or if anyone knows perhaps a good place to start for tackling this issue

Fixes #1496
2023-03-23 09:20:06 +01:00
Marc Vertes
c4a297cbdc interp: fix use of function as field of a recursive struct.
The logic of patching reflect struct representation has been fixed in presence of function fields and to better handle indirect recursivity (struct recursive through nested struct).

Fixes #1519.
2023-03-21 11:50:05 +01:00
Eng Zer Jun
c473dceda8 test: use t.TempDir to create temporary test directory
This pull request replaces `os.MkdirTemp` with `t.TempDir`. We can use the `t.TempDir` function from the `testing` package to create temporary directory. The directory created by `t.TempDir` is automatically removed when the test and all its subtests complete. 

Reference: https://pkg.go.dev/testing#T.TempDir

```go
func TestFoo(t *testing.T) {
	// before
	tmpDir, err := os.MkdirTemp("", "")
	if err != nil {
		t.Fatalf("failed to create tmp directory: %v", err)
	}
	defer func() {
		if err := os.RemoveAll(dir); err != nil {
			t.Fatal(err)
		}
	}

	// now
	tmpDir := t.TempDir()
}
```
2023-03-16 21:40:05 +01:00
Marc Vertes
f202764973 cli: disable race detector if GOFLAGS contains -buildmode=pie
This should allow to build the package on AlpineLinux. Also document the constraint of having to install the source under $GOPATH/src/github.com/traefik/yaegi until Go modules are supported.

Fixes #1523.
2023-03-16 11:54:15 +01:00
sasaba
9d658604be interp: fix type assertion issues
closes #1514

hi!
I had the same problem as #1514 and I wanted to fix it, I found
When asserting *crypto/rsa.PublicKey, using the typ attribute of node to get an nil rtype, resulting in the assertion result being nok

This code contains the same problem
```go
package main

import (
	"log"
	"crypto/rsa"
)

func main() {

    var pKey interface{} = &rsa.PublicKey{}

    if _, ok := pKey.(*rsa.PublicKey); ok {
        log.Println("ok")
    } else {
        log.Println("nok")
    }
}

```

So I submitted this Pull Request, hope it will be merged
2023-03-14 15:34:05 +01:00
Denys Smirnov
166fff7072 interp: add safeguards when searching for vendor root.
With certain yaegi configuration on Windows, the loop in `previousRoot` can be infinite, because it fails to recognize driver letters as root.

This change adds a few more safeguards: checks path prefix earlier and checks if `filepath.Dir` produces an empty path.
2023-03-13 09:20:06 +01:00
Marc Vertes
8efc4f0735 interp: improve handling of methods defined on interfaces
For methods defined on interfaces (vs concrete methods), the resolution of the method is necessarily delayed at the run time and can not be completed at compile time.

The selectorExpr processing has been changed to correctly identify calls on interface methods which were confused as fields rather than methods (due to the fact that in a interface definition, methods are fields of the interface).

Then at runtime, method lookup has been fixed to correctly recurse in nested valueInterface wrappers and to find embedded interface fields in case of struct or pointer to struct.

Finally, remove receiver processing in `call()`.The receiver is already processed at method resolution and in genFunctionWrapper. Removing redundant processing in call fixes handling of variadic method, simplifies the code and makes it faster.

With those fixes, it is now possible to load and run `go.uber.org/zap` in yaegi. In turn, it makes possible for yaegi to run plugins dependent on zap, such as coraza-waf.

Fixes #1515, 
Fixes #1172,
Fixes #1275,
Fixes #1485.
2023-03-06 16:46:06 +01:00
Marc Vertes
6aa4f45c42 interp: wrap source functions when used as input parameters.
It allows to use interpreter functions as parameters in the runtime, even for defered callbacks, or when passed as empty interfaces, as for runtime.SetFinalizer.

Fixes #1503
v0.15.0
2023-02-08 12:04:05 +01:00
Marc Vertes
f3dbce93a4 interp: improve handling of generic types
When generating a new type, the parameter type was not correctly duplicated in the new AST. This is fixed by making copyNode recursive if needed. The out of order processing of generic types has also been fixed.

Fixes #1488
2023-02-08 11:48:05 +01:00
Marc Vertes
0e3ea5732a update to go1.20
* update to go1.20

* lint

* fix: ci

---------

Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
2023-02-03 14:32:05 +01:00
mpl
1679870ea3 Suppress http.ErrAbortHandler panics, as in the stdlib
Fixes https://github.com/traefik/traefik/issues/8937
2023-01-17 14:26:04 +01:00
ttoad
9b4ea62f69 Fix gorountine arguments not copied.
Fixes #1498
2023-01-16 18:38:04 +01:00
Denys Smirnov
eee72d1aae Fix deadlock after parsing directory with no files
Currently if interpreter tries to eval an empty directory with no Go files it will error (as it should), but all future calls to eval will deadlock, because the mutex is not unlocked correctly. I believe this is a critical issue that must be addressed.
2022-12-13 11:46:05 +01:00
Denys Smirnov
97cf8c4210 Expose package name of a compiled source
Exposing package name of the compiled source allows using it in `Eval`, as well as in logs and for other purposes.
2022-11-07 16:22:12 +01:00
Marc Vertes
7bb8b4631f interp: fix processing of aliased types
For a long time, there was a confusion between aliased types and named types (due to my misunderstanding of alias types in Go at that time). The type category `aliasT` has been renamed into `linkedT`, which is correct semantically. 

Aliased types are only declared using `typeSpecAssign`, and its processing is now distinct from `typeSpec` statement, used for named types.

A `linkedT` type is obtained by a statement like `type A uint32`, where `A` type category is therefore `linkedT`.

An aliased type is obtained by a statement like `type A = uint32` (notice the `=` sign, translating into `typeSpecAssign`).

The semantic difference is that in the first linkedT form, `A` and `uint32` are 2 distinct types, instead of being strictly equivalent in the `typeSpecAssign` form (the 2 names lead to one type definition).


Fixes #1416.
v0.14.3
2022-10-26 17:00:07 +02:00
Marc Vertes
9f43170708 interp: error instead of panic when assigning to a constant
Add early detection of assigning to a constant during compiling instead of panicking at runtime.
2022-10-25 18:16:10 +02:00
Marc Vertes
71112dbe87 interp: fix return of untyped values for defined types
Fixes #1475
2022-10-25 17:20:06 +02:00
Marc Vertes
4a8093609f interp: fix handling interface in operators
In case of interface values, make sure that the concrete type is preserved during type inference.

Fixes #1466.
2022-10-25 14:02:05 +02:00
Marc Vertes
7865c90737 interp: fix case behavior for values converted to empty interface
Fixes #1465.
2022-10-25 09:38:05 +02:00
Marc Vertes
e4e3d11772 interp: fix the logic to skip source files based on OS or CPU arch
For example, on architecture GOARCH=amd64, a file named `foobar_amd64.go` would be skipped instead of being read and parsed. The function `skipFile` is fixed and missing tests are added.
2022-10-24 15:48:04 +02:00
Marc Vertes
a5242cbb9e interp: retry type definition if an array size is undefined
In case of forward definition of a constant, a symbol may be undefined when attempting to compute the array size in type analysis. Just mark the type as incomplete instead of aborting directly, to allow resolution at a second pass.

Fixes #1470.
2022-10-24 10:44:06 +02:00
Fernandez Ludovic
c4d1bf5029 chore: update actions/cache to v3 2022-10-21 16:21:21 +02:00
Marc Vertes
e003140c6e interp: improve internal handling of functions
Up to now functions could be stored as node values in frame (as for interpreter defined functions) or function values, directly callable by the Go runtime. We now always store functions in the later form, making the processing of functions, anonymous closures and methods simpler and more robust. All functions, once compiled are always directly callable, with no further wrapping necessary.

Fixes #1459.
2022-10-19 17:54:08 +02:00
Marc Vertes
6b8c94e6c4 interp: check that send operate on channel value
Not performing this check was leading to a panic at run-time. It now fails early with a compile error.

Fixes #1453.
2022-10-04 12:00:08 +02:00
Marc Vertes
143e4a4559 interp: fix type assertion for wrapped empty interface
Although empty interfaces are usually not wrapped, for compatibility with the runtime, we may have to wrap them sometime into `valueInterface` type.

It allows to preserve interpreter type metadata for interface values exchanged with the runtime. It is necessary to resolve methods and receivers in the absence of reflect support.

During type assertions on empty interfaces, we now handle a possible valueInterface and dereference the original value to pursue the type assertion.

In the same change, we have improved the format of some panic messages at runtime to give location of offending source at interpreter level.

This change will allow to fix traefik/traefik#9362.
2022-10-03 17:50:09 +02:00
Marc Vertes
dfeddbe823 interp: fix handling generic types with multiple type parameters
Those declarations involve the indexListExpr AST token, which was not handled in type.go. The same processing as for a single type parameter is applied.

Fixes #1460.
2022-09-22 13:50:09 +02:00
Marc Vertes
021824930d interp: improve type assertions
In type assertion at compile time, compare signatures between function types only.

Make `itype.numOut()` return the correct value for Go builtins (this was not strictly necessary due to above fix, but it is correct and improves maintainability).

Fixes #1454.
2022-09-12 22:30:08 +02:00
Marc Vertes
b8301f10a8 interp: add missing conversion for non integer array dimension
Fixes #1451.
2022-09-12 19:40:08 +02:00
Marc Vertes
2e8808317f interp: fix default comm clause in select
Do not attempt to init a non-existent channel setting when in
default communication clause in select.

Fixes #1442.
2022-09-12 15:32:08 +02:00
Marc Vertes
79747f3d6f interp: fix redeclarations containing a blank variable
In [short variable declarations](https://go.dev/ref/spec#Short_variable_declarations),
The reuse of existing symbols is possible only if a new variable is defined,
otherwise a new symbol must be created, which was not the case in the issue.

Search for new symbols and correctly ignore blank variables.

Fixes #1434.
v0.14.2
2022-09-02 16:44:07 +02:00
Marc Vertes
63825e7201 interp: fix use of interfaces in composite types
The representation of non empty interfaces defined in the interpreter is now identical between refType() and frameType() functions, which are used to generate interpreter objects.

Fixes #1447 and #1426.
2022-09-01 12:18:08 +02:00
Marc Vertes
03ccda1a69 interp: fix type switch on arbitrary expressions
If the value on which to type-switch was already set (i.e. a variable),
there was no problem. But if it had to be obtained through a complex
expression (func call, array index, etc...), then the code to retrieve
the value prior type-switch was not scheduled. This is now fixed.

This issue is nasty because the behavior is silently changed,
leading potentially to further unrelated issues or runtime panics.

Fixes #1444.
2022-08-25 12:04:08 +02:00
Marc Vertes
e02621577f interp: improve handling of composed interfaces wrappers
This change implements a workaround to better support composed
interfaces in yaegi and let the interpreter define objects which
implement multiple interfaces at once.

We use the existing MapTypes to store what possible composed interface
wrapper could be used for some interfaces. When generating an interface
wrapper, the wrapper with the highest number of implemented methods is
chosen.

This is still an imperfect solution but it improves the accuracy of
interpreter in some critical cases.

This workaround could be removed in future if/when golang/go#15924
is resolved.

Fixes #1425.
2022-08-25 10:44:11 +02:00
Marc Vertes
ab869c8d20 interp: improve method resolution for embedded interfaces
The function `getMethodByName()` is now able to look for
embedded `valueInterface` field for matching methods in interface
struct fields.
    
Fixes #1439 and #1427.
2022-08-17 18:14:10 +02:00
Marc Vertes
b2aa636ea0 interp: fix spurious variable declaration loop
The variable dependency check function was confused by a dependency
variable with the same name but in an external package.

This change is necessary to address #1427.
2022-08-10 16:10:08 +02:00
Marc Vertes
ae725fb3d9 interp: fix generic check on nil function
Related to issue https://github.com/traefik/traefik/issues/9231
v0.14.1
2022-08-05 18:20:08 +02:00