BruceSherwood/glowscript

inefficient, bizarre, and undocumented behavior of main()

Closed this issue · 1 comments

Concerning the main() program defined and returned by the compiler:

It is defined as main(wait). The meaning of the argument is not documented anywhere AFAICT. Insofar as programmers expect variable names to be self-documenting, one would expect that the argument would have something to do with waiting, sleeping, threading, CPU-sharing, and that sort of thing. No such luck.

  1. For starters, if you call main() with no argument at all, bizarre things happen. Among other things, main() will call itself, in such a way that substantial parts of it get executed twice. My advice is: Don't do this. Always call it with an argument: main(handbasket).

  2. Now the question arises, what should the argument be? As far as I can tell, the argument (handbasket) is supposed to be a function of one variable.

  3. The next question is, what is the argument to the handbasket function? Apparently / usually it is an Error object. It would be nice if the function printed a stack trace and then exited.

I have code to do this, if anybody's interested. It prints a stack trace under firefox. (Porting it to chrome shouldn't be too hard.)

I suggest looking at the function compileAndRun in untrusted/run.js, which is where GlowScript starts execution. Concerning the documentation issue, the trinket.io people told me, after they got external compilation to work, "This was all inspired by the run.html and run.js files in the untrusted folder in the glowscript repository".

As for "wait", it is a keyword used by Streamline to restructure a synchronous program (e.g. one containing an infinite loop) into an asynchronous one (placing the loop contents into a separate function that is called at appropriate times by the iteration rate set by the rate() function. At glowscript.org, programs written in JavaScript, CoffeeScript, and RapydScript are required to have an explicit keyword "wait" in functions such as rate() and scene.waitfor(). Part of the preprocessing of VPython source code is to insert "wait" where required, in order to support legacy code.

Here's a JavaScript program with explicit "wait" elements:
http://www.glowscript.org/#/user/GlowScriptDemos/folder/Examples/program/Bounce/edit