PMunch / ratel

128 stars 4 forks source link

Fixes #5 #6

Closed yglukhov closed 2 years ago

PMunch commented 2 years ago

Hmm, I just tested this and it has a slight problem. By compiling the example from the website you get this C code (I've cut away anything not related to the PROGMEM stuff):

static const tyArray__dZz0Jqb39aIV9b8WFOgJ0n7g dataX60gensym1___rateltest_83 PROGMEM = ("Hello world\n");
static const tyArray__0j8sSXZOeBGmu6W0H2n0Uw dataX60gensym12___rateltest_146 PROGMEM = ("Led is on\n");
static const tyArray__xnsf9cq2WcTBn9bwtKzIBeLg dataX60gensym17___rateltest_175 PROGMEM = ("Led is off\n");
N_LIB_PRIVATE N_NIMCALL(void, NimMainModule)(void) {
    send__rateltest_97(((NCSTRING) (dataX60gensym1___rateltest_83)), ((NI) 12));
    while (1) {
        nimZeroMem((void*)dataX60gensym12___rateltest_146, sizeof(tyArray__0j8sSXZOeBGmu6W0H2n0Uw));
        send__rateltest_97(((NCSTRING) (dataX60gensym12___rateltest_146)), ((NI) 10))
        nimZeroMem((void*)dataX60gensym17___rateltest_175, sizeof(tyArray__xnsf9cq2
        send__rateltest_97(((NCSTRING) (dataX60gensym17___rateltest_175)), ((NI) 11));
    }
}

So it seems to work fine in the global scope, but then break inside the loop. This is the same behaviour as the old code. The trick to get it to import pgmspace.h is great though!

PMunch commented 2 years ago

I implemented a slightly different fix in https://github.com/PMunch/ratel/commit/7b0c0538c0221cebd7fd71d0a7a1ea115e6c052a. By making them globals it stopped Nim from creating the zeroing calls. I was also able to make it be defined by using let which means that Nim shouldn't allow the user to try and modify it. That commit also improves upon the escaping procedures and now if you define the same literal string twice in your code it will only be saved to progmem once which is pretty neat. It also implements your trick to ensure that the header is imported wherever it is needed.