From 5709a3756c8390d5c18ed26119caecf89fcd129f Mon Sep 17 00:00:00 2001 From: Peter Bex Date: Tue, 7 May 2019 21:00:34 +0200 Subject: [PATCH] Fix how define-foreign-type defines its conversion procedures It used to call "define", but this may not be available in the current macro environment (or refer to something else than Scheme "define"), so it was changed in de342aacd5bd18a6533e4eb4e04c2955e0f02959 to use the ##core#set compiler form instead. This would cause problems when define-foreign-type was used inside a module, because later references to the type would trigger an "undefined export" error, which would cause the module to be unresolvable. These procedures are gensymed, so they should not actually be registered for export, but this restores the old behaviour based on how "define" works at toplevel. The compiler form already checks that it is used at toplevel, so we may safely make the assumption that this won't expand to an internal define in a lambda. --- core.scm | 7 +++++++ tests/compiler-tests.scm | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/core.scm b/core.scm index d9aa8a4c..0f08a002 100644 --- a/core.scm +++ b/core.scm @@ -1237,6 +1237,13 @@ (mark-variable ret '##compiler#always-bound) (hide-variable arg) (hide-variable ret) + ;; NOTE: Above we already check + ;; we're in toplevel context, so + ;; we can unconditionally register + ;; the export here. + ;; TODO: Remove after fixing #1615 + (##sys#register-export arg (##sys#current-module)) + (##sys#register-export ret (##sys#current-module)) (walk `(##core#begin (##core#set! ,arg ,(first conv)) diff --git a/tests/compiler-tests.scm b/tests/compiler-tests.scm index 6e5c8b27..d4585e37 100644 --- a/tests/compiler-tests.scm +++ b/tests/compiler-tests.scm @@ -69,6 +69,19 @@ (import x) (assert (= 1 ((bar 42)))) + +;; Test custom foreign type conversions + +(module y (my-add1) + (import scheme (chicken base) (chicken foreign)) + + (define-foreign-type my-int integer add1 sub1) + + (define my-add1 (foreign-lambda* my-int ((my-int x)) "C_return(x+1);"))) + +(import y) +(assert (= 2 (my-add1 1))) + ;;; rev. 14574 (reported by Peter Bex) ; ; - type specifiers in foreign-lambda in macros are incorrectly renamed -- 2.11.0