Download Self Handbook Documentation

Transcript
Self Handbook Documentation, Release 2 for Self 4.4
7. This conversion is an escape: it passes the C value unchanged to Self. It is an error to use it if the C value is not
an oop.
8. The proxy (fctProxy) object that is returned by these conversions is not being created by the glue code.
Rather a proxy (fctProxy) must be passed down from the Self level. This proxy (fctProxy), a result
proxy, will then be side effected by the glue: the value that the foreign function returns will be stored in the
result proxy together with the requested type seal. It is required that the result proxy is dead when passed down
(else a liveProxyError results). After being side-effected and returned, the result proxy is live. The result
proxy is the last argument of the function that the glue macro expands to.
8.13.16 A complete application using foreign functions
This section gives a description of a complete application which uses foreign functions. The aim is to present a realistic
and complete example of how foreign functions may be used. The complete source for the example is found in the
directory objects/applications/serverDemo in the Self distribution.
The example used is an application that allows Self expressions to be easily evaluated by non- Self processes. Having
this, it then becomes possible to start Self processes from a UNIX prompt (shell) or to specify pipe lines in which
some of the processes are Self processes. For example in
proto% cat someFile | tokenize | sort -r | capitalize | tee lst
it may be the case that the filters tokenize and capitalize perform most of their work in Self. Likewise, the command
proto% mail
may invoke some fancy mail reader written in Self rather than the standard UNIX mail reader.
To see how the above can be accomplished, please refer to Figure 5 below. The left side of the figure shows the
external view of a typical UNIX process. It has two files: stdin and stdout (for simplicity we ignore stderr). Stdin
is often connected to the keyboard so that characters typed here can be read from the file stdin. Likewise, stdout is
typically connected to the console so that the process can display output by writing it to the file stdout. Stdin and
stdout can also be connected to “regular” files, if the process was started with redirection. The right side of Figure 5
shows a two stage pipe line. Here stdout of the first process is connected to stdin of the second process.
Figure 8.3: Figure 5: A single UNIX process and an pipe line.
Figure 5 illustrates a simple trick that in many situations allows Self processes to behave as if they are full-fledged
UNIX processes. A Self process is represented by a “real” UNIX process which transparently communicates with
the Self process over a pair of connected sockets. The communication is bidirectional: input to the UNIX process is
relayed to the Self process over the socket connection, and output produced by the Self process is sent over the same
socket connection to the UNIX process which relays it to stdout. The right part of Figure 5 shows how the UNIX/Self
process pair can fit seamlessly into a pipe line.
Source code that facilitates setting up such UNIX/Self process pairs is included in the Self distribution. The source
consists of two parts: one being a Self program (called server), the other being a C++ program (called toself ). When
the server is started, it creates a socket, binds a name to it and then listens for connections on it. toself establishes
connections to the server program. The first line that is transmitted when a connection has been set up goes from
toself to the server. The line contains a Self expression. Upon receiving it, the server forks a new process to
evaluate the expression in the context of the lobby augmented with a slot, stdio, that contains a unixFile-like object
8.13. Interfacing with other languages
129