eval(string) scope ?

J

Jocke P

Hi,

I had the idea to create a walkthrough sessions for users,
printing statements, then executing one by one.

I find that eval on separate strings doesn't put values in the same scope,
so any statement using an earlier value throws ReferenceError.

Here's a minimal session:

session = [
"/* Hit enter to execute next statement.\nType 'quit' to stop session. */",
"x = 'test'",
"if (x == 'test') alert('wohoo!')"
]

Here's code to execute it. It uses a custom method in my V8 shell:

for(line in session) {
input = prompt(session[line]); // prompt wraps C getline()
if (input == 'quit') break;
eval(session[line]);
}

Apparently eval operates on each line separately,
so I get reference error on the last line in session.

How could I connect the evals so they operate on the same scope?

Thanks,

Jocke
 
J

Jocke P

In the asking, a google session suggests itself, providing the answer.
Pack it all in a function and I'm roaling.

function RunSession() {
session = [ ...]
for(line in session) eval(session[line]) ...etc
}

All values end up in the global object as well, so usable after the session. Nice.

Oh well, cheers 2 all anyway,

jp
 
L

Lasse Reichstein Nielsen

Jocke P said:
I had the idea to create a walkthrough sessions for users,
printing statements, then executing one by one.

I find that eval on separate strings doesn't put values in the same scope,
so any statement using an earlier value throws ReferenceError.

They should, so you are doing something wrong.
The scope of a direct call to eval is the surrounding scope. Variables
are created in that scope. The following should work:

var lines = ["var x = 42;", "var y = x + x;", "alert(y);"];
for (var i = 0; i < lines.length; i++) { eval(lines); }
Here's a minimal session:

session = [
"/* Hit enter to execute next statement.\nType 'quit' to stop session. */",
"x = 'test'",

Here you don't declare x as a variable at all, so it should be created
as a property of the global object.
"if (x == 'test') alert('wohoo!')"
]

Here's code to execute it. It uses a custom method in my V8 shell:

for(line in session) {
input = prompt(session[line]); // prompt wraps C getline()
if (input == 'quit') break;
eval(session[line]);
}

Running your test in Chrome gives the "wohoo!" alert.
Apparently eval operates on each line separately,
Yes.

so I get reference error on the last line in session.

No. At least not for that reason.
How could I connect the evals so they operate on the same scope?

They should do. You need to tell us more about what you do.

/L
 
J

JR

Hi,

I had the idea to create a walkthrough sessions for users,
printing statements, then executing one by one.

I find that eval on separate strings doesn't put values in the same scope,
so any statement using an earlier value throws ReferenceError.

Here's a minimal session:

session = [
   "/* Hit enter to execute next statement.\nType 'quit' to stop session. */",
   "x = 'test'",
   "if (x == 'test') alert('wohoo!')"
]

Here's code to execute it. It uses a custom method in my V8 shell:

for(line in session) {
   input = prompt(session[line]);  // prompt wraps C getline()
   if (input == 'quit') break;
   eval(session[line]);

}

Apparently eval operates on each line separately,
so I get reference error on the last line in session.

How could I connect the evals so they operate on the same scope?

Thanks,

        Jocke

The for-in statement is an object iterator. Quoting Douglas Crockford:
"- Do not use Array when you do not need .length. You should be using
an Object. It is best to not use the term "associative array" when
working in JavaScript because it will confuse you."

According to Ecma-262, section 12.6.4, "The mechanics of enumerating
the properties [...] is implementation dependent." The order of
enumeration is defined by the object; in other words: the order in
which the *lines* (in session) are produced is not guaranteed.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top