Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading module multiple times is not idempotent #17

Open
icexin opened this issue Mar 8, 2020 · 0 comments · May be fixed by #18
Open

Loading module multiple times is not idempotent #17

icexin opened this issue Mar 8, 2020 · 0 comments · May be fixed by #18

Comments

@icexin
Copy link

icexin commented Mar 8, 2020

Code as follows

func TestDoubleLoadModule(t *testing.T) {
	loader := CreateLoaderFromSource(`{
		module.exports = "bar";
	}`, "")

	testLoad := func() {
		vm := New()
		vm.AddModule("foo", loader)
		v, err := vm.Require("foo", ".")
		if err != nil {
			t.Fatal(err)
		}
		s, _ := v.ToString()
		if s != "bar" {
			t.Errorf("expect bar, got %s", s)
		}
	}
	testLoad()
	testLoad()
}

Output of test

--- FAIL: TestDoubleLoadModule (0.00s)
    motto_test.go:85: expect bar, got [object Object]
FAIL

The reason is that multiple calls to ModuleLoader cause the source to be repeatedly assigned.

source = "(function(module) {var require = module.require;var exports = module.exports;var __dirname = module.__dirname;" + source + "\n})"

@icexin icexin linked a pull request Mar 8, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant