Browse Source

WIP Dictionary introspection support

* Fix introspect-test-struct to actually return false on failure
windows-imgui
Macoy Madson 2 months ago
parent
commit
741ce83e89
  1. 3
      .gitignore
  2. 250
      src/Dictionary.cake
  3. 14
      src/Introspection.cake
  4. 4
      test/src/GameLibTests.cake

3
.gitignore

@ -72,4 +72,5 @@ test/data/Models/*
test/imgui.ini
test/TestSerialize.cakedata
test/TestDynamicArraySerialize.cakedata
test/TestDynamicArraySerialize.cakedata
test/TestDictionarySerialize.cakedata

250
src/Dictionary.cake

@ -173,6 +173,229 @@
(token-splice-rest body tokens)))
(return true))
;;
;; Introspection
;;
(comptime-cond
('Introspection
(import "Introspection.cake")
(defmacro dict-header-pointer-void (void-ptr-to-dict any element-size any)
(tokenize-push output
(stbds_header (- (type-cast (token-splice void-ptr-to-dict) (* char))
(token-splice element-size))))
(return true))
(defmacro dict-get-length-void (void-ptr-to-dict any element-size any)
(tokenize-push output
(? (token-splice void-ptr-to-dict)
(- (path
(dict-header-pointer-void (token-splice void-ptr-to-dict element-size))
> length)
1)
0))
(return true))
(defmacro dict-free-void (void-ptr-to-dict any element-size any)
(tokenize-push output
(when (token-splice void-ptr-to-dict)
(stbds_hmfree_func (dict-header-pointer-void (token-splice void-ptr-to-dict element-size))
(token-splice element-size))))
(return true))
(defmacro dict-set-struct-void (void-ptr-to-dict any element-size any
new-element-key any new-element-key-size any
struct-to-add any)
(tokenize-push output
(set (deref (token-splice void-ptr-to-dict))
(stbds_hmput_key_wrapper (deref (token-splice void-ptr-to-dict))
(token-splice element-size)
(addr (token-splice new-element-key))
(token-splice new-element-key-size)
STBDS_HM_BINARY))
(memcpy
(+ (* (token-splice element-size)
(path (dict-header-pointer-void (token-splice void-ptr-to-dict element-size)) > temp))
(type-cast (deref (token-splice void-ptr-to-dict)) (* char)))
(token-splice struct-to-add)
(token-splice element-size)))
(return true))
;; Protect against really crazy array sizes due to e.g. text parsing error
(var reasonable-max-dictionary-read-length (const int) 100000)
(introspect-override-register-handler 'write-s-expr
(metadata-field-has-tag field "'dictionary")
write-dictionary-ptr
(var dictionary-ptr-write (* (* void))
(offset-pointer-to-type struct-to-write value-offset (* (* void))))
(if (and (deref dictionary-ptr-write)
(dict-get-length-void (deref dictionary-ptr-write)
(path (path field > field-type-metadata) > struct-size)))
(scope
(fprintf out-file " (dictionary ")
(var num-elements int
(dict-get-length-void (deref dictionary-ptr-write)
(path (path field > field-type-metadata) > struct-size)))
;; NOT required, because otherwise it'd make hand-editing a pain. This is for performance
;; only; the reader should not take this as a guarantee, hence "hint"
(fprintf out-file ":length-hint %d " num-elements)
(each-in-range num-elements i
(var current-element (* void)
(offset-pointer-to-type (deref dictionary-ptr-write)
(* i (path (path field > field-type-metadata) > struct-size))
(* void)))
(unless (write-introspect-struct-s-expr
(path field > field-type-metadata)
current-element out-file
;; TODO: Pay attention to parent output settings
(? (= i (- num-elements 1)) write-introspect-struct-default
write-introspect-struct-add-newline))
(return false)))
(fprintf out-file ")"))
(fprintf out-file " null"))
(return true))
(introspect-override-register-handler 'read-s-expr
(metadata-field-has-tag field "'dictionary")
read-dictionary-ptr
(var dictionary-ptr-read (* (* void))
(offset-pointer-to-type struct-out value-offset (* (* void))))
;; You need to clean up the elements; I'd just clear them in this function, but that could lead
;; to memory leaks
(assert (not (dict-get-length-void (deref dictionary-ptr-read)
(path (path field > field-type-metadata) > struct-size))))
;; Determine some details about elements
(var element-key-offset size_t 0)
(var element-key-size size_t 0)
(each-in-range (path field > field-type-metadata > num-members) i
(var possible-key-field (* (const metadata-field))
(addr (at i (path field > field-type-metadata > members))))
(when (= 0 (strcmp "key" (path possible-key-field > name)))
(set element-key-offset (path possible-key-field > offset))
(set element-key-size (path possible-key-field > element-size))
(break)))
(unless element-key-size
(fprintf stderr "error: failed to find field named \"key\" in element struct %s\n"
(path field > field-type-metadata > name))
(dict-free-void (deref dictionary-ptr-read)
(path (path field > field-type-metadata) > struct-size))
(set (deref dictionary-ptr-read) null)
(return false))
(var start-tag (* (const char)) "(dictionary")
(var start-tag-length (const size_t) (strlen start-tag))
(var length-hint-tag (* (const char)) ":length-hint")
(var length-hint-tag-length (const size_t) (strlen length-hint-tag))
(unless (= 0 (strncmp start-tag value-argument-start start-tag-length))
(fprintf stderr "error: expected \"%s\", got \"%s\"\n" start-tag value-argument-start)
(return false))
(var start-arguments (* (const char)) (+ value-argument-start start-tag-length))
(var argument-start (* (const char)) null)
(var argument-end (* (const char)) null)
(s-expr-get-next-argument-start-end start-arguments (addr argument-start) (addr argument-end))
(while (and argument-start argument-end)
(cond
((= 0 (strncmp length-hint-tag argument-start length-hint-tag-length))
(s-expr-get-next-argument-start-end argument-end (addr argument-start)
(addr argument-end))
;; TODO Reduce duplication!
(var basic-type-buffer ([] 64 char) (array 0))
(var value-length size_t (- argument-end argument-start))
(when (> value-length (array-size basic-type-buffer))
(fprintf stderr "error: failed to copy basic type of size %d into buffer of size %d from value "
(type-cast value-length int) (type-cast (array-size basic-type-buffer) int))
(print-string-range argument-start argument-end true)
(return false))
(memcpy basic-type-buffer argument-start value-length)
(var length-hint int (atoi basic-type-buffer))
(assert (< length-hint reasonable-max-dictionary-read-length))
(ignore ;; TODO: Dictionary initial size, which doesn't seem exposed by stb
(fprintf stderr "Reserving capacity %d\n" length-hint)
(dict-set-capacity-with-element-size
dictionary-ptr-read
(path (path field > field-type-metadata) > struct-size)
length-hint)))
(true
;; TODO: Expose option to not use malloc
(var new-element (* void) (malloc (path (path field > field-type-metadata) > struct-size)))
(unless (read-introspect-struct-s-expr (path field > field-type-metadata)
new-element
argument-start string-allocate)
(fprintf stderr "error: failed to read element into dictionary: ")
(print-string-range argument-start argument-end true)
(dict-free-void (deref dictionary-ptr-read)
(path (path field > field-type-metadata) > struct-size))
(set (deref dictionary-ptr-read) null)
(return false))
(var new-element-key (* void)
(offset-pointer-to-type new-element element-key-offset (* void)))
(dict-set-struct-void dictionary-ptr-read
(path (path field > field-type-metadata) > struct-size)
new-element-key element-key-size
new-element)))
(s-expr-get-next-argument-start-end argument-end (addr argument-start)
(addr argument-end)))
(return true))
;; (introspect-override-register-handler 'compare
;; (metadata-field-has-tag field "'dictionary")
;; compare-dictionary
;; (var dictionary-a (* (* void))
;; (offset-pointer-to-type struct-a value-offset (* (* void))))
;; (var dictionary-b (* (* void))
;; (offset-pointer-to-type struct-b value-offset (* (* void))))
;; (when (!= (deref dictionary-a) (deref dictionary-b))
;; (var num-elements-a int (dict-get-length-void (deref dictionary-a)
;; (path (path field > field-type-metadata) > struct-size)))
;; (var num-elements-b int (dict-get-length-void (deref dictionary-b)
;; (path (path field > field-type-metadata) > struct-size)))
;; (when (!= num-elements-a num-elements-b)
;; (when print-difference
;; (fprintf stderr "structs differ by field '%s' [%d] length (%d vs %d)\n"
;; (path field > name)
;; (type-cast dispatch-value-index int)
;; (type-cast (dict-length dictionary-a) int)
;; (type-cast (dict-length dictionary-b) int)))
;; (return (- num-elements-a num-elements-b)))
;; (each-in-range num-elements-a i
;; (var current-element-a (* void)
;; (offset-pointer-to-type (deref dictionary-a)
;; (* i (path (path field > field-type-metadata) > struct-size))
;; (* void)))
;; (var current-element-b (* void)
;; (offset-pointer-to-type (deref dictionary-b)
;; (* i (path (path field > field-type-metadata) > struct-size))
;; (* void)))
;; (var return-value int
;; (compare-introspect-struct-internal (path field > field-type-metadata)
;; current-element-a
;; current-element-b
;; print-difference))
;; (when (!= 0 return-value)
;; (when print-difference
;; (fprintf stderr "structs differ by field '%s' [%d][%d]\n"
;; (path field > name)
;; (type-cast dispatch-value-index int) i))
;; (return return-value))))
;; (return 0))
;; (introspect-override-register-handler 'free
;; (metadata-field-has-tag field "'dictionary")
;; free-dict-ptr
;; (var dict-ptr (* (* void))
;; (offset-pointer-to-type struct-to-destroy value-offset (* (* void))))
;; (dict-free (deref dict-ptr))
;; (set (deref dict-ptr) null))
))
;;
;; Tests
;;
@ -278,4 +501,29 @@
(each-in-array items i
(free (type-cast (field (at i items) key) (* void))))
(strdict-free my-dictionary))
(return 0))))
(return 0))
(comptime-cond
('Introspection
(import "Introspection.cake")
(def-introspect-struct dict-entry
key int
value float)
(def-introspect-struct my-dict
dictionary (* dict-entry) (override 'dictionary)) ;; TODO strdict
(defun test--dictionary-introspection (&return int)
(var baseline my-dict (array 0))
(var my-entry dict-entry (array 1 42.f))
(dict-set-struct (field baseline dictionary) my-entry)
;; (dynarray-set-length (field baseline items) 10)
;; (each-item-addr-in-dynarray (field baseline items) i data (* array-data)
;; (set (path data > value) (type-cast i float)))
(unless (introspect-test-struct my-dict--metadata (addr baseline)
"TestDictionarySerialize.cakedata")
(return 1))
(free-introspect-struct-fields my-dict--metadata (addr baseline) free)
(return 0))))))

14
src/Introspection.cake

@ -1275,15 +1275,15 @@
(fprintf stderr "Baseline:\n")
(unless (write-introspect-struct-s-expr struct-metadata baseline stderr
write-introspect-struct-add-newline)
(return 1))
(return false))
(scope ;; Write to a file
(var out-file (* FILE) (fopen serialization-filename "w"))
(unless out-file (return 1))
(unless out-file (return false))
(unless (write-introspect-struct-s-expr struct-metadata baseline out-file
write-introspect-struct-add-newline)
(fclose out-file)
(return 1))
(return false))
(fclose out-file))
(scope ;; Now read it back
@ -1291,14 +1291,14 @@
(memset read-struct 0 (path struct-metadata > struct-size))
(scope
(var in-file (* FILE) (fopen serialization-filename "r"))
(unless in-file (return 1))
(unless in-file (return false))
(var file-contents (* (const char)) (read-file-into-memory in-file))
(unless (read-introspect-struct-s-expr struct-metadata read-struct
file-contents malloc)
(free (type-cast file-contents (* void)))
(fclose in-file)
(free read-struct)
(return 1))
(return false))
(free (type-cast file-contents (* void)))
(fclose in-file))
@ -1309,7 +1309,7 @@
(free-introspect-struct-fields
struct-metadata read-struct free)
(free read-struct)
(return 1))
(return false))
(unless (introspect-struct-= struct-metadata
read-struct baseline)
@ -1317,7 +1317,7 @@
(free-introspect-struct-fields
struct-metadata read-struct free)
(free read-struct)
(return 1))
(return false))
(free-introspect-struct-fields
struct-metadata read-struct free)

4
test/src/GameLibTests.cake

@ -51,8 +51,8 @@
(gamelib-run-test
"Auto Test (data structures only)"
(array platform-config
"../src/AutoTest.cake" "../src/Dictionary.cake"
"../src/Introspection.cake" "../src/DynamicArray.cake")))
"../src/AutoTest.cake" "../src/Introspection.cake"
"../src/Dictionary.cake" "../src/DynamicArray.cake")))
(test-opengl-only
(gamelib-run-test

Loading…
Cancel
Save