Chapter 25. Extvar-Declaration

ATS puts great emphasis on interacting with other programming languages.

Suppose that I have in some C code a (global) integer variable of the name foo and I want to increase in some ATS code the value stored in foo by 1. This can be done as follows:

val x0 = $extval(int, "foo") // get the value of foo val p_foo = $extval(ptr, "&foo") // get the address of foo val () = $UNSAFE.ptr_set<int> (p_foo, x0 + 1) // update foo

where the address-of operator (&) in C is needed for taking the address of foo. If I want to interact in ATS with a language that does not support the address-of operator (e.g., JavaScript and Python), then I can do it as follows:

extvar "foo" = x0 + 1

where the keyword extvar indicates that the string following it refers to an external variable (or left-value) that should be updated with the value of the expression on the right-hand side of the equality symbol following the string. Of course, this works for languages like C that do support the address-of operator as well. This so-called extvar-declaration can also be written as follows:

extern var "foo" = x0 + 1

where extvar expands into extern var.

As for another example, let us suppose that foo2 is a record variable that contains two integer fields named first and second. Then the following code assigns integers 1 and 2 to these two fields of foo2:

extvar "foo2.first" = 1 extvar "foo2.second" = 2

By its very nature, the feature of extvar-declaration is inherently unsafe, and it should only be used with caution.

Please find on-line the entirety of the code presented in this chapter.