2010-04-09 00:30:10 +02:00
|
|
|
|
;;;; foreign.test --- FFI. -*- mode: scheme; coding: utf-8; -*-
|
|
|
|
|
|
;;;;
|
2011-01-30 22:00:35 +01:00
|
|
|
|
;;;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
|
2010-04-09 00:30:10 +02:00
|
|
|
|
;;;;
|
|
|
|
|
|
;;;; This library is free software; you can redistribute it and/or
|
|
|
|
|
|
;;;; modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
|
;;;; License as published by the Free Software Foundation; either
|
|
|
|
|
|
;;;; version 3 of the License, or (at your option) any later version.
|
|
|
|
|
|
;;;;
|
|
|
|
|
|
;;;; This library 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
|
|
|
|
|
|
;;;; Lesser General Public License for more details.
|
|
|
|
|
|
;;;;
|
|
|
|
|
|
;;;; You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
|
;;;; License along with this library; if not, write to the Free Software
|
|
|
|
|
|
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
|
;;; See also ../standalone/test-ffi for FFI tests.
|
|
|
|
|
|
;;;
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (test-foreign)
|
|
|
|
|
|
#:use-module (system foreign)
|
2010-06-01 13:26:11 +02:00
|
|
|
|
#:use-module (rnrs bytevectors)
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
#:use-module (srfi srfi-1)
|
2010-09-04 14:38:20 +02:00
|
|
|
|
#:use-module (srfi srfi-26)
|
2010-04-09 00:30:10 +02:00
|
|
|
|
#:use-module (test-suite lib))
|
|
|
|
|
|
|
2011-03-30 20:42:37 -07:00
|
|
|
|
|
|
|
|
|
|
(with-test-prefix "dynamic-pointer"
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if-exception
|
|
|
|
|
|
"error message"
|
|
|
|
|
|
'(misc-error . "^Symbol not found")
|
|
|
|
|
|
(dynamic-func "does_not_exist___" (dynamic-link))))
|
|
|
|
|
|
|
2010-04-09 00:30:10 +02:00
|
|
|
|
|
|
|
|
|
|
(with-test-prefix "null pointer"
|
|
|
|
|
|
|
2011-01-30 22:47:35 +01:00
|
|
|
|
(pass-if "pointer?"
|
|
|
|
|
|
(pointer? %null-pointer))
|
|
|
|
|
|
|
2010-04-09 00:30:10 +02:00
|
|
|
|
(pass-if "zero"
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
(= 0 (pointer-address %null-pointer)))
|
2010-04-09 00:30:10 +02:00
|
|
|
|
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
(pass-if "null pointer identity"
|
|
|
|
|
|
(eq? %null-pointer (make-pointer 0)))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "null-pointer? %null-pointer"
|
|
|
|
|
|
(null-pointer? %null-pointer))
|
2010-04-09 00:30:10 +02:00
|
|
|
|
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
(pass-if-exception "pointer->bytevector %null-pointer"
|
2010-04-09 00:30:10 +02:00
|
|
|
|
exception:null-pointer-error
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
(pointer->bytevector %null-pointer 7)))
|
2010-04-09 00:30:10 +02:00
|
|
|
|
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
|
|
|
|
|
|
(with-test-prefix "make-pointer"
|
|
|
|
|
|
|
2011-01-30 22:47:35 +01:00
|
|
|
|
(pass-if "pointer?"
|
|
|
|
|
|
(pointer? (make-pointer 123)))
|
|
|
|
|
|
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
(pass-if "address preserved"
|
2010-09-23 10:55:24 +02:00
|
|
|
|
(= 123 (pointer-address (make-pointer 123))))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "equal?"
|
|
|
|
|
|
(equal? (make-pointer 123) (make-pointer 123)))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "equal? modulo finalizer"
|
|
|
|
|
|
(let ((finalizer (dynamic-func "scm_is_pair" (dynamic-link))))
|
|
|
|
|
|
(equal? (make-pointer 123)
|
|
|
|
|
|
(make-pointer 123 finalizer))))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "not equal?"
|
|
|
|
|
|
(not (equal? (make-pointer 123) (make-pointer 456)))))
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
|
2011-03-17 10:39:02 +01:00
|
|
|
|
|
|
|
|
|
|
(with-test-prefix "pointer<->scm"
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "immediates"
|
|
|
|
|
|
(equal? (pointer->scm (scm->pointer #\newline))
|
|
|
|
|
|
#\newline))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "non-immediates"
|
|
|
|
|
|
(equal? (pointer->scm (scm->pointer "Hello, world!"))
|
|
|
|
|
|
"Hello, world!")))
|
|
|
|
|
|
|
2011-01-30 22:00:35 +01:00
|
|
|
|
|
2011-02-08 22:40:55 +01:00
|
|
|
|
(define-wrapped-pointer-type foo
|
|
|
|
|
|
foo?
|
2011-01-30 22:00:35 +01:00
|
|
|
|
wrap-foo unwrap-foo
|
|
|
|
|
|
(lambda (x p)
|
|
|
|
|
|
(format p "#<foo! ~a>" (pointer-address (unwrap-foo x)))))
|
|
|
|
|
|
|
|
|
|
|
|
(with-test-prefix "define-wrapped-pointer-type"
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "foo?"
|
|
|
|
|
|
(foo? (wrap-foo %null-pointer)))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "unwrap-foo"
|
|
|
|
|
|
(let ((p (make-pointer 123)))
|
|
|
|
|
|
(eq? p (unwrap-foo (wrap-foo p)))))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "identity"
|
|
|
|
|
|
(let ((p1 (make-pointer 123))
|
|
|
|
|
|
(p2 (make-pointer 123)))
|
|
|
|
|
|
(eq? (wrap-foo p1)
|
|
|
|
|
|
(wrap-foo p2))))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "printer"
|
|
|
|
|
|
(string=? "#<foo! 123>"
|
|
|
|
|
|
(with-output-to-string
|
|
|
|
|
|
(lambda ()
|
|
|
|
|
|
(write (wrap-foo (make-pointer 123))))))))
|
|
|
|
|
|
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
(with-test-prefix "pointer<->bytevector"
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
|
|
|
|
|
|
(pass-if "bijection"
|
|
|
|
|
|
(let ((bv #vu8(0 1 2 3 4 5 6 7)))
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
(equal? (pointer->bytevector (bytevector->pointer bv)
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
(bytevector-length bv))
|
|
|
|
|
|
bv)))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "pointer from bits"
|
|
|
|
|
|
(let* ((bytes (iota (sizeof '*)))
|
2011-05-06 17:43:37 +02:00
|
|
|
|
(bv (u8-list->bytevector bytes))
|
|
|
|
|
|
(fold (case (native-endianness)
|
|
|
|
|
|
((little) fold-right)
|
|
|
|
|
|
((big) fold)
|
|
|
|
|
|
(else (error "unsupported endianness")))))
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
(= (pointer-address
|
Simplify the (system foreign) API.
Suggested by Neil Jerram.
* libguile/foreign.h (SCM_FOREIGN_TYPE, SCM_FOREIGN_VALUE_REF,
SCM_FOREIGN_VALUE_SET, SCM_FOREIGN_LEN, SCM_FOREIGN_TYPED_P,
SCM_FOREIGN_VALUE_P, SCM_VALIDATE_FOREIGN_VALUE, scm_foreign_set_x,
scm_foreign_type): Remove.
(scm_foreign_ref): Rename to...
(scm_foreign_address): ... this.
(scm_take_foreign_pointer): Update.
(SCM_FOREIGN_POINTER): Remove CTYPE argument. Update callers.
(scm_make_pointer): New declaration.
* libguile/foreign.c (scm_to_uintptr, scm_from_uintptr): New macros.
(scm_make_pointer): New function.
(scm_take_foreign_pointer): Remove TYPE and LEN arguments. Update
callers.
(scm_foreign_ref): Remove to...
(scm_foreign_address): ... this. Remove type-related code.
(scm_foreign_set_x): Remove.
(scm_foreign_to_bytevector): Change argument order; make LEN argument
compulsory.
(scm_i_foreign_print): Remove type printing.
(unpack): Remove foreign-type checking.
* libguile/deprecated.c (scm_dynamic_args_call): Update accordingly.
* libguile/dynl.c (scm_dynamic_pointer): Remove the TYPE and LEN
arguments; update callers. Update to the new foreign API.
* libguile/dynl.h (scm_dynamic_pointer): Update.
* libguile/gsubr.c (create_gsubr): Update to the new foreign API.
* libguile/gsubr.h (SCM_SUBRF, SCM_SUBR_GENERIC): Ditto.
* libguile/snarf.h (SCM_IMMUTABLE_FOREIGN): Ditto.
* libguile/vm-i-system.c (subr_call): Ditto.
* module/system/foreign.scm (null-pointer?): New procedure.
* test-suite/standalone/test-ffi: Update to the new
`bytevector->foreign' signature.
* test-suite/tests/foreign.test ("null pointer")["null pointer
identity", "null-pointer? %null-pointer"]: New tests.
["foreign-set! other-null-pointer", "foreign->bytevector
other-null-pointer"]: Remove.
("make-pointer", "foreign<->bytevector"): New test prefixes.
2010-07-26 17:44:43 +02:00
|
|
|
|
(make-pointer (bytevector-uint-ref bv 0 (native-endianness)
|
|
|
|
|
|
(sizeof '*))))
|
2011-05-06 17:43:37 +02:00
|
|
|
|
(fold (lambda (byte address)
|
|
|
|
|
|
(+ byte (* 256 address)))
|
|
|
|
|
|
0
|
|
|
|
|
|
bytes))))
|
2010-07-26 19:36:03 +02:00
|
|
|
|
|
|
|
|
|
|
(pass-if "dereference-pointer"
|
|
|
|
|
|
(let* ((bytes (iota (sizeof '*)))
|
2011-05-06 17:43:37 +02:00
|
|
|
|
(bv (u8-list->bytevector bytes))
|
|
|
|
|
|
(fold (case (native-endianness)
|
|
|
|
|
|
((little) fold-right)
|
|
|
|
|
|
((big) fold)
|
|
|
|
|
|
(else (error "unsupported endianness")))))
|
Use "pointer" instead of "foreign" when dealing with wrapped pointers.
* libguile/foreign.h (scm_t_foreign_finalizer): Rename to...
(scm_t_pointer_finalizer): ... this.
(SCM_FOREIGN_P): Rename to...
(SCM_POINTER_P): this.
(SCM_VALIDATE_FOREIGN): Rename to...
(SCM_VALIDATE_POINTER): ... this.
(SCM_FOREIGN_HAS_FINALIZER): Rename to...
(SCM_POINTER_HAS_FINALIZER): ... this.
(scm_take_foreign_pointer): Rename to...
(scm_from_pointer): ... this.
(scm_foreign_address): Rename to...
(scm_pointer_address): ... this.
(scm_foreign_to_bytevector): Rename to...
(scm_pointer_to_bytevector): ... this.
(scm_foreign_set_finalizer_x): Rename to...
(scm_set_pointer_finalizer_x): ... this.
(scm_bytevector_to_foreign): Rename to...
(scm_bytevector_to_pointer): ... this.
(scm_i_foreign_print): Rename to...
(scm_i_pointer_print): ... this.
* libguile/foreign.c: Update accordingly.
* libguile/tags.h (scm_tc7_foreign): Rename to...
(scm_tc7_pointer): ... this.
* libguile/foreign.c, libguile/deprecated.c, libguile/dynl.c,
libguile/evalext.c, libguile/gc.c, libguile/goops.c, libguile/gsubr.c,
libguile/gsubr.h, libguile/print.c, libguile/snarf.h,
libguile/vm-i-system.c, module/system/foreign.scm,
test-suite/standalone/test-ffi, test-suite/tests/foreign.test: Update
accordingly.
2010-07-27 14:54:53 +02:00
|
|
|
|
(= (pointer-address
|
|
|
|
|
|
(dereference-pointer (bytevector->pointer bv)))
|
2011-05-06 17:43:37 +02:00
|
|
|
|
(fold (lambda (byte address)
|
|
|
|
|
|
(+ byte (* 256 address)))
|
|
|
|
|
|
0
|
|
|
|
|
|
bytes)))))
|
2010-07-28 12:02:50 +02:00
|
|
|
|
|
2010-08-15 16:42:33 +02:00
|
|
|
|
|
|
|
|
|
|
(with-test-prefix "pointer<->string"
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "bijection"
|
|
|
|
|
|
(let ((s "hello, world"))
|
|
|
|
|
|
(string=? s (pointer->string (string->pointer s)))))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "bijection [latin1]"
|
|
|
|
|
|
(with-latin1-locale
|
|
|
|
|
|
(let ((s "Szép jó napot!"))
|
2011-04-01 13:31:26 +02:00
|
|
|
|
(string=? s (pointer->string (string->pointer s))))))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "bijection, utf-8"
|
|
|
|
|
|
(let ((s "hello, world"))
|
|
|
|
|
|
(string=? s (pointer->string (string->pointer s "utf-8")
|
|
|
|
|
|
-1 "utf-8"))))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "bijection, utf-8 [latin1]"
|
|
|
|
|
|
(let ((s "Szép jó napot!"))
|
|
|
|
|
|
(string=? s (pointer->string (string->pointer s "utf-8")
|
|
|
|
|
|
-1 "utf-8")))))
|
|
|
|
|
|
|
2010-08-15 16:42:33 +02:00
|
|
|
|
|
2011-02-09 00:08:14 +01:00
|
|
|
|
|
|
|
|
|
|
(with-test-prefix "pointer->procedure"
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if-exception "object instead of pointer"
|
|
|
|
|
|
exception:wrong-type-arg
|
|
|
|
|
|
(let ((p (pointer->procedure '* %null-pointer '(*))))
|
|
|
|
|
|
(p #t))))
|
|
|
|
|
|
|
2010-09-03 15:12:54 +02:00
|
|
|
|
|
|
|
|
|
|
(with-test-prefix "procedure->pointer"
|
|
|
|
|
|
|
|
|
|
|
|
(define qsort
|
|
|
|
|
|
;; Bindings for libc's `qsort' function.
|
2010-09-06 22:24:44 +02:00
|
|
|
|
(pointer->procedure void
|
|
|
|
|
|
(dynamic-func "qsort" (dynamic-link))
|
|
|
|
|
|
(list '* size_t size_t '*)))
|
2010-09-03 15:12:54 +02:00
|
|
|
|
|
|
|
|
|
|
(define (dereference-pointer-to-byte ptr)
|
|
|
|
|
|
(let ((b (pointer->bytevector ptr 1)))
|
|
|
|
|
|
(bytevector-u8-ref b 0)))
|
|
|
|
|
|
|
|
|
|
|
|
(define input
|
|
|
|
|
|
'(7 1 127 3 5 4 77 2 9 0))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "qsort"
|
|
|
|
|
|
(if (defined? 'procedure->pointer)
|
|
|
|
|
|
(let* ((called? #f)
|
|
|
|
|
|
(cmp (lambda (x y)
|
|
|
|
|
|
(set! called? #t)
|
|
|
|
|
|
(- (dereference-pointer-to-byte x)
|
|
|
|
|
|
(dereference-pointer-to-byte y))))
|
|
|
|
|
|
(ptr (procedure->pointer int cmp (list '* '*)))
|
|
|
|
|
|
(bv (u8-list->bytevector input)))
|
|
|
|
|
|
(qsort (bytevector->pointer bv) (bytevector-length bv) 1
|
|
|
|
|
|
(procedure->pointer int cmp (list '* '*)))
|
|
|
|
|
|
(and called?
|
|
|
|
|
|
(equal? (bytevector->u8-list bv)
|
|
|
|
|
|
(sort input <))))
|
|
|
|
|
|
(throw 'unresolved)))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if-exception "qsort, wrong return type"
|
|
|
|
|
|
exception:wrong-type-arg
|
|
|
|
|
|
|
|
|
|
|
|
(if (defined? 'procedure->pointer)
|
|
|
|
|
|
(let* ((cmp (lambda (x y) #f)) ; wrong return type
|
|
|
|
|
|
(ptr (procedure->pointer int cmp (list '* '*)))
|
|
|
|
|
|
(bv (u8-list->bytevector input)))
|
|
|
|
|
|
(qsort (bytevector->pointer bv) (bytevector-length bv) 1
|
|
|
|
|
|
(procedure->pointer int cmp (list '* '*)))
|
|
|
|
|
|
#f)
|
|
|
|
|
|
(throw 'unresolved)))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if-exception "qsort, wrong arity"
|
|
|
|
|
|
exception:wrong-num-args
|
|
|
|
|
|
|
|
|
|
|
|
(if (defined? 'procedure->pointer)
|
|
|
|
|
|
(let* ((cmp (lambda (x y z) #f)) ; wrong arity
|
|
|
|
|
|
(ptr (procedure->pointer int cmp (list '* '*)))
|
|
|
|
|
|
(bv (u8-list->bytevector input)))
|
|
|
|
|
|
(qsort (bytevector->pointer bv) (bytevector-length bv) 1
|
|
|
|
|
|
(procedure->pointer int cmp (list '* '*)))
|
|
|
|
|
|
#f)
|
2010-09-04 14:38:20 +02:00
|
|
|
|
(throw 'unresolved)))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "bijection"
|
|
|
|
|
|
(if (defined? 'procedure->pointer)
|
|
|
|
|
|
(let* ((proc (lambda (x y z)
|
|
|
|
|
|
(+ x y z 0.0)))
|
|
|
|
|
|
(ret double)
|
|
|
|
|
|
(args (list float int16 double))
|
2010-09-06 22:24:44 +02:00
|
|
|
|
(proc* (pointer->procedure ret
|
|
|
|
|
|
(procedure->pointer ret proc args)
|
|
|
|
|
|
args))
|
2010-09-04 14:38:20 +02:00
|
|
|
|
(arg1 (map (cut / <> 2.0) (iota 123)))
|
|
|
|
|
|
(arg2 (iota 123 32000))
|
|
|
|
|
|
(arg3 (map (cut / <> 4.0) (iota 123 100 4))))
|
|
|
|
|
|
(equal? (map proc arg1 arg2 arg3)
|
|
|
|
|
|
(map proc* arg1 arg2 arg3)))
|
2011-03-29 23:35:24 +02:00
|
|
|
|
(throw 'unresolved)))
|
|
|
|
|
|
|
2011-11-16 23:51:34 +01:00
|
|
|
|
(pass-if "procedures returning a pointer"
|
|
|
|
|
|
(if (defined? 'procedure->pointer)
|
|
|
|
|
|
(let* ((called? #f)
|
|
|
|
|
|
(proc (lambda (i) (set! called? #t) (make-pointer i)))
|
|
|
|
|
|
(pointer (procedure->pointer '* proc (list int)))
|
|
|
|
|
|
(proc* (pointer->procedure '* pointer (list int)))
|
|
|
|
|
|
(result (proc* 777)))
|
|
|
|
|
|
(and called? (equal? result (make-pointer 777))))
|
|
|
|
|
|
(throw 'unresolved)))
|
|
|
|
|
|
|
2011-03-29 23:35:24 +02:00
|
|
|
|
(pass-if "procedures returning void"
|
|
|
|
|
|
(if (defined? 'procedure->pointer)
|
|
|
|
|
|
(let* ((called? #f)
|
|
|
|
|
|
(proc (lambda () (set! called? #t)))
|
|
|
|
|
|
(pointer (procedure->pointer void proc '()))
|
|
|
|
|
|
(proc* (pointer->procedure void pointer '())))
|
|
|
|
|
|
(proc*)
|
|
|
|
|
|
called?)
|
2011-11-26 22:27:32 +01:00
|
|
|
|
(throw 'unresolved)))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "procedure is retained"
|
|
|
|
|
|
;; The lambda passed to `procedure->pointer' must remain live.
|
|
|
|
|
|
(if (defined? 'procedure->pointer)
|
|
|
|
|
|
(let* ((ptr (procedure->pointer int
|
|
|
|
|
|
(lambda (x) (+ x 7))
|
|
|
|
|
|
(list int)))
|
|
|
|
|
|
(procs (unfold (cut >= <> 10000)
|
|
|
|
|
|
(lambda (i)
|
|
|
|
|
|
(pointer->procedure int ptr (list int)))
|
|
|
|
|
|
1+
|
|
|
|
|
|
0)))
|
|
|
|
|
|
(gc) (gc) (gc)
|
|
|
|
|
|
(every (cut = <> 9)
|
|
|
|
|
|
(map (lambda (f) (f 2)) procs)))
|
2010-09-03 15:12:54 +02:00
|
|
|
|
(throw 'unresolved))))
|
|
|
|
|
|
|
2010-07-28 12:02:50 +02:00
|
|
|
|
|
|
|
|
|
|
(with-test-prefix "structs"
|
|
|
|
|
|
|
2010-11-11 16:36:29 +01:00
|
|
|
|
(pass-if "sizeof { int8, double }"
|
|
|
|
|
|
(= (sizeof (list int8 double))
|
|
|
|
|
|
(+ (alignof double) (sizeof double))))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "sizeof { short, int, long, pointer }"
|
|
|
|
|
|
(let ((layout (list short int long '*)))
|
|
|
|
|
|
(>= (sizeof layout)
|
|
|
|
|
|
(reduce + 0.0 (map sizeof layout)))))
|
|
|
|
|
|
|
2011-03-10 22:24:23 +01:00
|
|
|
|
(pass-if "alignof { int8, double, int8 }"
|
|
|
|
|
|
;; alignment of the most strictly aligned component
|
|
|
|
|
|
(let ((layout (list int8 double int8)))
|
|
|
|
|
|
(= (alignof layout) (alignof double))))
|
|
|
|
|
|
|
2010-07-28 12:02:50 +02:00
|
|
|
|
(pass-if "parse-c-struct"
|
|
|
|
|
|
(let ((layout (list int64 uint8))
|
|
|
|
|
|
(data (list -300 43)))
|
2010-11-08 23:08:48 +01:00
|
|
|
|
(equal? (parse-c-struct (make-c-struct layout data)
|
|
|
|
|
|
layout)
|
|
|
|
|
|
data)))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "alignment constraints honored"
|
|
|
|
|
|
(let ((layout (list int8 double))
|
|
|
|
|
|
(data (list -7 3.14)))
|
Have `parse-c-struct' and `make-c-struct' support `int', pointers, etc.
Reported by Tristan Colgate <tcolgate@gmail.com>.
* module/system/foreign.scm: Call `load-extension' at compile-time too.
(compile-time-value): New macro.
(integer-ref, integer-set): New procedures.
(define-integer-reader, define-integer-writer): New macros.
(%read-int, %read-long, %write-int!, %write-long!, %read-unsigned-int,
%read-unsigned-long, %write-unsigned-int!, %write-unsigned-long!,
%read-size_t, %write-size_t!, %read-pointer, %write-pointer!): New
procedures.
(*writers*): Add writers for `int', `unsigned-int', `long',
`unsigned-long', `size_t', and `*'.
(*readers*): Likewise.
* test-suite/tests/foreign.test ("structs")["int8, pointer",
"unsigned-long, int8, size_t", "long, int, pointer"]: New tests.
2010-11-11 16:09:22 +01:00
|
|
|
|
(equal? (parse-c-struct (make-c-struct layout data)
|
|
|
|
|
|
layout)
|
|
|
|
|
|
data)))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "int8, pointer"
|
|
|
|
|
|
(let ((layout (list uint8 '*))
|
|
|
|
|
|
(data (list 222 (make-pointer 7777))))
|
|
|
|
|
|
(equal? (parse-c-struct (make-c-struct layout data)
|
|
|
|
|
|
layout)
|
|
|
|
|
|
data)))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "unsigned-long, int8, size_t"
|
|
|
|
|
|
(let ((layout (list unsigned-long int8 size_t))
|
|
|
|
|
|
(data (list (expt 2 17) -128 (expt 2 18))))
|
|
|
|
|
|
(equal? (parse-c-struct (make-c-struct layout data)
|
|
|
|
|
|
layout)
|
|
|
|
|
|
data)))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "long, int, pointer"
|
|
|
|
|
|
(let ((layout (list long int '*))
|
|
|
|
|
|
(data (list (- (expt 2 17)) -222 (make-pointer 777))))
|
2010-11-11 16:26:50 +01:00
|
|
|
|
(equal? (parse-c-struct (make-c-struct layout data)
|
|
|
|
|
|
layout)
|
|
|
|
|
|
data)))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "int8, pointer, short, double"
|
|
|
|
|
|
(let ((layout (list int8 '* short double))
|
|
|
|
|
|
(data (list 77 %null-pointer -42 3.14)))
|
2011-03-10 22:24:23 +01:00
|
|
|
|
(equal? (parse-c-struct (make-c-struct layout data)
|
|
|
|
|
|
layout)
|
|
|
|
|
|
data)))
|
|
|
|
|
|
|
|
|
|
|
|
(pass-if "int8, { int8, double, int8 }, int16"
|
|
|
|
|
|
(let ((layout (list int8 (list int8 double int8) int16))
|
|
|
|
|
|
(data (list 77 (list 42 4.2 55) 88)))
|
2010-07-28 12:02:50 +02:00
|
|
|
|
(equal? (parse-c-struct (make-c-struct layout data)
|
|
|
|
|
|
layout)
|
|
|
|
|
|
data))))
|