Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InternalError: Too much recursion [closed]

I'm making a simple JavaScript Life implementation to experiment with JavaScript 1.8's new features, and I'm getting an "InternalError: too much recursion" using this code, and a world size of 300×300:

function LifeWorld(hDim, vDim) {
    var data = let(that = this) Array.make(hDim, function(x) Array.make(vDim, function(y) new LifeCell(that, x, y)));

    this.item = function(x, y) {
        return (data[x] && data[x][y]) || null;
    };

    this.draw = function(g) {
        g.fillRect(0, 0, this.scale, this.scale);
    };

    this.scale = 5;
    this.offsetX = 0;
    this.offsetY = 0;

    // Finally, initialize all the cells and let them recognize their neighbours:
    try {
        for(let i = 0; i < ARRAY_SIZE * ARRAY_SIZE; i++) {
            this.item(i / ARRAY_SIZE | 0, i % ARRAY_SIZE).init();
        }
    } catch(e) {
        alert(e);
    }
}

function LifeCell(world, x, y) {
    var ul, u, ur,
        l,      r,
        bl, b, br;

    Object.defineProperties(this, {
        world: {
            get: function() {
                return this.world;
            }
        },
        x: {
            get: function() {
                return this.x;
            }
        },
        y: {
            get: function() {
                return this.y;
            }
        }
    });

    this.init = function() {
        alert('Init ' + x + ', ' + y);
        [ul, u, ur,
         l,      r,
         bl, b, br] = [world.item(this.x - 1, this.y - 1), world.item(this.x, this.y - 1), world.item(this.x + 1, this.y - 1),
                       world.item(this.x - 1, this.y),     world.item(this.x, this.y),     world.item(this.x + 1, this.y),
                       world.item(this.x - 1, this.y + 1), world.item(this.x, this.y + 1), world.item(this.x + 1, this.y + 1)];
        delete this.init;
    };
}

I get one alert, "Init 0, 0," so everything before the initialization works correctly, but then I get the exception message. It looks like it must have something to do with world.item, but I don't know how — world.item just returns something.

I can't debug using Firebug, either, because this code apparently makes it crash Firefox. Can anyone figure out what's going wrong here?

like image 534
Ry- Avatar asked Nov 26 '11 21:11

Ry-


1 Answers

Your recursion is coming from this piece of code:

x: {
    get: function() {
        return this.x;
    }
},

You're returning the getter itself, which will return the getter itself, etc, which causes an endless recursion. It seems to be the case for all your getters. For this reason it might be better to drop the idea of getters as they can cause frustration like this.

http://jsfiddle.net/8hVwb/

like image 149
pimvdb Avatar answered Sep 30 '22 20:09

pimvdb