Yes, this is the 'native' function in the new 64-bit version. Is not documented
in the reference yet, was only discussed in the mailing list so far. You could
search for 'native' in
http://www.mail-archive.com/picolisp@software-lab.de
In a nutshell, it works like this:
: (native "@" "getenv" 'S "TERM")
-> "xterm"
'native' takes a shared library ("@" refers to the main program), a function
name (here "getenv"), a spec for the return value type, and arguments.
The return value type may be
NIL void
B byte # Byte (unsigned)
C char # Character (UTF-8, 1-3 bytes)
I int # Integer (32 bit)
N long # Long or pointer (64 bit)
S string # String (UTF-8)
or a nested list of these primitives for filling a structure.
: (native "@" "printf" 'I "abc%d%s^J" (+ 3 4) (pack "X" "Y" "Z"))
abc7XYZ
-> 8
(use Tim
(native "@" "time" NIL '(Tim (8 B . 8))) # time_t 8
(native "@" "localtime" '(I . 9) (cons NIL (8) Tim)) )
This returns the first nine integer values of 'localtime'.
If you write a function
long cosine(long angle, int scale) {
double scl = (double)scale;
return round(scl * cos((double)angle / scl));
}
into a shared library "dll.so", then you can call it as
: (setq *Scl 5)
: (native "./dll.so" "cosine" 'N 3.14159 1.0)
-> -100000
You can also write such functions as inline-C. In a Lisp source file
you write
(load "lib/native.l")
(gcc "math" NIL
(cosine (Angle Scale) 'N Angle Scale) )
#include <math.h>
long cosine(long angle, int scale) {
double scl = (double)scale;
return round(scl * cos((double)angle / scl));
}
/**/
(setq *Scl 5)
and then you can call it directly
: (cosine 3.14159 1.0)
-> -100000
It is compiled on-the-fly, and stored as a defintion in the symol 'cosine'.