Created
April 21, 2013 09:12
-
-
Save akimboyko/5429024 to your computer and use it in GitHub Desktop.
JavaScript eval() vs. Roslyn.Scripting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <!-- | |
| Metaprogramming via scripting | |
| from book 'Metaprogramming in .NET' | |
| by Kevin Hazzard and Jason Bock | |
| http://www.manning.com/hazzard/ | |
| --> | |
| <html> | |
| <head> | |
| <script type="text/javascript"> | |
| function convert() { | |
| var fromValue = eval(fromVal.value); | |
| toVal.innerHTML = eval(formula.value).toString(); | |
| } | |
| </script> | |
| </head> | |
| <body> | |
| <span>fromValue:</span> | |
| <input id="fromVal" type="text"/><br/> | |
| <span>formula:</span> | |
| <input id="formula" type="text"/><br/> | |
| <input type="button" onclick="javascript:convert();" | |
| value="Convert" /><br/> | |
| <span>toValue:</span> <span id="toVal"></span> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <packages> | |
| <package id="Roslyn" version="1.2.20906.2" targetFramework="net45" /> | |
| <package id="Roslyn.Compilers" version="1.2.20906.2" targetFramework="net45" /> | |
| <package id="Roslyn.Compilers.Common" version="1.2.20906.2" targetFramework="net45" /> | |
| <package id="Roslyn.Compilers.CSharp" version="1.2.20906.2" targetFramework="net45" /> | |
| <package id="Roslyn.Services.Common" version="1.2.20906.2" targetFramework="net45" /> | |
| <package id="Roslyn.Services.CSharp" version="1.2.20906.2" targetFramework="net45" /> | |
| </packages> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Dynamic; | |
| using Roslyn.Services.Interactive; | |
| using Roslyn.Scripting.CSharp; | |
| // limitations of scripts: | |
| // * no `dynamic` keyword allowed | |
| // * no async/await allowed | |
| // For example: | |
| // var x = 6; | |
| // System.Math.Sqrt(x * 7) | |
| var scripts = new [] | |
| { | |
| System.Console.ReadLine(), //fromValue: | |
| System.Console.ReadLine() //formula: | |
| }; | |
| var engine = new ScriptEngine(); | |
| Array.ForEach( | |
| new[] { typeof(object).Assembly }, | |
| @assembly => engine.AddReference(@assembly)); | |
| Array.ForEach( | |
| new[] { "System" }, | |
| @namespace => engine.ImportNamespace(@namespace)); | |
| var session = engine.CreateSession(); | |
| object resultModel = null; | |
| foreach(var script in scripts) | |
| { | |
| resultModel = session | |
| .CompileSubmission<object>(script) | |
| .Execute(); | |
| } | |
| Console.WriteLine(resultModel); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment