summaryrefslogtreecommitdiff
path: root/libguile/foreign.c
diff options
context:
space:
mode:
authorAndy Wingo <wingo@pobox.com>2011-03-17 10:39:02 +0100
committerAndy Wingo <wingo@pobox.com>2011-03-17 10:39:02 +0100
commit148c3317691d5b7d2414179031f87905454cb11a (patch)
treed5c8549de65fb07c83ab52461ef75574bd9bec5f /libguile/foreign.c
parentf5fc7e5710438389b21c5c754e959a5554561868 (diff)
downloadguile-148c3317691d5b7d2414179031f87905454cb11a.tar.gz
add pointer->scm, scm->pointer
* libguile/foreign.c (scm_pointer_to_scm, scm_scm_to_pointer): New functions, useful to pass and receive SCM values to and from foreign functions. * module/system/foreign.scm: Export the new functions. * doc/ref/api-foreign.texi (Foreign Variables): Add docs. * test-suite/tests/foreign.test ("pointer<->scm"): Tests.
Diffstat (limited to 'libguile/foreign.c')
-rw-r--r--libguile/foreign.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/libguile/foreign.c b/libguile/foreign.c
index 0f07c60ff..494ab5b4c 100644
--- a/libguile/foreign.c
+++ b/libguile/foreign.c
@@ -177,6 +177,34 @@ SCM_DEFINE (scm_pointer_address, "pointer-address", 1, 0, 0,
}
#undef FUNC_NAME
+SCM_DEFINE (scm_pointer_to_scm, "pointer->scm", 1, 0, 0,
+ (SCM pointer),
+ "Unsafely cast @var{pointer} to a Scheme object.\n"
+ "Cross your fingers!")
+#define FUNC_NAME s_scm_pointer_to_scm
+{
+ SCM_VALIDATE_POINTER (1, pointer);
+
+ return SCM_PACK ((scm_t_bits) SCM_POINTER_VALUE (pointer));
+}
+#undef FUNC_NAME
+
+SCM_DEFINE (scm_scm_to_pointer, "scm->pointer", 1, 0, 0,
+ (SCM scm),
+ "Return a foreign pointer object with the @code{object-address}\n"
+ "of @var{scm}.")
+#define FUNC_NAME s_scm_scm_to_pointer
+{
+ SCM ret;
+
+ ret = scm_from_pointer ((void*) SCM_UNPACK (scm), NULL);
+ if (SCM_NIMP (ret))
+ register_weak_reference (ret, scm);
+
+ return ret;
+}
+#undef FUNC_NAME
+
SCM_DEFINE (scm_pointer_to_bytevector, "pointer->bytevector", 2, 2, 0,
(SCM pointer, SCM len, SCM offset, SCM uvec_type),
"Return a bytevector aliasing the @var{len} bytes pointed\n"