namespace.lisp 1.52 KB
Newer Older
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;; ParenScript namespace system
3 4 5

(in-package :parenscript)

6 7 8 9 10
(defun lisp-symbol-to-ps-identifier (symbol context)
  (case context
    (:special-form (symbol-name symbol))
    (:macro symbol)
    (otherwise (symbol-name symbol))))
11

12
;;; Symbol obfuscation
13
(defvar *obfuscation-table* (make-hash-table))
14

15 16
(defun obfuscate-package (package-designator)
  (setf (gethash (find-package package-designator) *obfuscation-table*) (make-hash-table)))
17

18 19
(defun unobfuscate-package (package-designator)
  (remhash (find-package package-designator) *obfuscation-table*))
20

21 22 23 24 25 26
(defun maybe-obfuscate-symbol (symbol)
  (let ((obfuscated-symbol-table (gethash (symbol-package symbol) *obfuscation-table*)))
    (if obfuscated-symbol-table
        (or (gethash symbol obfuscated-symbol-table)
            (setf (gethash symbol obfuscated-symbol-table) (ps-gensym "G")))
        symbol)))
27

28
;;; Interface for printing identifiers
29

30 31 32 33 34 35
(defvar *package-prefix-table* (make-hash-table))

(defmacro ps-package-prefix (package)
  "Place for storing a string to be prefixed to any symbols in the
designated package when translating ParenScript code."
  `(gethash (find-package ,package) *package-prefix-table*))
36

37
(defun js-translate-symbol (symbol)
38 39 40 41
  (let ((possibly-obfuscated-symbol (maybe-obfuscate-symbol symbol)))
    (if (ps-package-prefix (symbol-package symbol))
        (format nil "~A~A" (ps-package-prefix (symbol-package symbol)) (symbol-to-js possibly-obfuscated-symbol))
        (symbol-to-js possibly-obfuscated-symbol))))