eval: Isolate global ns of modules.

This fixes #590.
This commit is contained in:
Qi Xiao 2018-02-28 22:14:04 -05:00
parent d925c5c8e8
commit 98a8e2f666
4 changed files with 7 additions and 3 deletions

View File

@ -295,7 +295,7 @@ func loadModule(fm *Frame, name string) (Ns, error) {
0, len(src.code), fm.addTraceback(), false,
}
op, err := newFm.Compile(n, src)
op, err := compile(newFm.Builtin.static(), modGlobal.static(), n, src)
if err != nil {
return nil, err
}

View File

@ -39,7 +39,7 @@ var builtinSpecialTests = []Test{
// return.
{"fn f []{ put a; return; put b }; f", want{out: strs("a")}},
// Modules (see setup_datadir_test.go for setup)
// Modules (see testmain_test.go for setup)
// "use" imports a module.
{`use lorem; put $lorem:name`, want{out: strs("lorem")}},
// imports are lexically scoped
@ -61,6 +61,9 @@ var builtinSpecialTests = []Test{
// relative uses
{`use a/b/c/x; put $x:d $x:lorem`, want{out: strs("a/b/c/d", "lorem")}},
// Variables defined in the default global scope is invisible from modules
NewTest("x = foo; use put-x").WantAnyErr(),
// TODO: Test module namespace
}

View File

@ -206,7 +206,7 @@ func (ev *Evaler) eval(op Op, ports []*Port, src *Source) error {
return ec.Eval(op)
}
// Compile compiles elvish code in the global scope. If the error is not nil, it
// Compile compiles Elvish code in the global scope. If the error is not nil, it
// always has type CompilationError.
func (ev *Evaler) Compile(n *parse.Chunk, src *Source) (Op, error) {
return compile(ev.Builtin.static(), ev.Global.static(), n, src)

View File

@ -68,6 +68,7 @@ var mods = map[string]string{
"a/b/c/d": "name = a/b/c/d",
"a/b/c/x": "use ./d; d = $d:name; use ../../../lorem; lorem = $lorem:name",
"has/init": "put has/init",
"put-x": "put $x",
}
var libDir string