unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Ted Zlatanov <tzz@lifelogs.com>
To: emacs-devel@gnu.org
Subject: Re: DSO-style FFI
Date: Wed, 09 Oct 2013 19:21:09 -0400	[thread overview]
Message-ID: <874n8qt4ii.fsf@flea.lifelogs.com> (raw)
In-Reply-To: 877gdnfq84.fsf@fleche.redhat.com

On Tue, 08 Oct 2013 14:43:39 -0600 Tom Tromey <tromey@redhat.com> wrote: 

Ted> The foreign function has to take Emacs Lisp_Objects (maybe just strings
Ted> and numbers) and package its return data in a Lisp_Object.  So how do we
Ted> handle that glue with libffi or anything else without promising some
Ted> minimal internal Emacs ABI?

Tom> The idea is to have the FFI code be part of Emacs.  So when Emacs
Tom> changes, it does too.  What doesn't change is the API presented to
Tom> elisp.

Tom> This is how FFI works in other languages -- Common Lisp, Python,
Tom> probably Guile (I didn't check), Java (it's part of reflection there),
Tom> etc.

It would really help if we discussed a small example, and forgive my
ignorance here.

Say I have a transforming function:

int gnutls_xform(char* datain, char* dataout);

I want to use a Lisp_Object, a string, for `datain'; and get back another
Lisp_Object, again a string, in `dataout'.

So, something like this would be done on a given function_pointer to
`gnutls_xform', with some given Lisp_Object `input_data':

       ffi_cif cif;
       ffi_type *args[2];
       void *values[2];
       char *in = SDATA (input_data);
       char *out = xzmalloc(100);
       int rc;
     
       args[0] = &ffi_type_pointer;
       values[0] = &in;

       args[1] = &ffi_type_pointer;
       values[1] = &out;
          
       /* Initialize the cif */
       if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1,
     		       &ffi_type_uint, args) == FFI_OK)
         {
           ffi_call(&cif, function_pointer, &rc, values);
           /* rc now holds the result of the call */
         }
       
       Lisp_Object ret = Qnil;
       // if rc shows success...
       if (rc != ERROR)
       {
          ret = make_string (out);
       }

       xfree(out);

This would be more generic of course, but keeping it simple for now
really helps.

As far as the dynamic function loading to give us function_pointer, I am
guessing it's done with libltdl, by calling

  module = lt_dlopen("emacs_gnutls_module");

and then

  lt_dlsym(module, "gnutls_xform");

as shown in
http://www.gnu.org/software/libtool/manual/html_node/Libltdl-interface.html

Is that more or less where we should be going?  Is this today's state of
the art?  Based on the other languages you listed, it seems so.

My immediate concerns are security (these modules can do almost
anything); concurrency between modules and Emacs; and between modules
loaded by concurrent Emacs threads in a hypothetical future where we
have such threads. But in terms of practical integration they seem
workable.

Thanks for your patience
Ted




  reply	other threads:[~2013-10-09 23:21 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-06  9:15 GNU Emacs-libnettle-libhogweed integration patch v1 Ted Zlatanov
2013-10-06  9:58 ` bignum support in Emacs with libgmp (was: GNU Emacs-libnettle-libhogweed integration patch v1) Ted Zlatanov
2013-10-06 16:09 ` GNU Emacs-libnettle-libhogweed integration patch v1 Eli Zaretskii
2013-10-06 21:07   ` Ted Zlatanov
2013-10-06 16:51 ` Stefan Monnier
2013-10-06 16:58   ` Eli Zaretskii
2013-10-06 21:19   ` Ted Zlatanov
2013-10-07  4:02     ` Stefan Monnier
2013-10-07 11:41       ` Ted Zlatanov
2013-10-07 22:03         ` Ted Zlatanov
2013-10-07 22:58           ` Stefan Monnier
2013-10-07 23:43             ` Emacs crypto use cases (was: GNU Emacs-libnettle-libhogweed integration patch v1) Ted Zlatanov
2013-10-08  3:02               ` Emacs crypto use cases Stefan Monnier
2013-10-08 10:33                 ` Ted Zlatanov
2013-10-08 13:17                   ` Stephen J. Turnbull
2013-10-08 16:35                   ` DSO-style FFI (was: Emacs crypto use cases) Stefan Monnier
2013-10-08 17:32                     ` DSO-style FFI Tom Tromey
2013-10-08 19:42                       ` Ted Zlatanov
2013-10-08 20:43                         ` Tom Tromey
2013-10-09 23:21                           ` Ted Zlatanov [this message]
2013-10-10  8:09                             ` Andreas Schwab
2013-10-08 20:47                         ` Davis Herring
2013-10-09 22:26                           ` Ted Zlatanov
2013-10-09 23:52                             ` Davis Herring
2013-10-10  1:25                               ` Ted Zlatanov
2013-10-10  4:36                                 ` DSO-style DSOs (this is NOT an FFI!) Stephen J. Turnbull
2013-10-09  1:48                       ` DSO-style FFI Stephen J. Turnbull
2013-10-09  2:40                       ` Stefan Monnier
2013-10-12 15:34                         ` Michael Welsh Duggan
2013-10-12 18:55                           ` Stefan Monnier
2013-10-18 13:31                             ` Ted Zlatanov
2013-10-19 14:41                               ` Stefan Monnier
2013-10-19 15:08                               ` Stefan Monnier
2013-10-19 17:33                               ` Andy Moreton
2013-10-19 19:44                                 ` Ted Zlatanov
2013-10-12 23:36                           ` Stephen J. Turnbull
2013-10-08 19:50                     ` Ted Zlatanov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=874n8qt4ii.fsf@flea.lifelogs.com \
    --to=tzz@lifelogs.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).