By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,314 Members | 2,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,314 IT Pros & Developers. It's quick & easy.

variable instantiation & eval

P: n/a
hi,
What should be the output of the following code-snippet?
===
var x = "global";
function f() {
var x = 0;
eval("function x() { return false; }");
delete x;
alert(x);
}
f();
===
- Rhino (1.6 release 5), Opera (9.23) and Konqueror (3.5.7) print
"function x() { return false; }"
- Firefox (2.0.0.6) prints "0".
- and I think it should be "global"...

Relevant sections of the spec are: 10.1.3 where it states: "... If the
variable object already has a property with this name, replace its value
and attributes",
and 10.2.2 with "Variable instantiation is performed using the calling
context's variable object and using empty property attributes".

mfg,
// florian loitsch

Sep 11 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Florian Loitsch wrote:
What should be the output of the following code-snippet?
===
var x = "global";
function f() {
var x = 0;
eval("function x() { return false; }");
delete x;
You might want to check the return value of that delete x, knowing that
is essential I think to understand what the alert later outputs.
It seems the implementations don't delete x (i.e. delete x yields
false), that is why
alert(x);
}
f();
===
- Rhino (1.6 release 5), Opera (9.23) and Konqueror (3.5.7) print
"function x() { return false; }"
those give the function as the result.
- Firefox (2.0.0.6) prints "0".
That seems odd to me and looks like a bug in the handling of the eval
call with the function declaration.
- and I think it should be "global"...
That seems to be the right result if the delete x yielded true.

The specification of the var statement says:
"If the variable statement occurs inside a FunctionDeclaration, the
variables are defined with function-local scope in
that function, as described in section 10.1.3. Otherwise, they are
defined with global scope (that is, they are created
as members of the global object, as described in section 10.1.3) using
property attributes { DontDelete }."

One could read that as saying that only a global variable declaration
should create a property with "DontDelete", in that case your
expectation of the result of your test case showing "global" seems right
but I a bit doubtful of my analysis with all implementations seemingly
agreeing on setting "DontDelete" on the local var statement too.

Let's hear some other opinions.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 11 '07 #2

P: n/a
Martin Honnen wrote:
Florian Loitsch wrote:
>What should be the output of the following code-snippet?
===
var x = "global";
function f() {
var x = 0;
eval("function x() { return false; }");
delete x;

You might want to check the return value of that delete x,
knowing that is essential I think to understand what the
alert later outputs. It seems the implementations don't
delete x (i.e. delete x yields false), that is why
> alert(x);
}
f();
===
- Rhino (1.6 release 5), Opera (9.23) and Konqueror (3.5.7)
print "function x() { return false; }"

those give the function as the result.
>- Firefox (2.0.0.6) prints "0".

That seems odd to me and looks like a bug in the handling of
the eval call with the function declaration.
That certainly is an odd result. JavaScript(tm) has function statements
in addition to having the function expressions and function declarations
required by ECMA 262, which (not being formally specified themselves)
may account for this outcome. But the string arguments to - eval - are
supposed to be handled as an ECMAScript 'Program' and I don't see how
the contents of the evaled string could be anything but a function
declaration in that context.
>- and I think it should be "global"...

That seems to be the right result if the delete x yielded true.

The specification of the var statement says:
"If the variable statement occurs inside a FunctionDeclaration,
the variables are defined with function-local scope in that
function, as described in section 10.1.3. Otherwise, they are defined
with global scope (that is, they are created
as members of the global object, as described in section
10.1.3) using property attributes { DontDelete }."

One could read that as saying that only a global variable
declaration should create a property with "DontDelete",
One could read it that way in isolation, but taken with section 10.2.3,
where for "Function code" it says "Variable instantiation is performed
using the activation object as the variable object and using property
attributes { DontDelete }", it would be difficult to sustains the
"only".
in that case your expectation of the result of your test
case showing "global" seems right but I a bit doubtful
of my analysis with all implementations seemingly agreeing on setting
"DontDelete" on the local var statement too.
Yes, the DontDelete on the function local variable definition is
required. The question here, and the bug(s) here, are with variable
instantiation for eval. For "Eval Code" ECMS 262 says "variable
instantiation is performed using the calling context's variable object
and using empty property attributes", so no DontDelete. And section
10.1.3 says, for "FunctionDeclaration" that "If the variable object
already ahs a property with this name, replace its value and
attributes." So the - var x - creates an - x - property of the variable
object which does have the DontDelete attribute during variable
instantiation for the execution of the function, but the variable
instantiation for the - eval - call should _replace_ both the value of
that - x - property and its attributes, and the replacement attributes
are empty so the final sate of the variable object's- x - property after
the eval call should be its having no DontDelete attribute. The - x -
property of the variable object should then be deletable so the - delete
x; - expression should delete it (removing the property form the
variable object), and unmask the global - x - variable as a consequence.
Let's hear some other opinions.
There certainly are bugs here, but not particularly important ones.
Using - eval - for its divergent attribute setting does not seem to have
any usefully exploitable applications.

Richard.

Sep 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.