GNU TeXmacsUser manualReferenceAboutEnglish
femtolisp: the binding with C++

1.The layers

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).

2.A tmscm is a root of the garbage collector

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:

3.Errors never cross C++ code

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.

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.

4.Values

Strings

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.

Numbers

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.

Symbols

symbol_to_tmscm and tmscm_to_symbol, by their names.

The objects of TeXmacs

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.

5.The glue

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.

6.Starting

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.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".