variable instantiation & eval

F

Florian Loitsch

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
 
H

Henry

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"...
<snip>

Depends on how you take "Semantically, this step must follow the
creation of the FormalParameterList and FunctionDeclaration
properties" in the Variable Instantiation section for variable
declarations. That is, if you took it to mean that the processing of
the FunctionDeclaration in the - eval - should be treated as if it had
happened before the processing of the variable declaration of - x -
then "function x() { return false; }" is the expected result (as the
variable declaration's setting of the attributes would then have been
the last attributes set). But as - eval - code has its own Variable
Instantiation phase I don't see how the order of variable
instantiation for the containing execution context should matter, so
"global" should be the correct answer.
 
M

Martin Honnen

Florian said:
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.
 
R

Richard Cornford

Martin said:
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


those give the function as the result.


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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top