error if given an unrewindable partial continuation
[bpt/guile.git] / libguile / dynl.c
index 1326b8b..0175c33 100644 (file)
@@ -1,21 +1,22 @@
 /* dynl.c - dynamic linking
  *
  * Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 2001, 2002,
- * 2003, 2008, 2009 Free Software Foundation, Inc.
+ * 2003, 2008, 2009, 2010 Free Software Foundation, Inc.
  * 
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3 of
+ * the License, or (at your option) any later version.
  *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
  */
 
 
@@ -47,6 +48,7 @@ maybe_drag_in_eprintf ()
 #include <string.h>
 
 #include "libguile/_scm.h"
+#include "libguile/libpath.h"
 #include "libguile/dynl.h"
 #include "libguile/smob.h"
 #include "libguile/keywords.h"
@@ -56,6 +58,7 @@ maybe_drag_in_eprintf ()
 #include "libguile/lang.h"
 #include "libguile/validate.h"
 #include "libguile/dynwind.h"
+#include "libguile/foreign.h"
 
 #include <ltdl.h>
 
@@ -97,7 +100,7 @@ sysdep_dynl_unlink (void *handle, const char *subr)
 }
    
 static void *
-sysdep_dynl_func (const char *symb, void *handle, const char *subr)
+sysdep_dynl_value (const char *symb, void *handle, const char *subr)
 {
   void *fptr;
 
@@ -112,7 +115,24 @@ sysdep_dynl_func (const char *symb, void *handle, const char *subr)
 static void
 sysdep_dynl_init ()
 {
+  char *env;
+
   lt_dlinit ();
+
+  env = getenv ("GUILE_SYSTEM_EXTENSIONS_PATH");
+  if (env && strcmp (env, "") == 0)
+    /* special-case interpret system-ltdl-path=="" as meaning no system path,
+       which is the case during the build */
+    ; 
+  else if (env)
+    /* FIXME: should this be a colon-separated path? Or is the only point to
+       allow the build system to turn off the installed extensions path? */
+    lt_dladdsearchdir (env);
+  else
+    {
+      lt_dladdsearchdir (SCM_LIB_DIR);
+      lt_dladdsearchdir (SCM_EXTENSIONS_DIR);
+    }
 }
 
 scm_t_bits scm_tc16_dynamic_obj;
@@ -194,27 +214,31 @@ SCM_DEFINE (scm_dynamic_unlink, "dynamic-unlink", 1, 0, 0,
 #undef FUNC_NAME
 
 
-SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0, 
-            (SCM name, SCM dobj),
-           "Return a ``handle'' for the function @var{name} in the\n"
+SCM_DEFINE (scm_dynamic_pointer, "dynamic-pointer", 3, 1, 0, 
+            (SCM name, SCM type, SCM dobj, SCM len),
+           "Return a ``handle'' for the pointer @var{name} in the\n"
            "shared object referred to by @var{dobj}.  The handle\n"
-           "can be passed to @code{dynamic-call} to actually\n"
-           "call the function.\n\n"
+           "aliases a C value, and is declared to be of type\n"
+            "@var{type}. Valid types are defined in the\n"
+            "@code{(system vm ffi)} module.\n\n"
+            "This facility works by asking the operating system for\n"
+            "the address of a symbol, then assuming that it aliases a\n"
+            "value of a given type. Obviously, the user must be very\n"
+            "careful to ensure that the value actually is of the\n"
+            "declared type, or bad things will happen.\n\n"
            "Regardless whether your C compiler prepends an underscore\n"
            "@samp{_} to the global names in a program, you should\n"
            "@strong{not} include this underscore in @var{name}\n"
            "since it will be added automatically when necessary.")
-#define FUNC_NAME s_scm_dynamic_func
+#define FUNC_NAME s_scm_dynamic_pointer
 {
-  /* The returned handle is formed by casting the address of the function to a
-   * long value and converting this to a scheme number
-   */
-
-  void (*func) ();
+  void *val;
+  scm_t_foreign_type t;
 
   SCM_VALIDATE_STRING (1, name);
+  t = scm_to_unsigned_integer (type, 0, SCM_FOREIGN_TYPE_LAST);
   /*fixme* GC-problem */
-  SCM_VALIDATE_SMOB (SCM_ARG2, dobj, dynamic_obj);
+  SCM_VALIDATE_SMOB (SCM_ARG3, dobj, dynamic_obj);
   if (DYNL_HANDLE (dobj) == NULL) {
     SCM_MISC_ERROR ("Already unlinked: ~S", dobj);
   } else {
@@ -223,15 +247,36 @@ SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0,
     scm_dynwind_begin (0);
     chars = scm_to_locale_string (name);
     scm_dynwind_free (chars);
-    func = (void (*) ()) sysdep_dynl_func (chars, DYNL_HANDLE (dobj), 
-                                          FUNC_NAME);
+    val = sysdep_dynl_value (chars, DYNL_HANDLE (dobj), FUNC_NAME);
     scm_dynwind_end ();
-    return scm_from_ulong ((unsigned long) func);
+    return scm_take_foreign_pointer (t, val,
+                                     SCM_UNBNDP (len) ? 0 : scm_to_size_t (len),
+                                     NULL);
   }
 }
 #undef FUNC_NAME
 
 
+SCM_DEFINE (scm_dynamic_func, "dynamic-func", 2, 0, 0, 
+            (SCM name, SCM dobj),
+           "Return a ``handle'' for the function @var{name} in the\n"
+           "shared object referred to by @var{dobj}.  The handle\n"
+           "can be passed to @code{dynamic-call} to actually\n"
+           "call the function.\n\n"
+           "Regardless whether your C compiler prepends an underscore\n"
+           "@samp{_} to the global names in a program, you should\n"
+           "@strong{not} include this underscore in @var{name}\n"
+           "since it will be added automatically when necessary.")
+#define FUNC_NAME s_scm_dynamic_func
+{
+  return scm_dynamic_pointer (name,
+                              scm_from_uint (SCM_FOREIGN_TYPE_VOID),
+                              dobj,
+                              SCM_UNDEFINED);
+}
+#undef FUNC_NAME
+
+
 SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0, 
             (SCM func, SCM dobj),
            "Call a C function in a dynamic object.  Two styles of\n"
@@ -256,7 +301,9 @@ SCM_DEFINE (scm_dynamic_call, "dynamic-call", 2, 0, 0,
   
   if (scm_is_string (func))
     func = scm_dynamic_func (func, dobj);
-  fptr = (void (*) ()) scm_to_ulong (func);
+  SCM_VALIDATE_FOREIGN_TYPED (SCM_ARG1, func, VOID);
+
+  fptr = SCM_FOREIGN_POINTER (func, void);
   fptr ();
   return SCM_UNSPECIFIED;
 }
@@ -284,8 +331,9 @@ SCM_DEFINE (scm_dynamic_args_call, "dynamic-args-call", 3, 0, 0,
 
   if (scm_is_string (func))
     func = scm_dynamic_func (func, dobj);
+  SCM_VALIDATE_FOREIGN_TYPED (SCM_ARG1, func, VOID);
 
-  fptr = (int (*) (int, char **)) scm_to_ulong (func);
+  fptr = SCM_FOREIGN_POINTER (func, void);
 
   argv = scm_i_allocate_string_pointers (args);
   for (argc = 0; argv[argc]; argc++)