![]() |
Pitfalls | ![]() |
Errors are swallowed. A Scheme error during eval, call or exec_file is printed on the console and the call returns the pair (key . args) (
Scheme/Guile/guile_tm.cpp
, TeXmacs_catcher). Most as_type conversions then return a neutral value (0, "", an empty tree, …), so as_int (call ("f")) yields 0 when f fails. Check the type of the result (is_int, is_tree, …) when it matters.
Calls by name are evaluated. call ("f", …) evaluates the string "f" on every call. Do not build such strings from user data, and prefer to keep an object with the procedure in code which is called often.
Strings versus symbols. object ("foo") is the Scheme string "foo"; use symbol_object ("foo") for the symbol.
exec_file always returns true. It compares the result of loading the file with the string "#<unspecified>", which never matches, not even after an error (
Scheme/Scheme/object.cpp
, exec_file). No caller uses the result.
eval_secure is broken. It evaluates (wrap-eval-secure expr), but wrap-eval-secure is not defined anywhere in
TeXmacs/progs
. The function has no callers; secure evaluation is implemented in Scheme (
kernel/texmacs/tm-secure.scm
).
Delayed commands and pauses. Only commands scheduled with exec_delayed_pause (and hence the Scheme macro delayed) can reschedule themselves by returning an integer; the result of a command scheduled with exec_delayed is ignored.
Editor routines need a current view. All routines of
build-glue-editor.scm
call get_current_editor (), which asserts that there is a current view. Scheme code which may run without a buffer (very early during startup, or in some background tasks) must not call them.
At most ten arguments. The generator refers to TMSCM_ARGn, which is only defined up to 10 in
guile_tm.hpp
. Routines with more arguments should take a list or an object.
The uint check does not reject negative numbers. TMSCM_ASSERT_UINT (
Scheme/Scheme/glue.cpp
) tests tmscm_is_int (i) && scm_positive_p (i), but scm_positive_p returns a Scheme boolean, and #f is not a null value in C++. A negative argument therefore passes the check, and is only rejected later by scm_to_uint, with an out-of-range error instead of a wrong-type error. The type is used by gnutls-random-number.
scheme_tree loses floating point numbers. tmscm_to_scheme_tree converts lists, symbols, strings, integers, booleans and trees; a floating point number (or any other value) becomes "?".
Destructors may run during garbage collection. A C++ value boxed in a black box is released by the smob free function, that is, during a garbage collection. Its destructor (and the destructors of whatever it holds) must not call Scheme. tmscm_object_rep itself respects this by deferring its cleanup (see protection from the garbage collector).
C++ exceptions inside glue routines. With USE_EXCEPTIONS (the default), a failed ASSERT in a glued routine throws a C++ exception, which leaves the glue function through the Guile evaluator up to the next C++ catch site (for a menu action, protected_call). The Scheme state between the two (dynamic wind handlers, catch frames) is not unwound in the usual Scheme way. Avoid relying on this in new code: check arguments and return an error value instead.
Regenerate after editing declarations. The CMake build compiles the committed glue_*.cpp; a change of a build-glue-*.scm file has no effect until the glue is regenerated (see regenerating the glue). The script build-glue runs the generator four times in a row on the same output file, which is redundant but harmless.
With DEBUG_ON, the evaluations started from C++ are not wrapped in error catches, so a Scheme error is not stopped at the boundary. In addition, while a black box is being freed (scm_busy), eval_scheme and string_to_tmscm return #f instead of doing their work.