and rename some others too

main
Matthew Butterick 6 years ago
parent 214f8992d9
commit 5fced8eda0

@ -64,13 +64,13 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
(cond
[(x:int? @len)
(define new-parent (mhash x:pointers-key null
'startOffset (pos port)
x:alt-start-offset-key (pos port)
x:parent-key parent))
(hash-set! new-parent x:pointer-offset-key (+ (pos port) (x:size array new-parent)))
(send @len x:encode (length array) port) ; encode length at front
(encode-items new-parent)
(for ([ptr (in-list (hash-ref new-parent x:pointers-key))]) ; encode pointer data at end
(send (hash-ref ptr 'type) x:encode (hash-ref ptr 'val) port))]
(send (hash-ref ptr 'type) x:encode (hash-ref ptr x:val-key) port))]
[else (encode-items parent)]))
(define/augride (x:size [val #f] [parent #f])

@ -14,9 +14,11 @@
(define x:pointer-size-key 'x:pointer-size)
(define x:pointers-key 'x:pointers)
(define x:pointer-offset-key 'x:pointer-offset)
(define x:val-key 'x:val)
(define x:alt-start-offset-key 'x:alt-start-offset)
(define private-keys (list x:parent-key x:start-offset-key x:current-offset-key x:length-key x:pointer-size-key
x:pointers-key x:pointer-offset-key))
x:pointers-key x:pointer-offset-key x:alt-start-offset-key))
(define (hash-ref* d . keys)
(for/fold ([d d])

@ -66,13 +66,13 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
[(global) (find-top-parent parent)]
[else (error 'unknown-pointer-style)]))
(define relative (+ (case @pointer-relative-to
[(local parent) (hash-ref new-parent 'startOffset)]
[(local parent) (hash-ref new-parent x:alt-start-offset-key)]
[(immediate) (+ (pos port) (send @offset-type x:size val-in parent))]
[(global) 0])))
(send @offset-type x:encode (- (hash-ref new-parent x:pointer-offset-key) relative) port)
(define-values (type val) (resolve-pointer @type val-in))
(hash-update! new-parent x:pointers-key
(λ (ptrs) (append ptrs (list (mhasheq 'type type 'val val x:parent-key parent)))))
(λ (ptrs) (append ptrs (list (mhasheq 'type type x:val-key val x:parent-key parent)))))
(hash-set! new-parent x:pointer-offset-key
(+ (hash-ref new-parent x:pointer-offset-key) (send type x:size val parent)))]
[else (send @offset-type x:encode @null-value port)]))

@ -71,19 +71,19 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(format "dict that contains superset of xstruct keys: ~a"
(dict-keys @fields)) (dict-keys val)))
(define parent (mhash x:pointers-key empty
'startOffset (pos port)
x:alt-start-offset-key (pos port)
x:parent-key parent-arg
'val val
x:val-key val
x:pointer-size-key 0))
(dict-set! parent x:pointer-offset-key (+ (pos port) (x:size val parent #f)))
(for ([(key type) (in-dict @fields)])
(send type x:encode (dict-ref val key) port parent))
(for ([ptr (in-list (dict-ref parent x:pointers-key))])
(send (dict-ref ptr 'type) x:encode (dict-ref ptr 'val) port (dict-ref ptr x:parent-key))))
(send (dict-ref ptr 'type) x:encode (dict-ref ptr x:val-key) port (dict-ref ptr x:parent-key))))
(define/augride (x:size [val #f] [parent-arg #f] [include-pointers #t])
(define parent (mhasheq x:parent-key parent-arg
'val val
x:val-key val
x:pointer-size-key 0))
(define fields-size (for/sum ([(key type) (in-dict @fields)]
#:when (xenomorphic-type? type))

@ -98,7 +98,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
"pointer: encode should handle null pointers"
(parameterize ([current-output-port (open-output-bytes)])
(define parent (mhash x:pointer-size-key 0
'startOffset 0
x:alt-start-offset-key 0
x:pointer-offset-key 0
x:pointers-key null))
(encode (x:pointer) #f #:parent parent)
@ -109,13 +109,13 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
"pointer: encode should handle local offsets"
(parameterize ([current-output-port (open-output-bytes)])
(define parent (mhash x:pointer-size-key 0
'startOffset 0
x:alt-start-offset-key 0
x:pointer-offset-key 1
x:pointers-key null))
(encode (x:pointer) 10 #:parent parent)
(check-equal? (hash-ref parent x:pointer-offset-key) 2)
(check-equal? (hash-ref parent x:pointers-key) (list (mhasheq 'type uint8
'val 10
x:val-key 10
x:parent-key parent)))
(check-equal? (get-output-bytes (current-output-port)) (bytes 1))))
@ -123,13 +123,13 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
"pointer: encode should handle immediate offsets"
(parameterize ([current-output-port (open-output-bytes)])
(define parent (mhash x:pointer-size-key 0
'startOffset 0
x:alt-start-offset-key 0
x:pointer-offset-key 1
x:pointers-key null))
(encode (x:pointer #:relative-to 'immediate) 10 #:parent parent)
(check-equal? (hash-ref parent x:pointer-offset-key) 2)
(check-equal? (hash-ref parent x:pointers-key) (list (mhasheq 'type uint8
'val 10
x:val-key 10
x:parent-key parent)))
(check-equal? (get-output-bytes (current-output-port)) (bytes 0))))
@ -137,13 +137,13 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
"pointer: encode should handle offsets relative to parent"
(parameterize ([current-output-port (open-output-bytes)])
(define parent (mhash x:parent-key (mhash x:pointer-size-key 0
'startOffset 3
x:alt-start-offset-key 3
x:pointer-offset-key 5
x:pointers-key null)))
(encode (x:pointer #:relative-to 'parent) 10 #:parent parent)
(check-equal? (hash-ref* parent x:parent-key x:pointer-offset-key) 6)
(check-equal? (hash-ref* parent x:parent-key x:pointers-key) (list (mhasheq 'type uint8
'val 10
x:val-key 10
x:parent-key parent)))
(check-equal? (get-output-bytes (current-output-port)) (bytes 2))))
@ -153,14 +153,14 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
(define parent (mhash x:parent-key
(mhash x:parent-key
(mhash x:parent-key (mhash x:pointer-size-key 0
'startOffset 3
x:alt-start-offset-key 3
x:pointer-offset-key 5
x:pointers-key null)))))
(encode (x:pointer #:relative-to 'global) 10 #:parent parent)
(check-equal? (hash-ref* parent x:parent-key x:parent-key x:parent-key x:pointer-offset-key) 6)
(check-equal? (hash-ref* parent x:parent-key x:parent-key x:parent-key x:pointers-key)
(list (mhasheq 'type uint8
'val 10
x:val-key 10
x:parent-key parent)))
(check-equal? (get-output-bytes (current-output-port)) (bytes 5))))
@ -168,19 +168,19 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
"pointer: encode should support void pointers"
(parameterize ([current-output-port (open-output-bytes)])
(define parent (mhash x:pointer-size-key 0
'startOffset 0
x:alt-start-offset-key 0
x:pointer-offset-key 1
x:pointers-key null))
(encode (x:pointer uint8 'void) (x:void-pointer uint8 55) #:parent parent)
(check-equal? (hash-ref parent x:pointer-offset-key) 2)
(check-equal? (hash-ref parent x:pointers-key) (list (mhasheq 'type uint8 'val 55 x:parent-key parent)))
(check-equal? (hash-ref parent x:pointers-key) (list (mhasheq 'type uint8 x:val-key 55 x:parent-key parent)))
(check-equal? (get-output-bytes (current-output-port)) (bytes 1))))
(test-case
"pointer: encode should throw if not a void pointer instance"
(parameterize ([current-output-port (open-output-bytes)])
(define parent (mhash x:pointer-size-key 0
'startOffset 0
x:alt-start-offset-key 0
x:pointer-offset-key 1
x:pointers-key null))
(check-exn exn:fail:contract? (λ () (encode (x:pointer uint8 'void) 44 #:parent parent)))))

@ -65,9 +65,9 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
(unless (dict? encode-me)
(raise-argument-error 'xversioned-struct-encode "dict" encode-me))
(define parent (mhash x:pointers-key null
'startOffset (pos port)
x:alt-start-offset-key (pos port)
x:parent-key parent-arg
'val encode-me
x:val-key encode-me
x:pointer-size-key 0))
(dict-set! parent x:pointer-offset-key (+ (pos port) (x:size encode-me parent #f)))
(unless (or (symbol? @type) (procedure? @type))
@ -83,12 +83,12 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
(for ([(key type) (in-dict fields)])
(send type x:encode (dict-ref encode-me key) port parent))
(for ([ptr (in-list (dict-ref parent x:pointers-key))])
(send (dict-ref ptr 'type) x:encode (dict-ref ptr 'val) port (dict-ref ptr x:parent-key))))
(send (dict-ref ptr 'type) x:encode (dict-ref ptr x:val-key) port (dict-ref ptr x:parent-key))))
(define/override (x:size [val #f] [parent-arg #f] [include-pointers #t])
(unless val
(raise-argument-error 'xversioned-struct-size "value" val))
(define parent (mhash x:parent-key parent-arg 'val val x:pointer-size-key 0))
(define parent (mhash x:parent-key parent-arg x:val-key val x:pointer-size-key 0))
(define version-size
(let ([struct-type @type])
(if (or (symbol? struct-type) (procedure? struct-type))

Loading…
Cancel
Save