Guides for implementing the external functions interface

I am currently working on a scripting language that FFI does not yet have. I would like to know what is the most convenient way to get it, assuming that I would like to write it like cool geeks - I would like to write FFI in a scripting language.

The programming language I need for the interface is C. Therefore, for the basics, I know that libdl.so is my best friend. Obviously, this is not the only thing I need, but the most important of them.

I have only a small idea of ​​what I need for this. I would like to get similar behavior from FFI, like ctypes python.

What do I need to know to go this far? I know that there is serious magic with data structures that I will have to deal with. How do I manage this so that I can do most of this serious magic in the scripting language itself? I would use such magic much more than just an external function interface. For example, I could transfer C-like binary data to files.

+3
source share
2 answers

I think a suitable answer requires a detailed essay .

, , . C, , C . . , C , , . C script:

/* arith.dll */
/* A sample C function callable from the scripting language. */

#include "my_script.h" // Data structures used by the script interpreter.

My_Script_Object* add(My_Script_Object* num1, My_Script_Object* num2)
{
   int a = My_Script_Object_To_Int(num1);
   int b = My_Script_Object_To_Int(num2);
   return Int_To_My_Script_Object(a + b);
}

/* End of arith.dll */

// Script using the dll
clib = open_library("arith.dll");

// if it has first-class functions
add_func = clib.find([add int int]);
if (cfunc != null)
{
   sum = add_func(10, 20);
   print(sum);
}

// otherwise
print(clib.call("add", 10 20));

. , ..

, , :

http://common-lisp.net/project/cffi/ http://www.nongnu.org/cinvoke/

+3

http://sourceware.org/libffi/

, , . , , , Java JNI libffi.

+1

Source: https://habr.com/ru/post/1713284/


All Articles