|
@c - |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
in the case of Dia, where the @code{shape} argument passes |
|
transiently in and out of the Scheme world, it would be quite wrong the |
|
@strong{delete} the underlying C shape just because the Scheme code has |
|
finished evaluation. How do we avoid this happening? |
|
@end itemize |
|
|
|
One resolution of these issues is for the Scheme-level representation of |
|
a shape to be a new, Scheme-specific C structure wrapped up as a foreign |
|
object. The foreign object is what is passed into and out of Scheme |
|
code, and the Scheme-specific C structure inside the foreign object |
|
points to Dia's underlying C structure so that the code for primitives |
|
like @code{square?} can get at it. |
|
|
|
To cope with an underlying shape being deleted while Scheme code is |
|
still holding onto a Scheme shape value, the underlying C structure |
|
should have a new field that points to the Scheme-specific foreign |
|
object. When a shape is deleted, the relevant code chains through to |
|
the Scheme-specific structure and sets its pointer back to the |
|
underlying structure to NULL. Thus the foreign object value for the |
|
shape continues to exist, but any primitive code that tries to use it |
|
will detect that the underlying shape has been deleted because the |
|
underlying structure pointer is NULL. |
|
|
|
So, to summarize the steps involved in this resolution of the problem |
|
(and assuming that the underlying C structure for a shape is |
|
@code{struct dia_shape}): |
|
|
|
@itemize @bullet |
|
@item |
|
Define a new Scheme-specific structure that @emph{points} to the |
|
underlying C structure: |
|
|
|
@lisp |
|
struct dia_guile_shape |
|
@{ |
|
struct dia_shape * c_shape; /* NULL => deleted */ |
|
@} |
|
@end lisp |
|
|
|
@item |
|
Add a field to @code{struct dia_shape} that points to its @code{struct |
|
dia_guile_shape} if it has one --- |
|
|
|
@lisp |
|
struct dia_shape |
|
@{ |
|
@dots{} |
|
struct dia_guile_shape * guile_shape; |
|
@} |
|
@end lisp |
|
|
|
@noindent |
|
--- so that C code can set @code{guile_shape->c_shape} to NULL when the |
|
underlying shape is deleted. |
|
|
|
@item |
|
Wrap @code{struct dia_guile_shape} as a foreign object type. |
|
|
|
@item |
|
Whenever you need to represent a C shape onto the Scheme level, create a |
|
foreign object instance for it, and pass that. |
|
|
|
@item |
|
In primitive code that receives a shape foreign object instance, check the |
|
@code{c_shape} field when decoding it, to find out whether the |
|
underlying C shape is still there. |
|
@end itemize |
|
|
|
As far as memory management is concerned, the foreign object values and |
|
their Scheme-specific structures are under the control of the garbage |
|
collector, whereas the underlying C structures are explicitly managed in |
|
exactly the same way that Dia managed them before we thought of adding |
|
Guile. |
|
|
|
When the garbage collector decides to free a shape foreign object value, |
|
it calls the @dfn{finalizer} function that was specified when defining |
|
the shape foreign object type. To maintain the correctness of the |
|
@code{guile_shape} field in the underlying C structure, this function |
|
should chain through to the underlying C structure (if it still exists) |
|
and set its @code{guile_shape} field to NULL. |
|
|
|
For full documentation on defining and using foreign object types, see |
|
@ref{Defining New Foreign Object Types}. |
|
|
|
|
|
@node Dia Primitives |
|
@subsubsection Writing Guile Primitives for Dia |
|
|
|
Once the details of object representation are decided, writing the |
|
primitive function code that you need is usually straightforward. |
|
|
|
A primitive is simply a C function whose arguments and return value are |
|
all of type @code{SCM}, and whose body does whatever you want it to do. |
|
As an example, here is a possible implementation of the @code{square?} |
|
primitive: |
|
|
|
@lisp |
|
static SCM square_p (SCM shape) |
|
@{ |
|
struct dia_guile_shape * guile_shape; |
|
|
|
/* Check that arg is really a shape object. */ |
|
scm_assert_foreign_object_type (shape_type, shape); |
|
|
|
/* Access Scheme-specific shape structure. */ |
|
guile_shape = scm_foreign_object_ref (shape, 0); |
|
|
|
/* Find out if underlying shape exists and is a |
|
square; return answer as a Scheme boolean. */ |
|
return scm_from_bool (guile_shape->c_shape && |
|
(guile_shape->c_shape->type == DIA_SQUARE)); |
|
@} |
|
@end lisp |
|
|
|
Notice how easy it is to chain through from the @code{SCM shape} |
|
parameter that @code{square_p} receives --- which is a foreign object |
|
--- to the Scheme-specific structure inside the foreign object, and |
|
thence to the underlying C structure for the shape. |
|
|
|
In this code, @code{scm_assert_foreign_object_type}, |
|
@code{scm_foreign_object_ref}, and @code{scm_from_bool} are from the |
|
standard Guile API. We assume that @code{shape_type} was given to us |
|
when we made the shape foreign object type, using |
|
@code{scm_make_foreign_object_type}. The call to |
|
@code{scm_assert_foreign_object_type} ensures that @var{shape} is indeed |
|
a shape. This is needed to guard against Scheme code using the |
|
@code{square?} procedure incorrectly, as in @code{(square? "hello")}; |
|
Scheme's latent typing means that usage errors like this must be caught |
|
at run time. |
|
|
|
Having written the C code for your primitives, you need to make them |
|
available as Scheme procedures by calling the @code{scm_c_define_gsubr} |
|
function. @code{scm_c_define_gsubr} (@pxref{Primitive Procedures}) |
|
takes arguments that specify the Scheme-level name for the primitive and |
|
how many required, optional and rest arguments it can accept. The |
|
@code{square?} primitive always requires exactly one argument, so the |
|
call to make it available in Scheme reads like this: |
|
|
|
@lisp |
|
scm_c_define_gsubr ("square?", 1, 0, 0, square_p); |
|
@end lisp |
|
|
|
For where to put this call, see the subsection after next on the |
|
structure of Guile-enabled code (@pxref{Dia Structure}). |
|
|
|
|
|
@node Dia Hook |
|
@subsubsection Providing a Hook for the Evaluation of Scheme Code |
|
|
|
To make the Guile integration useful, you have to design some kind of |
|
hook into your application that application users can use to cause their |
|
Scheme code to be evaluated. |
|
|
|
Technically, this is straightforward; you just have to decide on a |
|
mechanism that is appropriate for your application. Think of Emacs, for |
|
example: when you type @kbd{@key{ESC} :}, you get a prompt where you can |
|
type in any Elisp code, which Emacs will then evaluate. Or, again like |
|
Emacs, you could provide a mechanism (such as an init file) to allow |
|
Scheme code to be associated with a particular key sequence, and |
|
evaluate the code when that key sequence is entered. |
|
|
|
In either case, once you have the Scheme code that you want to evaluate, |
|
as a null terminated string, you can tell Guile to evaluate it by |
|
calling the @code{scm_c_eval_string} function. |
|
|
|
|
|
@node Dia Structure |
|
@subsubsection Top-level Structure of Guile-enabled Dia |
|
|
|
Let's assume that the pre-Guile Dia code looks structurally like this: |
|
|
|
@itemize @bullet |
|
@item |
|
@code{main ()} |
|
|
|
@itemize @bullet |
|
@item |
|
do lots of initialization and setup stuff |
|
@item |
|
enter Gtk main loop |
|
@end itemize |
|
@end itemize |
|
|
|
When you add Guile to a program, one (rather technical) requirement is |
|
that Guile's garbage collector needs to know where the bottom of the C |
|
stack is. The easiest way to ensure this is to use |
|
@code{scm_boot_guile} like this: |
|
|
|
@itemize @bullet |
|
@item |
|
@code{main ()} |
|
|
|
@itemize @bullet |
|
@item |
|
do lots of initialization and setup stuff |
|
@item |
|
@code{scm_boot_guile (argc, argv, inner_main, NULL)} |
|
@end itemize |
|
|
|
@item |
|
@code{inner_main ()} |
|
|
|
@itemize @bullet |
|
@item |
|
define all foreign object types |
|
@item |
|
export primitives to Scheme using @code{scm_c_define_gsubr} |
|
@item |
|
enter Gtk main loop |
|
@end itemize |
|
@end itemize |
|
|
|
In other words, you move the guts of what was previously in your |
|
@code{main} function into a new function called @code{inner_main}, and |
|
then add a @code{scm_boot_guile} call, with @code{inner_main} as a |
|
parameter, to the end of @code{main}. |
|
|
|
Assuming that you are using foreign objects and have written primitive |
|
code as described in the preceding subsections, you also need to insert |
|
calls to declare your new foreign objects and export the primitives to |
|
Scheme. These declarations must happen @emph{inside} the dynamic scope |
|
of the @code{scm_boot_guile} call, but also @emph{before} any code is |
|
run that could possibly use them --- the beginning of @code{inner_main} |
|
is an ideal place for this. |
|
|
|
|
|
@node Dia Advanced |
|
@subsubsection Going Further with Dia and Guile |
|
|
|
The steps described so far implement an initial Guile integration that |
|
already gives a lot of additional power to Dia application users. But |
|
there are further steps that you could take, and it's interesting to |
|
consider a few of these. |
|
|
|
In general, you could progressively move more of Dia's source code from |
|
C into Scheme. This might make the code more maintainable and |
|
extensible, and it could open the door to new programming paradigms that |
|
are tricky to effect in C but straightforward in Scheme. |
|
|
|
A specific example of this is that you could use the guile-gtk package, |
|
which provides Scheme-level procedures for most of the Gtk+ library, to |
|
move the code that lays out and displays Dia objects from C to Scheme. |
|
|
|
As you follow this path, it naturally becomes less useful to maintain a |
|
distinction between Dia's original non-Guile-related source code, and |
|
its later code implementing foreign objects and primitives for the |
|
Scheme world. |
|
|
|
For example, suppose that the original source code had a |
|
@code{dia_change_fill_pattern} function: |
|
|
|
@lisp |
|
void dia_change_fill_pattern (struct dia_shape * shape, |
|
struct dia_pattern * pattern) |
|
@{ |
|
/* real pattern change work */ |
|
@} |
|
@end lisp |
|
|
|
During initial Guile integration, you add a @code{change_fill_pattern} |
|
primitive for Scheme purposes, which accesses the underlying structures |
|
from its foreign object values and uses @code{dia_change_fill_pattern} |
|
to do the real work: |
|
|
|
@lisp |
|
SCM change_fill_pattern (SCM shape, SCM pattern) |
|
@{ |
|
struct dia_shape * d_shape; |
|
struct dia_pattern * d_pattern; |
|
|
|
@dots{} |
|
|
|
dia_change_fill_pattern (d_shape, d_pattern); |
|
|
|
return SCM_UNSPECIFIED; |
|
@} |
|
@end lisp |
|
|
|
At this point, it makes sense to keep @code{dia_change_fill_pattern} and |
|
@code{change_fill_pattern} separate, because |
|
@code{dia_change_fill_pattern} can also be called without going through |
|
Scheme at all, say because the user clicks a button which causes a |
|
C-registered Gtk+ callback to be called. |
|
|
|
But, if the code for creating buttons and registering their callbacks is |
|
moved into Scheme (using guile-gtk), it may become true that |
|
@code{dia_change_fill_pattern} can no longer be called other than |
|
through Scheme. In which case, it makes sense to abolish it and move |
|
its contents directly into @code{change_fill_pattern}, like this: |
|
|
|
@lisp |
|
SCM change_fill_pattern (SCM shape, SCM pattern) |
|
@{ |
|
struct dia_shape * d_shape; |
|
struct dia_pattern * d_pattern; |
|
|
|
@dots{} |
|
|
|
/* real pattern change work */ |
|
|
|
return SCM_UNSPECIFIED; |
|
@} |
|
@end lisp |
|
|
|
So further Guile integration progressively @emph{reduces} the amount of |
|
functional C code that you have to maintain over the long term. |
|
|
|
A similar argument applies to data representation. In the discussion of |
|
foreign objects earlier, issues arose because of the different memory |
|
management and lifetime models that normally apply to data structures in |
|
C and in Scheme. However, with further Guile integration, you can |
|
resolve this issue in a more radical way by allowing all your data |
|
structures to be under the control of the garbage collector, and kept |
|
alive by references from the Scheme world. Instead of maintaining an |
|
array or linked list of shapes in C, you would instead maintain a list |
|
in Scheme. |
|
|
|
Rather like the coalescing of @code{dia_change_fill_pattern} and |
|
@code{change_fill_pattern}, the practical upshot of such a change is |
|
that you would no longer have to keep the @code{dia_shape} and |
|
@code{dia_guile_shape} structures separate, and so wouldn't need to |
|
worry about the pointers between them. Instead, you could change the |
|
foreign object definition to wrap the @code{dia_shape} structure |
|
directly, and send @code{dia_guile_shape} off to the scrap yard. Cut |
|
out the middle man! |
|
|
|
Finally, we come to the holy grail of Guile's free software / extension |
|
language approach. Once you have a Scheme representation for |
|
interesting Dia data types like shapes, and a handy bunch of primitives |
|
for manipulating them, it suddenly becomes clear that you have a bundle |
|
of functionality that could have far-ranging use beyond Dia itself. In |
|
other words, the data types and primitives could now become a library, |
|
and Dia becomes just one of the many possible applications using that |
|
library --- albeit, at this early stage, a rather important one! |
|
|
|
In this model, Guile becomes just the glue that binds everything |
|
together. Imagine an application that usefully combined functionality |
|
from Dia, Gnumeric and GnuCash --- it's tricky right now, because no |
|
such application yet exists; but it'll happen some day @dots{} |
|
|
|
|
|
@node Scheme vs C |
|
@subsection Why Scheme is More Hackable Than C |
|
|
|
Underlying Guile's value proposition is the assumption that programming |
|
in a high level language, specifically Guile's implementation of Scheme, |
|
is necessarily better in some way than programming in C. What do we |
|
mean by this claim, and how can we be so sure? |
|
|
|
One class of advantages applies not only to Scheme, but more generally |
|
to any interpretable, high level, scripting language, such as Emacs |
|
Lisp, Python, Ruby, or @TeX{}'s macro language. Common features of all |
|
such languages, when compared to C, are that: |
|
|
|
@itemize @bullet |
|
@item |
|
They lend themselves to rapid and experimental development cycles, |
|
owing usually to a combination of their interpretability and the |
|
integrated development environment in which they are used. |
|
|
|
@item |
|
They free developers from some of the low level bookkeeping tasks |
|
associated with C programming, notably memory management. |
|
|
|
@item |
|
They provide high level features such as container objects and exception |
|
handling that make common programming tasks easier. |
|
@end itemize |
|
|
|
In the case of Scheme, particular features that make programming easier |
|
--- and more fun! --- are its powerful mechanisms for abstracting parts |
|
of programs (closures --- @pxref{About Closure}) and for iteration |
|
(@pxref{while do}). |
|
|
|
The evidence in support of this argument is empirical: the huge amount |
|
of code that has been written in extension languages for applications |
|
that support this mechanism. Most notable are extensions written in |
|
Emacs Lisp for GNU Emacs, in @TeX{}'s macro language for @TeX{}, and in |
|
Script-Fu for the Gimp, but there is increasingly now a significant code |
|
eco-system for Guile-based applications as well, such as Lilypond and |
|
GnuCash. It is close to inconceivable that similar amounts of |
|
functionality could have been added to these applications just by |
|
writing new code in their base implementation languages. |
|
|
|
|
|
@node Testbed Example |
|
@subsection Example: Using Guile for an Application Testbed |
|
|
|
As an example of what this means in practice, imagine writing a testbed |
|
for an application that is tested by submitting various requests (via a |
|
C interface) and validating the output received. Suppose further that |
|
the application keeps an idea of its current state, and that the |
|
``correct'' output for a given request may depend on the current |
|
application state. A complete ``white box''@footnote{A @dfn{white box} |
|
test plan is one that incorporates knowledge of the internal design of |
|
the application under test.} test plan for this application would aim to |
|
submit all possible requests in each distinguishable state, and validate |
|
the output for all request/state combinations. |
|
|
|
To write all this test code in C would be very tedious. Suppose instead |
|
that the testbed code adds a single new C function, to submit an |
|
arbitrary request and return the response, and then uses Guile to export |
|
this function as a Scheme procedure. The rest of the testbed can then |
|
be written in Scheme, and so benefits from all the advantages of |
|
programming in Scheme that were described in the previous section. |
|
|
|
(In this particular example, there is an additional benefit of writing |
|
most of the testbed in Scheme. A common problem for white box testing |
|
is that mistakes and mistaken assumptions in the application under test |
|
can easily be reproduced in the testbed code. It is more difficult to |
|
copy mistakes like this when the testbed is written in a different |
|
language from the application.) |
|
|
|
|
|
@node Programming Options |
|
@subsection A Choice of Programming Options |
|
|
|
The preceding arguments and example point to a model of Guile |
|
programming that is applicable in many cases. According to this model, |
|
Guile programming involves a balance between C and Scheme programming, |
|
with the aim being to extract the greatest possible Scheme level benefit |
|
from the least amount of C level work. |
|
|
|
The C level work required in this model usually consists of packaging |
|
and exporting functions and application objects such that they can be |
|
seen and manipulated on the Scheme level. To help with this, Guile's C |
|
language interface includes utility features that aim to make this kind |
|
of integration very easy for the application developer. |
|
|
|
This model, though, is really just one of a range of possible |
|
programming options. If all of the functionality that you need is |
|
available from Scheme, you could choose instead to write your whole |
|
application in Scheme (or one of the other high level languages that |
|
Guile supports through translation), and simply use Guile as an |
|
interpreter for Scheme. (In the future, we hope that Guile will also be |
|
able to compile Scheme code, so lessening the performance gap between C |
|
and Scheme code.) Or, at the other end of the C--Scheme scale, you |
|
could write the majority of your application in C, and only call out to |
|
Guile occasionally for specific actions such as reading a configuration |
|
file or executing a user-specified extension. The choices boil down to |
|
two basic questions: |
|
|
|
@itemize @bullet |
|
@item |
|
Which parts of the application do you write in C, and which in Scheme |
|
(or another high level translated language)? |
|
|
|
@item |
|
How do you design the interface between the C and Scheme parts of your |
|
application? |
|
@end itemize |
|
|
|
These are of course design questions, and the right design for any given |
|
application will always depend upon the particular requirements that you |
|
are trying to meet. In the context of Guile, however, there are some |
|
generally applicable considerations that can help you when designing |
|
your answers. |
|
|
|
@menu |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|