This repository was archived by the owner on Aug 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRubyInterpretedScript.cs
More file actions
124 lines (107 loc) · 2.87 KB
/
Copy pathRubyInterpretedScript.cs
File metadata and controls
124 lines (107 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using InVision.Framework.Scripting;
using IronRuby.Builtins;
using IronRuby.Compiler;
using Microsoft.Scripting.Hosting;
using System.Linq;
namespace InVision.Scripting.IronRuby
{
public class RubyInterpretedScript : DlrScript
{
private readonly RubyScriptManager _manager;
private ScriptScope _scope;
/// <summary>
/// Initializes a new instance of the <see cref="RubyInterpretedScript"/> class.
/// </summary>
/// <param name="manager">The manager.</param>
/// <param name="filename">The filename.</param>
/// <param name="compilerOutput">The compiler output.</param>
public RubyInterpretedScript(RubyScriptManager manager, string filename, string compilerOutput)
: base(filename, compilerOutput)
{
_manager = manager;
ResetScope();
}
/// <summary>
/// Gets the engine.
/// </summary>
/// <value>The engine.</value>
public ScriptEngine Engine
{
get { return _manager.Engine; }
}
/// <summary>
/// Gets the extension.
/// </summary>
/// <value>The extension.</value>
protected override string Extension
{
get { return ".rb"; }
}
/// <summary>
/// Gets the execution mode.
/// </summary>
/// <value>The execution mode.</value>
public override ExecutionMode ExecutionMode
{
get { return ExecutionMode.Interpreted; }
}
/// <summary>
/// Resets the scope.
/// </summary>
protected override void ResetScope()
{
_scope = _manager.Engine.CreateScope();
}
/// <summary>
/// Loads this instance.
/// </summary>
public override void LoadOrExecute()
{
var file = new StringBuilder();
foreach (Assembly assembly in References)
{
file.AppendFormat("require \"{0}\"", assembly.FullName).AppendLine();
}
file.AppendLine();
file.Append(File.ReadAllText(Filename));
ScriptSource source = _manager.Engine.CreateScriptSourceFromString(file.ToString());
source.Compile().Execute(_scope);
}
/// <summary>
/// Gets the method or function.
/// </summary>
/// <param name="name">The name.</param>
/// <returns></returns>
public override object GetMethodOrFunction(string name)
{
throw new NotImplementedException();
}
/// <summary>
/// Finds the service.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public override IEnumerable<T> FindServices<T>()
{
string searchType = typeof(T).FullName.Replace(".", "::");
foreach (var item in Engine.Runtime.Globals.GetItems())
{
if (!(item.Value is RubyClass))
continue;
var rubyClass = (RubyClass)item.Value;
var mixins = rubyClass.GetMixins();
bool hasInterface = mixins.Where(m => m.Name == searchType).Any();
if (hasInterface)
{
dynamic instance = Engine.Operations.CreateInstance(rubyClass);
yield return instance;
}
}
}
}
}