Hacker News new | past | comments | ask | show | jobs | submit login

Not so much a VM as a jit compiler.

In principle, you can use any compiler of your choice to fake this: Just create a dynamic library for each line that has been entered into the REPL that you immediately load and call into.

Now, 'all' you need to do is cut out the middleman and directly output properly relocated code to memory. There are compilers that can do so. Clang/LLVM is one one of them. TinyCC is another.




If so, isn't that rather similar to how Visual C++ allows you to modify code in flight while debugging by rolling back to the beginning of the function call?


Exist a distilled example of this? I'm totally lost in how make compiled/static code to work as dynamic.


The word "dynamic" in the grandparent post refers to dynamically relocatable object files, i.e. all the memory references in the binary are written as relative offsets so that the binary doesn't assume anything about what address it will have within the process when it gets loaded. Concretely, this means the difference between "JMP 0xDEADBEEF" versus "JMP (special_register)+0xBEEF" so that the .so or .dll file doesn't assume it'll get loaded at 0xDEAD0000.

For a less confusing explanation, see https://en.wikipedia.org/wiki/Position-independent_code

(Also: no, it's not really implemented by using one offset register for each .so file in the process.)


Interesting. How would a line compile on its own if it's, say, "n += 2;" without providing the n?


That is indeed an issue. One way to solve it is to make all declarations at top-level of the REPL into globals.

Another way would be to operate at block- instead of line-level and just not allow local variable to be shared between blocks. Ie

    int i = 5 ↵
    printf("%i", i) ↵
would fail, but

    { ↵
        int i = 5; ↵
        printf("%i", i); ↵
    } ↵
would not.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: