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

mvm_call() error on multiple declarations of same variable across for loops #48

Closed
Jerryxia32 opened this issue Nov 22, 2022 · 2 comments

Comments

@Jerryxia32
Copy link

This reduced test case

console.log = vmImport(1);
function sayHello() {
	var arr = [1, 2, 3];
	for (var i = 0; i < 3; ++i) {
		console.log(arr[i]);
	}
	for (var i = 0; i < 3; ++i) {
		console.log(arr[i]);
	}
}
vmExport(1234, sayHello);

causes mvm_call() error with a return value of 12 when entering sayHello(), on tag 0.0.25.

My Javascript expertise is limited, but after some poking around I think it's the second var i = 0 in the second for loop that chokes Microvium. If I just change the second var i = 0 into i = 0, it works fine.

@Jerryxia32 Jerryxia32 changed the title mvm_call() error on multiple declarations for same variable across for loops mvm_call() error on multiple declarations of same variable across for loops Nov 22, 2022
@coder-mike
Copy link
Owner

Thanks for the reporting this. I'll look into it.

In the meantime, can I suggest using let instead of var. In JS, var declarations are hoisted to the function level, so two var declarations with the same name in the same function are actually the same function-level var declaration, which can lead to subtle bugs in your code. Here's some more detail about the differences on Stack Overflow.

coder-mike added a commit that referenced this issue Nov 22, 2022
Fix #48 bug with duplicate var declarations
@coder-mike
Copy link
Owner

I've fixed this on main. The fix will go out with the next release. But as I say, I do recommend using let (or const) instead of var.

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

No branches or pull requests

2 participants