1 ;;; hash-table.lisp ---
3 ;; JSCL is free software: you can redistribute it and/or
4 ;; modify it under the terms of the GNU General Public License as
5 ;; published by the Free Software Foundation, either version 3 of the
6 ;; License, or (at your option) any later version.
8 ;; JSCL is distributed in the hope that it will be useful, but
9 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;; General Public License for more details.
13 ;; You should have received a copy of the GNU General Public License
14 ;; along with JSCL. If not, see <http://www.gnu.org/licenses/>.
16 ;;; Javascript dictionaries are the natural way to implement Common
17 ;;; Lisp hash tables. However, there is a big differences betweent
18 ;;; them which we need to work around. Javascript dictionaries require
19 ;;; the keys to be strings. To solve that, we map Lisp objects to
20 ;;; strings such that "equivalent" values map to the same string,
21 ;;; regarding the equality predicate used (one of `eq', `eql', `equal'
26 ;;; If a hash table has `eq' as test, we need to generate unique
27 ;;; strings for each Lisp object. To do this, we tag the objects with
28 ;;; a `$$jscl_id' property. As a special case, numbers are not
29 ;;; objects, but they can be used for indexin a Javascript dictionary,
30 ;;; we do not need to tag them.
31 (defvar *eq-hash-counter* 0)
37 (unless (in "$$jscl_id" x)
38 (oset (format nil "$~d" *eq-hash-counter*) x "$$jscl_id")
39 (incf *eq-hash-counter*))
40 (oget x "$$jscl_id"))))
42 ;;; We do not have bignums, so eql is equivalent to eq.
47 ;;; In the case of equal-based hash tables, we do not store the hash
48 ;;; in the objects, but compute a hash from the elements it contains.
52 (concat "(" (equal-hash (car x)) (equal-hash (cdr x)) ")"))
54 (concat "s" (integer-to-string (length x)) ":" (lisp-to-js x)))
58 (defun equalp-hash (x)
59 ;; equalp is not implemented as predicate. So I am skipping this one
64 (defun make-hash-table (&key (test #'eql))
65 (let* ((test-fn (fdefinition test))
68 ((eq test-fn #'eq) #'eq-hash)
69 ((eq test-fn #'eql) #'eql-hash)
70 ((eq test-fn #'equal) #'equal-hash)
71 ((eq test-fn #'equalp) #'equalp-hash))))
72 ;; TODO: Replace list with a storage-vector and tag
73 ;; conveniently to implemnet `hash-table-p'.
74 `(hash-table ,hash-fn ,(new))))
76 (defun gethash (key hash-table &optional default)
77 (let ((obj (caddr hash-table))
78 (hash (funcall (cadr hash-table) key)))
79 (values (oget obj hash)
82 (defun sethash (new-value key hash-table)
83 (let ((obj (caddr hash-table))
84 (hash (funcall (cadr hash-table) key)))
85 (oset new-value obj hash)
89 ;;; TODO: Please, implement (DEFUN (SETF foo) ...) syntax!
90 (define-setf-expander gethash (key hash-table &optional defaults)
91 (let ((g!key (gensym))
92 (g!hash-table (gensym))
94 (g!new-value (gensym)))
95 (values (list g!key g!hash-table g!defaults) ; temporary variables
96 (list key hash-table defaults) ; value forms
97 (list g!new-value) ; store variables
99 (sethash ,g!new-value ,g!key ,g!hash-table) ; storing form
101 `(gethash ,g!new-value ,g!key ,g!hash-table) ; accessing form
105 (defun remhash (key hash-table)
106 (let ((obj (caddr hash-table))
107 (hash (funcall (cadr hash-table) key)))
109 (delete-property hash obj))))