Embedding Julia in Julia

Suppose you were writing a GUI tool for Julia, in Julia. For instance if you were making an IDE or notebook-type interface with a Qt or Vulkan-based GUI. In order to execute Julia code that the user wants you to run within it, you wouldn’t want to execute it within the context of the Julia instance that was actually running the UI tool, for reasons of crash prevention, namespace clashes, and interactivity. How easy would it to be to embed a Julia instance within the tool, in the same way you would if your tool was written in c++? Sure, you could use a socket-type interface to a sub-interpreter like web-based UIs do, or you could use Julia’s distributed processing capabilities but you lose the performance and direct access benefits that you get from talking to Julia through shared address space like you can in c++, and the ability to directly query the interpreter with synchronous function calls.

One possibility might be to write c++ code that exports the julia jl_ functions from C back to Julia, but that seems convoluted, and would seem to obscure some of the advantages you should have because of the program invoking the interpreter being written in the same language as the code being passed to the sub-interpreter.

Are there any examples of doing this kind of thing?

Maybe this does what you want?

1 Like

Hmm, I’m not sure, it looks like it interprets the code in the context of the Julia session that called the interpreter?
What I would want is different session states, such that if the GUI tool used ‘using’ to import a package it wanted, that the user’s code running inside the tool would not see that package (w/o importing it itself)…or that garbage collection inside of the user’s code would be separate from inside the tools code… that defining a global function on either side would not cause it to be on the other side… that getting an exception in the users code would not proagate the exeception to the tool’s code, etc.

AFAIU you can implement a debugger with it: so it should support some kind of isolation. But for details we have to wait for the specialists:)

If not, I guess the best alternative would be to load Julia into another process, use OS message passing protocols to talk to it, and use SharedArrays when I want to avoid data copying.