Difference between revisions of "ECL/Wrapping constants"
Warning: preg_match(): Compilation failed: group name must start with a non-digit at offset 8 in /home/www/web437/html/mediawiki/includes/MagicWord.php on line 902
From formgames wiki
< ECL
(Created page with "{{ECL/experiments message}} == Documentation / References == * [http://sourceforge.net/mailarchive/message.php?msg_id=31157657 Thread at ecls-list: Wrapping C++ constants......") |
|||
Line 1: | Line 1: | ||
{{ECL/experiments message}} | {{ECL/experiments message}} | ||
− | = | + | = Example = |
− | + | DEFCONSTANT has to be wrapped into a LET block: | |
− | + | <pre> | |
+ | (ffi:clines "#define SOME_CONSTANT 123") | ||
+ | (let () (defconstant +some-constant+ (ffi:c-inline () () :int "SOME_CONSTANT" :one-liner t))) | ||
+ | </pre> | ||
− | + | Usage: | |
<pre> | <pre> | ||
− | + | (format t "+some-constant+: ~a~%" +some-constant+) | |
</pre> | </pre> | ||
− | + | Output: | |
− | ==== | + | <pre> |
+ | +some-constant+: 123 | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | Using DEFCONSTANT on the top-level | ||
+ | |||
+ | <pre> | ||
+ | (ffi:clines "#define SOME_CONSTANT 123") | ||
+ | (defconstant +some-constant+ (ffi:c-inline () () :int "SOME_CONSTANT" :one-liner t)) | ||
+ | </pre> | ||
+ | |||
+ | results in an error being thrown: | ||
+ | |||
+ | <pre> | ||
+ | ;;; Error: | ||
+ | ;;; in file rhino-lisp-api.lisp, position 42 | ||
+ | ;;; at (DEFCONSTANT +SOME-CONSTANT+ ...) | ||
+ | ;;; * The form (PROGN (SI:*MAKE-CONSTANT '+SOME-CONSTANT+ (FFI:C-INLINE NIL NIL :INT "SOME_CONSTANT" :ONE-LINER T)) (SI::REGISTER-GLOBAL '+SOME-CONSTANT+)) was not evaluated successfully. | ||
+ | ;;; Error detected: | ||
+ | ;;; The special form c-inline cannot be used in the interpreter: (NIL NIL INT SOME_CONSTANT ONE-LINER T)An error occurred during initialization: | ||
+ | COMPILE-FILE-ERROR while | ||
+ | compiling #<cl-source-file "rhino-lisp-api" "rhino-lisp-api">. | ||
+ | NMAKE : fatal error U1077: 'C:\Users\dietrich\home\cs\lang\lisp\ecl\64\git\install\ecl.exe' : return code '0x1' | ||
+ | Stop. | ||
+ | </pre> | ||
+ | |||
+ | |||
+ | = Documentation / References = | ||
+ | |||
+ | * [http://sourceforge.net/mailarchive/message.php?msg_id=31157657 Thread at ecls-list: Wrapping C++ constants...] | ||
+ | |||
+ | |||
+ | = Other examples from [http://sourceforge.net/p/ecls/ecl/ci/master/tree/contrib/sockets/sockets.lisp contrib/sockets/sockets.lisp] = | ||
* [http://sourceforge.net/p/ecls/ecl/ci/master/tree/contrib/sockets/sockets.lisp#l92] | * [http://sourceforge.net/p/ecls/ecl/ci/master/tree/contrib/sockets/sockets.lisp#l92] | ||
<pre> | <pre> | ||
− | + | (eval-when (:compile-toplevel :execute) | |
+ | (defmacro c-constant (c-name) | ||
+ | `(c-inline () () :int ,c-name :one-liner t)) | ||
+ | (defmacro define-c-constants (&rest args) | ||
+ | `(let () ; Prevents evaluation of constant value form | ||
+ | ,@(loop | ||
+ | for (lisp-name c-name) on args by #'cddr | ||
+ | collect `(defconstant ,lisp-name (c-constant ,c-name)))))) | ||
</pre> | </pre> | ||
Line 26: | Line 69: | ||
<pre> | <pre> | ||
− | + | (define-c-constants | |
+ | +af-inet+ "AF_INET" | ||
+ | +af-local+ #-sun4sol2 "AF_LOCAL" #+sun4sol2 "AF_UNIX" | ||
+ | +eagain+ "EAGAIN" | ||
+ | +eintr+ "EINTR") | ||
+ | </pre> | ||
+ | |||
+ | * [http://sourceforge.net/p/ecls/ecl/ci/master/tree/contrib/sockets/sockets.lisp#l1352] | ||
+ | |||
+ | <pre> | ||
+ | (defmacro define-socket-condition (symbol name) | ||
+ | `(let () ; Prevents evaluation of constant value at compilation time | ||
+ | (defconstant ,symbol (c-constant ,(symbol-name symbol))) | ||
+ | (define-condition ,name (socket-error) | ||
+ | ((symbol :reader socket-error-symbol :initform (quote ,symbol)))) | ||
+ | (export ',name) | ||
+ | (push (cons ,symbol (quote ,name)) *conditions-for-errno*))) | ||
</pre> | </pre> | ||
Line 32: | Line 91: | ||
<pre> | <pre> | ||
− | + | (defmacro define-name-service-condition (symbol name) | |
+ | `(let () | ||
+ | (defconstant ,symbol (c-constant ,(symbol-name symbol))) | ||
+ | (define-condition ,name (name-service-error) | ||
+ | ((symbol :reader name-service-error-symbol :initform (quote ,symbol)))) | ||
+ | (push (cons ,symbol (quote ,name)) *conditions-for-name-service-errno*) | ||
+ | (export (quote ,symbol)))) | ||
</pre> | </pre> |