From: Jocke P on
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
From: Jocke P on
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

Jocke P wrote:
> 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.
>
From: Lasse Reichstein Nielsen on
Jocke P <jopa(a)snospamtoreroom.se> writes:

> 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[i]); }

> 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
--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

From: JR on
On Jan 8, 9:40 pm, Jocke P <j...(a)snospamtoreroom.se> wrote:
> 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.

--
JR