mirror of
https://github.com/janet-lang/janet
synced 2025-05-05 00:44:14 +00:00
freeze with mutable keys should be determinsic help address #1535
This commit is contained in:
parent
611b2a6c3a
commit
682f0f584f
@ -2224,14 +2224,18 @@
|
|||||||
child values also immutable. Closures, fibers, and abstract types
|
child values also immutable. Closures, fibers, and abstract types
|
||||||
will not be recursively frozen, but all other types will.`
|
will not be recursively frozen, but all other types will.`
|
||||||
[x]
|
[x]
|
||||||
(case (type x)
|
(def tx (type x))
|
||||||
:array (tuple/slice (map freeze x))
|
(cond
|
||||||
:tuple (tuple/slice (map freeze x))
|
(or (= tx :array) (= tx :tuple))
|
||||||
:table (if-let [p (table/getproto x)]
|
(tuple/slice (map freeze x))
|
||||||
(freeze (merge (table/clone p) x))
|
|
||||||
(struct ;(map freeze (kvs x))))
|
(or (= tx :table) (= tx :struct))
|
||||||
:struct (struct ;(map freeze (kvs x)))
|
(let [sorted-kvs (array/join @[] ;(sort (map freeze (pairs x))))]
|
||||||
:buffer (string x)
|
(struct/with-proto (freeze (getproto x)) ;sorted-kvs))
|
||||||
|
|
||||||
|
(= tx :buffer)
|
||||||
|
(string x)
|
||||||
|
|
||||||
x))
|
x))
|
||||||
|
|
||||||
(defn thaw
|
(defn thaw
|
||||||
|
@ -896,11 +896,18 @@
|
|||||||
(struct/with-proto {:a [1 2 3]} :c 22 :b [1 2 3 4] :d "test" :e "test2"))
|
(struct/with-proto {:a [1 2 3]} :c 22 :b [1 2 3 4] :d "test" :e "test2"))
|
||||||
(table/setproto table-to-freeze @{:a @[1 2 3]})
|
(table/setproto table-to-freeze @{:a @[1 2 3]})
|
||||||
|
|
||||||
(assert (deep= {:a [1 2 3] :b [1 2 3 4] :c 22 :d "test" :e "test2"}
|
(assert (deep= struct-to-thaw (freeze table-to-freeze)))
|
||||||
(freeze table-to-freeze)))
|
|
||||||
(assert (deep= table-to-freeze-with-inline-proto (thaw table-to-freeze)))
|
(assert (deep= table-to-freeze-with-inline-proto (thaw table-to-freeze)))
|
||||||
(assert (deep= table-to-freeze-with-inline-proto (thaw struct-to-thaw)))
|
(assert (deep= table-to-freeze-with-inline-proto (thaw struct-to-thaw)))
|
||||||
|
|
||||||
|
# Check that freezing mutable keys is deterministic
|
||||||
|
# for issue #1535
|
||||||
|
(def hashes @{})
|
||||||
|
(repeat 200
|
||||||
|
(def x (freeze {@"" 1 @"" 2 @"" 3 @"" 4 @"" 5}))
|
||||||
|
(put hashes (hash x) true))
|
||||||
|
(assert (= 1 (length hashes)) "freeze mutable keys is deterministic")
|
||||||
|
|
||||||
# Make sure Carriage Returns don't end up in doc strings
|
# Make sure Carriage Returns don't end up in doc strings
|
||||||
# e528b86
|
# e528b86
|
||||||
(assert (not (string/find "\r"
|
(assert (not (string/find "\r"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user