* threads.c, threads.h (scm_cond_init): Undo unintentional API
[bpt/guile.git] / doc / ref / program.texi
CommitLineData
9401323e 1@page
226297eb
NJ
2@node Programming Overview
3@chapter An Overview of Guile Programming
4
5Guile is designed as an extension language interpreter that is
6straightforward to integrate with applications written in C (and C++).
7The big win here for the application developer is that Guile
8integration, as the Guile web page says, ``lowers your project's
9hacktivation energy.'' Lowering the hacktivation energy means that you,
10as the application developer, @emph{and your users}, reap the benefits
11that flow from being able to extend the application in a high level
12extension language rather than in plain old C.
13
1982a56a
NJ
14In abstract terms, it's difficult to explain what this really means and
15what the integration process involves, so instead let's begin by jumping
16straight into an example of how you might integrate Guile into an
17existing program, and what you could expect to gain by so doing. With
18that example under our belts, we'll then return to a more general
19analysis of the arguments involved and the range of programming options
20available.
21
22@menu
23* Extending Dia:: How one might extend Dia using Guile.
24* Scheme vs C:: Why Scheme is more hackable than C.
25* Testbed Example:: Example: using Guile in a testbed.
26* Programming Options:: Options for Guile programming.
27* User Programming:: How about application users?
28@end menu
29
30
31@node Extending Dia
32@section How One Might Extend Dia Using Guile
33
34Dia is a free software program for drawing schematic diagrams like flow
35charts and floor plans (REFFIXME). This section conducts the thought
36experiment of adding Guile to Dia. In so doing, it aims to illustrate
37several of the steps and considerations involved in adding Guile to
38applications in general.
39
40@menu
41* Dia Objective:: Deciding why you want to add Guile.
42* Dia Steps:: Four steps required to add Guile.
43* Dia Smobs:: How to represent Dia data in Scheme.
44* Dia Primitives:: Writing Guile primitives for Dia.
45* Dia Hook:: Providing a hook for Scheme evaluation.
46* Dia Structure:: Overall structure for adding Guile.
47* Dia Advanced:: Going further with Dia and Guile.
48@end menu
49
50
51@node Dia Objective
52@subsection Deciding Why You Want to Add Guile
53
54First off, you should understand why you want to add Guile to Dia at
55all, and that means forming a picture of what Dia does and how it does
56it. So, what are the constituents of the Dia application?
57
58@itemize @bullet
59@item
60Most importantly, the @dfn{application domain objects} --- in other
61words, the concepts that differentiate Dia from another application such
62as a word processor or spreadsheet: shapes, templates, connectors,
63pages, plus the properties of all these things.
64
65@item
66The code that manages the graphical face of the application, including
67the layout and display of the objects above.
68
69@item
70The code that handles input events, which indicate that the application
71user is wanting to do something.
72@end itemize
73
74@noindent
75(In other words, a textbook example of the @dfn{model - view -
76controller} paradigm.)
77
78Next question: how will Dia benefit once the Guile integration is
79complete? Several (positive!) answers are possible here, and the choice
80is obviously up to the application developers. Still, one answer is
81that the main benefit will be the ability to manipulate Dia's
82application domain objects from Scheme.
83
84Suppose that Dia made a set of procedures available in Scheme,
85representing the most basic operations on objects such as shapes,
86connectors, and so on. Using Scheme, the application user could then
87write code that builds upon these basic operations to create more
88complex procedures. For example, given basic procedures to enumerate
89the objects on a page, to determine whether an object is a square, and
90to change the fill pattern of a single shape, the user can write a
91Scheme procedure to change the fill pattern of all squares on the
92current page:
93
94@lisp
95(define (change-squares'-fill-pattern new-pattern)
96 (for-each-shape current-page
97 (lambda (shape)
98 (if (square? shape)
99 (change-fill-pattern shape new-pattern)))))
100@end lisp
101
102
103@node Dia Steps
104@subsection Four Steps Required to Add Guile
105
106Assuming this objective, four steps are needed to achieve it.
107
108First, you need a way of representing your application-specific objects
109--- such as @code{shape} in the previous example --- when they are
110passed into the Scheme world. Unless your objects are so simple that
111they map naturally into builtin Scheme data types like numbers and
112strings, you will probably want to use Guile's @dfn{SMOB} interface to
113create a new Scheme data type for your objects.
114
115Second, you need to write code for the basic operations like
116@code{for-each-shape} and @code{square?} such that they access and
117manipulate your existing data structures correctly, and then make these
118operations available as @dfn{primitives} on the Scheme level.
119
120Third, you need to provide some mechanism within the Dia application
121that a user can hook into to cause arbitrary Scheme code to be
122evaluated.
123
124Finally, you need to restructure your top-level application C code a
125little so that it initializes the Guile interpreter correctly and
126declares your @dfn{SMOBs} and @dfn{primitives} to the Scheme world.
127
128The following subsections expand on these four points in turn.
129
130
131@node Dia Smobs
132@subsection How to Represent Dia Data in Scheme
133
134For all but the most trivial applications, you will probably want to
135allow some representation of your domain objects to exist on the Scheme
136level. This is where the idea of SMOBs comes in, and with it issues of
137lifetime management and garbage collection.
138
139To get more concrete about this, let's look again at the example we gave
140earlier of how application users can use Guile to build higher-level
141functions from the primitives that Dia itself provides.
142
143@lisp
144(define (change-squares'-fill-pattern new-pattern)
145 (for-each-shape current-page
146 (lambda (shape)
147 (if (square? shape)
148 (change-fill-pattern shape new-pattern)))))
149@end lisp
150
151Consider what is stored here in the variable @code{shape}. For each
152shape on the current page, the @code{for-each-shape} primitive calls
153@code{(lambda (shape) @dots{})} with an argument representing that
154shape. Question is: how is that argument represented on the Scheme
155level? The issues are as follows.
156
157@itemize @bullet
158@item
159Whatever the representation, it has to be decodable again by the C code
160for the @code{square?} and @code{change-fill-pattern} primitives. In
161other words, a primitive like @code{square?} has somehow to be able to
162turn the value that it receives back into something that points to the
163underlying C structure describing a shape.
164
165@item
166The representation must also cope with Scheme code holding on to the
167value for later use. What happens if the Scheme code stores
168@code{shape} in a global variable, but then that shape is deleted (in a
169way that the Scheme code is not aware of), and later on some other
170Scheme code uses that global variable again in a call to, say,
171@code{square?}?
172
173@item
174The lifetime and memory allocation of objects that exist @emph{only} in
175the Scheme world is managed automatically by Guile's garbage collector
176using one simple rule: when there are no remaining references to an
177object, the object is considered dead and so its memory is freed. But
178for objects that exist in both C and Scheme, the picture is more
179complicated; in the case of Dia, where the @code{shape} argument passes
180transiently in and out of the Scheme world, it would be quite wrong the
181@strong{delete} the underlying C shape just because the Scheme code has
182finished evaluation. How do we avoid this happening?
183@end itemize
184
185One resolution of these issues is for the Scheme-level representation of
186a shape to be a new, Scheme-specific C structure wrapped up as a SMOB.
187The SMOB is what is passed into and out of Scheme code, and the
188Scheme-specific C structure inside the SMOB points to Dia's underlying C
189structure so that the code for primitives like @code{square?} can get at
190it.
191
192To cope with an underlying shape being deleted while Scheme code is
193still holding onto a Scheme shape value, the underlying C structure
194should have a new field that points to the Scheme-specific SMOB. When a
195shape is deleted, the relevant code chains through to the
196Scheme-specific structure and sets its pointer back to the underlying
197structure to NULL. Thus the SMOB value for the shape continues to
198exist, but any primitive code that tries to use it will detect that the
199underlying shape has been deleted because the underlying structure
200pointer is NULL.
201
202So, to summarize the steps involved in this resolution of the problem
203(and assuming that the underlying C structure for a shape is
204@code{struct dia_shape}):
205
206@itemize @bullet
207@item
208Define a new Scheme-specific structure that @emph{points} to the
209underlying C structure:
210
211@lisp
212struct dia_guile_shape
213@{
214 struct dia_shape * c_shape; /* NULL => deleted */
215@}
216@end lisp
217
218@item
219Add a field to @code{struct dia_shape} that points to its @code{struct
220dia_guile_shape} if it has one ---
221
222@lisp
223struct dia_shape
224@{
225 @dots{}
226 struct dia_guile_shape * guile_shape;
227@}
228@end lisp
229
230@noindent
231--- so that C code can set @code{guile_shape->c_shape} to NULL when the
232underlying shape is deleted.
233
234@item
235Wrap @code{struct dia_guile_shape} as a SMOB type.
236
237@item
238Whenever you need to represent a C shape onto the Scheme level, create a
239SMOB instance for it, and pass that.
240
241@item
242In primitive code that receives a shape SMOB instance, check the
243@code{c_shape} field when decoding it, to find out whether the
244underlying C shape is still there.
245@end itemize
246
247As far as memory management is concerned, the SMOB values and their
248Scheme-specific structures are under the control of the garbage
249collector, whereas the underlying C structures are explicitly managed in
250exactly the same way that Dia managed them before we thought of adding
251Guile.
252
253When the garbage collector decides to free a shape SMOB value, it calls
254the @dfn{SMOB free} function that was specified when defining the shape
255SMOB type. To maintain the correctness of the @code{guile_shape} field
256in the underlying C structure, this function should chain through to the
257underlying C structure (if it still exists) and set its
258@code{guile_shape} field to NULL.
259
260For full documentation on defining and using SMOB types, see
261@ref{Defining New Types (Smobs)}.
262
263
264@node Dia Primitives
265@subsection Writing Guile Primitives for Dia
266
267Once the details of object representation are decided, writing the
268primitive function code that you need is usually straightforward.
269
270A primitive is simply a C function whose arguments and return value are
271all of type @code{SCM}, and whose body does whatever you want it to do.
272As an example, here is a possible implementation of the @code{square?}
273primitive:
274
275@lisp
276#define FUNC_NAME "square?"
277static SCM square_p (SCM shape)
278@{
279 struct dia_guile_shape * guile_shape;
280
281 /* Check that arg is really a shape SMOB. */
282 SCM_VALIDATE_SHAPE (SCM_ARG1, shape);
283
284 /* Access Scheme-specific shape structure. */
285 guile_shape = SCM_SMOB_DATA (shape);
286
287 /* Find out if underlying shape exists and is a
288 square; return answer as a Scheme boolean. */
289 return SCM_BOOL (guile_shape->c_shape &&
290 (guile_shape->c_shape->type == DIA_SQUARE));
291@}
292#undef FUNC_NAME
293@end lisp
294
295Notice how easy it is to chain through from the @code{SCM shape}
296parameter that @code{square_p} receives --- which is a SMOB --- to the
297Scheme-specific structure inside the SMOB, and thence to the underlying
298C structure for the shape.
299
300In this code, @code{SCM_SMOB_DATA} and @code{SCM_BOOL} are macros from
301the standard Guile API. @code{SCM_VALIDATE_SHAPE} is a macro that you
302should define as part of your SMOB definition: it checks that the passed
303parameter is of the expected type. This is needed to guard against
304Scheme code using the @code{square?} procedure incorrectly, as in
305@code{(square? "hello")}; Scheme's latent typing means that usage errors
306like this must be caught at run time.
307
308Having written the C code for your primitives, you need to make them
309available as Scheme procedures by calling the @code{scm_c_define_gsubr}
310function. @code{scm_c_define_gsubr} (REFFIXME) takes arguments that
311specify the Scheme-level name for the primitive and how many required,
312optional and rest arguments it can accept. The @code{square?} primitive
313always requires exactly one argument, so the call to make it available
314in Scheme reads like this:
315
316@lisp
317scm_c_define_gsubr ("square?", 1, 0, 0, square_p);
318@end lisp
319
320For where to put this call, see the subsection after next on the
321structure of Guile-enabled code (@pxref{Dia Structure}).
322
323
324@node Dia Hook
325@subsection Providing a Hook for the Evaluation of Scheme Code
326
327To make the Guile integration useful, you have to design some kind of
328hook into your application that application users can use to cause their
329Scheme code to be evaluated.
330
331Technically, this is straightforward; you just have to decide on a
332mechanism that is appropriate for your application. Think of Emacs, for
333example: when you type @kbd{@key{ESC} :}, you get a prompt where you can
334type in any Elisp code, which Emacs will then evaluate. Or, again like
335Emacs, you could provide a mechanism (such as an init file) to allow
336Scheme code to be associated with a particular key sequence, and
337evaluate the code when that key sequence is entered.
338
339In either case, once you have the Scheme code that you want to evaluate,
340as a null terminated string, you can tell Guile to evaluate it by
341calling the @code{scm_c_eval_string} function.
342
343
344@node Dia Structure
345@subsection Top-level Structure of Guile-enabled Dia
346
347Let's assume that the pre-Guile Dia code looks structurally like this:
348
349@itemize @bullet
350@item
351@code{main ()}
352
353@itemize @bullet
354@item
355do lots of initialization and setup stuff
356@item
357enter Gtk main loop
358@end itemize
359@end itemize
360
361When you add Guile to a program, one (rather technical) requirement is
362that Guile's garbage collector needs to know where the bottom of the C
363stack is. The easiest way to ensure this is to use
364@code{scm_boot_guile} like this:
365
366@itemize @bullet
367@item
368@code{main ()}
369
370@itemize @bullet
371@item
372do lots of initialization and setup stuff
373@item
374@code{scm_boot_guile (argc, argv, inner_main, NULL)}
375@end itemize
376
377@item
378@code{inner_main ()}
379
380@itemize @bullet
381@item
382define all SMOB types
383@item
384export primitives to Scheme using @code{scm_c_define_gsubr}
385@item
386enter Gtk main loop
387@end itemize
388@end itemize
389
390In other words, you move the guts of what was previously in your
391@code{main} function into a new function called @code{inner_main}, and
392then add a @code{scm_boot_guile} call, with @code{inner_main} as a
393parameter, to the end of @code{main}.
394
395Assuming that you are using SMOBs and have written primitive code as
396described in the preceding subsections, you also need to insert calls to
397declare your new SMOBs and export the primitives to Scheme. These
398declarations must happen @emph{inside} the dynamic scope of the
399@code{scm_boot_guile} call, but also @emph{before} any code is run that
400could possibly use them --- the beginning of @code{inner_main} is an
401ideal place for this.
402
403
404@node Dia Advanced
405@subsection Going Further with Dia and Guile
406
407The steps described so far implement an initial Guile integration that
408already gives a lot of additional power to Dia application users. But
409there are further steps that you could take, and it's interesting to
410consider a few of these.
411
412In general, you could progressively move more of Dia's source code from
413C into Scheme. This might make the code more maintainable and
414extensible, and it could open the door to new programming paradigms that
415are tricky to effect in C but straightforward in Scheme.
416
417A specific example of this is that you could use the guile-gtk package,
418which provides Scheme-level procedures for most of the Gtk+ library, to
419move the code that lays out and displays Dia objects from C to Scheme.
420
421As you follow this path, it naturally becomes less useful to maintain a
422distinction between Dia's original non-Guile-related source code, and
423its later code implementing SMOBs and primitives for the Scheme world.
424
425For example, suppose that the original source code had a
426@code{dia_change_fill_pattern} function:
427
428@lisp
429void dia_change_fill_pattern (struct dia_shape * shape,
430 struct dia_pattern * pattern)
431@{
432 /* real pattern change work */
433@}
434@end lisp
435
436During initial Guile integration, you add a @code{change_fill_pattern}
437primitive for Scheme purposes, which accesses the underlying structures
438from its SMOB values and uses @code{dia_change_fill_pattern} to do the
439real work:
440
441@lisp
442SCM change_fill_pattern (SCM shape, SCM pattern)
443@{
444 struct dia_shape * d_shape;
445 struct dia_pattern * d_pattern;
446
447 @dots{}
448
449 dia_change_fill_pattern (d_shape, d_pattern);
450
451 return SCM_UNSPECIFIED;
452@}
453@end lisp
454
455At this point, it makes sense to keep @code{dia_change_fill_pattern} and
456@code{change_fill_pattern} separate, because
457@code{dia_change_fill_pattern} can also be called without going through
458Scheme at all, say because the user clicks a button which causes a
459C-registered Gtk+ callback to be called.
460
461But, if the code for creating buttons and registering their callbacks is
462moved into Scheme (using guile-gtk), it may become true that
463@code{dia_change_fill_pattern} can no longer be called other than
464through Scheme. In which case, it makes sense to abolish it and move
465its contents directly into @code{change_fill_pattern}, like this:
466
467@lisp
468SCM change_fill_pattern (SCM shape, SCM pattern)
469@{
470 struct dia_shape * d_shape;
471 struct dia_pattern * d_pattern;
472
473 @dots{}
474
475 /* real pattern change work */
476
477 return SCM_UNSPECIFIED;
478@}
479@end lisp
480
481So further Guile integration progressively @emph{reduces} the amount of
482functional C code that you have to maintain over the long term.
483
484A similar argument applies to data representation. In the discussion of
485SMOBs earlier, issues arose because of the different memory management
486and lifetime models that normally apply to data structures in C and in
487Scheme. However, with further Guile integration, you can resolve this
488issue in a more radical way by allowing all your data structures to be
489under the control of the garbage collector, and kept alive by references
490from the Scheme world. Instead of maintaining an array or linked list
491of shapes in C, you would instead maintain a list in Scheme.
492
493Rather like the coalescing of @code{dia_change_fill_pattern} and
494@code{change_fill_pattern}, the practical upshot of such a change is
495that you would no longer have to keep the @code{dia_shape} and
496@code{dia_guile_shape} structures separate, and so wouldn't need to
497worry about the pointers between them. Instead, you could change the
498SMOB definition to wrap the @code{dia_shape} structure directly, and
499send @code{dia_guile_shape} off to the scrap yard. Cut out the middle
500man!
501
502Finally, we come to the holy grail of Guile's free software / extension
503language approach. Once you have a Scheme representation for
504interesting Dia data types like shapes, and a handy bunch of primitives
505for manipulating them, it suddenly becomes clear that you have a bundle
506of functionality that could have far-ranging use beyond Dia itself. In
507other words, the data types and primitives could now become a library,
508and Dia becomes just one of the many possible applications using that
509library --- albeit, at this early stage, a rather important one!
510
511In this model, Guile becomes just the glue that binds everything
512together. Imagine an application that usefully combined functionality
513from Dia, Gnumeric and GnuCash --- it's tricky right now, because no
514such application yet exists; but it'll happen some day @dots{}
515
516
517@node Scheme vs C
518@section Why Scheme is More Hackable Than C
519
520Underlying Guile's value proposition is the assumption that programming
521in a high level language, specifically Guile's implementation of Scheme,
522is necessarily better in some way than programming in C. What do we
523mean by this claim, and how can we be so sure?
226297eb
NJ
524
525One class of advantages applies not only to Scheme, but more generally
526to any interpretable, high level, scripting language, such as Emacs
527Lisp, Python, Ruby, or @TeX{}'s macro language. Common features of all
528such languages, when compared to C, are that:
9401323e
NJ
529
530@itemize @bullet
531@item
226297eb
NJ
532They lend themselves to rapid and experimental development cycles,
533owing usually to a combination of their interpretability and the
534integrated development environment in which they are used.
9401323e
NJ
535
536@item
226297eb
NJ
537They free developers from some of the low level bookkeeping tasks
538associated with C programming, notably memory management.
9401323e
NJ
539
540@item
226297eb
NJ
541They provide high level features such as container objects and exception
542handling that make common programming tasks easier.
9401323e
NJ
543@end itemize
544
1982a56a
NJ
545In the case of Scheme, particular features that make programming easier
546--- and more fun! --- are its powerful mechanisms for abstracting parts
547of programs (closures --- @pxref{About Closure}) and for iteration
226297eb
NJ
548(@pxref{while do}).
549
550The evidence in support of this argument is empirical: the huge amount
551of code that has been written in extension languages for applications
552that support this mechanism. Most notable are extensions written in
553Emacs Lisp for GNU Emacs, in @TeX{}'s macro language for @TeX{}, and in
554Script-Fu for the Gimp, but there is increasingly now a significant code
555eco-system for Guile-based applications as well, such as Lilypond and
556GnuCash. It is close to inconceivable that similar amounts of
557functionality could have been added to these applications just by
558writing new code in their base implementation languages.
559
226297eb
NJ
560
561@node Testbed Example
562@section Example: Using Guile for an Application Testbed
563
564As an example of what this means in practice, imagine writing a testbed
565for an application that is tested by submitting various requests (via a
566C interface) and validating the output received. Suppose further that
567the application keeps an idea of its current state, and that the
568``correct'' output for a given request may depend on the current
569application state. A complete ``white box''@footnote{A @dfn{white box}
570test plan is one that incorporates knowledge of the internal design of
571the application under test.} test plan for this application would aim to
572submit all possible requests in each distinguishable state, and validate
573the output for all request/state combinations.
574
575To write all this test code in C would be very tedious. Suppose instead
576that the testbed code adds a single new C function, to submit an
577arbitrary request and return the response, and then uses Guile to export
578this function as a Scheme procedure. The rest of the testbed can then
579be written in Scheme, and so benefits from all the advantages of
580programming in Scheme that were described in the previous section.
581
582(In this particular example, there is an additional benefit of writing
583most of the testbed in Scheme. A common problem for white box testing
584is that mistakes and mistaken assumptions in the application under test
585can easily be reproduced in the testbed code. It is more difficult to
586copy mistakes like this when the testbed is written in a different
587language from the application.)
588
589
590@node Programming Options
591@section A Choice of Programming Options
592
593The preceding arguments and example point to a model of Guile
594programming that is applicable in many cases. According to this model,
595Guile programming involves a balance between C and Scheme programming,
596with the aim being to extract the greatest possible Scheme level benefit
597from the least amount of C level work.
598
599The C level work required in this model usually consists of packaging
600and exporting functions and application objects such that they can be
601seen and manipulated on the Scheme level. To help with this, Guile's C
602language interface includes utility features that aim to make this kind
603of integration very easy for the application developer. These features
604are documented later in this part of the manual: see REFFIXME.
605
606This model, though, is really just one of a range of possible
607programming options. If all of the functionality that you need is
608available from Scheme, you could choose instead to write your whole
609application in Scheme (or one of the other high level languages that
610Guile supports through translation), and simply use Guile as an
611interpreter for Scheme. (In the future, we hope that Guile will also be
612able to compile Scheme code, so lessening the performance gap between C
613and Scheme code.) Or, at the other end of the C--Scheme scale, you
614could write the majority of your application in C, and only call out to
615Guile occasionally for specific actions such as reading a configuration
616file or executing a user-specified extension. The choices boil down to
617two basic questions:
9401323e
NJ
618
619@itemize @bullet
620@item
621Which parts of the application do you write in C, and which in Scheme
622(or another high level translated language)?
623
624@item
625How do you design the interface between the C and Scheme parts of your
626application?
627@end itemize
628
629These are of course design questions, and the right design for any given
630application will always depend upon the particular requirements that you
226297eb
NJ
631are trying to meet. In the context of Guile, however, there are some
632generally applicable considerations that can help you when designing
633your answers.
9401323e
NJ
634
635@menu
636* Available Functionality:: What functionality is already available?
637* Basic Constraints:: Functional and performance constraints.
638* Style Choices:: Your preferred programming style.
639* Program Control:: What controls program execution?
9401323e
NJ
640@end menu
641
642
643@node Available Functionality
226297eb 644@subsection What Functionality is Already Available?
9401323e
NJ
645
646Suppose, for the sake of argument, that you would prefer to write your
647whole application in Scheme. Then the API available to you consists of:
648
649@itemize @bullet
650@item
651standard Scheme
652
653@item
654plus the extensions to standard Scheme provided by
655Guile in its core distribution
656
657@item
658plus any additional functionality that you or others have packaged so
659that it can be loaded as a Guile Scheme module.
660@end itemize
661
662A module in the last category can either be a pure Scheme module --- in
663other words a collection of utility procedures coded in Scheme --- or a
664module that provides a Scheme interface to an extension library coded in
665C --- in other words a nice package where someone else has done the work
666of wrapping up some useful C code for you. The set of available modules
667is growing quickly and already includes such useful examples as
668@code{(gtk gtk)}, which makes Gtk+ drawing functions available in
669Scheme, and @code{(database postgres)}, which provides SQL access to a
670Postgres database.
671
a7a7bb95
NJ
672Given the growing collection of pre-existing modules, it is quite
673feasible that your application could be implemented by combining a
674selection of these modules together with new application code written in
675Scheme.
676
677If this approach is not enough, because the functionality that your
226297eb
NJ
678application needs is not already available in this form, and it is
679impossible to write the new functionality in Scheme, you will need to
680write some C code. If the required function is already available in C
681(e.g. in a library), all you need is a little glue to connect it to the
682world of Guile. If not, you need both to write the basic code and to
683plumb it into Guile.
9401323e
NJ
684
685In either case, two general considerations are important. Firstly, what
686is the interface by which the functionality is presented to the Scheme
687world? Does the interface consist only of function calls (for example,
688a simple drawing interface), or does it need to include @dfn{objects} of
689some kind that can be passed between C and Scheme and manipulated by
690both worlds. Secondly, how does the lifetime and memory management of
691objects in the C code relate to the garbage collection governed approach
692of Scheme objects? In the case where the basic C code is not already
693written, most of the difficulties of memory management can be avoided by
694using Guile's C interface features from the start.
695
226297eb
NJ
696For the full documentation on writing C code for Guile and connecting
697existing C code to the Guile world, see REFFIXME.
9401323e
NJ
698
699
700@node Basic Constraints
226297eb 701@subsection Functional and Performance Constraints
9401323e
NJ
702
703
704@node Style Choices
226297eb 705@subsection Your Preferred Programming Style
9401323e
NJ
706
707
708@node Program Control
226297eb 709@subsection What Controls Program Execution?
9401323e 710
9401323e
NJ
711
712@node User Programming
713@section How About Application Users?
714
715So far we have considered what Guile programming means for an
716application developer. But what if you are instead @emph{using} an
717existing Guile-based application, and want to know what your
718options are for programming and extending this application?
719
720The answer to this question varies from one application to another,
721because the options available depend inevitably on whether the
722application developer has provided any hooks for you to hang your own
723code on and, if there are such hooks, what they allow you to
724do.@footnote{Of course, in the world of free software, you always have
725the freedom to modify the application's source code to your own
726requirements. Here we are concerned with the extension options that the
727application has provided for without your needing to modify its source
728code.} For example@dots{}
729
730@itemize @bullet
731@item
732If the application permits you to load and execute any Guile code, the
733world is your oyster. You can extend the application in any way that
734you choose.
735
736@item
737A more cautious application might allow you to load and execute Guile
738code, but only in a @dfn{safe} environment, where the interface
739available is restricted by the application from the standard Guile API.
740
741@item
742Or a really fearful application might not provide a hook to really
743execute user code at all, but just use Scheme syntax as a convenient way
744for users to specify application data or configuration options.
745@end itemize
746
747In the last two cases, what you can do is, by definition, restricted by
748the application, and you should refer to the application's own manual to
749find out your options.
750
751The most well known example of the first case is Emacs, with its
752extension language Emacs Lisp: as well as being a text editor, Emacs
753supports the loading and execution of arbitrary Emacs Lisp code. The
754result of such openness has been dramatic: Emacs now benefits from
755user-contributed Emacs Lisp libraries that extend the basic editing
756function to do everything from reading news to psychoanalysis and
757playing adventure games. The only limitation is that extensions are
758restricted to the functionality provided by Emacs's built-in set of
759primitive operations. For example, you can interact and display data by
85a9b4ed 760manipulating the contents of an Emacs buffer, but you can't pop-up and
9401323e
NJ
761draw a window with a layout that is totally different to the Emacs
762standard.
763
764This situation with a Guile application that supports the loading of
765arbitrary user code is similar, except perhaps even more so, because
766Guile also supports the loading of extension libraries written in C.
767This last point enables user code to add new primitive operations to
768Guile, and so to bypass the limitation present in Emacs Lisp.
769
770At this point, the distinction between an application developer and an
771application user becomes rather blurred. Instead of seeing yourself as
772a user extending an application, you could equally well say that you are
773developing a new application of your own using some of the primitive
774functionality provided by the original application. As such, all the
775discussions of the preceding sections of this chapter are relevant to
776how you can proceed with developing your extension.