Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically generate API export specifications for packages #20

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#:define-enum-type
#:define-error-map
#:define-api
#:define-quick-api
#:define-library
#:define-aggregate-library
#:build-bindings
Expand Down
54 changes: 54 additions & 0 deletions quick-api.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
(in-package #:sbcl-librarian)

(defun package-specs (package handle-type)
(labels ((function-spec-from-symbol (sym)
(let ((func (fboundp sym)))
(if (null func)
(values)
(let ((signature (sb-introspect:function-type func))
(lambda-list (sb-introspect:function-lambda-list func)))
`(,sym ,(return-type signature) ,(typed-args lambda-list signature))))))

(return-type (signature)
(ffi-type (third signature)))

(atomic-type (type-spec)
(etypecase type-spec
(list
(ecase (first type-spec)
(values (atomic-type (second type-spec)))
(mod 'integer)
(not t)
(or t)))
(symbol type-spec)))

(ffi-type (type-spec)
(case (atomic-type type-spec)
(boolean :bool)
(integer :int)
(simple-string :string)
(double-float :double)
(otherwise handle-type)))

(typed-args (lambda-list signature)
(loop :for type-spec :in (second signature)
:for name :in lambda-list
:collect `(,name ,(ffi-type type-spec)))))
(let ((specs '()))
(do-external-symbols (sym package)
(unless (fboundp sym)
(format t "~a~%" sym))
(push (function-spec-from-symbol sym) specs))
specs)))

(defmacro define-quick-api (name (&key package (function-prefix "") error-map handle-type error-type))
(let ((specs `((:type ,error-type ,handle-type)
(:function ,@(package-specs package handle-type)))))
`(progn
,@(callable-definitions-from-spec function-prefix error-map specs)
(defvar ,name
(make-instance 'api
:name ',name
:error-map ',error-map
:function-prefix ,function-prefix
:specs ',specs)))))