From 2d9c09c03691e4310c6ee946e5c05b5d21d18123 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 9 Jun 2017 07:51:51 -0700 Subject: [PATCH] next: unify helper modules --- pitfall/pitfall/directory.rkt | 31 +++++++++++++++++++------------ pitfall/pitfall/helper.rkt | 1 + pitfall/restructure/array.rkt | 13 ++++++++----- pitfall/restructure/helper.rkt | 3 +++ pitfall/restructure/utils.rkt | 2 +- 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/pitfall/pitfall/directory.rkt b/pitfall/pitfall/directory.rkt index d08f991a..9750170c 100644 --- a/pitfall/pitfall/directory.rkt +++ b/pitfall/pitfall/directory.rkt @@ -3,21 +3,28 @@ (provide (all-defined-out)) +#| +https://github.com/mbutterick/fontkit/blob/master/src/tables/directory.js +|# (define TableEntry (make-object RStruct - (list (cons 'tag (make-object RString 4)) - (cons 'checkSum uint32be) - (cons 'offset uint32be) - (cons 'length uint32be)))) + (dictify 'tag (make-object RString 4) + 'checkSum uint32be + 'offset uint32be + 'length uint32be))) -(define Directory (make-object RStruct - (list (cons 'tag (make-object RString 4)) - (cons 'numTables uint16be) - (cons 'searchRange uint16be) - (cons 'entrySelector uint16be) - (cons 'rangeShift uint16be) - ;; todo next: derive the `14` from 'numTables - (cons 'tables (make-object RArray TableEntry 14))))) +(define-subclass RStruct (RDirectory) + (super-new) + (define/public (process) + 'boom)) + +(define Directory (make-object RDirectory + (dictify 'tag (make-object RString 4) + 'numTables uint16be + 'searchRange uint16be + 'entrySelector uint16be + 'rangeShift uint16be + 'tables (make-object RArray TableEntry 'numTables)))) (module+ test (require rackunit) diff --git a/pitfall/pitfall/helper.rkt b/pitfall/pitfall/helper.rkt index dae1798b..7d6ab462 100644 --- a/pitfall/pitfall/helper.rkt +++ b/pitfall/pitfall/helper.rkt @@ -47,6 +47,7 @@ (define-hashifier mhash make-hash) (define-hashifier mhasheq make-hasheq) (define-hashifier mhasheqv make-hasheqv) +(define (dictify . xs) (listify xs)) (module+ test (check-equal? (mhash 'k "v") (make-hash (list (cons 'k "v"))))) diff --git a/pitfall/restructure/array.rkt b/pitfall/restructure/array.rkt index 269c99dc..72e6ffcb 100644 --- a/pitfall/restructure/array.rkt +++ b/pitfall/restructure/array.rkt @@ -8,12 +8,15 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee |# (define-subclass RStreamcoder (RArray type [length #f] [lengthType 'count]) - - + (define/augment (decode stream [parent #f]) - (caseq lengthType - [(count) (for/list ([i (in-range length)]) - (send type decode stream this))])) + (let ([length (if length + (resolveLength length stream parent) + (send stream length))]) + + (caseq lengthType + [(count) (for/list ([i (in-range length)]) + (send type decode stream this))]))) (define/public (size) (unfinished)) diff --git a/pitfall/restructure/helper.rkt b/pitfall/restructure/helper.rkt index 9eb77d7b..a25dcf49 100644 --- a/pitfall/restructure/helper.rkt +++ b/pitfall/restructure/helper.rkt @@ -72,6 +72,9 @@ (define-hashifier mhasheq make-hasheq) (define-hashifier mhasheqv make-hasheqv) +(provide dictify) +(define (dictify . xs) (listify xs)) + (define (port-position port) (define-values (l c p) (port-next-location port)) p) diff --git a/pitfall/restructure/utils.rkt b/pitfall/restructure/utils.rkt index 90c57374..5b2a8c1d 100644 --- a/pitfall/restructure/utils.rkt +++ b/pitfall/restructure/utils.rkt @@ -6,6 +6,6 @@ (cond [(number? length) length] [(procedure? length) (length parent)] - [(and parent (string? length) (ยท parent length))] + [(and parent (symbol? length) (hash-ref parent length))] ; treat as key into RStruct parent [(and stream (is-a? length Number) (send length decode stream))] [else (raise-argument-error 'resolveLength "fixed-size item" length)])) \ No newline at end of file