1
0
mirror of https://github.com/janet-lang/janet synced 2025-12-13 12:08:07 +00:00

Allow table/to-struct to take a prototype.

Use this prototype struct in freeze.
This commit is contained in:
Calvin Rose
2024-12-19 19:41:19 -06:00
parent 682f0f584f
commit 1b49934e4f
2 changed files with 17 additions and 7 deletions

View File

@@ -2230,8 +2230,16 @@
(tuple/slice (map freeze x))
(or (= tx :table) (= tx :struct))
(let [sorted-kvs (array/join @[] ;(sort (map freeze (pairs x))))]
(struct/with-proto (freeze (getproto x)) ;sorted-kvs))
(let [temp-tab @{}]
# Handle multiple unique keys that freeze. Result should
# be independent of iteration order.
(eachp [k v] x
(def kk (freeze k))
(def vv (freeze v))
(def old (get temp-tab kk))
(def new (if (= nil old) vv (max vv old)))
(put temp-tab kk new))
(table/to-struct temp-tab (freeze (getproto x))))
(= tx :buffer)
(string x)