change parent key name

main
Matthew Butterick 6 years ago
parent 686d4e76aa
commit 1f447658a4

@ -27,7 +27,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
(define/augride (x:decode port parent)
(define new-parent (if (x:int? @len)
(mhasheq 'parent parent
(mhasheq x:parent-key parent
x:start-offset-key (pos port)
x:current-offset-key 0
x:length-key @len)
@ -65,7 +65,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
[(x:int? @len)
(define new-parent (mhash 'pointers null
'startOffset (pos port)
'parent parent))
x:parent-key parent))
(dict-set! new-parent 'pointerOffset (+ (pos port) (x:size array new-parent)))
(send @len x:encode (length array) port) ; encode length at front
(encode-items new-parent)
@ -79,7 +79,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
(cond
[val (define-values (new-parent len-size)
(if (x:int? @len)
(values (mhasheq 'parent parent) (send @len x:size))
(values (mhasheq x:parent-key parent) (send @len x:size))
(values parent 0)))
(define items-size (for/sum ([item val])
(send @type x:size item new-parent)))

@ -10,8 +10,9 @@
(define x:start-offset-key 'x:start-offset)
(define x:current-offset-key 'x:current-offset)
(define x:length-key 'x:length)
(define x:parent-key 'x:parent)
(define private-keys (list 'parent x:start-offset-key x:current-offset-key x:length-key))
(define private-keys (list x:parent-key x:start-offset-key x:current-offset-key x:length-key))
(define (dict-ref* d . keys)
(for/fold ([d d])

@ -17,7 +17,7 @@ https://github.com/mbutterick/restructure/blob/master/src/LazyArray.coffee
(define starting-pos (pos port)) ; ! placement matters. `resolve-length` will change `pos`
(define len (resolve-length @len port parent))
(define new-parent (if (x:int? @len)
(mhasheq 'parent parent
(mhasheq x:parent-key parent
x:start-offset-key starting-pos
x:current-offset-key 0
x:length-key @len)

@ -14,7 +14,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
(define (find-top-parent parent)
(cond
[(dict-ref parent 'parent #f) => find-top-parent]
[(dict-ref parent x:parent-key #f) => find-top-parent]
[else parent]))
(define (resolve-pointer type val)
@ -41,7 +41,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
(define relative (+ (case @pointer-relative-to
[(local) (dict-ref parent x:start-offset-key)]
[(immediate) (- (pos port) (send @offset-type x:size))]
[(parent) (dict-ref (dict-ref parent 'parent) x:start-offset-key)]
[(parent) (dict-ref (dict-ref parent x:parent-key) x:start-offset-key)]
[(global) (or (dict-ref (find-top-parent parent) x:start-offset-key) 0)]
[else (error 'unknown-pointer-style)])))
(define ptr (+ offset relative))
@ -62,7 +62,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
[val-in
(define new-parent (case @pointer-relative-to
[(local immediate) parent]
[(parent) (dict-ref parent 'parent)]
[(parent) (dict-ref parent x:parent-key)]
[(global) (find-top-parent parent)]
[else (error 'unknown-pointer-style)]))
(define relative (+ (case @pointer-relative-to
@ -72,7 +72,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
(send @offset-type x:encode (- (dict-ref new-parent 'pointerOffset) relative) port)
(define-values (type val) (resolve-pointer @type val-in))
(dict-update! new-parent 'pointers
(λ (ptrs) (append ptrs (list (mhasheq 'type type 'val val 'parent parent)))))
(λ (ptrs) (append ptrs (list (mhasheq 'type type 'val val x:parent-key parent)))))
(dict-set! new-parent 'pointerOffset
(+ (dict-ref new-parent 'pointerOffset) (send type x:size val parent)))]
[else (send @offset-type x:encode @null-value port)]))
@ -80,7 +80,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
(define/augment (x:size [val-in #f] [parent #f])
(define new-parent (case @pointer-relative-to
[(local immediate) parent]
[(parent) (dict-ref parent 'parent)]
[(parent) (dict-ref parent x:parent-key)]
[(global) (find-top-parent parent)]
[else (error 'unknown-pointer-style)]))
(define-values (type val) (resolve-pointer @type val-in))

@ -16,7 +16,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(define (xstruct-setup port parent len)
(define mheq (make-hasheq))
(dict-set*! mheq
'parent parent
x:parent-key parent
x:start-offset-key (pos port)
x:current-offset-key 0
x:length-key len)
@ -72,17 +72,17 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(dict-keys @fields)) (dict-keys val)))
(define parent (mhash 'pointers empty
'startOffset (pos port)
'parent parent-arg
x:parent-key parent-arg
'val val
'pointerSize 0))
(dict-set! parent 'pointerOffset (+ (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 'pointers))])
(send (dict-ref ptr 'type) x:encode (dict-ref ptr 'val) port (dict-ref ptr 'parent))))
(send (dict-ref ptr 'type) x:encode (dict-ref ptr 'val) port (dict-ref ptr x:parent-key))))
(define/augride (x:size [val #f] [parent-arg #f] [include-pointers #t])
(define parent (mhasheq 'parent parent-arg
(define parent (mhasheq x:parent-key parent-arg
'val val
'pointerSize 0))
(define fields-size (for/sum ([(key type) (in-dict @fields)]

@ -34,13 +34,13 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
"pointer: decode should support offsets relative to the parent"
(parameterize ([current-input-port (open-input-bytes (bytes 0 0 1 53))])
(pos (current-input-port) 2)
(check-equal? (decode (x:pointer #:relative-to 'parent) #:parent (mhash 'parent (mhash x:start-offset-key 2))) 53)))
(check-equal? (decode (x:pointer #:relative-to 'parent) #:parent (mhash x:parent-key (mhash x:start-offset-key 2))) 53)))
(test-case
"pointer: decode should support global offsets"
(parameterize ([current-input-port (open-input-bytes (bytes 1 2 4 0 0 0 53))])
(pos (current-input-port) 2)
(check-equal? (decode (x:pointer #:relative-to 'global) #:parent (mhash 'parent (mhash 'parent (mhash x:start-offset-key 2))))
(check-equal? (decode (x:pointer #:relative-to 'global) #:parent (mhash x:parent-key (mhash x:parent-key (mhash x:start-offset-key 2))))
53)))
(test-case
@ -69,15 +69,15 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
(test-case
"pointer: size should add to parent pointerSize"
(let ([parent (mhash 'parent (mhash 'pointerSize 0))])
(let ([parent (mhash x:parent-key (mhash 'pointerSize 0))])
(check-equal? (size (x:pointer #:relative-to 'parent) 10 #:parent parent) 1)
(check-equal? (dict-ref* parent 'parent 'pointerSize) 1)))
(check-equal? (dict-ref* parent x:parent-key 'pointerSize) 1)))
(test-case
"pointer: size should add to global pointerSize"
(let ([parent (mhash 'parent (mhash 'parent (mhash 'parent (mhash 'pointerSize 0))))])
(let ([parent (mhash x:parent-key (mhash x:parent-key (mhash x:parent-key (mhash 'pointerSize 0))))])
(check-equal? (size (x:pointer #:relative-to 'global) 10 #:parent parent) 1)
(check-equal? (dict-ref* parent 'parent 'parent 'parent 'pointerSize) 1)))
(check-equal? (dict-ref* parent x:parent-key x:parent-key x:parent-key 'pointerSize) 1)))
(test-case
"pointer: size should handle void pointers"
@ -116,7 +116,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
(check-equal? (dict-ref parent 'pointerOffset) 2)
(check-equal? (dict-ref parent 'pointers) (list (mhasheq 'type uint8
'val 10
'parent parent)))
x:parent-key parent)))
(check-equal? (get-output-bytes (current-output-port)) (bytes 1))))
(test-case
@ -130,38 +130,38 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
(check-equal? (dict-ref parent 'pointerOffset) 2)
(check-equal? (dict-ref parent 'pointers) (list (mhasheq 'type uint8
'val 10
'parent parent)))
x:parent-key parent)))
(check-equal? (get-output-bytes (current-output-port)) (bytes 0))))
(test-case
"pointer: encode should handle offsets relative to parent"
(parameterize ([current-output-port (open-output-bytes)])
(define parent (mhash 'parent (mhash 'pointerSize 0
(define parent (mhash x:parent-key (mhash 'pointerSize 0
'startOffset 3
'pointerOffset 5
'pointers null)))
(encode (x:pointer #:relative-to 'parent) 10 #:parent parent)
(check-equal? (dict-ref* parent 'parent 'pointerOffset) 6)
(check-equal? (dict-ref* parent 'parent 'pointers) (list (mhasheq 'type uint8
(check-equal? (dict-ref* parent x:parent-key 'pointerOffset) 6)
(check-equal? (dict-ref* parent x:parent-key 'pointers) (list (mhasheq 'type uint8
'val 10
'parent parent)))
x:parent-key parent)))
(check-equal? (get-output-bytes (current-output-port)) (bytes 2))))
(test-case
"pointer: encode should handle global offsets"
(parameterize ([current-output-port (open-output-bytes)])
(define parent (mhash 'parent
(mhash 'parent
(mhash 'parent (mhash 'pointerSize 0
(define parent (mhash x:parent-key
(mhash x:parent-key
(mhash x:parent-key (mhash 'pointerSize 0
'startOffset 3
'pointerOffset 5
'pointers null)))))
(encode (x:pointer #:relative-to 'global) 10 #:parent parent)
(check-equal? (dict-ref* parent 'parent 'parent 'parent 'pointerOffset) 6)
(check-equal? (dict-ref* parent 'parent 'parent 'parent 'pointers)
(check-equal? (dict-ref* parent x:parent-key x:parent-key x:parent-key 'pointerOffset) 6)
(check-equal? (dict-ref* parent x:parent-key x:parent-key x:parent-key 'pointers)
(list (mhasheq 'type uint8
'val 10
'parent parent)))
x:parent-key parent)))
(check-equal? (get-output-bytes (current-output-port)) (bytes 5))))
(test-case
@ -173,7 +173,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
'pointers null))
(encode (x:pointer uint8 'void) (x:void-pointer uint8 55) #:parent parent)
(check-equal? (dict-ref parent 'pointerOffset) 2)
(check-equal? (dict-ref parent 'pointers) (list (mhasheq 'type uint8 'val 55 'parent parent)))
(check-equal? (dict-ref parent 'pointers) (list (mhasheq 'type uint8 'val 55 x:parent-key parent)))
(check-equal? (get-output-bytes (current-output-port)) (bytes 1))))
(test-case

@ -66,7 +66,7 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
(raise-argument-error 'xversioned-struct-encode "dict" encode-me))
(define parent (mhash 'pointers null
'startOffset (pos port)
'parent parent-arg
x:parent-key parent-arg
'val encode-me
'pointerSize 0))
(dict-set! parent 'pointerOffset (+ (pos port) (x:size encode-me parent #f)))
@ -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 'pointers))])
(send (dict-ref ptr 'type) x:encode (dict-ref ptr 'val) port (dict-ref ptr 'parent))))
(send (dict-ref ptr 'type) x:encode (dict-ref ptr 'val) 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 'parent parent-arg 'val val 'pointerSize 0))
(define parent (mhash x:parent-key parent-arg 'val val 'pointerSize 0))
(define version-size
(let ([struct-type @type])
(if (or (symbol? struct-type) (procedure? struct-type))

Loading…
Cancel
Save