Introduction How not to design a scripting language

How not to Design a Scripting Language

Paul Biggar

Department of Science and Statistics Trinity College Dublin

StackOverflow London, 28th October, 2009

How not to Design a Scripting Language Paul Biggar PhD Dissertation Design and Implementation of an Ahead-of- PHP

phc (http://phpcompiler.org)

Introduction How not to design a scripting language

About me

• PhD candidate, Trinity College Dublin • Topic: , optimizations, scripting languages.

How not to Design a Scripting Language Paul Biggar Introduction How not to design a scripting language

About me

• PhD candidate, Trinity College Dublin • Topic: Compilers, optimizations, scripting languages.

PhD Dissertation Design and Implementation of an Ahead-of-time PHP Compiler

phc (http://phpcompiler.org)

How not to Design a Scripting Language Paul Biggar • Speed

Introduction How not to design a scripting language

How not to design a scripting language

• Compilers • Scripting Languages

How not to Design a Scripting Language Paul Biggar Introduction How not to design a scripting language

How not to design a scripting language

• Compilers • Scripting Languages

• Speed

How not to Design a Scripting Language Paul Biggar Common Features: • Dynamic typing • Duck typing • Interpreted by default • FFI via C API

Introduction How not to design a scripting language

What is a scripting language? • Javascript • Lua • • PHP • Python • Ruby

How not to Design a Scripting Language Paul Biggar Introduction How not to design a scripting language

What is a scripting language? • Javascript • Lua • Perl • PHP • Python • Ruby

Common Features: • Dynamic typing • Duck typing • Interpreted by default • FFI via C API

How not to Design a Scripting Language Paul Biggar NOT THE DRAGON BOOK Engineering a Compiler by Cooper/Torczon

Modern Compiler Implementation in X by Appel

• Compilers: Not too hard, sometimes portable, optimizations • Just-in-time compilers: Very difficult, unportable, fast .

Introduction How not to design a scripting language

Language implementation

• Interpreters: Easy, portable

How not to Design a Scripting Language Paul Biggar • Just-in-time compilers: Very difficult, unportable, fast NOTinterpreter THE DRAGON. BOOK Engineering a Compiler by Cooper/Torczon

Modern Compiler Implementation in X by Appel

Introduction How not to design a scripting language

Language implementation

• Interpreters: Easy, portable • Compilers: Not too hard, sometimes portable, optimizations

How not to Design a Scripting Language Paul Biggar • Just-in-time compilers: Very difficult, unportable, fast interpreter.

Introduction How not to design a scripting language

Language implementation

• Interpreters: Easy, portable • Compilers: Not too hard, sometimes portable, optimizations NOT THE DRAGON BOOK Engineering a Compiler by Cooper/Torczon

Modern Compiler Implementation in X by Appel

How not to Design a Scripting Language Paul Biggar NOT THE DRAGON BOOK Engineering a Compiler by Cooper/Torczon

Modern Compiler Implementation in X by Appel

Introduction How not to design a scripting language

Language implementation

• Interpreters: Easy, portable • Compilers: Not too hard, sometimes portable, optimizations • Just-in-time compilers: Very difficult, unportable, fast interpreter.

How not to Design a Scripting Language Paul Biggar 1 Elegant and well designed, 2 High of abstraction, 3 Dynamic typing (and duck typing).

Introduction How not to design a scripting language

What’s right with scripting languages?

How not to Design a Scripting Language Paul Biggar 2 High level of abstraction, 3 Dynamic typing (and duck typing).

Introduction How not to design a scripting language

What’s right with scripting languages?

1 Elegant and well designed,

How not to Design a Scripting Language Paul Biggar 3 Dynamic typing (and duck typing).

Introduction How not to design a scripting language

What’s right with scripting languages?

1 Elegant and well designed, 2 High level of abstraction,

How not to Design a Scripting Language Paul Biggar Introduction How not to design a scripting language

What’s right with scripting languages?

1 Elegant and well designed, 2 High level of abstraction, 3 Dynamic typing (and duck typing).

How not to Design a Scripting Language Paul Biggar Problem: Language designed for one interpreters specific interpreter

• Run-time source code • Foreign Function Interface

Introduction How not to design a scripting language

What’s wrong with scripting languages?

Symptoms: Speed, Portability

How not to Design a Scripting Language Paul Biggar Problem: Language designed for one specific interpreter

• Foreign Function Interface

Introduction How not to design a scripting language

What’s wrong with scripting languages?

Symptoms: Speed, Portability

Problem: Language designed for interpreters

• Run-time source code execution

How not to Design a Scripting Language Paul Biggar Problem: Language designed for interpreters

Introduction How not to design a scripting language

What’s wrong with scripting languages?

Symptoms: Speed, Portability

Problem: Language designed for one specific interpreter

• Run-time source code execution • Foreign Function Interface

How not to Design a Scripting Language Paul Biggar Introduction How not to design a scripting language

FFI FFI

Foreign Function Interface based on CPython interpreter • Access to C libraries • C applications using Python scripts • Rewrite hot code in C

How not to Design a Scripting Language Paul Biggar Introduction How not to design a scripting language

FFI FFI (good) implications

• Libraries not that slow • Can break out of Python for slow code.

How not to Design a Scripting Language Paul Biggar Introduction How not to design a scripting language

FFI FFI (bad) implications

• Language is allowed to be slow • Must break out of Python for speed.

How not to Design a Scripting Language Paul Biggar • Reimplementations

Introduction How not to design a scripting language

FFI FFI (worse) implications

• Legacy issues

How not to Design a Scripting Language Paul Biggar Introduction How not to design a scripting language

FFI FFI (worse) implications

• Legacy issues • Reimplementations

How not to Design a Scripting Language Paul Biggar • Only one way of FFI is better • Declarative is best

• Any reimplementation can reuse the same libraries without any modifications • CPython itself can change without hassle

Introduction How not to design a scripting language

FFI FFI solution

Don’t expose yourself! • Importing functions into Python with a Domain Specific Language is good

How not to Design a Scripting Language Paul Biggar • Declarative is best

• Any reimplementation can reuse the same libraries without any modifications • CPython itself can change without hassle

Introduction How not to design a scripting language

FFI FFI solution

Don’t expose yourself! • Importing functions into Python with a Domain Specific Language is good • Only one way of FFI is better

How not to Design a Scripting Language Paul Biggar • Any reimplementation can reuse the same libraries without any modifications • CPython itself can change without hassle

Introduction How not to design a scripting language

FFI FFI solution

Don’t expose yourself! • Importing functions into Python with a Domain Specific Language is good • Only one way of FFI is better • Declarative is best

How not to Design a Scripting Language Paul Biggar Introduction How not to design a scripting language

FFI FFI solution

Don’t expose yourself! • Importing functions into Python with a Domain Specific Language is good • Only one way of FFI is better • Declarative is best

• Any reimplementation can reuse the same libraries without any modifications • CPython itself can change without hassle

How not to Design a Scripting Language Paul Biggar eval (mysql_read (...)[0]); username="myname" $langpassword= ....="mypass; " includeserver="srv.domain.com("localisation/locale." $lang.php");

• meta-programming • . files • localization

Introduction How not to design a scripting language

Compiled and interpreted models Dynamic source code generation

• eval and dynamic include/import

How not to Design a Scripting Language Paul Biggar username="myname" $langpassword= ....="mypass; " includeserver="srv.domain.com("localisation/locale." $lang.php");

