Metaprogramming is the ability to treat the source code as first-class data and emit it to the program. In Multi-stage compile-time metaprogramming the above procedure can be repeated in infinite nesting level at compile time.
We extend JavaScript with the essentials of multi-staged metaprogramming and implement it in SpiderMonkey JavaScript engine. We indicate the minimal modification occurred in SpiderMonkey by minor extending the lexer, parser, AST, reflection (parse) and the addition of the unparsing procedure, some library functions, the staged logic and a debugging system.
- Quasi-quotes .< >. Are holding JavaScript code definitions in order to convey them to AST form.
- Escape .~ It prevents an expression to convert its context to AST by evaluating its current value. It is defined only inside a Quasi-quote definitions.
- Escape-value .@ Is used when we need to prevent the conversion of an expression to AST by matching the current evaluated value to an AST form.
- Inline .! Injects the declared location with the AST of the evaluated value, eliminating itself
- Execute .& Defines a statement that will be executed in the staged process.
.& { //This block of code will be executed at compile time
// Assign to variable printArgAst the source code of 2 arguments
var printArgAst = .< 'compile time', 'metaprogramming'; >.;
// Assign to variable logAst the source code of console.log,
// enriched with the printArgAst arguments;
var logAst = .< console.log( .~printArgAst ); >.;
}
// pure javascript statement
console.log( 'runtime pure statement' );
// emit to final program the logAst
.!logAst;
We deploy the project at visual studio 2010. You can find the solution at:
js\Project\SpiderMonkey\SpiderMonkey.sln
You can find the html/css/js assets and examples at:
js\Project\MultiStagedExec\Src
Compile
add JS_STAGEDJS
flag to preprocessor
Run
-i, --inputfile, input filepath
-o, --outputfile, output filepath
example> -i power.js -o power_final.js
Compile
add JS_STAGEDJS_DBG
flag to preprocessor
Run
-i, --inputfile, input filepath
-o, --outputfile, output filepath
-b, --browser, browser executable location
example> -i power.js -o power_final.js -b chrome
Compile
add JS_STAGEDJS_SERVICE
flag to preprocessor
Run
(optional) --port, web server port
Src/staged_client/js/main.js, variable SERVER_URL
Thesis submitted by Yannis Apostolidis In partial fulfillment of the requirements for the Masters’ of Science degree in Computer Science.
Thesis Supervisor Anthony Savidis
The work reported in this thesis has been conducted at the Human Computer Interaction (HCI) laboratory of the Institute of Computer Science (ICS) of the Foundation for Research and Technology - Hellas (FORTH), and has been financially supported by a FORTH-ICS scholarship.