Download Self Handbook Documentation
Transcript
Self Handbook Documentation, Release 2 for Self 4.4
if (key == 0) {
failure(FH, "key == 0 is identity map");
return NULL;
}
for (i = 0; str[i]; i++)
res[i] = str[i] + key;
res[i] = ’\0’;
return res;
}
/* Make glue expand to full functions, not just prototypes. */
# define WHAT_GLUE FUNCTIONS
C_func_2(string,, encrypt, encrypt_glue, fail, string,, int,)
# undef WHAT_GLUE
Observe that the fail_opt argument now has the value fail and that the encrypt function raises an exception,
using failure, if the key is 0. There are two ways to raise exceptions:
extern "C" void failure(void *FH, char *msg);
extern "C" void unix_failure(void *FH, int err = -1);
In both cases, the FH argument is the “failure handle” that was passed by the C_func_N macro. The second argument
to failure is a string. It will be passed to the “IfFail block” in Self. unix_failure takes an optional integer as
its second argument. If this integer has the value -1, or is missing, the value of errno is used instead. The integer
is interpreted as a UNIX error number, from which a corresponding string is constructed. The string is then, as for
failure, passed to the “IfFail block” at the call site in Self.
Warning: After calling failure or unix_failure a normal return must be done. The value
returned (in the example NULL) is ignored.
8.13.7 Reading and assigning global variables
Reading the value of a global variable is done using the C_get_var macro. Assigning a value to a global variable is
done using C_set_var. Both macros expand into a C++ function that converts between Self and C representation,
and reads or assigns the variable. Here is the general syntax:
C_get_var(cnvt_res,aux_res, expr, gfname)
C_set_var(var, expr_c0,expr_a0, gfname)
A concrete example is reading the value of the variable errno, which can be done using:
C_get_var(int,, errno, get_errno_glue)
The meaning of the each argument is:
• cnvt_res,‘‘aux_res‘‘: how to convert the value of the global variable that is being read to a Self object.
In the errno example, cnvt_res is int and aux_res is empty, since the type of errno is int. The
cnvt_res,‘‘aux_res‘‘ can be any one of the result conversions found in Table 3.
• expr is the variable whose value is being read. In the errno example, it is simply errno, but in general, it
may actually be any expression that is valid in a global context, even an expression involving function calls.
• gfname: the name of the C++ function that C_get_var or C_set_var expands into.
• var is the name of a global variable that a value is assigned to. In general, var, may be any expression that in
a global context evaluates to an l-value.
8.13. Interfacing with other languages
121