Introduction
MzScheme includes functionality that makes it possible to interact with foreign data and foreign function, available through the foreign.ss module in MzLib. Traditional Scheme code is usually safe: if the process crashes the blame is always on C code. Using C extensions makes it possible to extend Scheme -- the added code can lead to crashes, but the blame for crashes is still at the C side.
The foreign capabilities of MzScheme makes it possible to use Scheme in
situation that would otherwise require writing a C extension, so Scheme code
can be used to substitute C. Doing this is, obviously, as unsafe as writing C
-- so a crash can be now blamed on either C or Scheme code which interacts
with the foreign world. To make things a bit safer, the foreign.ss
module will not provide you with any of the unsafe operations by default.
Instead, you get an `unsafe!' macro, which you can use to make the
unsafe operations available1. Using this
macro should be considered as a declaration that your code is itself unsafe,
therefore can lead to serious problems in case of bugs: it is your
responsibility to provide a safe interface. By this we constrain the blame for
crashes to either C code or such unsafe Scheme code.
In rare cases you might want to provide an unsafe interface, for example, if
you write a module that provides more foreign-interaction functionality. In
these cases you should use the `provide*' macro with
`unsafe' bindings, and the `define-unsafer' to provide an
unsafe!-like macro that will make these bindings available. See the
foreign.ss source for details. Providing users with unsafe operations
without this facility is considered a bug in your code.
In addition to this manual, Inside PLT MzScheme can be used to learn about
the internals of MzScheme. Note that using ffi-lib with #f
instead of a file name will use the running process as a library, making all
C-level functions (including the standard library) available.
1 For additional safety, the
unsafe! is protected (see section 9.4 in PLT MzScheme: Language Manual).