Arc Forumnew | comments | leaders | submitlogin
1 point by dido 5970 days ago | link | parent

You could try using an Appel trampoline to do tail calls in JavaScript. See for instance here:

and here:;item=Appel%20trampoline

This last should be exceptionally interesting for you, as it contains an actual implementation of the Appel trampoline technique in JavaScript!

This technique is also used in the Chicken Scheme compiler among other places.

2 points by nostrademons 5970 days ago | link

I considered it - there was a point where I thought "Wouldn't it be cool if I implemented Cheney-on-the-MTA in JavaScript?" JavaScript doesn't have setjmp/longjmp, but it can be faked with exceptions. But I wasn't sure about the garbage-collection aspect, since you don't have the same fine-grained control over memory that you do in C, and I was afraid that just holding onto the continuation closure would accidentally capture the whole rest of the stack (because of arguments.caller), trading a stack overflow for a massive memory leak. And since I didn't want to spend too much time on the project, I decided to punt on the whole thing.

I think the setTimeout trampoline is better anyways - in addition to cleaning everything up, it also gives the browser's event loop a chance to run, so you don't risk locking up the browser.