//#################################################################### //library test suite eval(readFile('NamespaceJS_Library_Compiled')) //--normal case----------------------------------------------- test01 = ' fn = function(c){ c("test01 passed") } alert(callcc(fn)) ' runWithLibrary(test01) //--nested continuations ------------------------------------- test02 = ' fn1 = function(c1){ fn2 = function(c2){ c1("test02 passed") } callcc(fn2) c1("test02 failed") //this should never be reached } alert(callcc(fn1)) ' runWithLibrary(test02) //--nested continuations--------------------------------------- test03 = ' fn1 = function(c1){ fn2 = function(c2){ c2("test03 failed") } callcc(fn2) c1("test03 passed") // this should be reached } alert(callcc(fn1)) ' runWithLibrary(test03) //--continuation calls within try-catch------------------------- test04 = ' fn = function(c){ try { c("test04 passed") } catch (e) { throw "test04 failed" } } alert(callcc(fn)) ' runWithLibrary(test04) //--throw inside a callcc---------------------------------------- test05 = ' fn = function(c){ throw "test05 passed" } try { callcc(fn) alert("test05 failed") } catch (e) { alert(e) } ' runWithLibrary(test05) //--return-from within try-catch---------------------------------- test06 = ' fn = function(){ try { returnFrom(fn,"test06 passed") } catch (e) { throw "test06 failed" } } alert(returnFromableFunction(fn)()) ' runWithLibrary(test06) //--throw inside a return-from-able function----------------------- test07 = ' fn = function(){ throw "test07 passed" } try { returnFromableFunction(fn)() alert("test07 failed") } catch (e) { alert(e) } ' runWithLibrary(test07) //--continuation calls within a return-from-able function------------ test08 = ' fn = function(c){ returnFromableFunction( function(){ c("test08 passed") } )() throw "test08 failed" //this should never be reached } alert(callcc(fn)) ' runWithLibrary(test08) //--return-from inside a callcc-------------------------------------- test09 = ' fn = function(){ callcc( function(c){ returnFrom(fn, "test09 passed") } ) throw "test09 failed" } alert(returnFromableFunction(fn)()) ' runWithLibrary(test09) #################################################################### #################################################################### #################################################################### OLD EXAMPLES #################################################################### #################################################################### #################################################################### Example 1 eval(readFile('NamespaceJS_Library_Compiled')) program_str =' newFunction1 = returnFromableFunction( function1 = function() { var function2 = function() {returnFrom(function1, "function2")} function2() return "function1" } ) alert("returning straight from " + newFunction1()) ' a = NSJSParser.matchAll(oldLibrary + program_str, "topLevel") b = NSJSTranslator.match(a, "trans") eval(b) #################################################################### Example 2 eval(readFile('NamespaceJS_Library_Compiled')) program_str2 =' take3 = function(context) { for (i=0;i<3;i++) { itemLIFO.pop(); count++; } alert("People have bought 3 items. Total items left in storage = " + itemLIFO.length) if (count > 25) context.returnFromCoroutine("We\'re broke. Let\'s quit."); else context.yieldTo(stock5); } stock5 = function(context) { if (itemLIFO.length > 10) { alert("There are too many items in storage. Let\'s wait until we have space."); context.yieldTo(take3); } for (i=0;i<5;i++) itemLIFO.push(0); alert("Our supplier just sent us 5 more items. Total items left in storage = " + itemLIFO.length); context.yieldTo(take3); } itemLIFO = []; count = 0; alert(runCoroutines(stock5)); ' a = NSJSParser.matchAll(oldLibrary + program_str2, "topLevel") b = NSJSTranslator.match(a, "trans") eval(b)