structify pointer

main
Matthew Butterick 6 years ago
parent ee9a861090
commit 21b380587d

@ -68,7 +68,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
(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 x:pointer-type-key) x:encode (hash-ref ptr x:val-key) port))]
(send (x:ptr-type ptr) x:encode (x:ptr-val ptr) port))]
[else (encode-items parent)]))
(define/augride (x:size [val #f] [parent #f])

@ -4,6 +4,8 @@
"generic.rkt")
(provide (all-defined-out))
(struct x:ptr (type val parent) #:transparent #:mutable)
(define x:version-key 'x:version)
(define x:start-offset-key 'x:start-offset)
(define x:current-offset-key 'x:current-offset)

@ -72,7 +72,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Pointer.coffee
(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 x:pointer-type-key type x:val-key val x:parent-key parent)))))
(λ (ptrs) (append ptrs (list (x:ptr type val 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)]))

@ -74,7 +74,7 @@ https://github.com/mbutterick/restructure/blob/master/src/Struct.coffee
(for ([(key type) (in-dict @fields)])
(send type x:encode (dict-ref field-data key) port parent))
(for ([ptr (in-list (hash-ref parent x:pointers-key))])
(send (dict-ref ptr x:pointer-type-key) x:encode (dict-ref ptr x:val-key) port (dict-ref ptr x:parent-key))))
(send (x:ptr-type ptr) x:encode (x:ptr-val ptr) port (x:ptr-parent ptr))))
(define/augride (x:size [val #f] [parent-arg #f] [include-pointers #t])
(define parent (mhasheq x:parent-key parent-arg

@ -114,9 +114,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
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 x:pointer-type-key uint8
x:val-key 10
x:parent-key parent)))
(check-equal? (hash-ref parent x:pointers-key) (list (x:ptr uint8 10 parent)))
(check-equal? (get-output-bytes (current-output-port)) (bytes 1))))
(test-case
@ -128,9 +126,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
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 x:pointer-type-key uint8
x:val-key 10
x:parent-key parent)))
(check-equal? (hash-ref parent x:pointers-key) (list (x:ptr uint8 10 parent)))
(check-equal? (get-output-bytes (current-output-port)) (bytes 0))))
(test-case
@ -142,9 +138,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
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 x:pointer-type-key uint8
x:val-key 10
x:parent-key parent)))
(check-equal? (hash-ref* parent x:parent-key x:pointers-key) (list (x:ptr uint8 10 parent)))
(check-equal? (get-output-bytes (current-output-port)) (bytes 2))))
(test-case
@ -159,9 +153,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
(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 x:pointer-type-key uint8
x:val-key 10
x:parent-key parent)))
(list (x:ptr uint8 10 parent)))
(check-equal? (get-output-bytes (current-output-port)) (bytes 5))))
(test-case
@ -173,7 +165,7 @@ https://github.com/mbutterick/restructure/blob/master/test/Pointer.coffee
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 x:pointer-type-key uint8 x:val-key 55 x:parent-key parent)))
(check-equal? (hash-ref parent x:pointers-key) (list (x:ptr uint8 55 parent)))
(check-equal? (get-output-bytes (current-output-port)) (bytes 1))))
(test-case

@ -81,7 +81,7 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee
(for ([(key type) (in-dict fields)])
(send type x:encode (dict-ref field-data key) port parent))
(for ([ptr (in-list (dict-ref parent x:pointers-key))])
(send (dict-ref ptr x:pointer-type-key) x:encode (dict-ref ptr x:val-key) port (dict-ref ptr x:parent-key))))
(send (x:ptr-type ptr) x:encode (x:ptr-val ptr) port (x:ptr-parent ptr))))
(define/override (x:size [val #f] [parent-arg #f] [include-pointers #t])
(unless val

Loading…
Cancel
Save