Thanks again. What is jl_init("/Users/inorton/git/jl71/usr/lib/")?
On Saturday, September 10, 2016 at 12:44:32 AM UTC+8, Isaiah wrote: > > (Disregard my suggestion about `Ptr{TestType}`, `jl_call1` only takes > boxed arguments so that won't work) > > The problem is that the passed argument type does not match any available > method (because it isn't actually passed a julia type at all) so `jl_call1` > silently gives up. The `jl_call*` methods are in TRY/CATCH blocks to > prevent spilling errors across the API, and they do not print error > messages. > > The following example works, it's not really efficient but it does > demonstrate type construction from C: > > #include <julia.h> > #include <iostream> > using namespace std; > > struct TestType { > double a; > double b; > }; > int main(int argc, char *argv[]) > { > TestType A, *ret; > A.a=2.; > A.b=3.; > jl_init("/Users/inorton/git/jl71/usr/lib/"); > jl_load("TestArg.jl"); > jl_value_t * mod = (jl_value_t*)jl_eval_string("TestArg"); > > // construct a TestType instance > > jl_value_t* jl_A = > jl_new_struct((jl_datatype_t*)jl_get_function((jl_module_t*)mod, > "TestType"), > jl_box_float64(A.a), > jl_box_float64(A.b)); > > jl_function_t * func = jl_get_function((jl_module_t*)mod,"TestFunc"); > jl_call1(func, jl_A); > > jl_atexit_hook(0); > return 0; > } > > > Using the embedding API requires a substantial depth of understanding of , > including stack vs heap and argument passing, as well as dynamic language > concepts such as boxing, and rooting. The embedding section of the manual > is designed to provide an overview, but ultimately you are going to have to > read and grok the Julia source to get every little detail. > > On Fri, Sep 9, 2016 at 9:23 AM, K leo <cnbi...@gmail.com <javascript:>> > wrote: > >> Isaiah, >> >> Thanks for the reply. >> >> I tried your advice A, i.e. "change to an immutable on julia side, and >> change function signature to 'Ptr{TestType}'", and the code behaves the >> same as before, i.e. it compiles OK and runs OK but does not show the >> output of the println in the function. >> >> I guess at the moment what puzzles me is not whether the data get passed >> correctly (because I am not trying to access the data yet), but why the one >> single statement in the Julia function is not executed. That function >> appears to be taken correctly because if I change the function name in >> Julia then there is a core dump. Any thoughts? >> >> I have not yet tried your advice B as I am not too clear about it. >> >> >> On Friday, September 9, 2016 at 8:52:30 PM UTC+8, Isaiah wrote: >>> >>> In c, 'struct TestType' is plain old data, not a Julia type >>> (jl_value_t). Either change to an immutable on julia side, and change >>> function signature to 'Ptr{TestType}'; or allocate a 'jl_value_t*' with >>> the correct type tag, and set the fields. >>> >>> On Friday, September 9, 2016, K leo <cnbi...@gmail.com> wrote: >>> >>>> I tried the following, it compiles OK and runs OK, but it appears the >>>> julia function is not called (because there is no output from the println >>>> statement). What is wrong? >>>> >>>> #include <julia.h> >>>>> #include <iostream> >>>>> using namespace std; >>>>> struct TestType { >>>>> double a; >>>>> double b; >>>>> }; >>>>> int main(int argc, char *argv[]) >>>>> { >>>>> TestType A, *ret; >>>>> A.a=2.; >>>>> A.b=3.; >>>>> jl_init(NULL); >>>>> jl_load("TestArg.jl"); >>>>> jl_value_t * mod = (jl_value_t*)jl_eval_string("TestArg"); >>>>> jl_function_t * func = >>>>> jl_get_function((jl_module_t*)mod,"TestFunc"); >>>>> jl_call1(func, (jl_value_t *)&A); >>>>> jl_atexit_hook(0); >>>>> return 0; >>>>> } >>>>> # TestArg.jl >>>>> module TestArg >>>>> type TestType >>>>> a::Float64 >>>>> b::Float64 >>>>> end >>>>> function TestFunc(data::TestType) >>>>> println("in TestArg.TestFunc ") >>>>> end >>>>> end >>>> >>>> >>>> >