[m-users.] Mapping of C values to constructors

Sean Charles (emacstheviking) objitsu at gmail.com
Sat Sep 25 02:01:06 AEST 2021


Hi,
As my source of inspiration and with reference to:
https://github.com/FlyingJester/SDL2.m/blob/master/sdl2.m <https://github.com/FlyingJester/SDL2.m/blob/master/sdl2.m>
around line 554….
                                                          _________\/_______
    case SDL_KEYDOWN: Event = createYesEvent(createKeyDown(e.key.keysym.sym)); break;
    default: Event = createYesEvent(createUnknown()); break;
  }
}

I think this is what is causing my crash problem, I copied that line of code and the key definitions and when I press a key down, in my C code I display the name and in the Mercury code I display what it got back and they are most definitely not the same. I suspect that the value of e.key.keysym is sometimes out of range for the key type and causing the crash.

*** Mercury runtime: caught segmentation violation ***
cause: address not mapped to object
address involved: 0x0
This may have been caused by a stack overflow, due to unbounded recursion.
exiting from signal handler
[1]    57015 segmentation fault  ./main


For example, pressing the `1` key causes this output:

>>> SDL_KEYDOWN 49
KEY-DOWN: key_g: 1
>>> SDL_KEYUP 49
KEY-UP: key_g: 1

key_g just happens to be the 50th value in the declaration of `key` so its plain to me that there needs to be some kind of mapping between them. I don’t know what the best way is to do that. On lines 97-102 of the linked file we have:

:- pred key_name(string, key).
:- mode key_name(in, uo) is semidet.
:- mode key_name(di, uo) is semidet.
:- mode key_name(uo, in) is det.
:- mode key_name(uo, di) is det.
:- mode key_name(in, in) is semidet. % implied.

I could use this code to map back from the string name and then hand that into the constructor, perhaps that’s what the intention is/was, I shall certainly try. It just seems somehow horribly inefficient but then again I guess a mapping has to happen. The only other option I was thinking about was to reflect the entire set of key definitions back into Mercury using `pragma_foreign_enum` and then change the key constructor to just be a an integer.

Any suggestions / input most welcome.

Thanks again,
Sean.





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20210924/8e1a3d85/attachment.html>


More information about the users mailing list