summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lisplib.c6
-rw-r--r--share/txr/stdlib/ffi.tl12
-rw-r--r--txr.142
3 files changed, 57 insertions, 3 deletions
diff --git a/lisplib.c b/lisplib.c
index 3e57d627..02c84529 100644
--- a/lisplib.c
+++ b/lisplib.c
@@ -524,9 +524,9 @@ static val ffi_set_entries(val dlt, val fun)
{
val name[] = {
lit("with-dyn-lib"), lit("deffi"), lit("deffi-type"), lit("deffi-cb"),
- lit("deffi-var"), lit("typedef"), lit("sizeof"), lit("alignof"),
- lit("offsetof"), lit("arraysize"), lit("elemsize"), lit("elemtype"),
- lit("ffi"), lit("carray-ref"), lit("carray-sub"),
+ lit("deffi-sym"), lit("deffi-var"), lit("typedef"), lit("sizeof"),
+ lit("alignof"), lit("offsetof"), lit("arraysize"), lit("elemsize"),
+ lit("elemtype"), lit("ffi"), lit("carray-ref"), lit("carray-sub"),
nil
};
set_dlt_entries(dlt, name, fun);
diff --git a/share/txr/stdlib/ffi.tl b/share/txr/stdlib/ffi.tl
index a041692f..a430f777 100644
--- a/share/txr/stdlib/ffi.tl
+++ b/share/txr/stdlib/ffi.tl
@@ -96,6 +96,18 @@
(defmacro deffi-cb-unsafe (:form f name rettype argtypes)
(sys:deffi-cb-expander f name rettype argtypes nil nil))
+(defmacro deffi-sym (:form f name var-expr : type-sym)
+ (let ((var-ref (cond
+ ((stringp var-expr)
+ ^(dlsym-checked sys:ffi-lib ,var-expr))
+ ((consp var-expr)
+ (mac-param-bind f (sym ver) var-expr
+ ^(dlvsym-checked sys:ffi-lib ,sym ,ver)))
+ (t var-expr))))
+ ^(defparml ,name ,(if type-sym
+ ^(cptr-cast ',type-sym ,var-ref)
+ var-ref))))
+
(defmacro deffi-var (:form f name var-expr type)
(let ((var-ref (cond
((stringp var-expr)
diff --git a/txr.1 b/txr.1
index 27b1bad9..42c6bb0f 100644
--- a/txr.1
+++ b/txr.1
@@ -56438,6 +56438,48 @@ form need not be surrounded by a call to the
macro.
.RE
+.coNP Macro @ deffi-sym
+.synb
+.mets (deffi-sym < name < var-expr <> [ type-sym ])
+.syne
+.desc
+The
+.code deffi-sym
+macro defines a global lexical variable called
+.code name
+whose value is a
+.code cptr
+object that refers to a symbol in a foreign library.
+
+The
+.meta name
+argument gives the name for the variable to be defined.
+This definition takes place place as if by the
+.code defparml
+macro.
+
+The
+.meta var-expr
+is syntax which specifies the foreign pointer, using exactly the same
+conventions as described for the
+.code deffi-var
+macro, allowing for a short-hand notation if this form is
+enclosed in a
+.code with-dyn-lib
+macro invocation.
+
+The optional
+.meta type-sym
+argument must be a symbol. If it is absent, it defaults to nil.
+This argument specifies the type label for the
+.code cptr
+object which holds the pointer to the foreign symbol.
+
+The result value of
+.meta deffi-sym
+is the symbol
+.metn name .
+
.coNP Macro @ typedef
.synb
.mets (typedef < name << type-syntax )