Skip to content

Commit 5adfb75

Browse files
committed
Revert some changes.
1 parent 611b2a6 commit 5adfb75

File tree

5 files changed

+28
-25
lines changed

5 files changed

+28
-25
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
All notable changes to this project will be documented in this file.
33

44
## ??? - Unreleased
5-
- Fix `deep=` and `deep-not=` to better handle degenerate cases with mutable table keys
5+
- Add `struct/rawget` to get values from a struct without a prototype.
6+
- Fix `deep=` and `deep-not=` to better handle degenerate cases with mutable table keys. Keys are now compared by value rather than
7+
structure to avoid degenerate cases.
68
- Long strings will now dedent on `\r\n` instead of just `\n`.
79
- Add `ev/to-file` for synchronous resource operations
810

src/boot/boot.janet

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,8 +2247,6 @@
22472247
:string (buffer ds)
22482248
ds))
22492249

2250-
(def- mutable-types {:table true :array true :buffer true})
2251-
22522250
(defn deep-not=
22532251
``Like `not=`, but mutable types (arrays, tables, buffers) are considered
22542252
equal if they have identical structure. Much slower than `not=`.``
@@ -2270,20 +2268,12 @@
22702268
(or (= tx :struct) (= tx :table))
22712269
(or (not= (length x) (length y))
22722270
(do
2271+
(def rawget (if (= tx :table) table/rawget struct/rawget))
22732272
(var ret false)
2274-
(def mut-keys-x @{})
22752273
(eachp [k v] x
2276-
(if (get mutable-types (type k))
2277-
(let [kk (freeze k)]
2278-
(put mut-keys-x kk (put (get mut-keys-x kk @{}) (freeze v) true)))
2279-
(if (deep-not= (get y k) v) (break (set ret true)))))
2280-
(when (next mut-keys-x) # handle case when we have mutable keys separately
2281-
(def mut-keys-y @{})
2282-
(eachp [k v] y
2283-
(if (get mutable-types (type k))
2284-
(let [kk (freeze k)]
2285-
(put mut-keys-y kk (put (get mut-keys-y kk @{}) (freeze v) true)))))
2286-
(set ret (deep-not= mut-keys-x mut-keys-y)))
2274+
(def yv (rawget y k))
2275+
(if (= nil yv) (break (set ret true)))
2276+
(if (deep-not= yv v) (break (set ret true))))
22872277
ret))
22882278
(= tx :buffer) (not= 0 (- (length x) (length y)) (memcmp x y))
22892279
(not= x y))))

src/core/struct.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,24 @@ JANET_CORE_FN(cfun_struct_to_table,
294294
return janet_wrap_table(tab);
295295
}
296296