• .rc files • localization

Introduction How not to design a scripting language

Compiled and interpreted models Dynamic source code generation

• eval and dynamic include/import • meta-programming eval (mysql_read (...)[0]);

How not to Design a Scripting Language Paul Biggar $lang= ....; include ("localisation/locale.$lang.php");

eval (mysql_read (...)[0]); • localization

Introduction How not to design a scripting language

Compiled and interpreted models Dynamic source code generation

• eval and dynamic include/import • meta-programming • .rc files username="myname" password="mypass" server="srv.domain.com"

How not to Design a Scripting Language Paul Biggar eval (mysql_read (...)[0]); username="myname" password="mypass" server="srv.domain.com"

Introduction How not to design a scripting language

Compiled and interpreted models Dynamic source code generation

• eval and dynamic include/import • meta-programming • .rc files • localization $lang= ....; include ("localisation/locale.$lang.php");

How not to Design a Scripting Language Paul Biggar Introduction How not to design a scripting language

Compiled and interpreted models Dynamic source code generation

We don’t even know the full program source!!

How not to Design a Scripting Language Paul Biggar t= ...; for_tempalert(i== ($(0strlen(t);;’li’ i< strlen(t);)[).get(0].nodeName);0).nodeName); i++) {for (i=0; i< _temp; i++) { s[i]= t[i]; } s[i]= t[i]; }

