Created
October 23, 2011 17:32
-
-
Save einblicker/1307616 to your computer and use it in GitHub Desktop.
Delimited Continuation in Rhino
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function callcc(f) { | |
return f(new Continuation()); | |
} | |
importPackage(java.util); | |
var metaCont = new java.util.Stack(); | |
function abort(thunk) { | |
return metaCont.peek()(thunk()); | |
} | |
function reset(thunk) { | |
return callcc(function(k) { | |
metaCont.push(function(v){ | |
metaCont.pop(); | |
return k(v); | |
}); | |
return abort(thunk); | |
}); | |
} | |
function shift(f) { | |
return callcc(function(k){ | |
return abort(function(){ | |
return f(function(v){ | |
return reset(function(){ | |
return k(v); | |
}); | |
}); | |
}); | |
}); | |
} | |
reset(function(){ | |
var x = shift(function(k){ return k(k(123)); }); //maybe a bug | |
return x + 456; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exception in thread "main" java.lang.IllegalStateException
at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3372)
at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:120)
at org.mozilla.javascript.tools.shell.Main.processFileSecure(Main.java:601)
at org.mozilla.javascript.tools.shell.Main.processFile(Main.java:560)
at org.mozilla.javascript.tools.shell.Main.processSource(Main.java:531)
at org.mozilla.javascript.tools.shell.Main.processFiles(Main.java:179)
at org.mozilla.javascript.tools.shell.Main$IProxy.run(Main.java:100)
at org.mozilla.javascript.Context.call(Context.java:504)
at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:505)
at org.mozilla.javascript.tools.shell.Main.exec(Main.java:161)
at org.mozilla.javascript.tools.shell.Main.main(Main.java:136)
Workaround for the bug(s):
Comment out the line that throws the IllegalStateException in ScriptRuntime.java and modify abort as follows:
function abort(thunk) {
var v = thunk();
return metaCont.peek()(v);
}