From 56d927c72dd621e122bf95eef8144c362cf35870 Mon Sep 17 00:00:00 2001 From: tionis Date: Fri, 19 May 2023 21:18:48 +0200 Subject: [PATCH 1/3] added thaw to complement freeze --- src/boot/boot.janet | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 0ccda788..1195ce11 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -2142,6 +2142,19 @@ :buffer (string x) x)) +(defn thaw + `Thaw an object (make it mutable) and do a deep copy, making + child value also mutable. Closures, fibers, and abstract + types will not be recursively thawed, but all other types will` + [ds] + (case (type ds) + :table (walk-dict thaw ds) + :struct (walk-dict thaw ds) + :array (walk-ind thaw ds) + :tuple (walk-ind thaw ds) + :string (buffer ds) + ds)) + (defn macex ``Expand macros completely. `on-binding` is an optional callback for whenever a normal symbolic binding From 320ba80ca1b7a3ddd3e0f3f7895e3ae406e63fc9 Mon Sep 17 00:00:00 2001 From: tionis Date: Sat, 20 May 2023 14:00:33 +0200 Subject: [PATCH 2/3] added support for tables/structs with prototypes in thaw --- src/boot/boot.janet | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 1195ce11..eb745ab1 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -2148,8 +2148,8 @@ types will not be recursively thawed, but all other types will` [ds] (case (type ds) - :table (walk-dict thaw ds) - :struct (walk-dict thaw ds) + :table (walk-dict thaw (table/proto-flatten ds)) + :struct (walk-dict thaw (struct/proto-flatten ds)) :array (walk-ind thaw ds) :tuple (walk-ind thaw ds) :string (buffer ds) From 9cc0645a1e40944a0523a237bda10f050e342e55 Mon Sep 17 00:00:00 2001 From: tionis Date: Sat, 20 May 2023 17:35:25 +0200 Subject: [PATCH 3/3] added test for thaw and freeze --- src/boot/boot.janet | 4 ++-- test/suite0010.janet | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/boot/boot.janet b/src/boot/boot.janet index eb745ab1..0186a4e2 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -2148,10 +2148,10 @@ types will not be recursively thawed, but all other types will` [ds] (case (type ds) - :table (walk-dict thaw (table/proto-flatten ds)) - :struct (walk-dict thaw (struct/proto-flatten ds)) :array (walk-ind thaw ds) :tuple (walk-ind thaw ds) + :table (walk-dict thaw (table/proto-flatten ds)) + :struct (walk-dict thaw (struct/proto-flatten ds)) :string (buffer ds) ds)) diff --git a/test/suite0010.janet b/test/suite0010.janet index 5ac0cd16..b41142e8 100644 --- a/test/suite0010.janet +++ b/test/suite0010.janet @@ -253,4 +253,13 @@ # Check missing struct proto bug. (assert (struct/getproto (struct/with-proto {:a 1} :b 2 :c nil)) "missing struct proto") +# Test thaw and freeze +(def table-to-freeze @{:c 22 :b [1 2 3 4] :d @"test" :e "test2"}) +(def table-to-freeze-with-inline-proto @{:a @[1 2 3] :b @[1 2 3 4] :c 22 :d @"test" :e @"test2"}) +(def struct-to-thaw (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]}) +(assert (deep= {:a [1 2 3] :b [1 2 3 4] :c 22 :d "test" :e "test2"} (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 struct-to-thaw))) + (end-suite)