![]() |
The Scheme interpreter and the C++/Scheme glue | ![]() |
Most of the user interface of TeXmacs (menus, keyboard bindings, editing commands, converters, dialogs) is written in Scheme, while the document model, the typesetter and the rendering are written in C++. This chapter describes the layer which connects the two: how the interpreter is started, how C++ code holds, inspects and calls Scheme values, and how C++ routines are made available as Scheme procedures by the glue.
It is written for developers who want to export a new C++ routine to Scheme, call Scheme code from C++, or debug a problem at the boundary. The user level view of the Scheme side (modules, tm-define, contextual overloading) is described in the Scheme developer guide, and the list of all exported routines in the glue auto-documentation.
All file names below are relative to
src/src/
unless stated otherwise.
The connection consists of four layers:
The only back-end which is compiled is Guile (
Scheme/Guile/
). Everything which depends on the Guile version is hidden behind a thin abstraction, the tmscm layer: the type tmscm (a Guile SCM) and functions such as tmscm_cons, tmscm_is_string or string_to_tmscm.
The class object (
Scheme/scheme.hpp
,
Scheme/Scheme/object.cpp
) is a reference counted handle on a Scheme value which protects it from the garbage collector. It comes with constructors from the usual TeXmacs types, predicates, conversions (as_int, as_tree, …) and the functions eval, call and exec_delayed, by which C++ code runs Scheme code.
Trees, URLs, commands, widgets, observers, patches and modifications are passed to Scheme as black boxes: a single Guile smob type which wraps a blackbox, that is, a type tagged copy of the C++ value.
About 1200 C++ routines are exported to Scheme. They are declared in three tables (
Scheme/Glue/build-glue-basic.scm
,
build-glue-editor.scm
,
build-glue-server.scm
), from which a generator written in Scheme produces C++ wrapper functions (
glue_basic.cpp
,
glue_editor.cpp
,
glue_server.cpp
) that check and convert the arguments, call the routine and convert the result.
Calls go in both directions:
C++ calls Scheme: Scheme calls C++:
call ("menu-expand", …) (show-header #f)
| |
v v
object -> call_scheme tmg_show_header (tmscm arg1)
| check, convert, call
v |
Scheme code (progs/*.scm) v
get_server()->show_header (false) |
Scheme/scheme.hpp
The interface seen by the rest of TeXmacs: the class object, its predicates and conversions, eval, call, exec_delayed, protected_call, scheme_cmd and the preference routines.
Scheme/Scheme/object.hpp,
object.cpp
The representation tmscm_object_rep of objects and the implementation of the interface above.
Scheme/Scheme/glue.hpp,
glue.cpp
The conversions between tmscm and all TeXmacs types used by the glue, the corresponding argument checks (TMSCM_ASSERT_TYPE), a few helper routines which only exist to be exported, and initialize_glue, which installs all glue routines. The generated files are compiled as part of
glue.cpp
, which includes them.
Scheme/Guile/guile_tm.hpp,
guile_tm.cpp
The Guile back-end: the tmscm layer, the selection of the Guile API generation (GUILE_A to GUILE_D), start_scheme, initialize_scheme, evaluation with error catching, and the black box smob.
Scheme/Tiny/
An experimental TinyScheme back-end, not compiled by any of the build systems.
Scheme/Glue/build-glue.scm
The glue generator.
Scheme/Glue/build-glue-basic.scm,
build-glue-editor.scm,
build-glue-server.scm
The declarations of the exported routines.
Scheme/Glue/glue_basic.cpp,
glue_editor.cpp,
glue_server.cpp
The generated wrappers (do not edit).
Scheme/Glue/build-glue, build-auto-doc,
make-apidoc-module.scm,
make-apidoc-doc.scm
Shell scripts which run the generator, and generators for the list of glue symbols (
TeXmacs/progs/prog/glue-symbols.scm
) and for the glue documentation (
TeXmacs/doc/devel/scheme/api/glue-auto-doc.en.tm
).
Kernel/Abstractions/blackbox.hpp
The type tagged containers blackbox and whitebox_rep<T>.
TeXmacs/progs/kernel/texmacs/tm-define.scm
The macros tm-define, tm-property and lazy-define.