Fix non-first uses of a module.

The implementation of useOp has not been updated correctly to accomodate the
fact that the namespaces are no longer modified in place.

This fixes #1212.
This commit is contained in:
Qi Xiao 2021-01-09 23:17:26 +00:00
parent 02d9252b5d
commit 28f86fefc6
2 changed files with 7 additions and 5 deletions

View File

@ -307,12 +307,11 @@ func useFromFile(fm *Frame, spec, path string, r diag.Ranger) (*Ns, error) {
// TODO: Make access to fm.Evaler.modules concurrency-safe.
func evalModule(fm *Frame, key string, src parse.Source, r diag.Ranger) (*Ns, error) {
// Make an empty scope to evaluate the module in.
ns := new(Ns)
// Load the namespace before executing. This prevent circular use'es from
// resulting in an infinite recursion.
// Make an empty namespace before executing. This prevent circular use'es
// from resulting in an infinite recursion.
fm.Evaler.modules[key] = new(Ns)
ns, err := fm.Eval(src, r, new(Ns))
fm.Evaler.modules[key] = ns
ns, err := fm.Eval(src, r, ns)
if err != nil {
// Unload the namespace.
delete(fm.Evaler.modules, key)

View File

@ -152,6 +152,9 @@ func TestUse(t *testing.T) {
That(`use a/b/c/d; put $d:name`).Puts("a/b/c/d"),
// module is cached after first use
That(`use has-init; use has-init`).Puts("has-init"),
// repeated uses result in the same namespace being imported
That("use lorem; use lorem lorem2; put $lorem:name $lorem2:name").
Puts("lorem", "lorem"),
// overriding module
That(`use d; put $d:name; use a/b/c/d; put $d:name`).
Puts("d", "a/b/c/d"),