X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=src%2Fpackage.lisp;h=6fd379be7fe1107cf7defb8fbaed71613030b4b8;hb=c482b4547542853e71a3af2870c87ea366069913;hp=615d1e4fff5a2053397afcca6f2e2a7de55b1957;hpb=208c73a2f0efe2a798ac6ea959687c613dc7d5e8;p=jscl.git diff --git a/src/package.lisp b/src/package.lisp index 615d1e4..6fd379b 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -1,19 +1,46 @@ -;; Packages +;;; package.lisp --- + +;; JSCL is free software: you can redistribute it and/or +;; modify it under the terms of the GNU General Public License as +;; published by the Free Software Foundation, either version 3 of the +;; License, or (at your option) any later version. +;; +;; JSCL is distributed in the hope that it will be useful, but +;; WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;; General Public License for more details. +;; +;; You should have received a copy of the GNU General Public License +;; along with JSCL. If not, see . + +(/debug "loading package.lisp!") (defvar *package-list* nil) (defun list-all-packages () - *package-list*) + (copy-list *package-list*)) + +(defun %make-package (name use) + (let ((package (new))) + (setf (oget package "packageName") name) + (setf (oget package "symbols") (new)) + (setf (oget package "exports") (new)) + (setf (oget package "use") use) + (if (find name *package-list* :key (lambda (s) (oget s "packageName")) :test #'equal) + (error "A package namded `~a' already exists." name) + (push package *package-list*)) + package)) + +(defun resolve-package-list (packages) + (let (result) + (dolist (package (mapcar #'find-package-or-fail packages)) + (pushnew package result :test #'eq)) + (reverse result))) (defun make-package (name &key use) - (let ((package (new)) - (use (mapcar #'find-package-or-fail use))) - (oset package "packageName" name) - (oset package "symbols" (new)) - (oset package "exports" (new)) - (oset package "use" use) - (push package *package-list*) - package)) + (%make-package + (string name) + (resolve-package-list use))) (defun packagep (x) (and (objectp x) (in "symbols" x))) @@ -28,7 +55,7 @@ (defun find-package-or-fail (package-designator) (or (find-package package-designator) - (error "Package unknown."))) + (error "The name `~S' does not designate any package." package-designator))) (defun package-name (package-designator) (let ((package (find-package-or-fail package-designator))) @@ -49,9 +76,6 @@ (defvar *common-lisp-package* (make-package "CL")) -(defvar *js-package* - (make-package "JS")) - (defvar *user-package* (make-package "CL-USER" :use (list *common-lisp-package*))) @@ -63,9 +87,29 @@ (defvar *package* *common-lisp-package*) -(defmacro in-package (package-designator) - `(eval-when-compile - (setq *package* (find-package-or-fail ,package-designator)))) +(defmacro in-package (string-designator) + `(eval-when (:compile-toplevel :load-toplevel :execute) + (setq *package* (find-package-or-fail ',string-designator)))) + +(defmacro defpackage (package &rest options) + (let (use) + (dolist (option options) + (ecase (car option) + (:use + (setf use (append use (cdr option)))))) + `(eval-when (:compile-toplevel :load-toplevel :execute) + (%defpackage ',(string package) ',use)))) + +(defun redefine-package (package use) + (setf (oget package "use") use) + package) + +(defun %defpackage (name use) + (let ((package (find-package name)) + (use (resolve-package-list use))) + (if package + (redefine-package package use) + (%make-package name use)))) ;; This function is used internally to initialize the CL package ;; with the symbols built during bootstrap. @@ -74,11 +118,14 @@ (if (in "package" symbol) (find-package-or-fail (oget symbol "package")) *common-lisp-package*)) - (symbols (%package-symbols package))) - (oset symbol "package" package) + (symbols (%package-symbols package)) + (exports (%package-external-symbols package))) + (setf (oget symbol "package") package) + (setf (oget symbols (symbol-name symbol)) symbol) + ;; Turn keywords self-evaluated and export them. (when (eq package *keyword-package*) - (oset symbol "value" symbol)) - (oset symbols (symbol-name symbol) symbol))) + (setf (oget symbol "value") symbol) + (setf (oget exports (symbol-name symbol)) symbol)))) (defun find-symbol (name &optional (package *package*)) (let* ((package (find-package-or-fail package)) @@ -95,6 +142,11 @@ (when (in name exports) (return (values (oget exports name) :inherit))))))))) + +;;; It is a function to call when a symbol is interned. The function +;;; is invoked with the already interned symbol as argument. +(defvar *intern-hook* nil) + (defun intern (name &optional (package *package*)) (let ((package (find-package-or-fail package))) (multiple-value-bind (symbol foundp) @@ -104,35 +156,67 @@ (let ((symbols (%package-symbols package))) (oget symbols name) (let ((symbol (make-symbol name))) - (oset symbol "package" package) + (setf (oget symbol "package") package) (when (eq package *keyword-package*) - (oset symbol "value" symbol) + (setf (oget symbol "value") symbol) (export (list symbol) package)) - (when (eq package *js-package*) - (let ((sym-name (symbol-name symbol)) - (args (gensym))) - ;; Generate a trampoline to call the JS function - ;; properly. This trampoline is very inefficient, - ;; but it still works. Ideas to optimize this are - ;; provide a special lambda keyword - ;; cl::&rest-vector to avoid list argument - ;; consing, as well as allow inline declarations. - (fset symbol - (eval `(lambda (&rest ,args) - (let ((,args (list-to-vector ,args))) - (%js-call (%js-vref ,sym-name) ,args))))) - ;; Define it as a symbol macro to access to the - ;; Javascript variable literally. - (%define-symbol-macro symbol `(%js-vref ,(string symbol))))) - (oset symbols name symbol) + (when *intern-hook* + (funcall *intern-hook* symbol)) + (setf (oget symbols name) symbol) (values symbol nil))))))) (defun symbol-package (symbol) (unless (symbolp symbol) - (error "it is not a symbol")) + (error "`~S' is not a symbol." symbol)) (oget symbol "package")) (defun export (symbols &optional (package *package*)) (let ((exports (%package-external-symbols package))) (dolist (symb symbols t) - (oset exports (symbol-name symb) symb)))) + (setf (oget exports (symbol-name symb)) symb)))) + +(defun %map-external-symbols (function package) + (map-for-in function (%package-external-symbols package))) + +(defun %map-symbols (function package) + (map-for-in function (%package-symbols package)) + (dolist (used (package-use-list package)) + (%map-external-symbols function used))) + +(defun %map-all-symbols (function) + (dolist (package *package-list*) + (map-for-in function (%package-symbols package)))) + +(defun %map-all-external-symbols (function) + (dolist (package *package-list*) + (map-for-in function (%package-external-symbols package)))) + +(defmacro do-symbols ((var &optional (package '*package*) result-form) + &body body) + `(block nil + (%map-symbols + (lambda (,var) ,@body) + (find-package ,package)) + ,result-form)) + +(defmacro do-external-symbols ((var &optional (package '*package*) + result-form) + &body body) + `(block nil + (%map-external-symbols + (lambda (,var) ,@body) + (find-package ,package)) + ,result-form)) + +(defmacro do-all-symbols ((var &optional result-form) &body body) + `(block nil (%map-all-symbols (lambda (,var) ,@body)) ,result-form)) + +(defmacro do-all-external-symbols ((var &optional result-form) &body body) + `(block nil (%map-all-external-symbols (lambda (,var) ,@body)) ,result-form)) + +(defun find-all-symbols (string &optional external-only) + (let (symbols) + (dolist (package *package-list* symbols) + (multiple-value-bind (symbol status) (find-symbol string package) + (when (if external-only (eq status :external) status) + (pushnew symbol symbols :test #'eq))))))