fungos / cr

cr.h: A Simple C Hot Reload Header-only Library
https://fungos.github.io/cr-simple-c-hot-reload/
MIT License
1.54k stars 101 forks source link

CR_LOAD called on first update, contrary to what the documentation states. Is this correct? #60

Closed danielytics closed 2 years ago

danielytics commented 3 years ago

Hi,

In the documentation for CR_LOAD, it says:

This does not happen when a plugin is loaded for the first time as there is no state to restore;

However, I'm observing that it gets called on initial call to cr_plugin_update. Is this the expected behaviour (and the documentation is wrong)?

My test code:

// main.cpp
#define CR_HOST
#include <cr.h>

int main (int argc, char* argv[])
{
    cr_plugin ctx;
    cr_plugin_open(ctx, "./test.so");
    cr_plugin_update(ctx);
    cr_plugin_close(ctx);
    return 0;
}
// test.cpp
#include <cr.h>
#include <iostream>

CR_EXPORT int cr_main(cr_plugin* ctx, cr_op operation)
{
    switch (operation) {
        case CR_STEP:
        {
            std::cout << "STEP\n";
            break;
        }
        case CR_CLOSE:
        {
            std::cout << "CLOSE\n";
            break;
        }
        case CR_LOAD:
        {
            std::cout << "LOAD\n";
            break;
        }
        case CR_UNLOAD:
        {
            std::cout << "UNLOAD\n";
            break;
        }
    }
    return 0;
}

Compiled with:

clang++ -fPIC -Icr -c test.cpp -o test.o
clang++ -shared test.o -o test.so
clang++ -Icr -c main.cpp -o main.o
clang++ -ldl main.o -o main

Running ./main gives the output:

LOAD
STEP
CLOSE
fungos commented 3 years ago

Thanks for reporting that, I'll take a look once I have a bit of time. I suspect the behavior changed after a few fixes on the versioning flow and it is a bit hard to take a decision now without risking breaking any users. I will need to improve the tests and try to track down when this changed. But I would probably accept a PR to change the documentation to reflect this behavior in the meantime.

danielytics commented 3 years ago

I'm not sure what to change the documentation to... But what I can do, later in the week or at the weekend, when I have a little time, is run git bisect to find the last commit where it doesn't call CR_LOAD on first step.