Skip to content

Commit b6c2923

Browse files
committed
привязка методов контекста к создаваемому экземпляру
1 parent 32310b3 commit b6c2923

6 files changed

Lines changed: 66 additions & 27 deletions

File tree

src/OneScript.Core/Compilation/Binding/ScopeBindingDescriptor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ private ScopeBindingDescriptor(ScopeBindingKind kind, IAttachableContext target,
4141
public static ScopeBindingDescriptor Static(IAttachableContext target)
4242
=> new ScopeBindingDescriptor(ScopeBindingKind.Static, target, -1);
4343

44-
public static ScopeBindingDescriptor ThisScope()
45-
=> new ScopeBindingDescriptor(ScopeBindingKind.ThisScope, null, -1);
44+
public static ScopeBindingDescriptor ThisScope(IAttachableContext target=null)
45+
=> new ScopeBindingDescriptor(ScopeBindingKind.ThisScope, target, -1);
4646

4747
public static ScopeBindingDescriptor FrameScope(int index)
4848
=> new ScopeBindingDescriptor(ScopeBindingKind.FrameScope, null, index);

src/OneScript.Core/Compilation/CompilerFrontendBase.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ This Source Code Form is subject to the terms of the
44
was not distributed with this file, You can obtain one
55
at http://mozilla.org/MPL/2.0/.
66
----------------------------------------------------------*/
7-
using System;
8-
using System.Collections.Generic;
97
using OneScript.Compilation.Binding;
8+
using OneScript.Contexts;
109
using OneScript.DependencyInjection;
1110
using OneScript.Execution;
1211
using OneScript.Language;
1312
using OneScript.Language.LexicalAnalysis;
1413
using OneScript.Language.SyntaxAnalysis;
1514
using OneScript.Language.SyntaxAnalysis.AstNodes;
1615
using OneScript.Sources;
16+
using System;
17+
using System.Collections.Generic;
1718

1819
namespace OneScript.Compilation
1920
{
@@ -64,6 +65,17 @@ public IExecutableModule Compile(SourceCode source, IBslProcess process, Type cl
6465
return CompileInternal(symbols, parsedModule, classType, process);
6566
}
6667

68+
public IExecutableModule Compile<T>(SourceCode source, IBslProcess process, T target)
69+
where T : IAttachableContext
70+
{
71+
var lexer = CreatePreprocessor(source);
72+
var symbols = PrepareSymbols(target);
73+
var parsedModule = ParseSyntaxConstruction(lexer, source, p => p.ParseStatefulModule());
74+
75+
return CompileInternal(symbols, parsedModule, typeof(T), process);
76+
}
77+
78+
6779
public IExecutableModule CompileExpression(SourceCode source)
6880
{
6981
var lexer = new DefaultLexer
@@ -91,7 +103,7 @@ public IExecutableModule CompileBatch(SourceCode source)
91103

92104
protected abstract IExecutableModule CompileBatchInternal(SymbolTable symbols, ModuleNode parsedModule);
93105

94-
private SymbolTable PrepareSymbols()
106+
private SymbolTable PrepareSymbols(IAttachableContext target = null)
95107
{
96108
var actualTable = new SymbolTable();
97109
if (SharedSymbols != default)
@@ -104,7 +116,7 @@ private SymbolTable PrepareSymbols()
104116
}
105117

106118
ModuleSymbols ??= new SymbolScope();
107-
actualTable.PushScope(ModuleSymbols, ScopeBindingDescriptor.ThisScope());
119+
actualTable.PushScope(ModuleSymbols, ScopeBindingDescriptor.ThisScope(target));
108120

109121
return actualTable;
110122
}

src/OneScript.Core/Compilation/ICompilerFrontend.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ This Source Code Form is subject to the terms of the
44
was not distributed with this file, You can obtain one
55
at http://mozilla.org/MPL/2.0/.
66
----------------------------------------------------------*/
7-
using System;
8-
using System.Collections.Generic;
97
using OneScript.Compilation.Binding;
8+
using OneScript.Contexts;
109
using OneScript.Execution;
1110
using OneScript.Language;
1211
using OneScript.Sources;
12+
using System;
13+
using System.Collections.Generic;
1314

1415
namespace OneScript.Compilation
1516
{
@@ -27,9 +28,13 @@ public interface ICompilerFrontend
2728

2829
IErrorSink ErrorSink { get; }
2930

30-
IExecutableModule Compile(SourceCode source, IBslProcess process, Type classType = null);
31-
32-
IExecutableModule CompileExpression(SourceCode source);
31+
IExecutableModule Compile(SourceCode source, IBslProcess process, Type classType = null);
32+
33+
public IExecutableModule Compile<T>(SourceCode source, IBslProcess process, T target)
34+
where T : IAttachableContext;
35+
36+
37+
IExecutableModule CompileExpression(SourceCode source);
3338

3439
IExecutableModule CompileBatch(SourceCode source);
3540
}

src/ScriptEngine.HostedScript/LibraryLoader.cs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ This Source Code Form is subject to the terms of the
55
at http://mozilla.org/MPL/2.0/.
66
----------------------------------------------------------*/
77

8-
using ScriptEngine.Machine;
9-
using ScriptEngine.Machine.Contexts;
10-
using System;
11-
using System.Collections.Generic;
12-
using System.IO;
13-
using System.Linq;
148
using OneScript.Commons;
159
using OneScript.Compilation;
1610
using OneScript.Contexts;
1711
using OneScript.Exceptions;
1812
using OneScript.Execution;
13+
using OneScript.Sources;
1914
using OneScript.Values;
2015
using ScriptEngine.Libraries;
16+
using ScriptEngine.Machine;
17+
using ScriptEngine.Machine.Contexts;
18+
using System;
19+
using System.Collections.Generic;
20+
using System.IO;
21+
using System.Linq;
2122

2223
namespace ScriptEngine.HostedScript
2324
{
@@ -28,7 +29,7 @@ public class LibraryLoader : AutoScriptDrivenObject<LibraryLoader>
2829
private readonly ScriptingEngine _engine;
2930

3031
private readonly bool _customized;
31-
private readonly Stack<LibraryLoadingContext> _librariesInProgress = new Stack<LibraryLoadingContext>();
32+
private readonly Stack<LibraryLoadingContext> _librariesInProgress = [];
3233

3334
private struct DelayLoadedScriptData
3435
{
@@ -70,17 +71,30 @@ private LibraryLoader(IRuntimeEnvironment env,
7071
_libManager = libManager;
7172
_engine = engine;
7273
_customized = false;
73-
}
74-
74+
}
75+
76+
private LibraryLoader(ScriptingEngine engine) : base(EmptyModule.Instance,true)
77+
{
78+
_env = engine.Environment;
79+
_libManager = engine.LibraryManager;
80+
_engine = engine;
81+
_customized = true;
82+
}
83+
7584
#region Static part
76-
85+
7786
public static LibraryLoader Create(ScriptingEngine engine, string processingScript, IBslProcess process)
7887
{
7988
var compiler = engine.GetCompilerService();
8089
var code = engine.Loader.FromFile(processingScript);
81-
var module = CompileModule(compiler, code, typeof(LibraryLoader), process);
82-
83-
return new LibraryLoader(module, engine.Environment, engine.LibraryManager, engine, process);
90+
91+
var loader = new LibraryLoader(engine);
92+
CompileModule(compiler, code, loader, process);
93+
94+
loader.InitOwnData();
95+
loader._engine.InitializeSDO(loader, process);
96+
97+
return loader;
8498
}
8599

86100
public static LibraryLoader Create(ScriptingEngine engine, IBslProcess process)
@@ -200,7 +214,7 @@ private bool CustomizedProcessing(string libraryPath, IBslProcess process)
200214
return DefaultProcessing(libraryPath, process);
201215
}
202216

203-
CallScriptMethod(eventIdx, new[] { libPathValue, defaultLoading, cancelLoading }, process);
217+
CallScriptMethod(eventIdx, [libPathValue, defaultLoading, cancelLoading], process);
204218

205219
if (cancelLoading.AsBoolean()) // Отказ = Ложь
206220
return false;
@@ -209,7 +223,6 @@ private bool CustomizedProcessing(string libraryPath, IBslProcess process)
209223
return DefaultProcessing(libraryPath, process);
210224

211225
return true;
212-
213226
}
214227

215228
private bool DefaultProcessing(string libraryPath, IBslProcess process)

src/ScriptEngine/Machine/Contexts/AutoScriptDrivenObject.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,15 @@ public static IExecutableModule CompileModule(ICompilerFrontend compiler, Source
152152
compiler.FillSymbols(typeof(AutoScriptDrivenObject<T>));
153153
return compiler.Compile(src, process, type);
154154
}
155+
156+
public static IExecutableModule CompileModule(ICompilerFrontend compiler, SourceCode src, T instance, IBslProcess process)
157+
{
158+
compiler.FillSymbols(typeof(AutoScriptDrivenObject<T>));
159+
160+
var module = compiler.Compile<T>(src, process, instance);
161+
instance.SetModule(module);
162+
return module;
163+
}
155164
}
156165

157166
}

src/ScriptEngine/Machine/Contexts/ScriptDrivenObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected ScriptDrivenObject()
4545
{
4646
}
4747

48-
protected void SetModule(StackRuntimeModule module)
48+
protected void SetModule(IExecutableModule module)
4949
{
5050
_module = module;
5151
}

0 commit comments

Comments
 (0)