Introduction How not to design a scripting language

Compiled and interpreted models So they can’t be compiled (ahead-of-time) Downsides: • Must use FFI for speed • Static analysis • Cool optimizations can’t happen

How not to Design a Scripting Language Paul Biggar t= ...; _tempalert= ($(strlen(t);’li’)[).get(0].nodeName);0).nodeName); for (i=0; i< _temp; i++) { s[i]= t[i]; }

Introduction How not to design a scripting language

Compiled and interpreted models So they can’t be compiled (ahead-of-time) Downsides: • Must use FFI for speed • Static analysis • Cool optimizations can’t happen

t= ...; for (i=0; i< strlen(t); i++) { s[i]= t[i]; }

How not to Design a Scripting Language Paul Biggar t= ...; foralert(i= ($(0;’li’ i< strlen(t);)[).get(0].nodeName);0).nodeName); i++) { s[i]= t[i]; }

Introduction How not to design a scripting language

Compiled and interpreted models So they can’t be compiled (ahead-of-time) Downsides: • Must use FFI for speed • Static analysis • Cool optimizations can’t happen

t= ...; _temp= strlen(t); for (i=0; i< _temp; i++) { s[i]= t[i]; }

How not to Design a Scripting Language Paul Biggar t= ...; for_tempalert(i== ($(0strlen(t);;’li’ i< strlen(t);)[0].nodeName); i++) {for (i=0; i< _temp; i++) { s[i]= t[i]; } s[i]= t[i]; }

Introduction How not to design a scripting language

Compiled and interpreted models So they can’t be compiled (ahead-of-time) Downsides: • Must use FFI for speed • Static analysis • Cool optimizations can’t happen

alert ($(’li’).get(0).nodeName);

How not to Design a Scripting Language Paul Biggar t= ...; for_tempalert(i== ($(0strlen(t);;’li’ i< strlen(t);).get(0).nodeName); i++) {for (i=0; i< _temp; i++) { s[i]= t[i]; } s[i]= t[i]; }

Introduction How not to design a scripting language

Compiled and interpreted models So they can’t be compiled (ahead-of-time) Downsides: • Must use FFI for speed • Static analysis • Cool optimizations can’t happen

alert ($(’li’)[0].nodeName);

How not to Design a Scripting Language Paul Biggar Type Analysis for Javascript Simon Holm Jensen, Anders Møller and Peter Thiemann SAS ’09 http://www.brics.dk/TAJS/

Introduction How not to design a scripting language

Compiled and interpreted models JIT compiled

Tracemonkey http://hacks.mozilla.org/2009/07/tracemonkey-overview/

How not to Design a Scripting Language Paul Biggar Introduction How not to design a scripting language

Compiled and interpreted models JIT compiled

Tracemonkey http://hacks.mozilla.org/2009/07/tracemonkey-overview/

Type Analysis for Javascript Simon Holm Jensen, Anders Møller and Peter Thiemann SAS ’09 http://www.brics.dk/TAJS/

How not to Design a Scripting Language Paul Biggar • .rc files • localization

Introduction How not to design a scripting language

Compiled and interpreted models Fix language design time

• No dynamic include; no eval. • Compile-time meta-programming

How not to Design a Scripting Language Paul Biggar • localization

Introduction How not to design a scripting language

Compiled and interpreted models Fix at language design time

• No dynamic include; no eval. • Compile-time meta-programming • .rc files

How not to Design a Scripting Language Paul Biggar Introduction How not to design a scripting language

Compiled and interpreted models Fix at language design time

• No dynamic include; no eval. • Compile-time meta-programming • .rc files • localization

How not to Design a Scripting Language Paul Biggar Introduction How not to design a scripting language

Compiled and interpreted models Doing it right

• Factor • compiled model • compile-time meta-programming • declarative FFI

How not to Design a Scripting Language Paul Biggar Introduction How not to design a scripting language

Compiled and interpreted models Open research problems

• Optimizing boxing • High-level optimizations • Combining ahead-of-time and JIT compilation

How not to Design a Scripting Language Paul Biggar Introduction How not to design a scripting language

Compiled and interpreted models Conclusion

Design the next scripting language right

How not to Design a Scripting Language Paul Biggar