From b4f242193d22687ffa725248e680324b0574d440 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 26 Dec 2020 15:38:04 -0600 Subject: [PATCH] Improve hash function for numbers. --- CHANGELOG.md | 3 +++ src/boot/boot.janet | 10 ++++++---- src/core/value.c | 7 +++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4bcfec5..4288299f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to this project will be documented in this file. ## Unreleased - ??? +- Importing modules that start with `/` is now the only way to import from project root. + Before, this would import from / on disk. +- Change hash function for numbers. - Improve error handling of `dofile`. ## 1.13.1 - 2020-12-13 diff --git a/src/boot/boot.janet b/src/boot/boot.janet index cbdefdfd..df30fc86 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -815,7 +815,7 @@ (sort-help a lo right by) (sort-help a left hi by)) a) - + (defn sort "Sort an array in-place. Uses quick-sort and is not a stable sort." [a &opt by] @@ -2598,7 +2598,8 @@ (unmarshal image load-image-dict)) (defn- check-. [x] (if (string/has-prefix? "." x) x)) -(defn- not-check-. [x] (unless (string/has-prefix? "." x) x)) +(defn- not-check-. [x] (unless (or (string/has-prefix? "/" x) (string/has-prefix? "." x)) x)) +(defn- check-/ [x] (if (string/has-prefix? "/" x) x)) (def module/paths ``` @@ -2634,8 +2635,8 @@ (defn- find-prefix [pre] (or (find-index |(and (string? ($ 0)) (string/has-prefix? pre ($ 0))) module/paths) 0)) - (def all-index (find-prefix ":all:")) - (array/insert module/paths all-index [(string ":all:" ext) loader not-check-.]) + (def all-index (find-prefix ".:all:")) + (array/insert module/paths all-index [(string ".:all:" ext) loader check-/]) (def sys-index (find-prefix ":sys:")) (array/insert module/paths sys-index [(string ":sys:/:all:" ext) loader not-check-.]) (def curall-index (find-prefix ":cur:/:all:")) @@ -2700,6 +2701,7 @@ (undef mod-filter) (undef check-.) (undef not-check-.) +(undef check-/) (def module/loading `Table mapping currently loading modules to true. Used to prevent diff --git a/src/core/value.c b/src/core/value.c index 1be7966c..8b45a397 100644 --- a/src/core/value.c +++ b/src/core/value.c @@ -261,6 +261,13 @@ int32_t janet_hash(Janet x) { case JANET_STRUCT: hash = janet_struct_hash(janet_unwrap_struct(x)); break; + case JANET_NUMBER: { + uint64_t i = janet_u64(x); + uint32_t lo = (uint32_t)(i & 0xFFFFFFFF); + uint32_t hi = (uint32_t)(i >> 32); + hash = (int32_t)(hi ^ lo); + break; + } case JANET_ABSTRACT: { JanetAbstract xx = janet_unwrap_abstract(x); const JanetAbstractType *at = janet_abstract_type(xx);