Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is in possible to define embedded modules? #452

Open
k12Sergey opened this issue Sep 7, 2024 · 6 comments
Open

Is in possible to define embedded modules? #452

k12Sergey opened this issue Sep 7, 2024 · 6 comments

Comments

@k12Sergey
Copy link

Does julia poses fundamental limitations to declare bindings withoud shared lib (executable with embedded julia), like pybind11 PYBIND11_EMBEDDED_MODULE?

I try to create julia module and sand it through CxxWrap functions like this (it is not register function and crash during registration process)

   jlcxx::cxxwrap_init();
    jl_module_t* module = jl_new_module(jl_symbol("example"), nullptr);
    jlcxx::Module& newModule = jlcxx::registry().create_module(module);
    newModule.method("greet", &greet);

Its seems very interesting to embed julia in existing huge cpp project (which is very complicated to split in dll to make a module).

@k12Sergey
Copy link
Author

I try to use mingw and it obviously compile with julia CxxWrap module,
but still cant call

   jl_eval_string(
        R"(
        using example
        println(1))");

@barche
Copy link
Collaborator

barche commented Sep 8, 2024

If I understand correctly, this is more or less what is done in this test here: https://github.com/JuliaInterop/libcxxwrap-julia/blob/main/test/test_module.cpp

@k12Sergey
Copy link
Author

k12Sergey commented Sep 8, 2024

If I understand correctly, this is more or less what is done in this test here: https://github.com/JuliaInterop/libcxxwrap-julia/blob/main/test/test_module.cpp

Thanks for reply. Its seems like what i searching for.
But i cant manage to call method binded this way from embedded julia (test case is only checked names)

(its exactly an exmple except of std part and last lines, wehere i try to call binded method)

JLCXX_MODULE register_test_module(jlcxx::Module& mod)
{
    using namespace test_module;

    mod.add_type<Foo>("Foo")
        .method("getx", &Foo::getx);

    mod.method("test_method", [] () {return 1;});
}

void __dummy_protect(jl_value_t*) {}

int main(int argc, char *argv[])
{
    jlcxx::cxxwrap_init();

    jl_value_t* mod = jl_eval_string(R"(
        module TestModule
          const __cxxwrap_pointers = Ptr{Cvoid}[]
        end
    )");
    JL_GC_PUSH1(&mod);

    if (jlcxx::julia_type_name(jl_typeof(mod)) != "Module")
    {
        std::cout << "TestModule creation failed" << std::endl;
        return 1;
    }

    register_julia_module((jl_module_t*)mod, register_test_module);
    jl_call1(jl_get_function(jlcxx::get_cxxwrap_module(), "wraptypes"), mod);

    jl_value_t* dt = jl_eval_string("TestModule.Foo");
    if(jlcxx::julia_type_name(dt) != "Foo")
    {
        std::cout << "unexpected type name: " << jlcxx::julia_type_name(dt) << std::endl;
        return 1;
    }
// ***** CALL BINDED METHOD *****
    jl_eval_string(R"(
        v = TestModule.test_method()
        println(v)
    )");
}

There is no crash. Simply no output in cmd. (But it is with such line println(1), so its broken with call TestModule.test_method)

Does i understand correct: we could call for binded methods from within c++ embedded julia?

@k12Sergey
Copy link
Author

k12Sergey commented Sep 8, 2024

I found in the doc about @wrapmodule @wraptypes @wrapfunctions which called in example with

jl_call1(jl_get_function(jlcxx::get_cxxwrap_module(), "wraptypes"), mod);

add wrapfunctions trying to call function from module, no effect

@k12Sergey
Copy link
Author

wrapfunctions does not exist in jlcxx::get_cxxwrap_module() but wraptypes do.
in the following code ptr is not zero

jl_value_t* dt = jl_eval_string("TestModule.Foo");

even so

jl_eval_string("v = TestModule.Foo()")

not working

@barche
Copy link
Collaborator

barche commented Sep 20, 2024

I have to streamline this some more, ideally we should be able to call the CxxWrap.readmodule function as a first step, but this is currently not possible since it assumes a shared library path as input. It should be relatively easy to add a method to this function that takes a C function pointer directly, however.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants