Skip to content

Commit 29885f7

Browse files
authored
Merge pull request #431 from nagy/numbers-as-hash-keys
Make hash-table support numbers as keys
2 parents ffccdb1 + 65e4d35 commit 29885f7

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/prelude.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ internals.safe_char_downcase = function(x) {
224224
};
225225

226226
internals.xstring = function(x){
227+
if(typeof x === "number") return x.toString();
227228
const hasFillPointer = typeof x.fillpointer === 'number'
228229
const activechars = hasFillPointer? x.slice(0, x.fillpointer): x
229230
return activechars.join('');

tests/ffi.lisp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,11 @@
5858
(test (js-object-p obj))
5959
(test (js-object-signature obj)))
6060

61+
;;; test in can handle numbers
62+
63+
(let ((obj (make-new #j:Object)))
64+
(test (js-object-p obj))
65+
(test (oset 456 obj 123))
66+
(test (equal 456 (oget obj 123))))
67+
6168
;;; EOF

tests/hash-tables.lisp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,19 @@
133133
(let ((h (temp-hash-table :fill t)))
134134
(remhash 'one h)
135135
h))))
136+
137+
;;; Test numbers as keys
138+
(let ((ht (make-hash-table)))
139+
(test (eq nil (gethash 123 ht)))
140+
(test (eq 'foo (setf (gethash 123 ht) 'foo)))
141+
(test (eq 'foo (gethash 123 ht))))
142+
143+
;;; Test numbers are different than strings
144+
(let ((ht (make-hash-table :test #'equal)))
145+
(test (equal 'foo (setf (gethash 123 ht) 'foo)))
146+
(test (equal 'bar (setf (gethash "123" ht) 'bar)))
147+
(test (equal 'foo (gethash 123 ht)))
148+
(test (equal 'bar (gethash "123" ht)))
149+
(test (eq 2 (length (hash-table-keys ht)))))
150+
136151
;;; EOF

0 commit comments

Comments
 (0)