Variable values in Continuations

47 views
Skip to first unread message

Moshe Weinstock

unread,
May 31, 2015, 2:28:53 AM5/31/15
to mozill...@googlegroups.com
Hi all,

I have a function with a for loop in whose loop I've created a continuation.

For(var i=0 bla bla){
Code
Func captures cont
}

Lets call cont1 the cont generated in iteration1 of the loop.
When I call cont1 it's as if the value of i is not stored in the cont - it is where it was left off, rather than being 1.

I've found this link:
https://wiki.apache.org/cocoon/RhinoWithContinuations
Which explains the phenomenon (search for NOTE on the page). The link is very old though.

Are conts still implemented this way in Rhino?

gabriel munteanu

unread,
May 31, 2015, 7:35:54 AM5/31/15
to mozill...@googlegroups.com
Hi Moshe,
It sounds like you have to catch the variable i in a closure
for(var i=0; i<l; i++) {
Code
(function(closuredVar){
Func captures cont.
})(i);
}

and in the Func that captures the continuation you use closuredVar for i.

gabi
> --
> You received this message because you are subscribed to the Google Groups "mozilla-rhino" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to mozilla-rhin...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Orel Weinstock

unread,
May 31, 2015, 7:40:29 AM5/31/15
to mozill...@googlegroups.com
Following that sense I'd rather loop with forEach and be done with it.
I'll give it a shot and report back to you.

Thanks! (Multum)
You received this message because you are subscribed to a topic in the Google Groups "mozilla-rhino" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mozilla-rhino/YvhatRHuy2Q/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mozilla-rhin...@googlegroups.com.

Orel Weinstock

unread,
May 31, 2015, 2:09:27 PM5/31/15
to mozill...@googlegroups.com
I've thought about this some more, and my conclusion is that the conts save only stack variables and not heap variables. Is this the explanation for why this is happening?

ian

unread,
Apr 30, 2017, 8:22:30 AM4/30/17
to mozilla-rhino
Orel, I wanted to follow up on this, in case others find this mailing list and have similar questions:

Variables declared under the global scope object are not serialized by ScriptableOutputStream. On serialization time, the global scope is serialized as PendingLookup(name = ""). (See https://github.com/mozilla/rhino/blob/master/src/org/mozilla/javascript/serialize/ScriptableOutputStream.java#L182 and https://github.com/mozilla/rhino/blob/master/src/org/mozilla/javascript/serialize/ScriptableOutputStream.java#L50)

When you declare `var x` outside of any functions, it's stored directly in the 'global' scope object, and thereby not serialized.

An easy workaround is to wrap your script in a function closure. Assuming your script is stored in a String called `script`, you can run 

cx.executeScriptWithContinuations(cx.compileString("(function() { " + script + "})();", "my script", 1, null), globalScope)

I believe this behavior violates the expectations set in the API, so we could work towards improving it.

On Sunday, May 31, 2015 at 9:09:27 PM UTC+3, Orel Moshe Weinstock wrote:
I've thought about this some more, and my conclusion is that the conts save only stack variables and not heap variables. Is this the explanation for why this is happening?

On Sunday, May 31, 2015, Orel Weinstock wrote:
Following that sense I'd rather loop with forEach and be done with it.
I'll give it a shot and report back to you.

Thanks! (Multum)


On Sunday, May 31, 2015, gabriel munteanu wrote:
Hi Moshe,
It sounds like you have to catch the variable i in a closure
for(var i=0; i<l; i++) {
   Code
   (function(closuredVar){
     Func captures cont.
   })(i);
}

and in the Func that captures the continuation you use closuredVar for i.

gabi


On Sun, May 31, 2015 at 9:28 AM, Moshe Weinstock
orelweinstock wrote:
Reply all
Reply to author
Forward
0 new messages