Hi,
I've been wondering how to do FFI in my toy interpreted language. The interpreter is written in Nim. Now I can create a ".gene" file and use the interpreter to run it. The question is if I want to implement FFI in Gene language, means I write half Gene and half Nim and let the interpreter run it, is this possible? If not, what are the alternatives?
Maybe store a Table[string, proc (args: seq[Value])] or something like that inside the VM, and create an opcode for Nim calls?
I'm not sure about the specifics of your VM. It would be helpful if you could provide some more information.
(Nitpicking: that's a classical interpreter and has nothing to do with "just in time" compilation. It always helps to get the basic vocabulary right.)
There are two ways to do what you need, one is to use libFFI, see https://github.com/nim-lang/Nim/blob/devel/compiler/evalffi.nim
The other, more traditional way is to demand a common callback type like proc (args: seq[Value]) {.nimcall.}. Extension writers then need to write Nim code that adapts the existing code to follow this interface, this implies marshalling code from int, float, string, tuples of these etc to your Value type.
It's also the reason why VMs create code interoperability problems and create a "pleasant" two layer debugging experiences where you're far more unproductive than simply sticking to a single good language (=Nim) that can do everything reasonably well.
Thanks a lot. That's exactly what I was looking for. I'll play with it and see what I can do.
Also thanks for pointing out the wrong terminology I used.