function called from function scoped for-of loop is not following function scope rules of variables

RESOLVED INVALID

Status

()

Core
JavaScript Engine
P5
critical
RESOLVED INVALID
2 years ago
2 years ago

People

(Reporter: shivarajnaidu, Unassigned)

Tracking

(4 keywords)

46 Branch
x86_64
Linux
64bit, access, highrisk, reproducible
Points:
---

Firefox Tracking Flags

(Not tracked)

Details

Attachments

(1 attachment)

(Reporter)

Description

2 years ago
Created attachment 8753391 [details]
Screenshot from 2016-05-17 21:05:49.png

User Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:46.0) Gecko/20100101 Firefox/46.0
Build ID: 20160425115534
Firefox for Android

Steps to reproduce:

Please analyze the following code and obsrve the behaviours of for-of loop inside function a and normal for loop inside function b. 
<!DOCTYPE html>
<html><head> <title>function scope test on FIREFOX 46 </title></head>
  
<body>
<h1>function scope test for-of loop on FIREFOX 46 </h1>
<h2 id="uv_hd1">heading</h2>
<h2 id="uv_hd2">heading</h2>
<h1 id="test_result">rsult will be appered here.</h1>
<button onclick="a()">click to run for-of loop</button>
<button onclick="b()">click to run the for loop</button>

<script type="text/javascript">

function uv_test (i) {
	var y = (typeof i == "number") ? x[i] :i;
	y.style.color="red";
};


function a () {

var x = document.querySelectorAll("h2");

/************
This function will run for-of loop;

here variable x is declared and intialised inside the function;

but , when i calling function named uv_test (without passing x), 
from for-of loop of this function , it doesn't throwing reference error for x;
Herre problem is the out side function is accesing the variable x 
(but it should not be like that.. because, here, variable x scoped only to function a);

Normal for loop is working properly.. 
it's throwing reference error, if i called it from normal for loop which is present inside function.

*************/
    for(var i of x) {
		uv_test (i);
	};

document.getElementById("test_result").innerHTML="for of loop";
};


function b () {

var x = document.querySelectorAll("h2");
/************
This function will run normal for loop;

here variable x is declared and intialised inside the function;
when I called the function uv_test from normal for loop which is present inside the one function.
it's throwing reference error; because variable x 's scope is confined to function b.. so reference error occured while calling function uv_test from function b's for loop.
*************/


	for(var i = 0; i < x.length; i++) {
			uv_test (i);
	};
document.getElementById("test_result").innerHTML="normal for loop";

};

</script>    

</body></html>



Actual results:

1] here, for-of loop is present inside the function a.
2]  we are making call from this loop to function uv_test .
3] function uv_test is not in the same scope of variable x either for-of loop.
4] but still it's accessing variable x;
5] function outside the scope of for-of loop is getting illegal access to function scoped variable x;


Expected results:

1] function outside the scope of for-of loop is getting illegal access to function scoped variable x;

2] But it should not be like that.. where as the for loop is working properly.
if function uv_test , called from normal for loop present inside function  b. it is throwing an reference error for variable x;

3] this is expected resit for
(Reporter)

Updated

2 years ago
Severity: normal → critical
OS: Unspecified → Linux
Hardware: Unspecified → x86_64
(Reporter)

Updated

2 years ago
Component: Untriaged → JavaScript Engine
Keywords: 64bit, access
Priority: -- → P5
Product: Firefox → Core
(Reporter)

Updated

2 years ago
Keywords: highrisk, reproducible
This doesn't do what you think it does, because `for (i of x)` doesn't do the same thing as a `for(;;)` loop.

The `for(i of x)` loop iterates over entries of `x`, not indices. Because of that, `uv_test` is called with <h2> element objects instead of numbers, so `typeof i == "number"` is `false`, so the `x[i]` branch of the ternary expression is never reached, and thus `x` is never accessed.
Status: UNCONFIRMED → RESOLVED
Last Resolved: 2 years ago
Resolution: --- → INVALID
I agree.

Thanks for the bug report, shivarajnaidu. In this case JS is behaving correctly, but in general, open source projects need bug reports from people like you to thrive and improve.
(Reporter)

Comment 3

2 years ago
My pleasure :)
You need to log in before you can comment on or make changes to this bug.