Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect empty loop #1825

Open
marcelaraujo opened this issue Jan 13, 2015 · 5 comments
Open

Detect empty loop #1825

marcelaraujo opened this issue Jan 13, 2015 · 5 comments

Comments

@marcelaraujo
Copy link

Hey, I found this bug when I have this template

FATAL ERROR: JS Allocation failed - process out of memory

https://gist.github.com/marcelaraujo/e4b35cb90a15699a5f8a

screen shot 2015-01-13 at 16 07 50

@ForbesLindesay
Copy link
Member

That compiles to an infinite loop:

- var n = 0
ul
    while n < 20
    = n++

is approximately like writing:

buf.push('<ul>');
while (n < 20) {
}
buf.push(n++);
buf.push('</ul>');

You need to indent the code within the while statement:

- var n = 0
ul
    while n < 20
        = n++

This error is unfortunately impossible to statically detect in the most general case (it requires solving the halting problem). We could, however, do one of two things that would perhaps produce useful results:

  1. Error on totally empty while block, at least if the condition is side effect free
  2. Record the start time when rendering templates, then check current time - start time inside any loops (for, while, do {...} while etc.) so we could time out after something like 10 seconds.

The first might be possible to make a non-breaking change, but it is very limited. The second option is a definite breaking change, and it makes for very slow feedback.

@marcelaraujo
Copy link
Author

Owwww

Is this an old known issue?

Hummmm.. I don't think that any of given solutions would be the best choice because we don't know how long the Jade will take to complete the compile process.

Is there not any regular expression could catch this case?

@vendethiel
Copy link
Contributor

This isn't an issue, the code is just misindented.

@ForbesLindesay
Copy link
Member

@marcelaraujo This is a known issue with all turing complete languages. Look up "The Halting Problem". It is one of the most famous computer science problems there is. It cannot be caught with a regular expression.

It would of course be possible to catch the specific case you illustrated (as I suggested in option 1) but it is not possible in the most general case. Consider the following example JavaScript program:

function foo() {
  return true;
}
var i = 0;
while (i % 2 === 0) {
  i++;
  if (foo()) i++;
}

As a human I can prove that that program never terminates. It gets stuck in an infinite loop. The only way to know that for a computer though, is to execute the program. We may sometimes be able to do clever tricks to spot cases where it will never finish, but it is not possible in all cases.

@TimothyGu TimothyGu changed the title Fatal Error Detect empty loop Jan 28, 2015
@ForbesLindesay
Copy link
Member

We can apply https://github.com/ForbesLindesay/halting-problem to loops, which will catch a lot of the most common of these cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants