julia> cval(x) = ccall(:jl_eval_string, Ptr{Void}, (Ptr{Cchar},), x)
cval (generic function with 1 method)
julia> cval("using DSP")
Ptr{Void} @0x00000000034bc280
julia> cval("DSP.hanning")
Ptr{Void} @0x00000000306408a0
julia> hanning = unsafe_pointer_to_objref(ptr)
hanning (generic function with 1 method)
On Tue, Sep 9, 2014 at 9:47 AM, Einar Otnes <[email protected]> wrote:
> It seems that your suggestion using ccall from within julia to play around
> with the c-api provides some info. Running your ccall command on a couple
> of "External Packages", i.e. DSP and HDF5 returns the Null pointer.
>
> pkg_ptr = ccall(:jl_eval_string,Ptr{Void},(Ptr{Cchar},),"DSP.Pkg.hanning")
> Ptr{Void} @0x0000000000000000
>
>
> pkg_ptr = ccall(:jl_eval_string,Ptr{Void},(Ptr{Cchar},),"Base.Pkg.sin")
> Ptr{Void} @0x0000000002a431f0
>
> pkg_ptr = ccall(:jl_eval_string,Ptr{Void},(Ptr{Cchar},),"Base.Pkg.cos")
> Ptr{Void} @0x00000000032acfa0
>
> pkg_ptr = ccall(:jl_eval_string,Ptr{Void},(Ptr{Cchar},),"HDF5.Pkg.h5open")
> Ptr{Void} @0x0000000000000000
>
>
> So, it seems we are not able to get hold of the external packages from
> within C. Any ideas?
>
> Best,
>
> Einar
>
>
> On Fri, Sep 5, 2014 at 5:46 PM, Jake Bolewski <[email protected]>
> wrote:
>
>> It's jl_eval_string located in jl_api.c in src.
>>
>> so you would do
>> jl_value_t * func2 = jl_eval_string("DSP.hanning")
>>
>> The best way to play around with Julia's c-api is within julia itself.
>> julia> pkg_ptr = ccall(:jl_eval_string, Ptr{Void}, (Ptr{Cchar},),
>> "Base.Pkg.clone")
>> Ptr{Void} @0x00007fd11c1754a0
>>
>> julia> unsafe_pointer_to_objref(ans)
>> clone (generic function with 2 methods)
>>
>> julia> typeof(ans)
>> Function
>>
>> That way you can prototype what you want much more easily.
>>
>> On Friday, September 5, 2014 9:32:43 AM UTC-4, Einar Otnes wrote:
>>>
>>> Thank you for your help on this. It seems that the 'jl_eval_global_var'
>>> function is local as I got the error "undefined reference to
>>> `jl_eval_global_var'" when linking. I replaced
>>> your suggestion with:
>>>
>>> jl_module_t* jl_dsp_module = (jl_module_t*)
>>> jl_get_binding(jl_main_module, jl_symbol("DSP"));
>>> jl_function_t* func2 = jl_get_function(jl_dsp_module,"hanning");
>>>
>>> This compiles OK, but I get a "segfault (core dumped)" when running the
>>> line with 'jl_get_function' .
>>>
>>> Any ideas or thoughts?
>>>
>>> Thanks,
>>>
>>> Einar
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> On Fri, Sep 5, 2014 at 11:15 AM, Ivar Nesje <[email protected]> wrote:
>>>
>>>> I would guess that something like
>>>>
>>>> module = jl_eval_global_var
>>>> <https://github.com/JuliaLang/julia/blob/6277015ee3d46f20149136d092525bec95b6e29d/src/julia.h#L917>
>>>> (jl_main_module, jl_sym
>>>> <https://github.com/JuliaLang/julia/blob/6277015ee3d46f20149136d092525bec95b6e29d/src/julia.h#L666>
>>>> ("MyModule"))
>>>>
>>>> would work, but I don't have the required testing setup to see if it
>>>> actually works. (Where is my C REPL?) The embedding API has not gotten much
>>>> attention (yet), so it is mostly documented in source, and it is likely
>>>> that there will be some adjustments.
>>>>
>>>>
>>>> On Friday, September 5, 2014 10:09:03 AM UTC+2, Einar Otnes wrote:
>>>>>
>>>>> Sorry, I'm a bit slow. How do I look up a binding for a specific
>>>>> module? In other words, how would I explicitly get to call the "fftfreq"
>>>>> function in the "DSP" module from C/C++?
>>>>>
>>>>> Is this this documented anywhere in Julia docs?
>>>>>
>>>>> Thanks,
>>>>> Einar
>>>>>
>>>>>
>>>>>
>>>>> On Wednesday, September 3, 2014 3:10:59 PM UTC+2, Isaiah wrote:
>>>>>>
>>>>>> If you have defined a module already (by eval'ing julia code?) then
>>>>>> you can look up the binding and cast that to a jl_module_t.
>>>>>> On Sep 3, 2014 8:14 AM, "Einar Otnes" <[email protected]> wrote:
>>>>>>
>>>>>>> Dear experts,
>>>>>>>
>>>>>>> I've looking at the documentation " Embedding Julia" (
>>>>>>> http://julia.readthedocs.org/en/latest/manual/embedding/) to figure
>>>>>>> out how I can call my own julia functions from within C, and I'm
>>>>>>> struggling
>>>>>>> to figure out how I should define the jl_module_t that corresponds the
>>>>>>> module I've defined. The examples show that there is an instance of
>>>>>>> jl_module_t, "jl_base_module", that you need to provide to be able to
>>>>>>> call a function defined in the base module. How do I define a
>>>>>>> corresponding
>>>>>>> jl_module_t type for the modules that are defined outside of standard
>>>>>>> julia, e.g. for the external packages or the modules I have created
>>>>>>> myself?
>>>>>>>
>>>>>>> Thanks for your help.
>>>>>>>
>>>>>>> Best regards,
>>>>>>>
>>>>>>> Einar Otnes
>>>>>>>
>>>>>>>
>>>
>