File: foreign.rkt

package info (click to toggle)
racket 6.1-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 91,944 kB
  • ctags: 38,251
  • sloc: ansic: 265,507; sh: 32,501; asm: 12,747; lisp: 6,958; cpp: 2,906; makefile: 2,284; pascal: 2,134; exp: 484; python: 366; xml: 11
file content (91 lines) | stat: -rw-r--r-- 3,830 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#lang racket/base
(require (for-syntax racket/base))

(define-syntax-rule (provide-except-unsafe (ulib ...) u! id ...)
  (begin
    (require ulib ...)
    (provide (except-out (all-from-out ulib ...) id ...))
    (define-syntax (u! stx)
      (syntax-case stx ()
        [(_) (with-syntax ([lib+ids (datum->syntax stx `((,#'combine-in ulib ...) id ...))])
               #'(require (only-in . lib+ids)))]))))

(provide-except-unsafe (ffi/unsafe ffi/unsafe/cvector ffi/vector) unsafe!
                       
 free end-stubborn-change
 ptr-ref ptr-set! cast
 make-sized-byte-string
 memcpy memmove memset
 malloc-immobile-cell free-immobile-cell
 malloc
 ffi-lib
 ffi-obj-ref
 get-ffi-obj
 set-ffi-obj!
 make-c-parameter
 define-c
 define-fun-syntax
 make-cvector*
 cpointer-tag set-cpointer-tag!
 cpointer-has-tag? cpointer-push-tag!
 cblock->list
 cblock->vector)

(provide provide* define-unsafer
         unsafe!)

;; This module is full of unsafe bindings that are not provided to requiring
;; modules.  Instead, an `unsafe!' binding is provided that makes these unsafe
;; bindings available.  The following two syntaxes do that: `provide*' is like
;; `provide', but using `(unsafe id)' registers an unsafe binding.  Then,
;; `define-unsafer' should be used with a binding that will expose the unsafe
;; bindings.  This might move elsewhere at some point if it turns out to be
;; useful in other contexts.
(provide provide* define-unsafer)
(define-syntaxes (provide* define-unsafer)
  (let ((unsafe-bindings '()))
    (values
     (lambda (stx)
       (syntax-case stx ()
         [(_ p ...)
          (let loop ([provides '()]
                     [unsafes  '()]
                     [ps (syntax->list #'(p ...))])
            (if (null? ps)
              (begin (set! unsafe-bindings
                           (append unsafe-bindings (reverse unsafes)))
                     (with-syntax ([(p ...) provides]) #'(provide p ...)))
              (syntax-case (car ps) (unsafe)
                [(unsafe u)
                 (syntax-case #'u (rename-out)
                   [(rename-out [from to])
                    (loop provides (cons (cons #'from #'to) unsafes) (cdr ps))]
                   [id (identifier? #'id)
                    (loop provides (cons (cons #'id #'id) unsafes) (cdr ps))]
                   [_
                    (raise-syntax-error 'provide* "bad unsafe usage"
                                        (car ps) stx)])]
                [_ (loop (cons (car ps) provides) unsafes (cdr ps))])))]))
     (lambda (stx)
       (syntax-case stx ()
         [(_ unsafe)
          (with-syntax ([(from ...)  (map car unsafe-bindings)]
                        [(to   ...)  (map cdr unsafe-bindings)]
                        [(id   ...) (generate-temporaries unsafe-bindings)])
            (set! unsafe-bindings '())
            #'(begin
                (provide (protect-out unsafe))
                (define-syntax (unsafe stx)
                  (syntax-case stx ()
                    [(_) (with-syntax ([(id ...) (list (datum->syntax
                                                        stx 'to stx)
                                                       ...)])
                           #'(begin (define-syntax id
                                      (make-rename-transformer (syntax-property
                                                                (syntax-property
                                                                 #'from
                                                                 'not-provide-all-defined
                                                                 #t)
                                                                'nominal-id
                                                                'to)))
                                    ...))]))))])))))