mirror of
https://github.com/janet-lang/janet
synced 2025-10-24 20:27:41 +00:00
Add fixes for marshalling weak containers - Fix #1488
Weak containers did not preserve their weakness when marshalled. This fixes that for tables and arrays, as well as adds some tests for this. Also exposes functions for creating weak tables in janet.h
This commit is contained in:
@@ -146,5 +146,80 @@ neldb\0\0\0\xD8\x05printG\x01\0\xDE\xDE\xDE'\x03\0marshal_tes/\x02
|
||||
(def item (ev/take newchan))
|
||||
(assert (= item newchan) "ev/chan marshalling"))
|
||||
|
||||
(end-suite)
|
||||
# Issue #1488 - marshalling weak values
|
||||
(testmarsh (array/weak 10) "marsh array/weak")
|
||||
(testmarsh (table/weak-keys 10) "marsh table/weak-keys")
|
||||
(testmarsh (table/weak-values 10) "marsh table/weak-values")
|
||||
(testmarsh (table/weak 10) "marsh table/weak")
|
||||
|
||||
# Now check that gc works with weak containers after marshalling
|
||||
|
||||
# Turn off automatic GC for testing weak references
|
||||
(gcsetinterval 0x7FFFFFFF)
|
||||
|
||||
# array
|
||||
(def a (array/weak 1))
|
||||
(array/push a @"")
|
||||
(assert (= 1 (length a)) "array/weak marsh 1")
|
||||
(def aclone (-> a marshal unmarshal))
|
||||
(assert (= 1 (length aclone)) "array/weak marsh 2")
|
||||
(gccollect)
|
||||
(assert (= 1 (length aclone)) "array/weak marsh 3")
|
||||
(assert (= 1 (length a)) "array/weak marsh 4")
|
||||
(assert (= nil (get a 0)) "array/weak marsh 5")
|
||||
(assert (= nil (get aclone 0)) "array/weak marsh 6")
|
||||
(assert (deep= a aclone) "array/weak marsh 7")
|
||||
|
||||
# table weak keys and values
|
||||
(def t (table/weak 1))
|
||||
(def keep-key :key)
|
||||
(def keep-value :value)
|
||||
(put t :abc @"")
|
||||
(put t :key :value)
|
||||
(assert (= 2 (length t)) "table/weak marsh 1")
|
||||
(def tclone (-> t marshal unmarshal))
|
||||
(assert (= 2 (length tclone)) "table/weak marsh 2")
|
||||
(gccollect)
|
||||
(assert (= 1 (length tclone)) "table/weak marsh 3")
|
||||
(assert (= 1 (length t)) "table/weak marsh 4")
|
||||
(assert (= keep-value (get t keep-key)) "table/weak marsh 5")
|
||||
(assert (= keep-value (get tclone keep-key)) "table/weak marsh 6")
|
||||
(assert (deep= t tclone) "table/weak marsh 7")
|
||||
|
||||
# table weak keys
|
||||
(def t (table/weak-keys 1))
|
||||
(put t @"" keep-value)
|
||||
(put t :key @"")
|
||||
(assert (= 2 (length t)) "table/weak-keys marsh 1")
|
||||
(def tclone (-> t marshal unmarshal))
|
||||
(assert (= 2 (length tclone)) "table/weak-keys marsh 2")
|
||||
(gccollect)
|
||||
(assert (= 1 (length tclone)) "table/weak-keys marsh 3")
|
||||
(assert (= 1 (length t)) "table/weak-keys marsh 4")
|
||||
(assert (deep= t tclone) "table/weak-keys marsh 5")
|
||||
|
||||
# table weak values
|
||||
(def t (table/weak-values 1))
|
||||
(put t @"" keep-value)
|
||||
(put t :key @"")
|
||||
(assert (= 2 (length t)) "table/weak-values marsh 1")
|
||||
(def tclone (-> t marshal unmarshal))
|
||||
(assert (= 2 (length tclone)) "table/weak-values marsh 2")
|
||||
(gccollect)
|
||||
(assert (= 1 (length t)) "table/weak-value marsh 3")
|
||||
(assert (deep= t tclone) "table/weak-values marsh 4")
|
||||
|
||||
# tables with prototypes
|
||||
(def t (table/weak-values 1))
|
||||
(table/setproto t @{:abc 123})
|
||||
(put t @"" keep-value)
|
||||
(put t :key @"")
|
||||
(assert (= 2 (length t)) "marsh weak tables with prototypes 1")
|
||||
(def tclone (-> t marshal unmarshal))
|
||||
(assert (= 2 (length tclone)) "marsh weak tables with prototypes 2")
|
||||
(gccollect)
|
||||
(assert (= 1 (length t)) "marsh weak tables with prototypes 3")
|
||||
(assert (deep= t tclone) "marsh weak tables with prototypes 4")
|
||||
(assert (deep= (getproto t) (getproto tclone)) "marsh weak tables with prototypes 5")
|
||||
|
||||
(end-suite)
|
||||
|
||||
Reference in New Issue
Block a user