Upload
jimmy-schementi
View
5.147
Download
1
Embed Size (px)
DESCRIPTION
NYC CodeCamp 2011 Talk - Iron Languages, Dynamic Language on .NET
Citation preview
IRONLanguages
Dynamic Languages for the .NET developer
github.com/IronLanguages
Jimmy [email protected]
JavaScriptSmalltalk
RubyTcl
MATLAB
xkcd.com
Executes many common behaviors, at runtime, that other languages might perform during compilation, if at all.
Most are dynamically-typed, but not all.
Dynamic TypingThe majority of its type checking is performed at run-time as opposed to at compile-time.
why?
Simple enough for non-programmers, capable enough for programmers
[1,2,3].ea
ch do |i|
puts i
end
print File.read("foo
.txt")
name = "Jimmy"
a.downcase rescue "No name"
class Foo def method_missing(m)
puts "called: #{m}"
endendFoo.new.dfhajsdhfl
"-" *
79
Scripting Languages
Dynamic Languages
“Python, like many good technologies, soon spreads virally throughout your development team and finds its way into all sorts of applications and tools…Python scripts are used in many areas of the game.”
Mustafa ThamerCivilization IV development team
http://www.unreal.com/media/banners/kismet1.jpg
http://logo.twentygototen.org/
Interactive>>> 2 + 24
.NET?
Dynamic Languages on .NET
Consumers
C#
Python Ruby
Keep it Simpledef fact n return 1 if n == 0 n * fact(n-1)end
puts fact 13
using System;
public class MathClass { public static int Factorial(int n) { if (n == 0) return 1; return n * Factorial(n – 1); } public static void Main(string[] args) { Console.WriteLine(Factorial(13)); }}
def fact(n): if n == 0: return 1 return n * fact(n-1)
print fact(13)
Scripting the .NET frameworkDriving .NET code from scriptsDomain-specific languages
Compiler Geek-OutHigh-level discussion on how compilers work and what the DLR does.
HostingIn-Application extensibility / customizationTreating code as data (or configuration)Discussion on best practices
DemoScripting .NET
rb> puts 2 + 2 4 # => nil
def add(): return 2 + a
traditional compiler front-end
return
Syntax Tree
return
Scan
2 + a
Token stream
add
Named(a)Const(2)
Parse
IronPython: Hand-written LL(1) parser
IronRuby: Gardens Point Parser Generator LALR(1)
compiler back-end on CLR
Syntax Tree
return
add
Named(a)Const(2)
ldc.i4.2 // load 2box [mscorlib]System.Int32ldarg.0 // load “a”call object LangHelpers::Add(object, object)ret
IL
public static object Add (object x, object y) { ... }
Runtime Library
Generate IL
compiler back-end on DLR
To Expression TreeLinq ExpressionTree
Return
MethodCallLangHelpers.Add
BoundExpression
ConstantExpression2
ConvertToObject
Variablea: Object
Syntax Tree
return
add
Named(a)Const(2)
.Dynamic puts(.S,1) @1( $#scope, $#self, .Call IronRuby.Runtime.RubyOps.CreateMutableStringL( "hi", .Constant<IronRuby.Builtins.RubyEncoding>(US-ASCII)))
puts 2 + 2 -> Expression tree
internal static Delegate/*!*/ CompileLambda( LambdaExpression/*!*/ lambda, bool debugMode, bool noAdaptiveCompilation, int compilationThreshold) {
if (debugMode) { return CompileDebug(lambda); } else if (noAdaptiveCompilation) { return lambda.Compile(); } else { return lambda.LightCompile(compilationThreshold); } }
IronRuby.Runtime.RubyScriptCode
static vs. dynamic dispatch
def yo (name): "hello " + nameprint yo("jimmy")
MethodCallExpression
Method : {RuntimeMethodInfo {Name: "Print"}}Arguments : [0] ActionExpression
ActionExpressionyo("jimmy")
Action : CallActionArguments : [0] {BoundExpression {Variable: Local{yo}}} [1] {ConstantExpression {"jimmy"}}
public static object Handle (object[] args, DynamicSite<object, object, object> site1,object obj1, object obj2)
{if (obj1 != null && obj1.GetType() == typeof(string) &&
obj2 != null && obj2.GetType() == typeof(string)) {
return StringOps.Add(Converter.ConvertToString(obj1), Converter.ConvertToString(obj2));
} return site1.UpdateBindingAndInvoke(obj1, obj2);}
print yo(1)
public static object Handle (object[] args, DynamicSite<object, object, object> site1,object obj1, object obj2)
{if (obj1 != null && obj1.GetType() == typeof(int) &&
obj2 != null && obj2.GetType() == typeof(int)) {
return Int32Ops.Add(Converter.ConvertToInt(obj1), Converter.ConvertToInt(obj2));
} if (obj1 != null && obj1.GetType() == typeof(string) && obj2 != null && obj2.GetType() == typeof(string)) {
return StringOps.Add(Converter.ConvertToString(obj1), Converter.ConvertToString(obj2));
} return site1.UpdateBindingAndInvoke(obj1, obj2);}
System.Dynamic.DynamicObject
Dynamic Language Runtime
Infrastructure for creating languagesFocus on dynamic compiler back-end.
Dynamic-lookup protocolDynamicObject: shared protocol between languages
Lightweight hosting APIOne API for all DLR languages
HostingHostingScriptRuntime
ScriptScope ScriptEngine
ScriptSource
var engine = Ruby.CreateEngine(); engine.Execute("puts 2 + 2");
var engine = Python.CreateEngine(); dynamic scope = engine.CreateScope(); scope.page = this; engine.Execute( "page.Message.Text = 'Hello from Python!'", scope);
var runtime = ScriptRuntime.CreateFromConfiguration(); var engine = ScriptEngine.CreateEngine("IronRuby"); dynamic scope = engine.CreateScope(); scope.page = this;engine.Execute("page.Message.Text = 'Hello from IronRuby!'", scope);
require 'IronPython'require 'Microsoft.Scripting'include Microsoft::Scripting::Hostinginclude IronPython::Hosting
python = Python.create_enginescope = python.create_scopepython.execute "class Foo(object): def bar(self): print 'Look ma, white-space-sensitivity!'", scopepython.execute "Foo().bar()", scope
# foo.py:class Foo(object): def bar(self): print 'Look ma, white-space-sensitivity!'
# bar.rb:foo_module = IronRuby.require 'foo' foo_module.foo.bar
DemoHosting
Hosting best-practices• Store scripts where you want with PlatformAdaptationLayer–Makes script file-system operations use database, source-control, whatever …
• Pick isolation level for scripts– In-App-Domain: you totally control– Out-App-Domain: limit permission level– Out of process: total isolation
Project Status• IronRuby is working towards 1.9 compat – Rails 3, FFI, static type system integration• IronPython working towards 2.7/3.0 compat – Django, IronClad, and other libraries.• Tooling– IronRuby Gems/Rake support– Debugging w/REPL
• Fully open source– Contributions welcome!
How you can participate• Use it at your company, and tell us about it!– Ask the mailing lists and stackoverflow for help– Log any bugs you find
• Contributing to the project– Even if you’re not a compiler hacker … – but hackers welcome!– samples, documentation, blogs, and talks are all welcome also
ironpython.net IronPython website & download
dlr.codeplex.com DLR documentation for hosters and language developers
jimmy.schementi.com me
ironruby.net
IronRuby website & download
?
IRONLanguages
Dynamic Languages for the .NET developer
github.com/IronLanguages
Jimmy [email protected]