Skip to content

Commit

Permalink
Merge pull request #431 from nagy/numbers-as-hash-keys
Browse files Browse the repository at this point in the history
Make hash-table support numbers as keys
  • Loading branch information
davazp committed Aug 23, 2022
2 parents ffccdb1 + 65e4d35 commit 29885f7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/prelude.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ internals.safe_char_downcase = function(x) {
};

internals.xstring = function(x){
if(typeof x === "number") return x.toString();
const hasFillPointer = typeof x.fillpointer === 'number'
const activechars = hasFillPointer? x.slice(0, x.fillpointer): x
return activechars.join('');
Expand Down
7 changes: 7 additions & 0 deletions tests/ffi.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@
(test (js-object-p obj))
(test (js-object-signature obj)))

;;; test in can handle numbers

(let ((obj (make-new #j:Object)))
(test (js-object-p obj))
(test (oset 456 obj 123))
(test (equal 456 (oget obj 123))))

;;; EOF
15 changes: 15 additions & 0 deletions tests/hash-tables.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,19 @@
(let ((h (temp-hash-table :fill t)))
(remhash 'one h)
h))))

;;; Test numbers as keys
(let ((ht (make-hash-table)))
(test (eq nil (gethash 123 ht)))
(test (eq 'foo (setf (gethash 123 ht) 'foo)))
(test (eq 'foo (gethash 123 ht))))

;;; Test numbers are different than strings
(let ((ht (make-hash-table :test #'equal)))
(test (equal 'foo (setf (gethash 123 ht) 'foo)))
(test (equal 'bar (setf (gethash "123" ht) 'bar)))
(test (equal 'foo (gethash 123 ht)))
(test (equal 'bar (gethash "123" ht)))
(test (eq 2 (length (hash-table-keys ht)))))

;;; EOF

0 comments on commit 29885f7

Please sign in to comment.