samtupy / nvgt

The Nonvisual Gaming Toolkit
https://nvgt.gg
Other
43 stars 27 forks source link

Figure out how to print proper exceptions if they take place while initializing global variables #29

Closed patricus3 closed 1 month ago

patricus3 commented 1 month ago

hi, I decided to make an experiment and made a program to divide by 0, just to see if it catches it and gives a nice informative error message. but it just said that variable isn't initialized it may happen from time to time in games, and it may be a nightmare to debug without a proper error handling here.

samtupy commented 1 month ago

Hi, When you post an issue like this, please also post the script you used to test it. Divide by 0 does work, but I'm guessing that you indeed didn't initialize a variable when testing, so the engine did give you the correct error. Observe the following script.

void main() {
 int v = 0;
 v /= 0;
}

You'll see that you get a divide by 0 compilation error as expected. Now sometimes the compiler can't tell you that a divide by 0 error is happening, so it must happen as a runtime error. Observe.

void main() {
    int v;
    int v2 = 1;
    v2 -= 1;
    v /= v2;
}

So you see, in either case I do not get an uninitialized variable error. This uninitialized variable thing happens if for example one of your scripts has int variable; instead of int variable = 0 If the compiler tries accessing a variable that has not yet ever been assigned to or set with a default value, you could get that warning. Hopefully this helps.

patricus3 commented 1 month ago

int test1=1; int test2=0; int test3=test1/test2; void main() { alert("borkidy bork",test3); show_window("test"); while(!key_pressed(KEY_ESCAPE)) { wait(5); } }``

samtupy commented 1 month ago

OK, now understood. I can try to fix this indeed, it's not really a divide by 0 issue more than it is that any exception reported while initializing a global is reported like that. Observe the following and that the same behavior is taking place.

class test {
    test() { throw("haha"); }
}
test t;
void main() {
    alert("test", "this should not execute");
}

This isn't executing because the test class throws an exception when it tries to initialize, and exepcitons are not being properly reported during global variable initialization. I'll see if I can get Angelscript to show such exceptions properly. Thanks!