![]() |
femtolisp: the binding with C++ | ![]() |
The rest of TeXmacs knows the Scheme interpreter through
scheme.hpp
(the class object, call, eval…). Below it,
Scheme/Scheme/object.cpp
,
glue.cpp
and the generated Scheme/Glue/glue_*.cpp use only the type tmscm and the functions and macros around it (tmscm_cons, string_to_tmscm, TMSCM_ASSERT…). Each interpreter implements these: for femtolisp,
femtolisp_tm.hpp
and
femtolisp_tm.cpp
, which
object.hpp
includes when USE_FEMTOLISP is defined.
The C++ code never includes the headers of femtolisp: their macros and functions (car, cdr, ptr, tag, symbol…) clash with names of TeXmacs. It uses
fl_tm.h
, a small C interface whose functions start with fltm_.
fl_core.c
implements it; that file also includes the sources of femtolisp, as one compilation unit, so the interface can use the insides of the interpreter (its stack, its types).
femtolisp has a copying collector: any allocation may move every object. A value kept in a C variable is then wrong after the next allocation, and the glue keeps values so all the time, as in
p= tmscm_cons (scheme_tree_to_tmscm (t[i]), p); |
So tmscm is a class: a value (fltm_value v) and the links of a doubly linked list, tmscm_roots. The constructors link the object, the destructor unlinks it, and at each collection femtolisp calls tmscm_relocate_roots, which updates the value of every object of the list. Any tmscm stays right across allocations, be it a local variable, a temporary, an argument or a member of an object of the heap.
What follows from it:
The constructor from a raw value is explicit: an integer does not become a tmscm silently.
tmscm_object_rep holds a tmscm: the stack of protected objects of the Guile and S7 interfaces is not needed.
A raw fltm_value must not be kept across a call which allocates. The functions of
fl_tm.h
marked allocates protect their own arguments, not the other values of their caller. In C++, keep a tmscm; in
fl_core.c
, push the value on the stack of femtolisp.
A tmscm costs a little more than a pointer: a call of the glue is as fast as with S7, the conversions of values a little slower.
An error of femtolisp is a longjmp. Over a C++ function it would skip the destructors of its tmscms, and leave addresses of the dead stack in the list of roots: the next collection would write there. Two rules prevent it.
From C++ to Scheme. Every call goes through fltm_apply or fltm_eval_string (call_scheme, eval_scheme, eval_scheme_file). They catch the error, report it (the Scheme function %report-error), and return it as a value, in the form of Guile's errors.
From Scheme to C++. A function of the glue runs inside the template tmscm_proc, whose body is the macro TMSCM_CALL_GLUE: the arguments are copied into tmscms and the function runs in a try block; an error is raised by tmscm_raise only after the block, when no C++ object with a destructor is left. TMSCM_ASSERT throws the C++ exception tmscm_error, which this block catches; any other C++ exception becomes the error misc-error.
A new C function called from Scheme must follow the second rule: install it with tmscm_install_procedure, which wraps it, and report its errors with TMSCM_ASSERT or by throwing a tmscm_error. Never call fltm_raise with a C++ object alive.
One error escapes these rules: the out of memory error, which an allocation raises (fltm_cons, fltm_string…) from wherever it is called, also from C++ code with live tmscms. The stack of femtolisp is printed before (fl_out_of_memory_hook); the state of TeXmacs after such an error is not to be trusted.
string_to_tmscm copies the bytes of the TeXmacs string into a femtolisp string, tmscm_to_string copies them back. There is no conversion of encoding: the bytes of Cork go through. A string may hold the byte 0.
An integer is a fixnum, or a boxed 64-bit integer when it does not fit (fltm_integer); the fixnums have 62 bits on a 64-bit machine, 30 bits in WebAssembly. tmscm_to_int throws a tmscm_error (out-of-range) for a value out of the range of int. tmscm_is_double is true for every number.
symbol_to_tmscm and tmscm_to_symbol, by their names.
A tree, a url, a command… is an opaque value of femtolisp which holds a pointer to a blackbox of the heap (blackbox_to_tmscm, tmscm_to_blackbox). Its type, made by fltm_define_opaque_type, has four functions: to print the value, to delete the blackbox when the value is collected, to compare two values for equal? (the == of the blackbox: trees are compared by their contents), and to hash it in agreement with this comparison.
tmscm_install_procedure (name, f, n, 0, 0) defines the builtin name of femtolisp as tmscm_proc<f>, which checks that it gets as many arguments as f takes (0 to 10; n is not used). The generated files Scheme/Glue/glue_*.cpp are the same for the three interpreters. When they must be made again, the femtolisp build runs the generators on the small S7 interpreter s7-run, as the S7 build does.
A few builtins which TeXmacs needs and femtolisp lacks are written in C in
fl_core.c
(the table fltm_builtin_info): the functions on strings of bytes (string-length, string-ref, substring, list->string…), %keyword?, %symbol?, %table-ref, %fingerprint, %function-become!, system. Their names starting with % are for the Scheme files of the interface only. The functions of Scheme which only the browser version has (web-javascript, web-files…) are defined in
src/Plugins/Vue/vue_gui.cpp
with tmscm_install_procedure, for S7 and femtolisp alike.
start_scheme calls fltm_init with the size of the heap and the boot image, which is in the program (
fl_boot.h
): nothing is read from the disk to start femtolisp. It then gives tmscm_relocate_roots to the collector. initialize_scheme defines the builtins of the glue and the type of the objects of TeXmacs, and TeXmacs loads scheme_init_file (), which is
progs/init-femtolisp.scm
.
The heap has two halves of the same size, which the collector copies one into the other; a half is doubled when more than 80% of it is still in use after a collection. Each half starts with 48 MB, about the heap which S7 has in TeXmacs. The environment variable TEXMACS_FL_HEAP gives another size, in MB (it is not checked). The heap stops growing at 8 GB, with an out of memory error (patches 0012 and 0023), which also prints the functions of the stack.