Embedding Julia and Packages

Hi,
I’m guessing the answer but anyway would like a confirmation. Reading the Embedding docs I see no reference to the possibility to call Julia modules from other languages, right?

There is this jl_function_t *func = jl_get_function(jl_base_module, "sqrt"); which suggest that other than jl_base_module could be used but couldn’t find any further documentation on the jl_get_function (also found #22190 but couldn’t understand the outcome of it too).

1 Like

jl_eval_string("Base")

Sorry, I used a bad naming. I meant to say if we could call functions from Julia Packages.
I am still investigating the possibility to establish a two-way communication between Julia and Red but for it to work data would have to be transmitted back and forth. But if can’t call functions from my Packages (or others) everything falls down.

It’s a bit unclear what do you mean by “calling” a module since modules are not callable. You can run arbitrary julia code with jl_eval_string, which includes importing and obtaining a reference to a module. Then you can get any global variables in the module with jl_get_global and call them or do whatever you want on them.

It’s a bit unclear what do you mean by “calling” a module

I rephrased it in the second post to call functions from Julia Packages which includes the need to be able to send data to the Julia side and eventually get some other back to caller in the external side.
Something called eval_string seams a bit limiting but I never ventured in this Julia domain.

You shouldn’t use eval_string for all calls. However, it is the right function to define your own julia helper. Similar to PyPy, since a lot of the features are defined in the language itself (not C), we define only a small number of C-API to evaluate julia code and to do some basic operations on objects and you generally need to create a few of your own julia function helpers to do what you want. It is the most natural way since a lot of the things you want to do are defined in julia and you can also take advantage of the JIT that way.

Thanks. Let’s say that I understand the general picture … now the details

Currently the best (only?) full non-C embedding example is probably pyjulia. It uses a global Dict (via PyCall) to store a reference to every wrapped object in order to prevent garbage collection.

For other general API usage examples from C(++), look at CxxWrap (uses a global array for persistent references), and for embedding see node-julia, and maybe julia.js (not sure how far along it is by now, but it looked promising).

Thanks. mexjulia.jl also has an embedding example. And I see that it uses jl_function_t *fn = jl_get_function(jl_main_module, fnName); (not jl_base_module like in the Embedding docs).