forked from EvilBeaver/OneScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostedScriptEngine.cs
More file actions
155 lines (128 loc) · 4.91 KB
/
HostedScriptEngine.cs
File metadata and controls
155 lines (128 loc) · 4.91 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
/*----------------------------------------------------------
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 OneScript.Sources;
using ScriptEngine.Machine;
using OneScript.Commons;
using OneScript.Compilation;
using OneScript.Contexts;
using OneScript.DependencyInjection;
using OneScript.Execution;
using OneScript.StandardLibrary;
using OneScript.StandardLibrary.Tasks;
using ScriptEngine.Machine.Contexts;
namespace ScriptEngine.HostedScript
{
public class HostedScriptEngine : IDisposable
{
private readonly ScriptingEngine _engine;
private SystemGlobalContext _globalCtx;
private readonly IRuntimeEnvironment _env;
private bool _isInitialized;
private readonly OneScriptLibraryOptions _workingConfig;
public HostedScriptEngine(ScriptingEngine engine)
{
_engine = engine;
_env = _engine.Environment;
_engine.AttachAssembly(typeof(HostedScriptEngine).Assembly);
_workingConfig = _engine.Services.Resolve<OneScriptLibraryOptions>();
SetGlobalContexts(engine.GlobalsManager);
}
public ScriptingEngine Engine => _engine;
private void SetGlobalContexts(IGlobalsManager manager)
{
_globalCtx = new SystemGlobalContext();
_globalCtx.EngineInstance = _engine;
_env.InjectObject(_globalCtx);
manager.RegisterInstance(_globalCtx);
var dynLoader = new DynamicLoadingFunctions(_engine);
_env.InjectObject(dynLoader);
manager.RegisterInstance(dynLoader);
var bgTasksManager = new BackgroundTasksManager(_engine.Services.Resolve<ExecutionContext>());
_env.InjectGlobalProperty(bgTasksManager, "ФоновыеЗадания", "BackgroundJobs", true);
}
public void Initialize()
{
if (!_isInitialized)
{
_engine.Initialize();
_isInitialized = true;
}
// System language
var systemLanguageCfg = _workingConfig.SystemLanguage;
Locale.SystemLanguageISOName = systemLanguageCfg ?? System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName;
}
public void InjectGlobalProperty(string name, string alias, IValue value, bool readOnly)
{
_env.InjectGlobalProperty(value, name, alias, readOnly);
}
public void InjectObject(IAttachableContext obj)
{
_env.InjectObject(obj);
}
public ScriptSourceFactory Loader => _engine.Loader;
public ICompilerFrontend GetCompilerService()
{
var compilerSvc = _engine.GetCompilerService();
compilerSvc.FillSymbols(typeof(UserScriptContextInstance));
return compilerSvc;
}
public Process CreateProcess(IHostApplication host, SourceCode src)
{
Initialize();
SetGlobalEnvironment(host, src);
if (_engine.Debugger.IsEnabled)
{
_engine.Debugger.Start();
_engine.Debugger.GetSession().WaitReadyToRun();
}
var compilerSvc = GetCompilerService();
DefineConstants(compilerSvc);
IExecutableModule module;
var bslProcess = _engine.NewProcess();
try
{
module = compilerSvc.Compile(src, bslProcess);
}
catch (CompilerException)
{
_engine.Debugger.NotifyProcessExit(1);
throw;
}
return InitProcess(bslProcess, host, module);
}
private void DefineConstants(ICompilerFrontend compilerSvc)
{
var definitions = _workingConfig.PreprocessorDefinitions;
foreach (var val in definitions)
{
compilerSvc.PreprocessorDefinitions.Add(val);
}
if (Utils.IsMonoRuntime)
{
compilerSvc.PreprocessorDefinitions.Add("MONO");
}
}
public IServiceContainer Services => _engine.Services;
public void SetGlobalEnvironment(IHostApplication host, SourceCode src)
{
_globalCtx.ApplicationHost = host;
_globalCtx.CodeSource = src;
_globalCtx.InitInstance();
}
private Process InitProcess(IBslProcess bslProcess, IHostApplication host, IExecutableModule module)
{
Initialize();
var process = new Process(bslProcess, host, module, _engine);
return process;
}
public void Dispose()
{
_engine?.Dispose();
}
}
}