practice
DoI: 10.1145/1965724.1965739
Passing
a Language
Through the
Eye of a needle
Article development led by
queue.acm.org
Many languages (not necessarily
scripting languages) support extending through a foreign function interface
(FFI). An FFI is not enough to allow a
function in the system language to
do all that a function in the script can
do. Nevertheless, in practice FFI covers most common needs for extending, such as access to external libraries and system calls. Embedding, on
the other hand, is more difficult to
support, because it usually demands
closer integration between the host
program and the script, and FFI alone
does not suffice.
In this article we discuss how embeddability can impact the design of
a language, and in particular how it
impacted the design of Lua from day
one. Lua3, 4 is a scripting language with
a particularly strong emphasis on embeddability. It has been embedded in
a wide range of applications and is a
leading language for scripting games. 2
The Eye of a needle
At first sight, the embeddability of a
scripting language seems to be a feature of the implementation of its interpreter. Given any interpreter, we
can attach an API to it to allow the host
program and the script to interact.
The design of the language itself, however, has a great influence on the way
it can be embedded. Conversely, if you
design a language with embeddability in mind, this mind-set will have a
great influence on the final language.
The typical host language for most
scripting languages is C, and APIs for
these languages are therefore mostly
composed of functions plus some
types and constants. This imposes a
natural but narrow restriction on the
design of an API for a scripting language: it must offer access to language
features through this eye of a needle.
Syntactical constructs are particularly
difficult to get through. For example,
in a scripting language where methods must be written lexically inside
their classes, the host language cannot add methods to a class unless
the API offers suitable mechanisms.
How the embeddability of Lua
impacted its design.
BY RoBERTo IERuSALIMSCh Y, LuIZ hEnRIquE DE FIGuEIREDo,
AnD WALDEMAR CELES
sCriptinG LAnGuAGes Are an important element
in the current landscape of programming
languages. A key feature of a scripting language
is its ability to integrate with a system language. 7
This integration takes two main forms: extending
and embedding. In the first form, you extend the
scripting language with libraries and functions
written in the system language and write your main
program in the scripting language. In the second
form, you embed the scripting language in a host
program (written in the system language) so that
the host can run scripts and call functions defined
in the scripts; the main program is the host program.
In this setting, the system language is usually called
the host language.