Error while running VM code in VMEmulator

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Error while running VM code in VMEmulator

sarthak
snakeGame.zip
Above is a zip file containing the code where I tried to implement the game of snake. But when I try to run the compiled code in VMEmulator it shows the following error:
Error:Allocated Memory size must be positive.
Why am I getting this error?
Reply | Threaded
Open this post in threaded view
|

Re: Error while running VM code in VMEmulator

ybakos
It seems that Jack classes must have at least one field defined. Your SnakeGame class only has static members. I haven't come across this before and never noticed it in the language spec (if that requirement even exists).

How to debug:
- press the stop button
- look at the Call Stack panel
- notice that the program crashes when SnakeGame.new is invoked.

Hence, you know the error may likely be due to something in the constructor.

When I add a field int x to your SnakeGame class, the game runs (but crashes due to another error).
Reply | Threaded
Open this post in threaded view
|

Re: Error while running VM code in VMEmulator

cadet1620
Administrator
ybakos wrote
It seems that Jack classes must have at least one field defined. Your SnakeGame class only has static members. I haven't come across this before and never noticed it in the language spec (if that requirement even exists).
Jack classes that are instantiated must have at least one field. As long as it's not constructed it's OK.
Here's a short test that will show the error.
class NoField
{
    constructor NoField new() {
        return this;
    }
    function void hello() {
        do Output.printString("Hello ");
        return;
    }
    method void world() {
        do Output.printString("world!");
        return;
    }
}

class Main
{
    function void main() {
        var NoField nof;
        do NoField.hello();
        let nof = NoField.new();
        do nof.world();
        return;
    }
}
Using JackCompiler this prints "Hello ERR5". With my compiler it prints "Hello world!". I would call this a JackCompiler error.

That Memory.alloc() can't handle zero length is documented, in the error code list if nowhere else.

The requirement to allocate at least 1 word when constructing zero length string constants is mentioned somewhere but I can't find it in a quick scan of chapters 11 and 12 where it would likely be.

I don't find any mention of the need to allocate at least 1 word when constructing objects. I would expect it it be somewhere in chapter 11 if it's anywhere.

My compiler code reads:
            # allocate and initialize this -- note that the OS chokes on
            # alloc(0) so we must alloc 1 word minimum.
            words = self.symbolTable.VarCount(SYMK_FIELD)
            if words == 0:
                words = 1
            self.vmWriter.WritePush(SEG_CONST, words)
            self.vmWriter.WriteCall('Memory.alloc', 1)

Note that I may have just done this out of habit. I never call malloc(0). I've been bit by too many subtle (and not so subtle) bugs in systems that claim to allow it!

--Mark