How can I view Dart global variables in the WebStorm debugger? - dart

Consider this Dart CLI program:
int result = 42;
main() {
print('Result: $result');
++result;
print('New result: $result');
}
result is a global variable. If I set a breakpoint in main(), I would expect to see result somewhere in WebStorm's debugger window. However, I don't:
Is it possible to view global variables in the debugger?

Related

FFI Metatype : "cannot change a protected metatable"

If a script uses ffi.metatype and crashes unexpectedly,the next script startup produces this error : “cannot change a protected metatable”,this makes debugging real hard as I have to restart my game each time,any way to circumvent this?
Here’s a test script that demonstrates this issue,make sure to run it twice.
ffi.cdef[[
typedef struct {
float x,y;
} Crash;
]]
local Crash_Metatype = ffi.metatype("Crash",{})
print(x + y)
You can't call ffi.metatype on the same C type twice. Either set a variable after doing so and don't do so again if it's already set, or wrap the call to it in pcall so you can ignore that error.

Dart: non-nullable local variable initialized in if block fails with "must be assigned before it can be used"

I'm using Dart 2.15.1 with default setting null safety enabled. I don't understand, why this does not work:
main() {
int test;
if (1 > 0) { test = 42; }
print(test); // <- The non-nullable local variable 'test' must be assigned before it can be used.
}
while this works without any error:
main() {
int test;
if (true) { test = 42; }
print(test); // <- no error here
}
It's not a big thing, I can certainly implement a workaround, but I'm curious why dart behaves like this.
Thank you for your help.
Dart infers some control flow at compile time, but only really for very simple boolean expressions, basically only literals.
The expression 0 < 1 is guaranteed to be true, but the language specification treats it as a run-time evaluated expression, the meaning of which cannot be known until after it has been evaluated, which means it has to be typed first, and flow analysis happens during the type analysis step.
So, the if is treated in the flow analysis as a branch that can go either way.

lua_next segfault on LUA_ENVIRONINDEX?

The following C program that uses the Lua API lua_next function to try and begin iterating LUA_ENVIRONINDEX table crashes in call to lua_next...
#include <lua5.1/lua.hpp>
int main() {
lua_State* L = luaL_newstate();
lua_pushnil(L);
lua_next(L, LUA_ENVIRONINDEX);
}
Any idea why?
The manual says that LUA_ENVIRONINDEX "gives the environment of the running C function". In your code, there is no "running C function" in the sense of Lua: main was not called from Lua.
Take lua.c. If you put your code in main, then there is a crash, as you have found out. If you put your code in pmain, which is called from Lua, then there is no crash.

Attempt to call field 'registersave' (a nil value)

I'm making a lua script for DeSmuMe, a Nintendo Ds Emulator. I wanted to use this command to save a value on save state: savestate.registersave(function() return frame end)
But the emulator gives me this error: :50: attempt to call field 'registersave' (a nil value). Why? How can i solve this error?
Have you seen this function in any working script?
I checked the source code of DeSmuMe on Sourceforge.
In a file called lua-engine.cpp (last changed 2015-09-15, so after the latest release) I found this:
static const struct luaL_reg statelib [] =
{
{"create", state_create},
{"save", state_save},
{"load", state_load},
#ifndef PUBLIC_RELEASE
{"verify", state_verify}, // for desync catching
#endif
// TODO
//{"loadscriptdata", state_loadscriptdata},
//{"savescriptdata", state_savescriptdata},
//{"registersave", state_registersave},
//{"registerload", state_registerload},
{NULL, NULL}
};
So obviously savestate.registersave hasn't made it into the Lua interface yet. So you can't use it.

Const double initialised from Lua

I have a global variable:
const double myvar = 5.1;
Now, I'm converting this to read these values from Lua.
However, I can't simply do:
const double myvar = lua_tonumber(L,1);
Since main() must first execute to start the Lua intepreter etc., but if I declare myvar afterwards, it will not be global.
Is there any way to do achieve a global const variable which takes it's value from Lua?
The subtle ramifications of const can be fully understood only by language lawyers, but the basic idea of a const variable is that its value is specified at compile time. Lua values cannot be created until there is a Lua interpreter, which requires calling lua_open(), which cannot be done until run time. So no, there is no (safe, sane) way of having a const variable whose value is determined by Lua.
You can violate constness like this:
*(double*) & myvar = lua_tonumber(L,1);
but it's a -very- bad practice.
Edit: Instead of declaring const variables you can do this:
static double myvar() {
// todo: check if global L is init
return lua_tonumber(L,1);
}
or even this:
static double myvar() {
return 1.15;
}

Resources