[plt-scheme] gentle intro to the new FFI?

From: Corey Sweeney (corey.sweeney at gmail.com)
Date: Fri Apr 29 17:19:11 EDT 2005

I've been playing with the ffi too.  So far i've making 2 examples.  I
don't know if they count as 'proper', but here's what i got so far:


;;;first one, add 2 numbers

;;;in c:
uint8 addints(uint8 n, uint8 m){
      printf(\"add routine, called!!!!\\n\");
      uint8 temp = (n+m);
      bool test_making_booleans = 0;
      bool is_carry_flag_setp = (temp < n) || (temp < m);
      if (1 == is_carry_flag_setp) printf(\"hey! the carry flag is set!!!\\n\");
      return (n+m);
}

;;;in scheme:
(define add-in-c
  (get-ffi-obj "addints" mathlib
                (_fun _uint8 _uint8 -> _uint8)))

(add-in-c 255 255)
;;;should return 254





;;next, process two lists of 8 bit numbers:
;;(it's called add32, but it's 8 bit numbers)

;;in c:
void* add32( uint8* array1, int array1_length, 
             uint8* array2, int array2_length, 
             int* output_length ){
      *output_length = 5;
      printf(\"add32, called!!!!\\n\");
      int temp;
      //uint8 output[MAX( array1_length, array2_length) + 1];
      uint8 output[5]={4,4,4,4,4};
      output[3]=5;
      //return (output);
      return (array1);
      //return (array2);
}
//right now it just returns the original array.


;;;in scheme 
(define out-length #f)

(define add-c-32
  (get-ffi-obj 'add32 mathlib
               (_fun (the-list : (_list i _uint8)) (_int = (length the-list)) 
                     (the-list2 : (_list i _uint8)) (_int = (length the-list2))
                     (the-output-length : (_ptr io _int)) -> (output : _pointer)
                     -> (list output the-output-length))))
                                 ;(ptr-ref output (_list-struct _uint8
_uint8 _uint8)))))
                                 ;(_list o _uint8 the-output-length)))))
(define tempz (add-c-32 (list 4 2 250 2 8 7) (list 1 2 3 4 3) 0))

(require (lib "1.ss" "srfi"))
(eval `(ptr-ref (first tempz) (_list-struct ,@(make-list (second
tempz) `_uint8))))


I'm sure there's a cleaner way to get lists out, but it demonstrates
the functionality of a lot of the components.  the lists going in
works perfect.


This might help some,

Corey


On 4/29/05, Eric Hanchrow <offby1 at blarg.net> wrote:
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> 
> I can see, in 299, foreign.ss -- and it's mighty intriguing.  I've
> already gotten it working, a little, but haven't figured out all the
> details.  The combination of documentation and examples probably
> contain all the information that I need, in theory, but it's pretty
> terse, and I haven't been able to figure it out ...  Is there anything
> else written that demonstrates the proper use of foreign.ss?
> --
> Okay, a word about VoteHere: This is the company that has no
> visible means of support.  It doesn't seem to sell anything.
>         -- Bev Harris, blackboxvoting.org
> 
>



Posted on the users mailing list.