Abstract |
Generative metaprogramming is the ability to treat source code as first
-
class data and emit it to
the program itself, thus modifying it. In multi
-
stage metaprogramming the above procedure can
be repeated in infinite nesting level. Usually, the metaprogramm
ing language is quite different
from the normal language, overall resulting in two distinct languages with separate respective
implementations.
In our work, we focused on JavaScript and extended the language with a metaprogramming layer
programmable direc
tly in JavaScript. This way, we managed to deliver a full
-
scale
metaprogramming framework where: (i) the normal language is a pure subset of the meta
-
language; and (ii) they share the same underlying implementation. Our JavaScript multi
-
staging
support has
been implemented by extending the open source Mozilla
SpiderMonkey JavaScript
engine. We
indicate the minimal modifications required in SpiderMonkey by performing minor
extensions in the syntax, parser and internal AST structures, and the addition of an u
nparser, a
staging loop, some library functions and a debugger backend component for AST inspection. Our
approach handles each stage as an isolated entity by collecting all the meta
-
annotations for the
specific depth, respecting the order, and creating a c
oherent program.
We validate our
implementation through
the implementation of
an email application using meta
-
programming to fully generate the user
-
interface code.
Additionally, we studied JavaScript source
code patterns from the real world libraries and
reengineered various parts to achieve improved
reusability and performance through metaprogramming. Finally, we supported metaprogram
debugging directly in the browser environment suing the available tools. For this purpose, we
implemented a debugger client as a web page application that communicates with the extended
SpiderMonkey debugger
backend. The client provides extra features particular to
metaprogramming debugging process,
including visualizations
of ASTs and respective unparsed
source code fragments.
|