Commit Graph

27 Commits

Author SHA1 Message Date
Marc Vertes
6346d11286 chore: move to new organization 2020-09-16 10:58:04 +02:00
Marc Vertes
341c69d922 feat: configure stdin, stdout and stderr per interpreter
The goal is to provide greater control of input, output and error
streams of the interpreter. It is now possible to specify those
as options when creating a new interpreter. The provided values
are propagated to relevant stdlib symbols (i.e fmt.Print, etc).
Care is taken to not update the global variables os.Stdout, os.Stdin
and os.Stderr, as to not interfere with the host process.

The REPL function is now simplified. The deprecated version is removed.

The tests are updated to take advantage of the simplified access
to the interpreter output and errors.

Fixes #752.
2020-08-31 15:42:03 +02:00
mpl
896bfeb5a1 interp: new EvalPath API
The recent changes that added some redeclaration checks implicitly added more
strictness related to namespaces and scopes which, among other things, broke
some uses that "accidentally" used to work.

For example, given

const script1 = `
	import "fmt"

	// more code
`
const script2 = `
	import "fmt"

	// some other code
`
If one Evals script1, then script2, with the same interpreter, without
specifying any scope, as the two fragments would be considered part of the same
(.go file) scope by default, a redeclaration error would be triggered because
import "fmt" is seen twice.

A work-around would have been to specify (a different) i.Name before each Eval
call, so that each script is considered as coming from a different .go file, and
hence are respectively in different scopes with respect to imports.

That lead us to realize we had to make specifying things such as file-scope, and
"incremental mode" (aka REPL), more obvious in the context of an Eval call.

In addition, we want to lay down the foundations for Yaegi being able to behave
more like the go tool wrt to various inputs, i.e. it should be able to take a
package directory, or an import path, as input, instead of just a .go file.

Hence the introduction of a new kind of Eval method (whose signature is not fixed yet):

func (interp *Interpreter) EvalPath(path string) (res reflect.Value, err error)

It partially solves the problem described above because:

1. the path given to EvalPath can be used as the file-scope hint mentioned
above, for now (even though the related implementation details might change).
2. Eval always runs in incremental mode, whereas EvalPath always runs in
non-incremental mode, hence clarifying the situation in that respect.

And to avoid confusion, the Name field of Interpreter is now non-exported,
since it is somewhat redundant with the path argument of EvalPath.

Note that #731 is not fully fixed (and might never be), as a requirement of the
proposed solution is to move the input bits of code into respective files
(instead of leaving them as strings).

Finally, some related bugfixes, documention changes, and some refactoring have
been included. Notably, there is no "empty scope" anymore, i.e. name defaults
to "_.go" when it is not specified.

Updates #731
Fixes #778
Fixes #798
Fixes #789 

Co-authored-by: Marc Vertes <mvertes@free.fr>
2020-08-20 13:14:15 +02:00
mpl
563270ca02 interp: support yet another vendoring case
* interp: support another vendoring case

Namely, when the vendor dir is a sibling (or an uncle) relative to the
current pkg

Fixes #758

* make linter happier

* address review comments

* fix, cleanup, add unit tests

* add dummy files to force dirs into git
2020-07-15 15:35:04 +02:00
mpl
4f3481b55c interp: support another vendoring case 2020-07-01 10:44:03 +02:00
Ludovic Fernandez
2765478137 chore: update linter to support go1.13 2019-10-09 14:14:04 +02:00
Marc Vertes
35e645c690 fix: correct handling of types alias of interfaces 2019-09-25 15:24:04 +02:00
Ludovic Fernandez
dea1f56f38 refactor: use struct to define interpreter Options. (#236) 2019-07-03 19:46:17 +02:00
Marc Vertes
274c0fc47a chore: API and code cleanup. Rename stdlib.Value into stdlib.Symbols (#231) 2019-06-27 12:40:04 +02:00
Marc Vertes
8910769b77 feat: functional options to New() (#149) 2019-04-05 21:36:48 +02:00
Marc Vertes
68e25cf68e fix: wrong returned type in define from function call in global context (#148) 2019-04-02 16:05:39 +02:00
Fernandez Ludovic
12b608b625 chore: rename to yaegi. 2019-03-20 10:15:35 +01:00
Marc Vertes
5a10046944 feat: simplify Use() (#115)
Change API from
func Use(val libValueMap, typ libTypeMap)
to
func Use(val libValueMap)
2019-03-13 12:24:59 +01:00
Marc Vertes
5677e0501e fix: correct propagation of values during eval (#95)
Eval returns the value of the root node of AST, so ensure
that node values are propagated back to ExprStmt and BlockStmt,
which should be sufficient to handle evaluation of simple
statements.
2019-02-19 14:51:44 +01:00
Marc Vertes
18330ad7f9 fix: function created by reflect.MakeFunc using closure had wrong type (#75)
Call genNodeWrapper on interpreted function returned by a closure,
itself being wrapped, to ensure that the function can be called from
binary, no matter the level of nesting closures.
2019-02-03 15:38:21 +01:00
Marc Vertes
6fc6560af3 fix: handle name import of src packages (#74)
Rename the package scope from internal package name to alias name.
Detect different packages in the same directory (forbidden by spec).
Update example to test named import.
2019-02-02 14:18:09 +01:00
Ludovic Fernandez
27a338d84a fix: recursively search for packages. (#72) 2019-02-01 11:47:41 +01:00
Ludovic Fernandez
3f0e693383 refactor: support imports/package/vendor. (#41)
* test: adds tests about package and vendor.

* refactor: support imports/package/vendor.

* review.
2019-01-24 16:16:49 +01:00
Ludovic Fernandez
c09c1869a7 refactor: remove obsolete tests for plugins. (#36) 2019-01-24 06:14:31 +01:00
Ludovic Fernandez
3f000e5fac refactor: clean and plugin tests (#26)
* refactor: remove gi_c
* refactor: new intergation example.
* chore: adds new targets.
* doc: adds issue template.
2019-01-22 15:53:52 +01:00
Marc Vertes
098829d316 style: fix warnings from golangci-lint printed by make check (#11)
* style: fix warnings from golangci-lint printed by `make check`
* style: err assign in if block when possible. Better use of switch/case
* style: err assign in if block when possible
2019-01-16 18:59:24 +01:00
Marc Vertes
29f5a29d28 rename interp2.go into inception.go 2018-12-19 16:39:23 +01:00
Marc Vertes
27cbeef38d Methods created in the interpreter can now be used by runtime 2018-12-19 15:10:26 +01:00
Marc Vertes
f8a4aba39b Add interpreter inception example 2018-12-11 17:23:24 -08:00
Marc Vertes
b0ef0a00db Interpreter now exports itself, so scripts can do Eval() too 2018-12-11 10:29:43 -08:00
Marc Vertes
480ef53daf Get rid of Export(). Fix golint warnings 2018-12-11 03:38:51 -08:00
Marc Vertes
6a23877ce2 Eval: wrap returned interpreter node into a callable function 2018-12-11 03:12:50 -08:00