The Dynamic Language Runtime (DLR) is Microsoft’s effort to bring dynamically typed programming languages to the .NET Framework platform. It has been around for some time now and is being improved now as I write. Currently it is version 0.91. This article aims to be a short introduction to the concepts of the DLR and the programming languages that are dependent of it.
The DLR is actually a runtime library which forms a layer upon the static Common Language Runtime (CLR). It relies heavily on reflection to create code that can be executed dynamically during runtime. So there are basically no changes to the CLR, just a layer of libraries making things seem to be carried out dynamically and then being able to work with other statically typed programming languages like C# and VB.
DLR defines these:
- A dynamic type system, to be shared by all languages utilizing the DLR services
- Dynamic method dispatch
- Dynamic code generation
- Hosting API, to make it easier for static programming languages to host the DLR
The DLR is built upon the idea of having an abstract syntax tree with expression nodes that are used to dynamically generate the corresponding code at runtime. The compiler of any dynamic programming language that is built on the DLR is generating these DLR abstract syntax trees which later is executed by the DLR libraries.
DynamicSite objects are caches contained in the assembly that are dynamically updated at runtime. Dynamic sites contains objects which points to methods for a fast lookup of their signatures at runtime. The invocation must check if a call is valid and if so bind it to the actual implementation of the method.
.NET Framework 4.0 will be the first release to contain the DLR libraries. You can download and try .NET Framework 4.0 Beta 1 along with Visual Studio 2010 Beta 1 from MSDN.
Microsoft has released the DLR under Microsoft Public License which is an open source license which allows anybody to redistribute it in their own project. That allows the Mono Project to bundle it with their future releases.
Dynamic programming languages
Moving on to the programming languages . The DLR makes it easier to implement dynamic programming languages as Python and Ruby, through IronPython and IronRuby, on the .NET platform. Defining a shared library which makes interoperability between them and the rest of the static languages possible. They have binders to both the CLR and the native main implementations.
IronPython is an open-source implementation of Python running on the DLR. It allows Python program to run and take advantage of the .NET Framework. There are binders to various platforms like CPython and the CLR. It also works with Silverlight (2.0 and above).
The project was initially started by Jim Hugunin, who previously wrote Jython (formerly JPython), the Java implementation of Python. Hugunin wanted to see if Python could run well on the .NET Framework and therefor he started writing IronPython. He thought that it would be inefficient but was rather surprised when it turned out to run very well. Later he was hired by Microsoft as a developer where he took the initiative to create the Dynamic Language Runtime. Since then he has been the leader for both the DLR/Iron Python-team.
from BookService import BookDictionary booksWrittenByBookerPrizeWinners = [book.Title for book in BookDictionary.GetAllBooks() if "Booker Prize" in book.Author.MajorAwards]
Likewise there is an implementation of Ruby called IronRuby. It has the same goals as IronPython and the original project called RubyCLR was started by John Lam, who now works as a Program Manager at the DLR Team. Initially it was just a binder to Ruby but it has evolved and is now a open-source project at Microsoft.
sb = StringBuilder.new sb.Append "Hello" sb.Append " World!" puts sb.ToString
Because of their open-source nature both languages run on Mono as well although there are no guarantees they will not do it as IronPython and IronRuby currently are under development.
There are other language implementations as well. Managed JScript and IronScheme among others.
From version 4.0 Microsoft’s implementation of C# will support dynamic typing through the dynamic keyword which makes it easier to interop with DLR languages and COM objects. Visual Basic 10 will have similar functionality with the Dim keyword. Mono will also add this soon. How this really works under the hood will be something to talk about in another blogpost.
Note:
Sun is currently working on a new version of the Java Virtual Machine, called the “Da Vinci Machine”, which adds support for dynamic programming languages to the Java Platform at runtime level. New op codes are being introduced to the instruction set of the Java Bytecode.
Update:
- Dynamic typing in C# is discussed in this article.
- Recommended article: Dynamic programming languages on the CLR




0 Responses to “Dynamic Language Runtime”