Skip to content

Instantly share code, notes, and snippets.

@jkominek
Created May 28, 2015 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkominek/214d2f0a95e6c1604122 to your computer and use it in GitHub Desktop.
Save jkominek/214d2f0a95e6c1604122 to your computer and use it in GitHub Desktop.
inspectors & parameters to limit the dynamic extent values can be used within
#lang racket
; idea: use inspectors & parameters to protect values so they
; can't be used outside of approved dynamic extents.
; probably not fully baked? (continuations, etc)
; (define-values (b lock) (locked-box important-v key))
; (parameterize ([lock key])
; ; in here b can be used
; (less-trusted-code b))
; ; now b can no longer be used, even if less-trusted-code kept a copy
(define-values (lockable-inspector-prop thing1 thing2)
(make-impersonator-property 'thing?))
(define (locked-box v key #:lock [lock (make-parameter #f)])
(values
(chaperone-box (box v)
(lambda (b v)
(if (eq? (lock) key)
v
(error "you don't have the key!")))
(lambda (b v)
(if (eq? (lock) key)
v
(error "you don't have the key!")))
lockable-inspector-prop
(void))
lock 'lock-parameter))
(define secret (gensym))
(define-values (b lock) (locked-box 'secretsquirrel secret))
; fail (caught)
(with-handlers ([values (lambda (e) (exn-message e))])
(unbox b))
; ok
(parameterize ([lock secret])
(unbox b))
; fail on print (uncaught)
(parameterize ([lock secret])
b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment