Skip to content

Instantly share code, notes, and snippets.

@jschementi
Created December 15, 2009 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jschementi/256632 to your computer and use it in GitHub Desktop.
Save jschementi/256632 to your computer and use it in GitHub Desktop.
diff --git a/sketchscript/sketchscript/MainWindow.xaml.cs b/sketchscript/sketchscript/MainWindow.xaml.cs
index 22a01f3..8e62ff1 100644
--- a/sketchscript/sketchscript/MainWindow.xaml.cs
+++ b/sketchscript/sketchscript/MainWindow.xaml.cs
@@ -17,6 +17,8 @@ using System.IO;
using AeroGlass;
using System.Diagnostics;
using SThread = System.Threading;
+using Microsoft.Scripting.Hosting;
+using IronRuby;
namespace SketchScript {
@@ -26,6 +28,10 @@ namespace SketchScript {
public TextBoxBuffer OutputBuffer { get; internal set; }
private bool _isCtrlPressed;
private bool _isOutputRedirected;
+
+ private ScriptEngine _rubyEngine;
+ private IronRuby.Runtime.RubyContext _rubyContext;
+ private ScriptScope _scope;
#endregion
#region Animations
@@ -61,6 +67,25 @@ namespace SketchScript {
this.Loaded += (s,e) => {
OutputBuffer = new TextBoxBuffer(_output);
+
+ // Initialize IronRuby
+ var runtime = ScriptRuntime.CreateFromConfiguration();
+ _rubyEngine = Ruby.GetEngine(runtime);
+ _rubyContext = Ruby.GetExecutionContext(_rubyEngine);
+ _scope = _rubyEngine.CreateScope();
+
+ runtime.LoadAssembly(typeof(Canvas).Assembly); // loads PresentationFramework
+ runtime.LoadAssembly(typeof(Brushes).Assembly); // loads PresentationCore
+ runtime.LoadAssembly(GetType().Assembly); // loads this exe
+ dynamic scope = _scope;
+ scope.canvas = _canvas;
+ scope.window = this;
+
+ // redirect stdout to the output window
+ _rubyContext.StandardOutput = OutputBuffer;
+
+ RegisterCallbacks();
+
KeyBindings();
};
}
@@ -73,7 +98,18 @@ namespace SketchScript {
public void RunCode(TextBox t) {
string code = t.SelectionLength > 0 ? t.SelectedText : t.Text;
- // TODO: run code!
+ // Run the code
+ var result = _rubyEngine.Execute(code, _scope);
+
+ // write the result to the output window
+
+ var output = string.Format("=> {0}\n", _rubyContext.Inspect(result));
+ OutputBuffer.write(output);
+
+ // add the code to the history
+ _history.AppendText(string.Format("{0}\n# {1}", code, output));
+
+ CaptureAnimationCallbacks();
}
/// <summary>
@@ -112,6 +148,14 @@ namespace SketchScript {
EachObject = null;
// TODO: get the "EachObject" callback
+
+ Action eachFrame = null;
+ _scope.TryGetVariable<Action>("each_frame", out eachFrame);
+ EachFrame = eachFrame;
+
+ Func<object, dynamic> eachObject = null;
+ _scope.TryGetVariable<Func<object, dynamic>>("each_object", out eachObject);
+ EachObject = eachObject;
}
/// <summary>
diff --git a/sketchscript/sketchscript/sketchscript.csproj b/sketchscript/sketchscript/sketchscript.csproj
index 076612e..591aefa 100644
--- a/sketchscript/sketchscript/sketchscript.csproj
+++ b/sketchscript/sketchscript/sketchscript.csproj
@@ -36,6 +36,15 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
+ <Reference Include="IronRuby">
+ <HintPath>..\ironruby\IronRuby.dll</HintPath>
+ </Reference>
+ <Reference Include="IronRuby.Libraries">
+ <HintPath>..\ironruby\IronRuby.Libraries.dll</HintPath>
+ </Reference>
+ <Reference Include="Microsoft.Scripting">
+ <HintPath>..\ironruby\Microsoft.Scripting.dll</HintPath>
+ </Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment