How to create embedded modules

When embedding python, we can create custom module, populate it with objects internal to application,
and execute embedded script within this module. How to access internals of hosting application from embedded julia script.

Welcome.

You can use modules and then use them, the module can be defined in the same file or another (using include), the module name does not implies a directory like in Python.
See Modules · The Julia Language

I think this can solve your doubts, if not you should ask more concrete questions.

Can you be more specific about what you mean by “custom module”? And how are you embedding Julia/Python into your application?

I mean creation of the modules on the fly… Just look at this excerpt from python docs…

Until now, the embedded Python interpreter had no access to functionality from the application itself. The Python API allows this by extending the embedded interpreter. That is, the embedded interpreter gets extended with routines provided by the application. While it sounds complex, it is not so bad.

static int numargs=0;

/* Return the number of arguments of the application command line */
static PyObject*
emb_numargs(PyObject *self, PyObject *args)
{
    if(!PyArg_ParseTuple(args, ":numargs"))
        return NULL;
    return PyLong_FromLong(numargs);
}

static PyMethodDef EmbMethods[] = {
    {"numargs", emb_numargs, METH_VARARGS,
     "Return the number of arguments received by the process."},
    {NULL, NULL, 0, NULL}
};

static PyModuleDef EmbModule = {
    PyModuleDef_HEAD_INIT, "emb", NULL, -1, EmbMethods,
    NULL, NULL, NULL, NULL
};

static PyObject*
PyInit_emb(void)
{
    return PyModule_Create(&EmbModule);

Looking at the julia.h, i can see _jl_module_t exist. But, how to add new methods. Also i cant’ figure out how to wrap my C function to _jl_method_t.

Unless it’s important to you that it’s structured the same way as in your Python example, this looks mostly like a question about how to call a C function in the application from within the embedded Julia code. The general idea is to use ccall and you can find an example of this in https://github.com/JuliaLang/julia/blob/master/test/embedding/embedding.c, lines 104-112.

Usually it’s easier to create high level functions around the ccall on the Julia side but it’s certainly possible to do it from C as well. With a sufficiently ambitious helper library it might even look similar to the Python example, but as far as I know nobody has yet had a big enough need to create such a helper library and make it public.

1 Like

Oh, this is exactly what i want ! To make it clear, i just want` to replace python scripting support in my application.