From e83dac781bd481a995484cd876b0ceeedb5a768a Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Wed, 12 Dec 2018 14:51:11 -0800 Subject: [PATCH] ctx to parent --- xenomorph/xenomorph/redo/array.rkt | 44 +++++++++---------- .../redo/test/versioned-struct-test.rkt | 2 +- xenomorph/xenomorph/redo/versioned-struct.rkt | 2 +- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/xenomorph/xenomorph/redo/array.rkt b/xenomorph/xenomorph/redo/array.rkt index 9a46a03a..31ecd383 100644 --- a/xenomorph/xenomorph/redo/array.rkt +++ b/xenomorph/xenomorph/redo/array.rkt @@ -11,11 +11,11 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee (define port (->input-port port-arg)) (parameterize ([current-input-port port]) (define new-parent (if (xint? (xarray-base-len xa)) - (mhasheq 'parent parent - '_startOffset (pos port) - '_currentOffset 0 - '_length (xarray-base-len xa)) - parent)) + (mhasheq 'parent parent + '_startOffset (pos port) + '_currentOffset 0 + '_length (xarray-base-len xa)) + parent)) (define decoded-len (resolve-length (xarray-base-len xa) #:parent parent)) (cond [(or (not decoded-len) (eq? (xarray-length-type xa) 'bytes)) @@ -38,38 +38,38 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee (raise-argument-error 'xarray-encode "sequence" array)) (define port (if (output-port? port-arg) port-arg (open-output-bytes))) (parameterize ([current-output-port port]) - (define (encode-items ctx) + (define (encode-items parent) ;; todo: should array with fixed length stop encoding after it reaches max? ;; cf. xstring, which rejects input that is too big for fixed length. (let* (#;[items (sequence->list array)] #;[item-count (length items)] #;[max-items (if (number? (xarray-len xa)) (xarray-len xa) item-count)]) (for ([item array]) - (encode (xarray-base-type xa) item #:parent ctx)))) + (encode (xarray-base-type xa) item #:parent parent)))) (cond [(xint? (xarray-base-len xa)) - (define ctx (mhash 'pointers null - 'startOffset (pos port) - 'parent parent)) - (dict-set! ctx 'pointerOffset (+ (pos port) (size xa array ctx))) - (encode (xarray-base-len xa) (length array)) ; encode length at front - (encode-items ctx) - (for ([ptr (in-list (dict-ref ctx 'pointers))]) ; encode pointer data at end - (encode (dict-ref ptr 'type) (dict-ref ptr 'val)))] + (let ([parent (mhash 'pointers null + 'startOffset (pos port) + 'parent parent)]) + (dict-set! parent 'pointerOffset (+ (pos port) (size xa array parent))) + (encode (xarray-base-len xa) (length array)) ; encode length at front + (encode-items parent) + (for ([ptr (in-list (dict-ref parent 'pointers))]) ; encode pointer data at end + (encode (dict-ref ptr 'type) (dict-ref ptr 'val))))] [else (encode-items parent)]) (unless port-arg (get-output-bytes port)))) -(define (xarray-size xa [val #f] [ctx #f]) +(define (xarray-size xa [val #f] [parent #f]) (when val (unless (sequence? val) (raise-argument-error 'xarray-size "sequence" val))) (cond - [val (let-values ([(ctx len-size) (if (xint? (xarray-base-len xa)) - (values (mhasheq 'parent ctx) (size (xarray-base-len xa))) - (values ctx 0))]) + [val (let-values ([(parent len-size) (if (xint? (xarray-base-len xa)) + (values (mhasheq 'parent parent) (size (xarray-base-len xa))) + (values parent 0))]) (+ len-size (for/sum ([item val]) - (size (xarray-base-type xa) item ctx))))] - [else (let ([item-count (resolve-length (xarray-base-len xa) #f #:parent ctx)] - [item-size (size (xarray-base-type xa) #f ctx)]) + (size (xarray-base-type xa) item parent))))] + [else (let ([item-count (resolve-length (xarray-base-len xa) #f #:parent parent)] + [item-size (size (xarray-base-type xa) #f parent)]) (* item-size item-count))])) (struct xarray-base (type len) #:transparent) diff --git a/xenomorph/xenomorph/redo/test/versioned-struct-test.rkt b/xenomorph/xenomorph/redo/test/versioned-struct-test.rkt index efd36235..f47d57bb 100644 --- a/xenomorph/xenomorph/redo/test/versioned-struct-test.rkt +++ b/xenomorph/xenomorph/redo/test/versioned-struct-test.rkt @@ -103,7 +103,7 @@ https://github.com/mbutterick/restructure/blob/master/test/VersionedStruct.coffe 1 (dictify 'name (+xstring uint8 'utf8) 'age uint8 'gender uint8)))]) - (set-xversioned-struct-post-decode! vstruct (λ (o stream ctx) (dict-set! o 'processed "true") o)) + (set-xversioned-struct-post-decode! vstruct (λ (o stream parent) (dict-set! o 'processed "true") o)) (parameterize ([current-input-port (open-input-bytes #"\x00\x05roxyb\x15")]) (check-equal? (dump (decode vstruct)) '((processed . "true") (version . 0) (age . 21) (name . "roxyb")))))) diff --git a/xenomorph/xenomorph/redo/versioned-struct.rkt b/xenomorph/xenomorph/redo/versioned-struct.rkt index 9fca8338..e07e7929 100644 --- a/xenomorph/xenomorph/redo/versioned-struct.rkt +++ b/xenomorph/xenomorph/redo/versioned-struct.rkt @@ -107,6 +107,6 @@ https://github.com/mbutterick/restructure/blob/master/src/VersionedStruct.coffee [(procedure? type) type] [(symbol? type) (λ (parent version) (dict-set! parent type version))])) (define (no-op-pre-encode val port) val) - (define (no-op-post-decode xvs port ctx) xvs) + (define (no-op-post-decode xvs port parent) xvs) (xversioned-struct type versions version-getter version-setter no-op-pre-encode no-op-post-decode))