luaJIT FFI

Dan Olson via digitalmars-d-ldc digitalmars-d-ldc at puremagic.com
Thu Apr 14 21:23:44 PDT 2016


andi <cadetschimmel at gmail.com> writes:

> On Monday, 11 April 2016 at 15:18:47 UTC, Dan Olson wrote:
>> andi <cadetschimmel at gmail.com> writes:
>>> next i did was to call into luaJIT from D, working as
>>> expected. then i
>>> thought, ok
>>> no problem to do vice versa, but i ran into problems. im not sure
>>> how
>>> to properly export
>>> those symbols into c global namespace, or maybe i get something
>>> totally wrong. doing extern (C) and defining my functions, but im
>>> having troubles to call them via luaJIT FFI.
>>
>> Hi,
>>
>> Can you post details on the problem?  Is it a link time error or
>> runtime?  OS (e.g Linux)?  Is main controlled by Lua (not using a D
>> main)?  If so, you will need to initialize D runtime (rt_init or
>> Runtime.initialize).
>
> Im using win7 and win10, visual studio 2013 + VisualD and
> ldc2-0.15.2-beta2-win64-msvc.
> and im using luaJIT 2.1 beta2
>
> i think its easier to simply upload everything i got so far, so here
> it is my current project:
> http://seed-engine.com/downloads/seed2_sdk.zip
>
> in src/seed2/main.d is my int main()
> in bin/core/core.lua im testing luaJITs FFI
>
> thx again

I haven't used Lua but just looking at your example core.lua, it looks
just like Lua FFI documentation for calling C code.  Can you post the
actual error you see?

I extracted interesting parts of your zip file.  I note that your main.d
has a D main() function, so you don't need to call rt_init().

-- from core.lua --
local ffi = require("ffi")
ffi.cdef[[
    void call_dfunction();
]]
print("hello from luaJIT")
ffi.C.call_dfunction()

-- from main.d --
extern (C) public void call_dfunction()
{
    writeln("call_dfunction");
}

You don't need public though.
-- 
Dan



More information about the digitalmars-d-ldc mailing list