2006-04-27
Javascript Partial Continuations
I've been playing around with Rhino, a Javascript interpreter written in Java, using it as a server side scripting language. Rhino supports continuations so I thought I'd try and port the bshift and breset implementation I ported to Factor to Javascript.
My initial attempt is in partial-continuations.js.This can be loaded into the Rhino interpreter using:
load("partial-continuations.js")
Rhino's 'load' understands URL's so you can even do:
load("http://www.bluishcoder.co.nz/code/partial-continuations.js")
A simple use of the 'range' example in Javascript is:
function range(rc, from, to) {
return bshift(rc, function(pcc) {
while(from <= to) {
pcc(from++);
}
return undefined;
});
function test1() {
breset(function(rc) {
print(range(rc, 1, 5));
return 0;
});
}
This works fine printing the number from 1 to 5. I've struck a bit of wierdness with the attempt to translate the CLU-iterator idiom mentioned in my previous post. My Javascript translation was:
function test3() {
var sum = 0;
breset(function(rc) {
var tmp = range(rc, 1, 10);
sum += tmp;
});
print(sum);
}
This works, printing '55'. But if I change it to the following it prints 10:
function test3() {
var sum = 0;
breset(function(rc) {
sum += range(rc, 1, 10);
});
print(sum);
}
For some reason the range call is not calling the entire breset scope. It's only calling from the 'range' onwards. Can anyone spot what's wrong with my implementation?