cs: fix make-ctype on converter that accepts extra arguments

Related to #3457
This commit is contained in:
Matthew Flatt 2021-01-15 12:18:53 -07:00
parent 1550179a37
commit e56d8c5ded
2 changed files with 25 additions and 2 deletions

View File

@ -1539,6 +1539,21 @@
(test #f register-process-global bstr #f)
(test #"data" cast (register-process-global orig-bstr #"data\0") _pointer _bytes))
;; ----------------------------------------
;; Make sure `make-ctype` is not confused by a conversion function that
;; could have extra arguments
(let ()
(define _strnum (make-ctype _int
(lambda (s [ignored 'ignore-me])
(test 'ignore-me values ignored)
(string->number s))
(lambda (n [ignored 'ignore-me])
(test 'ignore-me values ignored)
(number->string n))))
(test 5 cast "5" _strnum _int)
(test "5" cast 5 _int _strnum))
;; ----------------------------------------
(report-errs)

View File

@ -315,7 +315,7 @@
(create-compound-ctype (ctype-host-rep type)
(ctype-our-rep type)
type
racket-to-c
(protect-racket-to-c racket-to-c)
c-to-racket
(compound-ctype-get-decls type)
(compound-ctype-size type)
@ -325,9 +325,17 @@
(create-ctype (ctype-host-rep type)
(ctype-our-rep type)
type
racket-to-c
(protect-racket-to-c racket-to-c)
c-to-racket)]))
(define (protect-racket-to-c racket-to-c)
;; Make sure `racket-to-c` is not confused for an internal
;; variant that accepts a `who` argument:
(if (and (#%procedure? racket-to-c)
(chez:procedure-arity-includes? racket-to-c 2))
(lambda (v) (racket-to-c v))
racket-to-c))
;; Apply all the conversion wrappers of `type` to the Scheme value `v`
(define (s->c who type v)
(let* ([racket-to-c (ctype-scheme->c type)]