From aa5c987a94e79455e27094ed427c16ffdb90bb40 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Fri, 3 Mar 2023 18:24:02 -0600 Subject: [PATCH] Change semantics of bracketed tuple equality. Comparison between different bracket and normal tuples will now take into account the delimiter type. This solves strange non-locality issues in the compiler due to this false equality, and is more consistent with Janet's otherwise strong equality philosophy. --- CHANGELOG.md | 2 ++ src/core/ffi.c | 2 +- src/core/value.c | 5 +++++ test/suite0001.janet | 2 +- test/suite0002.janet | 4 ++-- 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92d32ea7..49d6ee48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ All notable changes to this project will be documented in this file. ## ??? - Unreleased +- Change semantics around bracket tuples to no longer be equal to regular tuples. +- Add `index` argument to `ffi/write` for symmetry with `ffi/read`. - Add `buffer/push-at` - Add `ffi/pointer-buffer` to convert pointers to buffers the cannot be reallocated. This allows easier manipulation of FFI memory, memory mapped files, and buffer memory shared between threads. diff --git a/src/core/ffi.c b/src/core/ffi.c index b453f1f6..ffd7301e 100644 --- a/src/core/ffi.c +++ b/src/core/ffi.c @@ -1517,7 +1517,7 @@ JANET_CORE_FN(cfun_ffi_pointer_buffer, "Create a buffer from a pointer. The underlying memory of the buffer will not be " "reallocated or freed by the garbage collector, allowing unmanaged, mutable memory " "to be manipulated with buffer functions. Attempts to resize or extend the buffer " - "beyond it's initial capacity will raise an error. As with many FFI functions, it is memory " + "beyond its initial capacity will raise an error. As with many FFI functions, this is memory " "unsafe and can potentially allow out of bounds memory access. Returns a new buffer.") { janet_sandbox_assert(JANET_SANDBOX_FFI); janet_arity(argc, 2, 4); diff --git a/src/core/value.c b/src/core/value.c index e3a192b8..36e381cd 100644 --- a/src/core/value.c +++ b/src/core/value.c @@ -272,6 +272,7 @@ int janet_equals(Janet x, Janet y) { const Janet *t1 = janet_unwrap_tuple(x); const Janet *t2 = janet_unwrap_tuple(y); if (t1 == t2) break; + if (JANET_TUPLE_FLAG_BRACKETCTOR & (janet_tuple_flag(t1) ^ janet_tuple_flag(t2))) return 0; if (janet_tuple_hash(t1) != janet_tuple_hash(t2)) return 0; if (janet_tuple_length(t1) != janet_tuple_length(t2)) return 0; push_traversal_node(janet_tuple_head(t1), janet_tuple_head(t2), 0); @@ -321,6 +322,7 @@ int32_t janet_hash(Janet x) { break; case JANET_TUPLE: hash = janet_tuple_hash(janet_unwrap_tuple(x)); + hash += (janet_tuple_flag(janet_unwrap_tuple(x)) & JANET_TUPLE_FLAG_BRACKETCTOR) ? 1 : 0; break; case JANET_STRUCT: hash = janet_struct_hash(janet_unwrap_struct(x)); @@ -412,6 +414,9 @@ int janet_compare(Janet x, Janet y) { case JANET_TUPLE: { const Janet *lhs = janet_unwrap_tuple(x); const Janet *rhs = janet_unwrap_tuple(y); + if (JANET_TUPLE_FLAG_BRACKETCTOR & (janet_tuple_flag(lhs) ^ janet_tuple_flag(rhs))) { + return (janet_tuple_flag(lhs) & JANET_TUPLE_FLAG_BRACKETCTOR) ? 1 : -1; + } push_traversal_node(janet_tuple_head(lhs), janet_tuple_head(rhs), 1); break; } diff --git a/test/suite0001.janet b/test/suite0001.janet index 63613fbb..3dca5fb6 100644 --- a/test/suite0001.janet +++ b/test/suite0001.janet @@ -228,7 +228,7 @@ (assert (= 14 (sum (map inc @[1 2 3 4]))) "sum map") (def myfun (juxt + - * /)) -(assert (= '[2 -2 2 0.5] (myfun 2)) "juxt") +(assert (= [2 -2 2 0.5] (myfun 2)) "juxt") # Case statements (assert diff --git a/test/suite0002.janet b/test/suite0002.janet index 9e76b1dd..f971df1a 100644 --- a/test/suite0002.janet +++ b/test/suite0002.janet @@ -41,10 +41,10 @@ # Looping idea (def xs - (seq [x :in '[-1 0 1] y :in '[-1 0 1] :when (not= x y 0)] (tuple x y))) + (seq [x :in [-1 0 1] y :in [-1 0 1] :when (not= x y 0)] (tuple x y))) (def txs (apply tuple xs)) -(assert (= txs '[[-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1]]) "nested seq") +(assert (= txs [[-1 -1] [-1 0] [-1 1] [0 -1] [0 1] [1 -1] [1 0] [1 1]]) "nested seq") # Generators (def gen (generate [x :range [0 100] :when (pos? (% x 4))] x))