luajit
Closed this issue · 11 comments
Hello,
Are these bindings compatible with luajit ?
Thanks.
Good question, I don't know. What are the prerequisites for luajit? What kind of application have you in mind?
I'm using luajit to normalize lambda terms by evaluating lua code as
part of the project Dedukti
(https://www.rocq.inria.fr/deducteam/Dedukti/index.html).
It would be nice if I could embed luajit in ocaml.
From the LuaJIT installation page I read:
LuaJIT is API-compatible with Lua 5.1. If you've already embedded Lua into your application,
you probably don't need to do anything to switch to LuaJIT, except link with a different library:
So you could try to install LuaJIT and then modify _oasis to link with LuaJIT instead of the "regular" Lua library. I can try myself, but I think... next week end.
Ok. I'll try that.
:-(
I was too curious and a quick test compiling and linking with LuaJIT resulted in...
$ ./cpcall
Must use luaL_newstate() for 64 bit target
Segmentation fault (core dumped)
I'll investigate further.
Ok @rsaill I found out where the problem resides. In the LuaJIT documentation, here it's clearly stated that you should not call lua_newstate directly. This makes sense, because LuaJIT wants to use it's own custom allocator for maximum performace.
The problem is that my OCaml binding also wants to use it's own allocator, not for performance but to ensure the thread safety of the library, OCaml side. Search the function custom_alloc here.
I can write a quick patch to be used with LuaJIT, but you lose thread safety.
The other possible "solution" is to create a One Global Lock to Rule Them All, so to serialize each and every call of the library, but I don't want this.
In the present state, the OCaml library is thread safe: you can have several OCaml threads and the only constraint is that you cannot share Lua states among different threads, this because Lua itself is not thread safe. But sharing a Lua state between two threads is kind of evil thing to do :-)
Making the library thread unsafe (OCaml side) means that you (the user) must manually ensure that, if you use threads, you call library functions only in one thread.
What do you think?
I'm not using OCaml threads (and don't plan to do) so I guess the thread safety is not very important for me.
Ok I can work on a patch to make ocaml-lua compatible with LuaJIT, but it will be a build option not enabled by default, something like:
$ ./configure --enable-luajit
Is it OK for you?
Just added a very quick patch. Check out the "luajit" branch and try compiling with something like:
./configure --enable-luajit --enable-memory-test --enable-debug --enable-stub-debug --enable-examples && make
Let me know if it works as expected.
Thanks for the quick patch.
It seems to work. I'll let you know how it goes after more testing.
Please, debug a lot! ;-)
If it works I'll merge the patch in master, but I have to write also some documentation.