luvit/luvit

Attempting to run the FFI's "Hello World" example (from luajit.org) results in an error when using luvit, but works in luajit (?)

Duckwhale opened this issue · 6 comments

So I've been trying to learn more about Lua/C interactions, and to start simple I attempted to run the easiest example I could find.
The code is taken from http://luajit.org/ext_ffi.html directly:

local ffi = require("ffi")

-- Works ONLY when using luajit
ffi.cdef [[
	int printf(const char *fmt, ...);
	]]

ffi.C.printf("Hello %s!", "world")

To my surprise, it didn't work when using luvit to run the script, raising the following error:

image

However, if I run the same code using luajit, it works as expected. Did I do something wrong?

I also tried running the second example, and it works in luvit as well as luajit:

local ffi = require("ffi")
ffi.cdef[[
int MessageBoxA(void *w, const char *txt, const char *cap, int type);
]]
ffi.C.MessageBoxA(nil, "Hello world!", "Test", 0)

Additional information:

image

image

Works for me on Linux:
image

Are you running this on Windows? You need to load msvcrt.

local C = ffi.os == "Windows" and ffi.load("msvcrt") or ffi.C

Yes, that's on Windows. Attempting to load the runtime didn't change anything, though.

Are there differences in how Luvit searches for files included via ffi or does luajit have something it doesn't? Seems like a strange issue to face, but then I don't understand how things work internally yet.

Luvit has absolutely nothing to do with FFI at all, all of this is completely handled by LuaJIT itself, the one included into the Luvit binary. So it doesn't really make sense that the same version of LuaJIT works when the other does not.

Though, I can't really make sure of any of this, since it looks like it only happen on Windows I assume.

I believe that you have to do this due to the way luvi is compiled for Windows. This trick works on my machine, so I'm not sure what's wrong for you.
image

Are you sure you're using the user-defined C and not ffi.C when testing?

Are you sure you're using the user-defined C and not ffi.C when testing?

Apparently I wasn't. It's working now, thank you! (Small victories 😂 )