297+
JANET_CORE_FN(cfun_struct_rawget,
298+
"(struct/rawget st key)",
299+
"Gets a value from a struct `st` without looking at the prototype struct. "
300+
"If `st` does not contain the key directly, the function will return "
301+
"nil without checking the prototype. Returns the value in the struct.") {
302+
janet_fixarity(argc, 2);
303+
JanetStruct st = janet_getstruct(argv, 0);
304+
return janet_struct_rawget(st, argv[1]);
305+
}
306+
297307
/* Load the struct module */
298308
void janet_lib_struct(JanetTable *env) {
299309
JanetRegExt struct_cfuns[] = {
300310
JANET_CORE_REG("struct/with-proto", cfun_struct_with_proto),
301311
JANET_CORE_REG("struct/getproto", cfun_struct_getproto),
302312
JANET_CORE_REG("struct/proto-flatten", cfun_struct_flatten),
303313
JANET_CORE_REG("struct/to-table", cfun_struct_to_table),
314+
JANET_CORE_REG("struct/rawget", cfun_struct_rawget),
304315
JANET_REG_END
305316
};
306317
janet_core_cfuns_ext(env, NULL, struct_cfuns);

test/suite-boot.janet

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -997,12 +997,12 @@
997997

998998
# issue #1535
999999
(loop [i :range [1 1000]]
1000-
(assert (deep= @{:key1 "value1" @"key" "value2"}
1001-
@{:key1 "value1" @"key" "value2"}) "deep= mutable keys"))
1000+
(assert (deep-not= @{:key1 "value1" @"key" "value2"}
1001+
@{:key1 "value1" @"key" "value2"}) "deep= mutable keys"))
10021002
(assert (deep-not= {"abc" 123} {@"abc" 123}) "deep= mutable keys vs immutable key")
1003-
(assert (deep= {@"" 1 @"" 2 @"" 3} {@"" 1 @"" 2 @"" 3}) "deep= duplicate mutable keys")
1004-
(assert (deep= {@"" @"" @"" @"" @"" 3} {@"" @"" @"" @"" @"" 3}) "deep= duplicate mutable keys 2")
1005-
(assert (deep= {@[] @"" @[] @"" @[] 3} {@[] @"" @[] @"" @[] 3}) "deep= duplicate mutable keys 3")
1006-
(assert (deep= {@{} @"" @{} @"" @{} 3} {@{} @"" @{} @"" @{} 3}) "deep= duplicate mutable keys 4")
1003+
(assert (deep-not= {@"" 1 @"" 2 @"" 3} {@"" 1 @"" 2 @"" 3}) "deep= duplicate mutable keys")
1004+
(def k1 @"")
1005+
(def k2 @"")
1006+
(assert (deep= {k1 1 k2 2} {k1 1 k2 2}) "deep= duplicate mutable keys 2")
10071007

10081008
(end-suite)

test/suite-marsh.janet

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ neldb\0\0\0\xD8\x05printG\x01\0\xDE\xDE\xDE'\x03\0marshal_tes/\x02
168168
(assert (= 1 (length a)) "array/weak marsh 4")
169169
(assert (= nil (get a 0)) "array/weak marsh 5")
170170
(assert (= nil (get aclone 0)) "array/weak marsh 6")
171-
(assert (deep= a aclone) "array/weak marsh 7")
171+
(assert (deep= (freeze a) (freeze aclone)) "array/weak marsh 7")
172172

173173
# table weak keys and values
174174
(def t (table/weak 1))
@@ -196,7 +196,7 @@ neldb\0\0\0\xD8\x05printG\x01\0\xDE\xDE\xDE'\x03\0marshal_tes/\x02
196196
(gccollect)
197197
(assert (= 1 (length tclone)) "table/weak-keys marsh 3")
198198
(assert (= 1 (length t)) "table/weak-keys marsh 4")
199-
(assert (deep= t tclone) "table/weak-keys marsh 5")
199+
(assert (deep= (freeze t) (freeze tclone)) "table/weak-keys marsh 5")
200200

201201
# table weak values
202202
(def t (table/weak-values 1))
@@ -207,7 +207,7 @@ neldb\0\0\0\xD8\x05printG\x01\0\xDE\xDE\xDE'\x03\0marshal_tes/\x02
207207
(assert (= 2 (length tclone)) "table/weak-values marsh 2")
208208
(gccollect)
209209
(assert (= 1 (length t)) "table/weak-value marsh 3")
210-
(assert (deep= t tclone) "table/weak-values marsh 4")
210+
(assert (deep= (freeze t) (freeze tclone)) "table/weak-values marsh 4")
211211

212212
# tables with prototypes
213213
(def t (table/weak-values 1))
@@ -219,7 +219,7 @@ neldb\0\0\0\xD8\x05printG\x01\0\xDE\xDE\xDE'\x03\0marshal_tes/\x02
219219
(assert (= 2 (length tclone)) "marsh weak tables with prototypes 2")
220220
(gccollect)
221221
(assert (= 1 (length t)) "marsh weak tables with prototypes 3")
222-
(assert (deep= t tclone) "marsh weak tables with prototypes 4")
222+
(assert (deep= (freeze t) (freeze tclone)) "marsh weak tables with prototypes 4")
223223
(assert (deep= (getproto t) (getproto tclone)) "marsh weak tables with prototypes 5")
224224

225225
(end-suite)

0 commit comments

Comments
 (0)