forked from EvilBeaver/OneScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptingEngine.cs
More file actions
208 lines (170 loc) · 7.14 KB
/
ScriptingEngine.cs
File metadata and controls
208 lines (170 loc) · 7.14 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using System;
using System.Linq;
using System.Threading.Tasks;
using OneScript.Compilation;
using OneScript.Contexts;
using OneScript.DependencyInjection;
using OneScript.Execution;
using OneScript.Types;
using ScriptEngine.Machine;
using ScriptEngine.Machine.Contexts;
using ScriptEngine.Compiler;
using ScriptEngine.Libraries;
namespace ScriptEngine
{
public class ScriptingEngine : IDisposable
{
private AttachedScriptsFactory _attachedScriptsFactory;
private IDebugController _debugController;
private IRuntimeEnvironment _runtimeEnvironment;
private readonly ILibraryManager _libraryManager;
public ScriptingEngine(ITypeManager types,
IGlobalsManager globals,
RuntimeEnvironment env,
OneScriptCoreOptions options,
IServiceContainer services)
{
TypeManager = types;
// FIXME: Пока потребители не отказались от статических инстансов, они будут жить и здесь
GlobalsManager = globals;
_runtimeEnvironment = env;
_libraryManager = env;
Loader = new ScriptSourceFactory();
Services = services;
ContextDiscoverer = new ContextDiscoverer(types, globals, services);
DebugController = services.TryResolve<IDebugController>();
Loader.ReaderEncoding = options.FileReaderEncoding;
}
public IServiceContainer Services { get; }
private ContextDiscoverer ContextDiscoverer { get; }
public IRuntimeEnvironment Environment => _runtimeEnvironment;
public ILibraryManager LibraryManager => _libraryManager;
public ITypeManager TypeManager { get; }
public IGlobalsManager GlobalsManager { get; }
private CodeGenerationFlags ProduceExtraCode { get; set; }
public void AttachAssembly(System.Reflection.Assembly asm, Predicate<Type> filter = null)
{
ContextDiscoverer.DiscoverClasses(asm, filter);
ContextDiscoverer.DiscoverGlobalContexts(Environment, asm, filter);
}
public void AttachExternalAssembly(System.Reflection.Assembly asm, IRuntimeEnvironment globalEnvironment)
{
ContextDiscoverer.DiscoverClasses(asm);
//var lastCount = globalEnvironment.AttachedContexts.Count();
ContextDiscoverer.DiscoverGlobalContexts(globalEnvironment, asm);
//var newCount = globalEnvironment.AttachedContexts.Count();
// while (lastCount < newCount)
// {
// MachineInstance.Current.AttachContext(globalEnvironment.AttachedContexts[lastCount]);
// ++lastCount;
// }
}
public void AttachExternalAssembly(System.Reflection.Assembly asm)
{
AttachExternalAssembly(asm, Environment);
}
public void Initialize()
{
SetDefaultEnvironmentIfNeeded();
EnableCodeStatistics();
//UpdateContexts();
_attachedScriptsFactory = new AttachedScriptsFactory(this);
AttachedScriptsFactory.SetInstance(_attachedScriptsFactory);
}
// public void UpdateContexts()
// {
// lock (this)
// {
// ExecutionDispatcher.Current ??= Services.Resolve<ExecutionDispatcher>();
// }
// MachineInstance.Current.SetMemory(Services.Resolve<ExecutionContext>());
// }
private void SetDefaultEnvironmentIfNeeded()
{
_runtimeEnvironment ??= new RuntimeEnvironment();
}
public ScriptSourceFactory Loader { get; }
public ICompilerFrontend GetCompilerService()
{
using var scope = Services.CreateScope();
var compiler = scope.Resolve<CompilerFrontend>();
compiler.SharedSymbols = _runtimeEnvironment.GetSymbolTable();
switch (System.Environment.OSVersion.Platform)
{
case PlatformID.Unix:
compiler.PreprocessorDefinitions.Add("Linux");
break;
case PlatformID.MacOSX:
compiler.PreprocessorDefinitions.Add("MacOS");
break;
case PlatformID.Win32NT:
compiler.PreprocessorDefinitions.Add("Windows");
break;
}
compiler.GenerateDebugCode = ProduceExtraCode.HasFlag(CodeGenerationFlags.DebugCode);
compiler.GenerateCodeStat = ProduceExtraCode.HasFlag(CodeGenerationFlags.CodeStatistics);
return compiler;
}
public UserScriptContextInstance NewObject(IExecutableModule module, IBslProcess process,
ExternalContextData externalContext = null)
{
var scriptContext = CreateUninitializedSDO(module, externalContext);
InitializeSDO(scriptContext, process);
return scriptContext;
}
public UserScriptContextInstance CreateUninitializedSDO(IExecutableModule module, ExternalContextData externalContext = null)
{
var scriptContext = new UserScriptContextInstance(module, true);
if (externalContext != null)
{
foreach (var item in externalContext)
{
scriptContext.AddProperty(item.Key, item.Value);
}
}
scriptContext.InitOwnData();
return scriptContext;
}
public void InitializeSDO(ScriptDrivenObject sdo, IBslProcess process)
{
sdo.Initialize(process);
}
public AttachedScriptsFactory AttachedScriptsFactory => _attachedScriptsFactory;
public IDebugController DebugController
{
get => _debugController;
private set
{
_debugController = value;
if (value != null)
{
ProduceExtraCode |= CodeGenerationFlags.DebugCode;
}
}
}
private void EnableCodeStatistics()
{
var collector = Services.TryResolve<ICodeStatCollector>();
if (collector == default)
return;
ProduceExtraCode |= CodeGenerationFlags.CodeStatistics;
}
#region IDisposable Members
public void Dispose()
{
DebugController?.Dispose();
AttachedScriptsFactory.SetInstance(null);
}
#endregion
/// <summary>
/// Инициализирует новый процесс
/// </summary>
public IBslProcess NewProcess() => Services.Resolve<IBslProcessFactory>().NewProcess();
}
}